package xml_test
import (
"strings"
"testing"
"github.com/tomwright/dasel/v3/model"
)
// TestXmlRoundTrip_ElementOrdering tests that interleaved same-named siblings
// preserve their original document order during round-trips (Issue #196).
func TestXmlRoundTrip_ElementOrdering(t *testing.T) {
tests := []struct {
name string
input string
expected string // exact expected output (use empty string to skip exact match)
contains []string // strings that must appear in output
orderedContains []string // strings that must appear in this order
}{
{
name: "issue 196 reproducer",
input: `
Look
Hello World
Above
Last
`,
expected: `
Look
Hello World
Above
Last
`,
},
{
name: "interleaved a-b-a pattern",
input: `
1
2
3
`,
expected: `
1
2
3
`,
},
{
name: "complex interleaving a-b-c-a-b",
input: `
1
2
3
4
5
`,
expected: `
1
2
3
4
5
`,
},
{
name: "all same name children",
input: `
- 1
- 2
- 3
`,
expected: `
- 1
- 2
- 3
`,
},
{
name: "no interleaving - unique children",
input: `
1
2
3
`,
expected: `
1
2
3
`,
},
{
name: "deeply nested interleaving",
input: `
1
2
3
text
4
5
6
`,
contains: []string{
"1",
"2",
"3",
"4",
"5",
"6",
},
orderedContains: []string{
"1",
"2",
"3",
},
},
{
name: "elements with attributes interleaved",
input: `
- first
middle
- second
`,
contains: []string{
`- first
`,
"middle",
`- second
`,
},
orderedContains: []string{
`- first
`,
"middle",
`- second
`,
},
},
{
name: "empty elements interleaved",
input: `
text
`,
expected: `
text
`,
},
{
name: "comments with interleaved elements",
input: `
1
2
3
`,
contains: []string{
"1",
"2",
"3",
},
orderedContains: []string{
"1",
"2",
"3",
},
},
{
name: "parent with text content and interleaved children",
input: `
Hello
World
Goodbye
`,
expected: `
Hello
World
Goodbye
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r, w := newTestReaderWriter(t)
data, err := r.Read([]byte(tt.input))
if err != nil {
t.Fatalf("Unexpected error reading XML: %s", err)
}
output, err := w.Write(data)
if err != nil {
t.Fatalf("Unexpected error writing XML: %s", err)
}
outputStr := string(output)
if tt.expected != "" {
if outputStr != tt.expected {
t.Errorf("Expected:\n%s\nGot:\n%s", tt.expected, outputStr)
}
}
for _, exp := range tt.contains {
if !strings.Contains(outputStr, exp) {
t.Errorf("Expected output to contain %q, got:\n%s", exp, outputStr)
}
}
if len(tt.orderedContains) > 1 {
assertOrderedContains(t, outputStr, tt.orderedContains)
}
})
}
}
// TestXmlRoundTrip_ElementOrderingMetadata tests that xml_child_order metadata
// is correctly populated during read.
func TestXmlRoundTrip_ElementOrderingMetadata(t *testing.T) {
t.Run("metadata records full child order", func(t *testing.T) {
r, _ := newTestReaderWriter(t)
data, err := r.Read([]byte(`
1
2
3
`))
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
rootVal, err := data.GetMapKey("root")
if err != nil {
t.Fatalf("Expected 'root' key: %s", err)
}
orderMeta, ok := rootVal.MetadataValue("xml_child_order")
if !ok {
t.Fatal("Expected xml_child_order metadata to exist")
}
order, ok := orderMeta.([]string)
if !ok {
t.Fatal("Expected xml_child_order to be []string")
}
expected := []string{"a", "b", "a"}
if len(order) != len(expected) {
t.Fatalf("Expected order length %d, got %d: %v", len(expected), len(order), order)
}
for i, name := range expected {
if order[i] != name {
t.Errorf("Expected order[%d] = %q, got %q", i, name, order[i])
}
}
})
t.Run("metadata set on elements with children", func(t *testing.T) {
r, _ := newTestReaderWriter(t)
data, err := r.Read([]byte(`text`))
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
rootVal, err := data.GetMapKey("root")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
// root has a child (leaf), so it should have ordering metadata
_, ok := rootVal.MetadataValue("xml_child_order")
if !ok {
t.Error("Expected xml_child_order metadata on root since it has children")
}
})
}
// TestXmlRoundTrip_FallbackWithoutMetadata tests that the writer correctly
// falls back to insertion-order iteration when xml_child_order is absent.
func TestXmlRoundTrip_FallbackWithoutMetadata(t *testing.T) {
t.Run("programmatically constructed value without metadata", func(t *testing.T) {
_, w := newTestReaderWriter(t)
// Build a map without xml_child_order metadata (simulating non-XML source).
root := model.NewMapValue()
inner := model.NewMapValue()
_ = inner.SetMapKey("b", model.NewStringValue("2"))
_ = inner.SetMapKey("a", model.NewStringValue("1"))
_ = root.SetMapKey("root", inner)
output, err := w.Write(root)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
outputStr := string(output)
// Without metadata, should use insertion order: b before a
bIdx := strings.Index(outputStr, "")
aIdx := strings.Index(outputStr, "")
if bIdx < 0 || aIdx < 0 {
t.Fatalf("Expected both and in output:\n%s", outputStr)
}
if bIdx > aIdx {
t.Errorf("Expected before (insertion order), got:\n%s", outputStr)
}
})
}
// TestXmlWriter_OrderingEdgeCases tests defensive code paths in the writer's
// ordering reconstruction logic.
func TestXmlWriter_OrderingEdgeCases(t *testing.T) {
t.Run("stale metadata - deleted key", func(t *testing.T) {
// Build a Value with xml_child_order referencing a key not in the map.
_, w := newTestReaderWriter(t)
root := model.NewMapValue()
inner := model.NewMapValue()
_ = inner.SetMapKey("a", model.NewStringValue("1"))
// "b" is NOT in the map, but IS in the ordering metadata.
inner.SetMetadataValue("xml_child_order", []string{"a", "b", "a"})
// Only "a" as scalar, so second "a" in metadata will also be skipped (scalar emitted once).
_ = root.SetMapKey("root", inner)
output, err := w.Write(root)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
outputStr := string(output)
if !strings.Contains(outputStr, "1") {
t.Errorf("Expected 1 in output, got:\n%s", outputStr)
}
// Should not panic or produce corrupt output.
})
t.Run("counter overflow - more metadata entries than slice values", func(t *testing.T) {
_, w := newTestReaderWriter(t)
root := model.NewMapValue()
inner := model.NewMapValue()
items := model.NewSliceValue()
_ = items.Append(model.NewStringValue("first"))
// Slice has 1 element, but metadata says 3 occurrences.
_ = inner.SetMapKey("item", items)
inner.SetMetadataValue("xml_child_order", []string{"item", "item", "item"})
_ = root.SetMapKey("root", inner)
output, err := w.Write(root)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
outputStr := string(output)
// Should produce only 1 - , not panic.
itemCount := strings.Count(outputStr, "
- ")
if itemCount != 1 {
t.Errorf("Expected exactly 1
- , got %d in:\n%s", itemCount, outputStr)
}
})
t.Run("new keys not in ordering - appended at end", func(t *testing.T) {
_, w := newTestReaderWriter(t)
root := model.NewMapValue()
inner := model.NewMapValue()
_ = inner.SetMapKey("a", model.NewStringValue("1"))
_ = inner.SetMapKey("b", model.NewStringValue("2"))
_ = inner.SetMapKey("c", model.NewStringValue("new"))
// Metadata only knows about "a" and "b", not "c".
inner.SetMetadataValue("xml_child_order", []string{"b", "a"})
_ = root.SetMapKey("root", inner)
output, err := w.Write(root)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
outputStr := string(output)
// "b" should come before "a" (per metadata), "c" appended at end.
bIdx := strings.Index(outputStr, "")
aIdx := strings.Index(outputStr, "")
cIdx := strings.Index(outputStr, "")
if bIdx < 0 || aIdx < 0 || cIdx < 0 {
t.Fatalf("Expected all three elements in output:\n%s", outputStr)
}
if bIdx > aIdx {
t.Errorf("Expected before per metadata ordering, got:\n%s", outputStr)
}
if cIdx < aIdx {
t.Errorf("Expected after (appended at end), got:\n%s", outputStr)
}
})
t.Run("invalid metadata type - fallback to insertion order", func(t *testing.T) {
_, w := newTestReaderWriter(t)
root := model.NewMapValue()
inner := model.NewMapValue()
_ = inner.SetMapKey("b", model.NewStringValue("2"))
_ = inner.SetMapKey("a", model.NewStringValue("1"))
// Set invalid metadata type (int instead of []string).
inner.SetMetadataValue("xml_child_order", 42)
_ = root.SetMapKey("root", inner)
output, err := w.Write(root)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
outputStr := string(output)
// Should fall back to insertion order: b before a.
bIdx := strings.Index(outputStr, "")
aIdx := strings.Index(outputStr, "")
if bIdx < 0 || aIdx < 0 {
t.Fatalf("Expected both elements in output:\n%s", outputStr)
}
if bIdx > aIdx {
t.Errorf("Expected fallback to insertion order ( before ), got:\n%s", outputStr)
}
})
t.Run("scalar duplicate guard - emit only once", func(t *testing.T) {
_, w := newTestReaderWriter(t)
root := model.NewMapValue()
inner := model.NewMapValue()
_ = inner.SetMapKey("a", model.NewStringValue("only-once"))
// Metadata says "a" twice, but value is scalar (not slice).
inner.SetMetadataValue("xml_child_order", []string{"a", "a"})
_ = root.SetMapKey("root", inner)
output, err := w.Write(root)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
outputStr := string(output)
aCount := strings.Count(outputStr, "")
if aCount != 1 {
t.Errorf("Expected exactly 1 element, got %d in:\n%s", aCount, outputStr)
}
})
t.Run("idempotent round-trip", func(t *testing.T) {
r, w := newTestReaderWriter(t)
input := `
1
2
3
`
// First round-trip
data1, err := r.Read([]byte(input))
if err != nil {
t.Fatalf("Read 1 error: %s", err)
}
output1, err := w.Write(data1)
if err != nil {
t.Fatalf("Write 1 error: %s", err)
}
// Second round-trip
data2, err := r.Read(output1)
if err != nil {
t.Fatalf("Read 2 error: %s", err)
}
output2, err := w.Write(data2)
if err != nil {
t.Fatalf("Write 2 error: %s", err)
}
if string(output1) != string(output2) {
t.Errorf("Round-trip not idempotent.\nFirst:\n%s\nSecond:\n%s", string(output1), string(output2))
}
})
}