|
| 1 | +package printer |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestResourcePrinter_Print_Success(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + printerOption ResourcePrinterOption |
| 14 | + printFormat PrintFormat |
| 15 | + jsonPathTemplate string // used only for JSON path print format |
| 16 | + data map[string]string |
| 17 | + expected string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "resource printer with json should print json when json output format is selected", |
| 21 | + printerOption: WithJSON(), |
| 22 | + printFormat: JSONFormat, |
| 23 | + data: map[string]string{"foo": "bar"}, |
| 24 | + expected: "{\n \"foo\": \"bar\"\n}", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "resource printer with json path should print part of json when json path output format is selected and template is specified", |
| 28 | + printerOption: WithJSONPath(), |
| 29 | + printFormat: JSONPathFormat, |
| 30 | + jsonPathTemplate: "{.foo}", |
| 31 | + data: map[string]string{"foo": "bar"}, |
| 32 | + expected: "[\n \"bar\"\n]\n", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "resource printer with yaml should print yaml when yaml output format is selected", |
| 36 | + printerOption: WithYAML(), |
| 37 | + printFormat: YAMLFormat, |
| 38 | + data: map[string]string{"foo": "bar"}, |
| 39 | + expected: "foo: bar\n", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "resource printer with table should print table when table output format is selected", |
| 43 | + printerOption: WithTable(func(_ interface{}) (TableData, error) { |
| 44 | + return TableData{ |
| 45 | + Headers: []string{"foo"}, |
| 46 | + SingleRow: []string{"bar"}, |
| 47 | + }, nil |
| 48 | + }), |
| 49 | + printFormat: TableFormat, |
| 50 | + expected: " FOO \n-------\n bar \n-------\n", |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + for _, test := range tests { |
| 55 | + test := test |
| 56 | + |
| 57 | + t.Run(test.name, func(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + |
| 60 | + var writer bytes.Buffer |
| 61 | + resourcePrinter := NewForResource(&writer, test.printerOption) |
| 62 | + resourcePrinter.output = test.printFormat |
| 63 | + resourcePrinter.template = test.jsonPathTemplate |
| 64 | + |
| 65 | + resourcePrinter.Print(test.data) |
| 66 | + |
| 67 | + assert.EqualValues(t, test.expected, writer.String()) |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestResourcePrinter_Print_Failure(t *testing.T) { |
| 73 | + tests := []struct { |
| 74 | + name string |
| 75 | + printerOption ResourcePrinterOption |
| 76 | + printFormat PrintFormat |
| 77 | + jsonPathTemplate string // used only for JSON path |
| 78 | + }{ |
| 79 | + { |
| 80 | + name: "resource printer should return error when unknown print format is selected", |
| 81 | + printerOption: WithJSON(), |
| 82 | + printFormat: "unknown print format", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "resource printer with json path should return error when json path print format is selected and template is not specified", |
| 86 | + printerOption: WithJSONPath(), |
| 87 | + printFormat: JSONPathFormat, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + for _, test := range tests { |
| 92 | + test := test |
| 93 | + |
| 94 | + t.Run(test.name, func(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + |
| 97 | + var writer bytes.Buffer |
| 98 | + resourcePrinter := NewForResource(&writer, test.printerOption) |
| 99 | + resourcePrinter.output = test.printFormat |
| 100 | + resourcePrinter.template = test.jsonPathTemplate |
| 101 | + |
| 102 | + err := resourcePrinter.Print(struct{}{}) |
| 103 | + |
| 104 | + assert.Error(t, err) |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
0 commit comments