| 88 | } |
| 89 | |
| 90 | func TestContext(t *testing.T) { |
| 91 | testCases := []struct { |
| 92 | name string |
| 93 | format string |
| 94 | expected string |
| 95 | }{ |
| 96 | { |
| 97 | name: "json format", |
| 98 | format: JSONFormatKey, |
| 99 | expected: `{"Name":"test"} |
| 100 | `, |
| 101 | }, |
| 102 | { |
| 103 | name: "table format", |
| 104 | format: `table {{.Name}}`, |
| 105 | expected: `NAME |
| 106 | test |
| 107 | `, |
| 108 | }, |
| 109 | } |
| 110 | for _, tc := range testCases { |
| 111 | t.Run(tc.name, func(t *testing.T) { |
| 112 | buf := bytes.NewBuffer(nil) |
| 113 | ctx := Context{ |
| 114 | Format: Format(tc.format), |
| 115 | Output: buf, |
| 116 | } |
| 117 | subContext := fakeSubContext{Name: "test"} |
| 118 | subFormat := func(f func(sub SubContext) error) error { |
| 119 | return f(subContext) |
| 120 | } |
| 121 | err := ctx.Write(&subContext, subFormat) |
| 122 | assert.NilError(t, err) |
| 123 | assert.Equal(t, buf.String(), tc.expected) |
| 124 | }) |
| 125 | } |
| 126 | } |