|
1 | 1 | package table
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "github.com/stretchr/testify/require" |
4 | 6 | "strings"
|
5 | 7 | "testing"
|
6 | 8 | "unicode/utf8"
|
@@ -150,6 +152,127 @@ func TestTable_AppendRows(t *testing.T) {
|
150 | 152 | assert.True(t, table.rowsConfigMap[3].AutoMerge)
|
151 | 153 | }
|
152 | 154 |
|
| 155 | +func TestTable_ImportGrid(t *testing.T) { |
| 156 | + t.Run("invalid grid", func(t *testing.T) { |
| 157 | + table := Table{} |
| 158 | + |
| 159 | + assert.False(t, table.ImportGrid(nil)) |
| 160 | + require.Len(t, table.rowsRaw, 0) |
| 161 | + |
| 162 | + assert.False(t, table.ImportGrid(123)) |
| 163 | + require.Len(t, table.rowsRaw, 0) |
| 164 | + |
| 165 | + assert.False(t, table.ImportGrid("abc")) |
| 166 | + require.Len(t, table.rowsRaw, 0) |
| 167 | + |
| 168 | + assert.False(t, table.ImportGrid(Table{})) |
| 169 | + require.Len(t, table.rowsRaw, 0) |
| 170 | + |
| 171 | + assert.False(t, table.ImportGrid(&Table{})) |
| 172 | + require.Len(t, table.rowsRaw, 0) |
| 173 | + }) |
| 174 | + |
| 175 | + a, b, c := 1, 2, 3 |
| 176 | + d, e, f := 4, 5, 6 |
| 177 | + g, h, i := 7, 8, 9 |
| 178 | + |
| 179 | + t.Run("valid 1d", func(t *testing.T) { |
| 180 | + inputs := []interface{}{ |
| 181 | + [3]int{a, b, c}, // array |
| 182 | + []int{a, b, c}, // slice |
| 183 | + &[]int{a, b, c}, // pointer to slice |
| 184 | + []*int{&a, &b, &c}, // slice of pointers-to-slices |
| 185 | + &[]*int{&a, &b, &c}, // pointer to slice of pointers |
| 186 | + } |
| 187 | + |
| 188 | + for _, grid := range inputs { |
| 189 | + message := fmt.Sprintf("grid: %#v", grid) |
| 190 | + |
| 191 | + table := Table{} |
| 192 | + table.Style().Options.SeparateRows = true |
| 193 | + assert.True(t, table.ImportGrid(grid), message) |
| 194 | + compareOutput(t, table.Render(), ` |
| 195 | ++---+ |
| 196 | +| 1 | |
| 197 | ++---+ |
| 198 | +| 2 | |
| 199 | ++---+ |
| 200 | +| 3 | |
| 201 | ++---+`, message) |
| 202 | + } |
| 203 | + }) |
| 204 | + |
| 205 | + t.Run("valid 2d", func(t *testing.T) { |
| 206 | + inputs := []interface{}{ |
| 207 | + [3][3]int{{a, b, c}, {d, e, f}, {g, h, i}}, // array of arrays |
| 208 | + [3][]int{{a, b, c}, {d, e, f}, {g, h, i}}, // array of slices |
| 209 | + [][]int{{a, b, c}, {d, e, f}, {g, h, i}}, // slice of slices |
| 210 | + &[][]int{{a, b, c}, {d, e, f}, {g, h, i}}, // pointer-to-slice of slices |
| 211 | + []*[]int{{a, b, c}, {d, e, f}, {g, h, i}}, // slice of pointers-to-slices |
| 212 | + &[]*[]int{{a, b, c}, {d, e, f}, {g, h, i}}, // pointer-to-slice of pointers-to-slices |
| 213 | + &[]*[]*int{{&a, &b, &c}, {&d, &e, &f}, {&g, &h, &i}}, // pointer-to-slice of pointers-to-slices of pointers |
| 214 | + } |
| 215 | + |
| 216 | + for _, grid := range inputs { |
| 217 | + message := fmt.Sprintf("grid: %#v", grid) |
| 218 | + |
| 219 | + table := Table{} |
| 220 | + table.Style().Options.SeparateRows = true |
| 221 | + assert.True(t, table.ImportGrid(grid), message) |
| 222 | + compareOutput(t, table.Render(), ` |
| 223 | ++---+---+---+ |
| 224 | +| 1 | 2 | 3 | |
| 225 | ++---+---+---+ |
| 226 | +| 4 | 5 | 6 | |
| 227 | ++---+---+---+ |
| 228 | +| 7 | 8 | 9 | |
| 229 | ++---+---+---+`, message) |
| 230 | + } |
| 231 | + }) |
| 232 | + |
| 233 | + t.Run("valid 2d with nil rows", func(t *testing.T) { |
| 234 | + inputs := []interface{}{ |
| 235 | + []*[]int{{a, b, c}, {d, e, f}, nil}, // slice of pointers-to-slices |
| 236 | + &[]*[]int{{a, b, c}, {d, e, f}, nil}, // pointer-to-slice of pointers-to-slices |
| 237 | + &[]*[]*int{{&a, &b, &c}, {&d, &e, &f}, nil}, // pointer-to-slice of pointers-to-slices of pointers |
| 238 | + } |
| 239 | + |
| 240 | + for _, grid := range inputs { |
| 241 | + message := fmt.Sprintf("grid: %#v", grid) |
| 242 | + |
| 243 | + table := Table{} |
| 244 | + table.Style().Options.SeparateRows = true |
| 245 | + assert.True(t, table.ImportGrid(grid), message) |
| 246 | + compareOutput(t, table.Render(), ` |
| 247 | ++---+---+---+ |
| 248 | +| 1 | 2 | 3 | |
| 249 | ++---+---+---+ |
| 250 | +| 4 | 5 | 6 | |
| 251 | ++---+---+---+`, message) |
| 252 | + } |
| 253 | + }) |
| 254 | + |
| 255 | + t.Run("valid 2d with nil columns and rows", func(t *testing.T) { |
| 256 | + inputs := []interface{}{ |
| 257 | + &[]*[]*int{{&a, &b, &c}, {&d, &e, nil}, nil}, |
| 258 | + } |
| 259 | + |
| 260 | + for _, grid := range inputs { |
| 261 | + message := fmt.Sprintf("grid: %#v", grid) |
| 262 | + |
| 263 | + table := Table{} |
| 264 | + table.Style().Options.SeparateRows = true |
| 265 | + assert.True(t, table.ImportGrid(grid), message) |
| 266 | + compareOutput(t, table.Render(), ` |
| 267 | ++---+---+---+ |
| 268 | +| 1 | 2 | 3 | |
| 269 | ++---+---+---+ |
| 270 | +| 4 | 5 | | |
| 271 | ++---+---+---+`, message) |
| 272 | + } |
| 273 | + }) |
| 274 | +} |
| 275 | + |
153 | 276 | func TestTable_Length(t *testing.T) {
|
154 | 277 | table := Table{}
|
155 | 278 | assert.Zero(t, table.Length())
|
|
0 commit comments