|
| 1 | +package zapdiff |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestParseEntityUniqueIdentifierWithText(t *testing.T) { |
| 10 | + id := "configurator/cluster[name='Test']/domain[text()='General']" |
| 11 | + segments, err := parseEntityUniqueIdentifier(id) |
| 12 | + if err != nil { |
| 13 | + t.Fatal(err) |
| 14 | + } |
| 15 | + |
| 16 | + if len(segments) != 3 { |
| 17 | + t.Fatalf("expected 3 segments, got %d", len(segments)) |
| 18 | + } |
| 19 | + |
| 20 | + if segments[2].tag != "domain" { |
| 21 | + t.Errorf("expected tag domain, got %s", segments[2].tag) |
| 22 | + } |
| 23 | + if segments[2].attr != "text()" { |
| 24 | + t.Errorf("expected attr text(), got %s", segments[2].attr) |
| 25 | + } |
| 26 | + if segments[2].value != "General" { |
| 27 | + t.Errorf("expected value General, got %s", segments[2].value) |
| 28 | + } |
| 29 | + if segments[2].isAttr { |
| 30 | + t.Errorf("expected isAttr false, got true") |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestFindElementLinesWithText(t *testing.T) { |
| 35 | + content := `<configurator> |
| 36 | + <cluster> |
| 37 | + <name>Test</name> |
| 38 | + <domain>General</domain> |
| 39 | + <domain>Special</domain> |
| 40 | + </cluster> |
| 41 | +</configurator>` |
| 42 | + |
| 43 | + tmpFile, err := os.CreateTemp("", "zapdiff_test_*.xml") |
| 44 | + if err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + defer os.Remove(tmpFile.Name()) |
| 48 | + |
| 49 | + if _, err := tmpFile.WriteString(content); err != nil { |
| 50 | + t.Fatal(err) |
| 51 | + } |
| 52 | + tmpFile.Close() |
| 53 | + |
| 54 | + id := "configurator/cluster[name='Test']/domain[text()='Special']" |
| 55 | + lines, startLine, err := findElementLines(tmpFile.Name(), id) |
| 56 | + if err != nil { |
| 57 | + t.Fatal(err) |
| 58 | + } |
| 59 | + |
| 60 | + if len(lines) != 1 { |
| 61 | + t.Fatalf("expected 1 line, got %d", len(lines)) |
| 62 | + } |
| 63 | + |
| 64 | + if startLine != 5 { // 1-indexed, line 5 is "<domain>Special</domain>" |
| 65 | + t.Errorf("expected startLine 5, got %d", startLine) |
| 66 | + } |
| 67 | + |
| 68 | + if !strings.Contains(lines[0], "<domain>Special</domain>") { |
| 69 | + t.Errorf("expected line to contain <domain>Special</domain>, got %s", lines[0]) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestFindElementLinesWithChildAttributes(t *testing.T) { |
| 74 | + content := `<configurator> |
| 75 | + <cluster> |
| 76 | + <name>Test</name> |
| 77 | + <domain attr="val">Special</domain> |
| 78 | + </cluster> |
| 79 | +</configurator>` |
| 80 | + |
| 81 | + tmpFile, err := os.CreateTemp("", "zapdiff_test_*.xml") |
| 82 | + if err != nil { |
| 83 | + t.Fatal(err) |
| 84 | + } |
| 85 | + defer os.Remove(tmpFile.Name()) |
| 86 | + |
| 87 | + if _, err := tmpFile.WriteString(content); err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + tmpFile.Close() |
| 91 | + |
| 92 | + id := "configurator/cluster[domain='Special']" |
| 93 | + lines, startLine, err := findElementLines(tmpFile.Name(), id) |
| 94 | + if err != nil { |
| 95 | + t.Fatal(err) |
| 96 | + } |
| 97 | + |
| 98 | + if len(lines) != 4 { |
| 99 | + t.Fatalf("expected 4 lines, got %d", len(lines)) |
| 100 | + } |
| 101 | + |
| 102 | + if startLine != 2 { |
| 103 | + t.Errorf("expected startLine 2, got %d", startLine) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestFindElementLinesWithMultiLineSelfClosing(t *testing.T) { |
| 108 | + content := `<configurator> |
| 109 | + <cluster name="Test" |
| 110 | + code="0x1234" |
| 111 | + /> |
| 112 | +</configurator>` |
| 113 | + |
| 114 | + tmpFile, err := os.CreateTemp("", "zapdiff_test_*.xml") |
| 115 | + if err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + defer os.Remove(tmpFile.Name()) |
| 119 | + |
| 120 | + if _, err := tmpFile.WriteString(content); err != nil { |
| 121 | + t.Fatal(err) |
| 122 | + } |
| 123 | + tmpFile.Close() |
| 124 | + |
| 125 | + id := "configurator/cluster[@name='Test']" |
| 126 | + lines, startLine, err := findElementLines(tmpFile.Name(), id) |
| 127 | + if err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + |
| 131 | + if len(lines) != 3 { |
| 132 | + t.Fatalf("expected 3 lines, got %d", len(lines)) |
| 133 | + } |
| 134 | + |
| 135 | + if startLine != 2 { |
| 136 | + t.Errorf("expected startLine 2, got %d", startLine) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestGenerateUnifiedDiff(t *testing.T) { |
| 141 | + lines1 := []string{"line1\n", "line2\n", "line3\n"} |
| 142 | + lines2 := []string{"line1\n", "line2 modified\n", "line3\n"} |
| 143 | + |
| 144 | + diffStr, err := GenerateUnifiedDiff(lines1, lines2, 10, 20) |
| 145 | + if err != nil { |
| 146 | + t.Fatal(err) |
| 147 | + } |
| 148 | + |
| 149 | + expected := "--- Ref\n+++ Generated\n@@ -10,3 +20,3 @@\n line1\n-line2\n+line2 modified\n line3\n" |
| 150 | + if diffStr != expected { |
| 151 | + t.Errorf("expected diff:\n%s\ngot:\n%s", expected, diffStr) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func TestGenerateUnifiedDiffGrouped(t *testing.T) { |
| 156 | + lines1 := []string{"A\n", "B\n", "C\n", "D\n"} |
| 157 | + lines2 := []string{"A\n", "X\n", "Y\n", "D\n"} |
| 158 | + |
| 159 | + diffStr, err := GenerateUnifiedDiff(lines1, lines2, 10, 20) |
| 160 | + if err != nil { |
| 161 | + t.Fatal(err) |
| 162 | + } |
| 163 | + |
| 164 | + expected := "--- Ref\n+++ Generated\n@@ -10,4 +20,4 @@\n A\n-B\n-C\n+X\n+Y\n D\n" |
| 165 | + if diffStr != expected { |
| 166 | + t.Errorf("expected diff:\n%s\ngot:\n%s", expected, diffStr) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func TestGetCustomDiffLines(t *testing.T) { |
| 171 | + content := `<configurator> |
| 172 | + <cluster code="0x0028"/> |
| 173 | + <attribute code="0x0017" side="server"/> |
| 174 | +</configurator>` |
| 175 | + |
| 176 | + tmpFile, err := os.CreateTemp("", "test_diff_*.xml") |
| 177 | + if err != nil { |
| 178 | + t.Fatal(err) |
| 179 | + } |
| 180 | + defer os.Remove(tmpFile.Name()) |
| 181 | + |
| 182 | + if _, err := tmpFile.WriteString(content); err != nil { |
| 183 | + t.Fatal(err) |
| 184 | + } |
| 185 | + tmpFile.Close() |
| 186 | + |
| 187 | + // We need to mock getParentID or use a targetID that works with it. |
| 188 | + // getParentID removes the last segment. |
| 189 | + // If targetID is "configurator/cluster[@code='0x0028']/attribute[@code='0x0017']" |
| 190 | + // parentID will be "configurator/cluster[@code='0x0028']" |
| 191 | + // This matches the structure in our content (textually for findElementLines). |
| 192 | + |
| 193 | + targetID := "configurator/cluster[@code='0x0028']/attribute[@code='0x0017']" |
| 194 | + |
| 195 | + lines, _, err := getCustomDiffLines(tmpFile.Name(), targetID) |
| 196 | + if err != nil { |
| 197 | + t.Fatal(err) |
| 198 | + } |
| 199 | + |
| 200 | + // Expected lines: |
| 201 | + // 1. Parent start: <cluster code="0x0028"/> |
| 202 | + // 2. Target: <attribute code="0x0017" side="server"/> |
| 203 | + // Parent close should NOT be appended because it was self-closing (len=1). |
| 204 | + |
| 205 | + if len(lines) != 2 { |
| 206 | + t.Fatalf("expected 2 lines, got %d: %v", len(lines), lines) |
| 207 | + } |
| 208 | + |
| 209 | + if !strings.Contains(lines[0], "<cluster") { |
| 210 | + t.Errorf("expected cluster line, got %s", lines[0]) |
| 211 | + } |
| 212 | + if !strings.Contains(lines[1], "<attribute") { |
| 213 | + t.Errorf("expected attribute line, got %s", lines[1]) |
| 214 | + } |
| 215 | +} |
0 commit comments