|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package rules |
| 4 | + |
| 5 | +import "testing" |
| 6 | + |
| 7 | +func TestFindEmptyContainers_PageWithEmpty(t *testing.T) { |
| 8 | + rawData := map[string]any{ |
| 9 | + "FormCall": map[string]any{ |
| 10 | + "Arguments": []any{ |
| 11 | + map[string]any{ |
| 12 | + "Widgets": []any{ |
| 13 | + map[string]any{ |
| 14 | + "$Type": "Forms$DivContainer", |
| 15 | + "Name": "emptyDiv", |
| 16 | + "Widgets": []any{}, |
| 17 | + }, |
| 18 | + }, |
| 19 | + }, |
| 20 | + }, |
| 21 | + }, |
| 22 | + } |
| 23 | + |
| 24 | + result := findEmptyContainers(rawData) |
| 25 | + if len(result) != 1 { |
| 26 | + t.Fatalf("expected 1 empty container, got %d", len(result)) |
| 27 | + } |
| 28 | + if result[0].Name != "emptyDiv" { |
| 29 | + t.Errorf("expected name 'emptyDiv', got %q", result[0].Name) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestFindEmptyContainers_PageWithChildren(t *testing.T) { |
| 34 | + rawData := map[string]any{ |
| 35 | + "FormCall": map[string]any{ |
| 36 | + "Arguments": []any{ |
| 37 | + map[string]any{ |
| 38 | + "Widgets": []any{ |
| 39 | + map[string]any{ |
| 40 | + "$Type": "Forms$DivContainer", |
| 41 | + "Name": "filledDiv", |
| 42 | + "Widgets": []any{ |
| 43 | + map[string]any{ |
| 44 | + "$Type": "Forms$TextBox", |
| 45 | + "Name": "txt1", |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + result := findEmptyContainers(rawData) |
| 56 | + if len(result) != 0 { |
| 57 | + t.Errorf("expected 0 empty containers, got %d", len(result)) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestFindEmptyContainers_SnippetStructure(t *testing.T) { |
| 62 | + rawData := map[string]any{ |
| 63 | + "Widgets": []any{ |
| 64 | + map[string]any{ |
| 65 | + "$Type": "Forms$DivContainer", |
| 66 | + "Name": "snippetEmpty", |
| 67 | + "Widgets": []any{}, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + result := findEmptyContainers(rawData) |
| 73 | + if len(result) != 1 { |
| 74 | + t.Fatalf("expected 1 empty container, got %d", len(result)) |
| 75 | + } |
| 76 | + if result[0].Name != "snippetEmpty" { |
| 77 | + t.Errorf("expected name 'snippetEmpty', got %q", result[0].Name) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestFindEmptyContainers_NestedInLayoutGrid(t *testing.T) { |
| 82 | + rawData := map[string]any{ |
| 83 | + "FormCall": map[string]any{ |
| 84 | + "Arguments": []any{ |
| 85 | + map[string]any{ |
| 86 | + "Widgets": []any{ |
| 87 | + map[string]any{ |
| 88 | + "$Type": "Forms$LayoutGrid", |
| 89 | + "Name": "grid1", |
| 90 | + "Rows": []any{ |
| 91 | + map[string]any{ |
| 92 | + "Columns": []any{ |
| 93 | + map[string]any{ |
| 94 | + "Widgets": []any{ |
| 95 | + map[string]any{ |
| 96 | + "$Type": "Forms$DivContainer", |
| 97 | + "Name": "nestedEmpty", |
| 98 | + "Widgets": []any{}, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + result := findEmptyContainers(rawData) |
| 113 | + if len(result) != 1 { |
| 114 | + t.Fatalf("expected 1 empty container, got %d", len(result)) |
| 115 | + } |
| 116 | + if result[0].Name != "nestedEmpty" { |
| 117 | + t.Errorf("expected name 'nestedEmpty', got %q", result[0].Name) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestFindEmptyContainers_NestedInTabContainer(t *testing.T) { |
| 122 | + rawData := map[string]any{ |
| 123 | + "FormCall": map[string]any{ |
| 124 | + "Arguments": []any{ |
| 125 | + map[string]any{ |
| 126 | + "Widgets": []any{ |
| 127 | + map[string]any{ |
| 128 | + "$Type": "Forms$TabContainer", |
| 129 | + "Name": "tabs", |
| 130 | + "TabPages": []any{ |
| 131 | + map[string]any{ |
| 132 | + "Widgets": []any{ |
| 133 | + map[string]any{ |
| 134 | + "$Type": "Forms$DivContainer", |
| 135 | + "Name": "tabEmpty", |
| 136 | + "Widgets": []any{}, |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + result := findEmptyContainers(rawData) |
| 149 | + if len(result) != 1 { |
| 150 | + t.Fatalf("expected 1 empty container, got %d", len(result)) |
| 151 | + } |
| 152 | + if result[0].Name != "tabEmpty" { |
| 153 | + t.Errorf("expected name 'tabEmpty', got %q", result[0].Name) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func TestFindEmptyContainers_NonContainer(t *testing.T) { |
| 158 | + rawData := map[string]any{ |
| 159 | + "FormCall": map[string]any{ |
| 160 | + "Arguments": []any{ |
| 161 | + map[string]any{ |
| 162 | + "Widgets": []any{ |
| 163 | + map[string]any{ |
| 164 | + "$Type": "Forms$TextBox", |
| 165 | + "Name": "txt1", |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + result := findEmptyContainers(rawData) |
| 174 | + if len(result) != 0 { |
| 175 | + t.Errorf("expected 0 empty containers, got %d", len(result)) |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +func TestFindEmptyContainers_Multiple(t *testing.T) { |
| 180 | + rawData := map[string]any{ |
| 181 | + "FormCall": map[string]any{ |
| 182 | + "Arguments": []any{ |
| 183 | + map[string]any{ |
| 184 | + "Widgets": []any{ |
| 185 | + map[string]any{ |
| 186 | + "$Type": "Forms$DivContainer", |
| 187 | + "Name": "empty1", |
| 188 | + "Widgets": []any{}, |
| 189 | + }, |
| 190 | + map[string]any{ |
| 191 | + "$Type": "Forms$DivContainer", |
| 192 | + "Name": "empty2", |
| 193 | + "Widgets": []any{}, |
| 194 | + }, |
| 195 | + }, |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + } |
| 200 | + |
| 201 | + result := findEmptyContainers(rawData) |
| 202 | + if len(result) != 2 { |
| 203 | + t.Fatalf("expected 2 empty containers, got %d", len(result)) |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +func TestFindEmptyContainersRecursive_FooterWidgets(t *testing.T) { |
| 208 | + w := map[string]any{ |
| 209 | + "$Type": "Forms$SomeContainer", |
| 210 | + "Name": "outer", |
| 211 | + "FooterWidgets": []any{ |
| 212 | + map[string]any{ |
| 213 | + "$Type": "Forms$DivContainer", |
| 214 | + "Name": "footerEmpty", |
| 215 | + "Widgets": []any{}, |
| 216 | + }, |
| 217 | + }, |
| 218 | + } |
| 219 | + |
| 220 | + result := findEmptyContainersRecursive(w) |
| 221 | + if len(result) != 1 { |
| 222 | + t.Fatalf("expected 1 empty container, got %d", len(result)) |
| 223 | + } |
| 224 | + if result[0].Name != "footerEmpty" { |
| 225 | + t.Errorf("expected name 'footerEmpty', got %q", result[0].Name) |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +func TestEmptyContainerRule_Metadata(t *testing.T) { |
| 230 | + r := NewEmptyContainerRule() |
| 231 | + if r.ID() != "MPR006" { |
| 232 | + t.Errorf("ID = %q, want MPR006", r.ID()) |
| 233 | + } |
| 234 | + if r.Category() != "correctness" { |
| 235 | + t.Errorf("Category = %q, want correctness", r.Category()) |
| 236 | + } |
| 237 | + if r.Name() != "EmptyContainer" { |
| 238 | + t.Errorf("Name = %q, want EmptyContainer", r.Name()) |
| 239 | + } |
| 240 | +} |
0 commit comments