Test various single output values for human-readable UI. Note that since OutputHuman defers to repl.FormatValue to render a single value, most of the test coverage should be in that package.
(t *testing.T)
| 19 | // OutputHuman defers to repl.FormatValue to render a single value, most of the |
| 20 | // test coverage should be in that package. |
| 21 | func TestOutputHuman_single(t *testing.T) { |
| 22 | testCases := map[string]struct { |
| 23 | value cty.Value |
| 24 | want string |
| 25 | wantErr bool |
| 26 | }{ |
| 27 | "string": { |
| 28 | value: cty.StringVal("hello"), |
| 29 | want: "\"hello\"\n", |
| 30 | }, |
| 31 | "list of maps": { |
| 32 | value: cty.ListVal([]cty.Value{ |
| 33 | cty.MapVal(map[string]cty.Value{ |
| 34 | "key": cty.StringVal("value"), |
| 35 | "key2": cty.StringVal("value2"), |
| 36 | }), |
| 37 | cty.MapVal(map[string]cty.Value{ |
| 38 | "key": cty.StringVal("value"), |
| 39 | }), |
| 40 | }), |
| 41 | want: `tolist([ |
| 42 | tomap({ |
| 43 | "key" = "value" |
| 44 | "key2" = "value2" |
| 45 | }), |
| 46 | tomap({ |
| 47 | "key" = "value" |
| 48 | }), |
| 49 | ]) |
| 50 | `, |
| 51 | }, |
| 52 | } |
| 53 | |
| 54 | for name, tc := range testCases { |
| 55 | t.Run(name, func(t *testing.T) { |
| 56 | streams, done := terminal.StreamsForTesting(t) |
| 57 | v := NewOutput(arguments.ViewOptions{ViewType: arguments.ViewHuman}, NewView(streams)) |
| 58 | |
| 59 | outputs := map[string]*states.OutputValue{ |
| 60 | "foo": {Value: tc.value}, |
| 61 | } |
| 62 | diags := v.Output("foo", outputs) |
| 63 | |
| 64 | if diags.HasErrors() { |
| 65 | if !tc.wantErr { |
| 66 | t.Fatalf("unexpected diagnostics: %s", diags) |
| 67 | } |
| 68 | } else if tc.wantErr { |
| 69 | t.Fatalf("succeeded, but want error") |
| 70 | } |
| 71 | |
| 72 | if got, want := done(t).Stdout(), tc.want; got != want { |
| 73 | t.Errorf("wrong result\ngot: %q\nwant: %q", got, want) |
| 74 | } |
| 75 | }) |
| 76 | } |
| 77 | } |
| 78 |