Repository navigation
Expand file tree
/
Copy pathxmlcompare_test.go
More file actions
122 lines (111 loc) · 2.76 KB
/
Copy pathxmlcompare_test.go
File metadata and controls
122 lines (111 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package xmlcompare
import "testing"
func TestEqual_PassingCase_WhitespaceAndOrderInsensitive(t *testing.T) {
a := `
<root>
<a id="1"> hello world </a>
<b x="1" y="2"/>
<c><d>text</d></c>
</root>`
// Same content but different order, different whitespace, and attribute order flipped
b := `
<root>
<c>
<d>text</d>
</c>
<b y="2" x="1"/>
<a id="1">hello world</a>
</root>`
ok, err := Equal(a, b)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !ok {
t.Fatalf("expected XMLs to be considered equal")
}
}
func TestEqual_Error_WrongNamespace(t *testing.T) {
a := `<ns1:root xmlns:ns1="urn:x"><child/></ns1:root>`
b := `<ns2:root xmlns:ns2="urn:y"><child/></ns2:root>`
ok, err := Equal(a, b)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ok {
t.Fatalf("expected mismatch because of different namespaces")
}
}
func TestEqual_Error_MissingAttribute(t *testing.T) {
a := `<root id="123"/>`
b := `<root/>`
ok, err := Equal(a, b)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ok {
t.Fatalf("expected mismatch because expected has no @id, actual does")
}
}
func TestEqual_Error_WrongAttributeValue(t *testing.T) {
a := `<root id="123"/>`
b := `<root id="999"/>`
ok, err := Equal(a, b)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ok {
t.Fatalf("expected mismatch because attribute values differ")
}
}
func TestEqual_Error_AdditionalChildTag(t *testing.T) {
a := `<root><a/><b/></root>`
b := `<root><a/></root>`
ok, err := Equal(a, b)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ok {
t.Fatalf("expected mismatch because of additional child <b>")
}
}
func TestEqual_Error_MissingChildTag(t *testing.T) {
a := `<root><a/></root>`
b := `<root><a/><b/></root>`
ok, err := Equal(a, b)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ok {
t.Fatalf("expected mismatch because actual is missing child <b>")
}
}
func TestEqual_TextDifference(t *testing.T) {
a := `<root><msg>hello</msg></root>`
b := `<root><msg>world</msg></root>`
ok, err := Equal(a, b)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ok {
t.Fatalf("expected mismatch because text differs")
}
}
func TestEqual_TextWhitespaceIsNormalized(t *testing.T) {
a := `<root><msg> hello world </msg></root>`
b := `<root><msg>hello world</msg></root>`
ok, err := Equal(a, b)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !ok {
t.Fatalf("expected equality because whitespace should be normalized")
}
}
func TestEqual_ParsingError(t *testing.T) {
a := `<root>` // invalid XML
b := `<root/>`
ok, err := Equal(a, b)
if err == nil || ok {
t.Fatalf("expected parsing error and false, got ok=%v err=%v", ok, err)
}
}