| 1 | package xml_test |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/tomwright/dasel/v3/model" |
| 8 | ) |
| 9 | |
| 10 | // TestXmlRoundTrip_ElementOrdering tests that interleaved same-named siblings |
| 11 | // preserve their original document order during round-trips (Issue #196). |
| 12 | func TestXmlRoundTrip_ElementOrdering(t *testing.T) { |
| 13 | tests := []struct { |
| 14 | name string |
| 15 | input string |
| 16 | expected string // exact expected output (use empty string to skip exact match) |
| 17 | contains []string // strings that must appear in output |
| 18 | orderedContains []string // strings that must appear in this order |
| 19 | }{ |
| 20 | { |
| 21 | name: "issue 196 reproducer", |
| 22 | input: `<message> |
| 23 | <heading>Look</heading> |
| 24 | <warning>Hello World</warning> |
| 25 | <heading>Above</heading> |
| 26 | <string>Last</string> |
| 27 | </message>`, |
| 28 | expected: `<message> |
| 29 | <heading>Look</heading> |
| 30 | <warning>Hello World</warning> |
| 31 | <heading>Above</heading> |
| 32 | <string>Last</string> |
| 33 | </message> |
| 34 | `, |
| 35 | }, |
| 36 | { |
| 37 | name: "interleaved a-b-a pattern", |
| 38 | input: `<root> |
| 39 | <a>1</a> |
| 40 | <b>2</b> |
| 41 | <a>3</a> |
| 42 | </root>`, |
| 43 | expected: `<root> |
| 44 | <a>1</a> |
| 45 | <b>2</b> |
| 46 | <a>3</a> |
| 47 | </root> |
| 48 | `, |
| 49 | }, |
| 50 | { |
| 51 | name: "complex interleaving a-b-c-a-b", |
| 52 | input: `<root> |
| 53 | <a>1</a> |
| 54 | <b>2</b> |
| 55 | <c>3</c> |
| 56 | <a>4</a> |
| 57 | <b>5</b> |
| 58 | </root>`, |
| 59 | expected: `<root> |
| 60 | <a>1</a> |
| 61 | <b>2</b> |
| 62 | <c>3</c> |
| 63 | <a>4</a> |
| 64 | <b>5</b> |
| 65 | </root> |
| 66 | `, |
| 67 | }, |
| 68 | { |
| 69 | name: "all same name children", |
| 70 | input: `<root> |
| 71 | <item>1</item> |
| 72 | <item>2</item> |
| 73 | <item>3</item> |
| 74 | </root>`, |
| 75 | expected: `<root> |
| 76 | <item>1</item> |
| 77 | <item>2</item> |
| 78 | <item>3</item> |
| 79 | </root> |
| 80 | `, |
| 81 | }, |
| 82 | { |
| 83 | name: "no interleaving - unique children", |
| 84 | input: `<root> |
| 85 | <a>1</a> |
| 86 | <b>2</b> |
| 87 | <c>3</c> |
| 88 | </root>`, |
| 89 | expected: `<root> |
| 90 | <a>1</a> |
| 91 | <b>2</b> |
| 92 | <c>3</c> |
| 93 | </root> |
| 94 | `, |
| 95 | }, |
| 96 | { |
| 97 | name: "deeply nested interleaving", |
| 98 | input: `<root> |
| 99 | <parent> |
| 100 | <x>1</x> |
| 101 | <y>2</y> |
| 102 | <x>3</x> |
| 103 | </parent> |
| 104 | <other>text</other> |
| 105 | <parent> |
| 106 | <a>4</a> |
| 107 | <b>5</b> |
| 108 | <a>6</a> |
| 109 | </parent> |
| 110 | </root>`, |
| 111 | contains: []string{ |
| 112 | "<x>1</x>", |
| 113 | "<y>2</y>", |
| 114 | "<x>3</x>", |
| 115 | "<a>4</a>", |
| 116 | "<b>5</b>", |
| 117 | "<a>6</a>", |
| 118 | }, |
| 119 | orderedContains: []string{ |
| 120 | "<x>1</x>", |
| 121 | "<y>2</y>", |
| 122 | "<x>3</x>", |
| 123 | }, |
| 124 | }, |
| 125 | { |
| 126 | name: "elements with attributes interleaved", |
| 127 | input: `<root> |
| 128 | <item id="1">first</item> |
| 129 | <other>middle</other> |
| 130 | <item id="2">second</item> |
| 131 | </root>`, |
| 132 | contains: []string{ |
| 133 | `<item id="1">first</item>`, |
| 134 | "<other>middle</other>", |
| 135 | `<item id="2">second</item>`, |
| 136 | }, |
| 137 | orderedContains: []string{ |
| 138 | `<item id="1">first</item>`, |
| 139 | "<other>middle</other>", |
| 140 | `<item id="2">second</item>`, |
| 141 | }, |
| 142 | }, |
| 143 | { |
| 144 | name: "empty elements interleaved", |
| 145 | input: `<root> |
| 146 | <a></a> |
| 147 | <b>text</b> |
| 148 | <a></a> |
| 149 | </root>`, |
| 150 | expected: `<root> |
| 151 | <a></a> |
| 152 | <b>text</b> |
| 153 | <a></a> |
| 154 | </root> |
| 155 | `, |
| 156 | }, |
| 157 | { |
| 158 | name: "comments with interleaved elements", |
| 159 | input: `<root> |
| 160 | <!-- first comment --> |
| 161 | <a>1</a> |
| 162 | <!-- middle comment --> |
| 163 | <b>2</b> |
| 164 | <a>3</a> |
| 165 | </root>`, |
| 166 | contains: []string{ |
| 167 | "<a>1</a>", |
| 168 | "<b>2</b>", |
| 169 | "<a>3</a>", |
| 170 | }, |
| 171 | orderedContains: []string{ |
| 172 | "<a>1</a>", |
| 173 | "<b>2</b>", |
| 174 | "<a>3</a>", |
| 175 | }, |
| 176 | }, |
| 177 | { |
| 178 | name: "parent with text content and interleaved children", |
| 179 | input: `<doc> |
| 180 | <title>Hello</title> |
| 181 | <para>World</para> |
| 182 | <title>Goodbye</title> |
| 183 | </doc>`, |
| 184 | expected: `<doc> |
| 185 | <title>Hello</title> |
| 186 | <para>World</para> |
| 187 | <title>Goodbye</title> |
| 188 | </doc> |
| 189 | `, |
| 190 | }, |
| 191 | } |
| 192 | |
| 193 | for _, tt := range tests { |
| 194 | t.Run(tt.name, func(t *testing.T) { |
| 195 | r, w := newTestReaderWriter(t) |
| 196 | |
| 197 | data, err := r.Read([]byte(tt.input)) |
| 198 | if err != nil { |
| 199 | t.Fatalf("Unexpected error reading XML: %s", err) |
| 200 | } |
| 201 | |
| 202 | output, err := w.Write(data) |
| 203 | if err != nil { |
| 204 | t.Fatalf("Unexpected error writing XML: %s", err) |
| 205 | } |
| 206 | |
| 207 | outputStr := string(output) |
| 208 | |
| 209 | if tt.expected != "" { |
| 210 | if outputStr != tt.expected { |
| 211 | t.Errorf("Expected:\n%s\nGot:\n%s", tt.expected, outputStr) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | for _, exp := range tt.contains { |
| 216 | if !strings.Contains(outputStr, exp) { |
| 217 | t.Errorf("Expected output to contain %q, got:\n%s", exp, outputStr) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if len(tt.orderedContains) > 1 { |
| 222 | assertOrderedContains(t, outputStr, tt.orderedContains) |
| 223 | } |
| 224 | }) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // TestXmlRoundTrip_ElementOrderingMetadata tests that xml_child_order metadata |
| 229 | // is correctly populated during read. |
| 230 | func TestXmlRoundTrip_ElementOrderingMetadata(t *testing.T) { |
| 231 | t.Run("metadata records full child order", func(t *testing.T) { |
| 232 | r, _ := newTestReaderWriter(t) |
| 233 | |
| 234 | data, err := r.Read([]byte(`<root> |
| 235 | <a>1</a> |
| 236 | <b>2</b> |
| 237 | <a>3</a> |
| 238 | </root>`)) |
| 239 | if err != nil { |
| 240 | t.Fatalf("Unexpected error: %s", err) |
| 241 | } |
| 242 | |
| 243 | rootVal, err := data.GetMapKey("root") |
| 244 | if err != nil { |
| 245 | t.Fatalf("Expected 'root' key: %s", err) |
| 246 | } |
| 247 | |
| 248 | orderMeta, ok := rootVal.MetadataValue("xml_child_order") |
| 249 | if !ok { |
| 250 | t.Fatal("Expected xml_child_order metadata to exist") |
| 251 | } |
| 252 | |
| 253 | order, ok := orderMeta.([]string) |
| 254 | if !ok { |
| 255 | t.Fatal("Expected xml_child_order to be []string") |
| 256 | } |
| 257 | |
| 258 | expected := []string{"a", "b", "a"} |
| 259 | if len(order) != len(expected) { |
| 260 | t.Fatalf("Expected order length %d, got %d: %v", len(expected), len(order), order) |
| 261 | } |
| 262 | for i, name := range expected { |
| 263 | if order[i] != name { |
| 264 | t.Errorf("Expected order[%d] = %q, got %q", i, name, order[i]) |
| 265 | } |
| 266 | } |
| 267 | }) |
| 268 | |
| 269 | t.Run("metadata set on elements with children", func(t *testing.T) { |
| 270 | r, _ := newTestReaderWriter(t) |
| 271 | |
| 272 | data, err := r.Read([]byte(`<root><leaf>text</leaf></root>`)) |
| 273 | if err != nil { |
| 274 | t.Fatalf("Unexpected error: %s", err) |
| 275 | } |
| 276 | |
| 277 | rootVal, err := data.GetMapKey("root") |
| 278 | if err != nil { |
| 279 | t.Fatalf("Unexpected error: %s", err) |
| 280 | } |
| 281 | |
| 282 | // root has a child (leaf), so it should have ordering metadata |
| 283 | _, ok := rootVal.MetadataValue("xml_child_order") |
| 284 | if !ok { |
| 285 | t.Error("Expected xml_child_order metadata on root since it has children") |
| 286 | } |
| 287 | }) |
| 288 | } |
| 289 | |
| 290 | // TestXmlRoundTrip_FallbackWithoutMetadata tests that the writer correctly |
| 291 | // falls back to insertion-order iteration when xml_child_order is absent. |
| 292 | func TestXmlRoundTrip_FallbackWithoutMetadata(t *testing.T) { |
| 293 | t.Run("programmatically constructed value without metadata", func(t *testing.T) { |
| 294 | _, w := newTestReaderWriter(t) |
| 295 | |
| 296 | // Build a map without xml_child_order metadata (simulating non-XML source). |
| 297 | root := model.NewMapValue() |
| 298 | inner := model.NewMapValue() |
| 299 | _ = inner.SetMapKey("b", model.NewStringValue("2")) |
| 300 | _ = inner.SetMapKey("a", model.NewStringValue("1")) |
| 301 | _ = root.SetMapKey("root", inner) |
| 302 | |
| 303 | output, err := w.Write(root) |
| 304 | if err != nil { |
| 305 | t.Fatalf("Unexpected error: %s", err) |
| 306 | } |
| 307 | |
| 308 | outputStr := string(output) |
| 309 | // Without metadata, should use insertion order: b before a |
| 310 | bIdx := strings.Index(outputStr, "<b>") |
| 311 | aIdx := strings.Index(outputStr, "<a>") |
| 312 | if bIdx < 0 || aIdx < 0 { |
| 313 | t.Fatalf("Expected both <b> and <a> in output:\n%s", outputStr) |
| 314 | } |
| 315 | if bIdx > aIdx { |
| 316 | t.Errorf("Expected <b> before <a> (insertion order), got:\n%s", outputStr) |
| 317 | } |
| 318 | }) |
| 319 | } |
| 320 | |
| 321 | // TestXmlWriter_OrderingEdgeCases tests defensive code paths in the writer's |
| 322 | // ordering reconstruction logic. |
| 323 | func TestXmlWriter_OrderingEdgeCases(t *testing.T) { |
| 324 | t.Run("stale metadata - deleted key", func(t *testing.T) { |
| 325 | // Build a Value with xml_child_order referencing a key not in the map. |
| 326 | _, w := newTestReaderWriter(t) |
| 327 | |
| 328 | root := model.NewMapValue() |
| 329 | inner := model.NewMapValue() |
| 330 | _ = inner.SetMapKey("a", model.NewStringValue("1")) |
| 331 | // "b" is NOT in the map, but IS in the ordering metadata. |
| 332 | inner.SetMetadataValue("xml_child_order", []string{"a", "b", "a"}) |
| 333 | // Only "a" as scalar, so second "a" in metadata will also be skipped (scalar emitted once). |
| 334 | _ = root.SetMapKey("root", inner) |
| 335 | |
| 336 | output, err := w.Write(root) |
| 337 | if err != nil { |
| 338 | t.Fatalf("Unexpected error: %s", err) |
| 339 | } |
| 340 | |
| 341 | outputStr := string(output) |
| 342 | if !strings.Contains(outputStr, "<a>1</a>") { |
| 343 | t.Errorf("Expected <a>1</a> in output, got:\n%s", outputStr) |
| 344 | } |
| 345 | // Should not panic or produce corrupt output. |
| 346 | }) |
| 347 | |
| 348 | t.Run("counter overflow - more metadata entries than slice values", func(t *testing.T) { |
| 349 | _, w := newTestReaderWriter(t) |
| 350 | |
| 351 | root := model.NewMapValue() |
| 352 | inner := model.NewMapValue() |
| 353 | items := model.NewSliceValue() |
| 354 | _ = items.Append(model.NewStringValue("first")) |
| 355 | // Slice has 1 element, but metadata says 3 occurrences. |
| 356 | _ = inner.SetMapKey("item", items) |
| 357 | inner.SetMetadataValue("xml_child_order", []string{"item", "item", "item"}) |
| 358 | _ = root.SetMapKey("root", inner) |
| 359 | |
| 360 | output, err := w.Write(root) |
| 361 | if err != nil { |
| 362 | t.Fatalf("Unexpected error: %s", err) |
| 363 | } |
| 364 | |
| 365 | outputStr := string(output) |
| 366 | // Should produce only 1 <item>, not panic. |
| 367 | itemCount := strings.Count(outputStr, "<item>") |
| 368 | if itemCount != 1 { |
| 369 | t.Errorf("Expected exactly 1 <item>, got %d in:\n%s", itemCount, outputStr) |
| 370 | } |
| 371 | }) |
| 372 | |
| 373 | t.Run("new keys not in ordering - appended at end", func(t *testing.T) { |
| 374 | _, w := newTestReaderWriter(t) |
| 375 | |
| 376 | root := model.NewMapValue() |
| 377 | inner := model.NewMapValue() |
| 378 | _ = inner.SetMapKey("a", model.NewStringValue("1")) |
| 379 | _ = inner.SetMapKey("b", model.NewStringValue("2")) |
| 380 | _ = inner.SetMapKey("c", model.NewStringValue("new")) |
| 381 | // Metadata only knows about "a" and "b", not "c". |
| 382 | inner.SetMetadataValue("xml_child_order", []string{"b", "a"}) |
| 383 | _ = root.SetMapKey("root", inner) |
| 384 | |
| 385 | output, err := w.Write(root) |
| 386 | if err != nil { |
| 387 | t.Fatalf("Unexpected error: %s", err) |
| 388 | } |
| 389 | |
| 390 | outputStr := string(output) |
| 391 | // "b" should come before "a" (per metadata), "c" appended at end. |
| 392 | bIdx := strings.Index(outputStr, "<b>") |
| 393 | aIdx := strings.Index(outputStr, "<a>") |
| 394 | cIdx := strings.Index(outputStr, "<c>") |
| 395 | if bIdx < 0 || aIdx < 0 || cIdx < 0 { |
| 396 | t.Fatalf("Expected all three elements in output:\n%s", outputStr) |
| 397 | } |
| 398 | if bIdx > aIdx { |
| 399 | t.Errorf("Expected <b> before <a> per metadata ordering, got:\n%s", outputStr) |
| 400 | } |
| 401 | if cIdx < aIdx { |
| 402 | t.Errorf("Expected <c> after <a> (appended at end), got:\n%s", outputStr) |
| 403 | } |
| 404 | }) |
| 405 | |
| 406 | t.Run("invalid metadata type - fallback to insertion order", func(t *testing.T) { |
| 407 | _, w := newTestReaderWriter(t) |
| 408 | |
| 409 | root := model.NewMapValue() |
| 410 | inner := model.NewMapValue() |
| 411 | _ = inner.SetMapKey("b", model.NewStringValue("2")) |
| 412 | _ = inner.SetMapKey("a", model.NewStringValue("1")) |
| 413 | // Set invalid metadata type (int instead of []string). |
| 414 | inner.SetMetadataValue("xml_child_order", 42) |
| 415 | _ = root.SetMapKey("root", inner) |
| 416 | |
| 417 | output, err := w.Write(root) |
| 418 | if err != nil { |
| 419 | t.Fatalf("Unexpected error: %s", err) |
| 420 | } |
| 421 | |
| 422 | outputStr := string(output) |
| 423 | // Should fall back to insertion order: b before a. |
| 424 | bIdx := strings.Index(outputStr, "<b>") |
| 425 | aIdx := strings.Index(outputStr, "<a>") |
| 426 | if bIdx < 0 || aIdx < 0 { |
| 427 | t.Fatalf("Expected both elements in output:\n%s", outputStr) |
| 428 | } |
| 429 | if bIdx > aIdx { |
| 430 | t.Errorf("Expected fallback to insertion order (<b> before <a>), got:\n%s", outputStr) |
| 431 | } |
| 432 | }) |
| 433 | |
| 434 | t.Run("scalar duplicate guard - emit only once", func(t *testing.T) { |
| 435 | _, w := newTestReaderWriter(t) |
| 436 | |
| 437 | root := model.NewMapValue() |
| 438 | inner := model.NewMapValue() |
| 439 | _ = inner.SetMapKey("a", model.NewStringValue("only-once")) |
| 440 | // Metadata says "a" twice, but value is scalar (not slice). |
| 441 | inner.SetMetadataValue("xml_child_order", []string{"a", "a"}) |
| 442 | _ = root.SetMapKey("root", inner) |
| 443 | |
| 444 | output, err := w.Write(root) |
| 445 | if err != nil { |
| 446 | t.Fatalf("Unexpected error: %s", err) |
| 447 | } |
| 448 | |
| 449 | outputStr := string(output) |
| 450 | aCount := strings.Count(outputStr, "<a>") |
| 451 | if aCount != 1 { |
| 452 | t.Errorf("Expected exactly 1 <a> element, got %d in:\n%s", aCount, outputStr) |
| 453 | } |
| 454 | }) |
| 455 | |
| 456 | t.Run("idempotent round-trip", func(t *testing.T) { |
| 457 | r, w := newTestReaderWriter(t) |
| 458 | |
| 459 | input := `<root> |
| 460 | <a>1</a> |
| 461 | <b>2</b> |
| 462 | <a>3</a> |
| 463 | </root>` |
| 464 | |
| 465 | // First round-trip |
| 466 | data1, err := r.Read([]byte(input)) |
| 467 | if err != nil { |
| 468 | t.Fatalf("Read 1 error: %s", err) |
| 469 | } |
| 470 | output1, err := w.Write(data1) |
| 471 | if err != nil { |
| 472 | t.Fatalf("Write 1 error: %s", err) |
| 473 | } |
| 474 | |
| 475 | // Second round-trip |
| 476 | data2, err := r.Read(output1) |
| 477 | if err != nil { |
| 478 | t.Fatalf("Read 2 error: %s", err) |
| 479 | } |
| 480 | output2, err := w.Write(data2) |
| 481 | if err != nil { |
| 482 | t.Fatalf("Write 2 error: %s", err) |
| 483 | } |
| 484 | |
| 485 | if string(output1) != string(output2) { |
| 486 | t.Errorf("Round-trip not idempotent.\nFirst:\n%s\nSecond:\n%s", string(output1), string(output2)) |
| 487 | } |
| 488 | }) |
| 489 | } |