|
| 1 | +package tableview |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +type scalarStruct struct { |
| 11 | + Name string `json:"name"` |
| 12 | + Age int `json:"age"` |
| 13 | + Active bool `json:"is_active"` |
| 14 | + Score float64 `json:"score"` |
| 15 | +} |
| 16 | + |
| 17 | +type nestedStruct struct { |
| 18 | + ID string `json:"id"` |
| 19 | + Config struct { |
| 20 | + Key string |
| 21 | + } |
| 22 | + Label string `json:"label"` |
| 23 | +} |
| 24 | + |
| 25 | +type manyFieldsStruct struct { |
| 26 | + F1 string `json:"f1"` |
| 27 | + F2 string `json:"f2"` |
| 28 | + F3 string `json:"f3"` |
| 29 | + F4 string `json:"f4"` |
| 30 | + F5 string `json:"f5"` |
| 31 | + F6 string `json:"f6"` |
| 32 | + F7 string `json:"f7"` |
| 33 | + F8 string `json:"f8"` |
| 34 | + F9 string `json:"f9"` |
| 35 | + F10 string `json:"f10"` |
| 36 | +} |
| 37 | + |
| 38 | +type noExportedFields struct { |
| 39 | + hidden string //nolint:unused |
| 40 | +} |
| 41 | + |
| 42 | +type jsonTagStruct struct { |
| 43 | + WorkspaceID string `json:"workspace_id"` |
| 44 | + DisplayName string `json:"display_name"` |
| 45 | + NoTag string |
| 46 | +} |
| 47 | + |
| 48 | +func TestAutoDetectScalarFields(t *testing.T) { |
| 49 | + iter := &fakeIterator[scalarStruct]{items: []scalarStruct{{Name: "alice", Age: 30, Active: true, Score: 9.5}}} |
| 50 | + cfg := AutoDetect[scalarStruct](iter) |
| 51 | + require.NotNil(t, cfg) |
| 52 | + assert.Len(t, cfg.Columns, 4) |
| 53 | + assert.Equal(t, "Name", cfg.Columns[0].Header) |
| 54 | + assert.Equal(t, "Age", cfg.Columns[1].Header) |
| 55 | + assert.Equal(t, "Is Active", cfg.Columns[2].Header) |
| 56 | + assert.Equal(t, "Score", cfg.Columns[3].Header) |
| 57 | + |
| 58 | + val := scalarStruct{Name: "bob", Age: 25, Active: false, Score: 7.2} |
| 59 | + assert.Equal(t, "bob", cfg.Columns[0].Extract(val)) |
| 60 | + assert.Equal(t, "25", cfg.Columns[1].Extract(val)) |
| 61 | + assert.Equal(t, "false", cfg.Columns[2].Extract(val)) |
| 62 | + assert.Equal(t, "7.2", cfg.Columns[3].Extract(val)) |
| 63 | +} |
| 64 | + |
| 65 | +func TestAutoDetectSkipsNestedFields(t *testing.T) { |
| 66 | + iter := &fakeIterator[nestedStruct]{items: []nestedStruct{{ID: "123", Label: "test"}}} |
| 67 | + cfg := AutoDetect[nestedStruct](iter) |
| 68 | + require.NotNil(t, cfg) |
| 69 | + assert.Len(t, cfg.Columns, 2) |
| 70 | + assert.Equal(t, "ID", cfg.Columns[0].Header) |
| 71 | + assert.Equal(t, "Label", cfg.Columns[1].Header) |
| 72 | +} |
| 73 | + |
| 74 | +func TestAutoDetectPointerType(t *testing.T) { |
| 75 | + iter := &fakeIterator[*scalarStruct]{items: []*scalarStruct{{Name: "ptr", Age: 1}}} |
| 76 | + cfg := AutoDetect[*scalarStruct](iter) |
| 77 | + require.NotNil(t, cfg) |
| 78 | + assert.Len(t, cfg.Columns, 4) |
| 79 | + |
| 80 | + val := &scalarStruct{Name: "ptr", Age: 1} |
| 81 | + assert.Equal(t, "ptr", cfg.Columns[0].Extract(val)) |
| 82 | + assert.Equal(t, "1", cfg.Columns[1].Extract(val)) |
| 83 | +} |
| 84 | + |
| 85 | +func TestAutoDetectCappedAtMaxColumns(t *testing.T) { |
| 86 | + iter := &fakeIterator[manyFieldsStruct]{items: []manyFieldsStruct{{}}} |
| 87 | + cfg := AutoDetect[manyFieldsStruct](iter) |
| 88 | + require.NotNil(t, cfg) |
| 89 | + assert.Len(t, cfg.Columns, maxAutoColumns) |
| 90 | +} |
| 91 | + |
| 92 | +func TestAutoDetectNoExportedFields(t *testing.T) { |
| 93 | + iter := &fakeIterator[noExportedFields]{items: []noExportedFields{{}}} |
| 94 | + cfg := AutoDetect[noExportedFields](iter) |
| 95 | + assert.Nil(t, cfg) |
| 96 | +} |
| 97 | + |
| 98 | +func TestAutoDetectJsonTags(t *testing.T) { |
| 99 | + iter := &fakeIterator[jsonTagStruct]{items: []jsonTagStruct{{}}} |
| 100 | + cfg := AutoDetect[jsonTagStruct](iter) |
| 101 | + require.NotNil(t, cfg) |
| 102 | + assert.Equal(t, "Workspace ID", cfg.Columns[0].Header) |
| 103 | + assert.Equal(t, "Display Name", cfg.Columns[1].Header) |
| 104 | + assert.Equal(t, "NoTag", cfg.Columns[2].Header) |
| 105 | +} |
| 106 | + |
| 107 | +func TestAutoDetectCaching(t *testing.T) { |
| 108 | + iter1 := &fakeIterator[scalarStruct]{items: []scalarStruct{{}}} |
| 109 | + cfg1 := AutoDetect[scalarStruct](iter1) |
| 110 | + |
| 111 | + iter2 := &fakeIterator[scalarStruct]{items: []scalarStruct{{}}} |
| 112 | + cfg2 := AutoDetect[scalarStruct](iter2) |
| 113 | + |
| 114 | + // Should return the same cached pointer. |
| 115 | + assert.Same(t, cfg1, cfg2) |
| 116 | +} |
| 117 | + |
| 118 | +func TestSnakeToTitle(t *testing.T) { |
| 119 | + tests := []struct { |
| 120 | + input string |
| 121 | + expected string |
| 122 | + }{ |
| 123 | + {"workspace_id", "Workspace ID"}, |
| 124 | + {"display_name", "Display Name"}, |
| 125 | + {"id", "ID"}, |
| 126 | + {"simple", "Simple"}, |
| 127 | + {"a_b_c", "A B C"}, |
| 128 | + } |
| 129 | + for _, tt := range tests { |
| 130 | + assert.Equal(t, tt.expected, snakeToTitle(tt.input)) |
| 131 | + } |
| 132 | +} |
0 commit comments