|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/olekukonko/tablewriter" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +// compareGolden compares actual output with golden file. |
| 30 | +func compareGolden(t *testing.T, actual, goldenFile string) { |
| 31 | + t.Helper() |
| 32 | + |
| 33 | + goldenPath := filepath.Join("testdata", goldenFile) |
| 34 | + expected, err := os.ReadFile(goldenPath) |
| 35 | + require.NoError(t, err, "Failed to read golden file: %s", goldenPath) |
| 36 | + |
| 37 | + require.Equal(t, string(expected), actual, "Output doesn't match golden file: %s", goldenFile) |
| 38 | +} |
| 39 | + |
| 40 | +func TestNewTableWriter(t *testing.T) { |
| 41 | + t.Parallel() |
| 42 | + |
| 43 | + t.Run("NoOptions", func(t *testing.T) { |
| 44 | + t.Parallel() |
| 45 | + |
| 46 | + var output bytes.Buffer |
| 47 | + table := NewTableWriter(&output) |
| 48 | + |
| 49 | + require.NotNil(t, table) |
| 50 | + require.IsType(t, &tablewriter.Table{}, table) |
| 51 | + |
| 52 | + table.Header("Name", "Age") |
| 53 | + _ = table.Append([]string{"John", "30"}) |
| 54 | + _ = table.Render() |
| 55 | + |
| 56 | + compareGolden(t, output.String(), "no_options.golden") |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("WithSingleOption", func(t *testing.T) { |
| 60 | + t.Parallel() |
| 61 | + |
| 62 | + var output bytes.Buffer |
| 63 | + table := NewTableWriter(&output, tablewriter.WithMaxWidth(80)) |
| 64 | + |
| 65 | + require.NotNil(t, table) |
| 66 | + require.IsType(t, &tablewriter.Table{}, table) |
| 67 | + |
| 68 | + table.Header("Name", "Age") |
| 69 | + _ = table.Append([]string{"John", "30"}) |
| 70 | + _ = table.Render() |
| 71 | + |
| 72 | + compareGolden(t, output.String(), "with_single_option.golden") |
| 73 | + }) |
| 74 | + |
| 75 | + t.Run("WithMultipleOptions", func(t *testing.T) { |
| 76 | + t.Parallel() |
| 77 | + |
| 78 | + var output bytes.Buffer |
| 79 | + table := NewTableWriter(&output, |
| 80 | + tablewriter.WithHeader([]string{"Name", "Age"}), |
| 81 | + tablewriter.WithMaxWidth(80), |
| 82 | + ) |
| 83 | + |
| 84 | + require.NotNil(t, table) |
| 85 | + require.IsType(t, &tablewriter.Table{}, table) |
| 86 | + |
| 87 | + _ = table.Append([]string{"John", "30"}) |
| 88 | + _ = table.Render() |
| 89 | + |
| 90 | + compareGolden(t, output.String(), "with_multiple_options.golden") |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("WithHeaderOption", func(t *testing.T) { |
| 94 | + t.Parallel() |
| 95 | + |
| 96 | + var output bytes.Buffer |
| 97 | + table := NewTableWriter(&output, tablewriter.WithHeader([]string{"Name", "Age"})) |
| 98 | + |
| 99 | + require.NotNil(t, table) |
| 100 | + require.IsType(t, &tablewriter.Table{}, table) |
| 101 | + |
| 102 | + _ = table.Append([]string{"John", "30"}) |
| 103 | + _ = table.Render() |
| 104 | + |
| 105 | + compareGolden(t, output.String(), "with_header_option.golden") |
| 106 | + }) |
| 107 | + |
| 108 | + t.Run("WithFooterOption", func(t *testing.T) { |
| 109 | + t.Parallel() |
| 110 | + |
| 111 | + var output bytes.Buffer |
| 112 | + table := NewTableWriter(&output, tablewriter.WithFooter([]string{"Total", "1"})) |
| 113 | + |
| 114 | + require.NotNil(t, table) |
| 115 | + require.IsType(t, &tablewriter.Table{}, table) |
| 116 | + |
| 117 | + table.Header("Name", "Age") |
| 118 | + _ = table.Append([]string{"John", "30"}) |
| 119 | + _ = table.Render() |
| 120 | + |
| 121 | + compareGolden(t, output.String(), "with_footer_option.golden") |
| 122 | + }) |
| 123 | + |
| 124 | + t.Run("EmptyTable", func(t *testing.T) { |
| 125 | + t.Parallel() |
| 126 | + |
| 127 | + var output bytes.Buffer |
| 128 | + table := NewTableWriter(&output) |
| 129 | + |
| 130 | + require.NotNil(t, table) |
| 131 | + require.IsType(t, &tablewriter.Table{}, table) |
| 132 | + |
| 133 | + table.Header("Name", "Age") |
| 134 | + _ = table.Render() |
| 135 | + |
| 136 | + compareGolden(t, output.String(), "empty_table.golden") |
| 137 | + }) |
| 138 | + |
| 139 | + t.Run("MultipleRows", func(t *testing.T) { |
| 140 | + t.Parallel() |
| 141 | + |
| 142 | + var output bytes.Buffer |
| 143 | + table := NewTableWriter(&output) |
| 144 | + |
| 145 | + require.NotNil(t, table) |
| 146 | + require.IsType(t, &tablewriter.Table{}, table) |
| 147 | + |
| 148 | + table.Header("Name", "Age", "City") |
| 149 | + _ = table.Append([]string{"John", "30", "New York"}) |
| 150 | + _ = table.Append([]string{"Jane", "25", "Boston"}) |
| 151 | + _ = table.Append([]string{"Bob", "35", "Chicago"}) |
| 152 | + _ = table.Render() |
| 153 | + |
| 154 | + compareGolden(t, output.String(), "multiple_rows.golden") |
| 155 | + }) |
| 156 | +} |
0 commit comments