-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_test.go
More file actions
525 lines (418 loc) · 12.3 KB
/
xml_test.go
File metadata and controls
525 lines (418 loc) · 12.3 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package flexml
import (
"strings"
"testing"
)
func TestParseValidXML(t *testing.T) {
// Test the basic case from the example in the requirements
xml := `<response><message>Greetings</message></response>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
nodes, ok := doc.DeepFind("message")
if !ok {
t.Fatal("Failed to find message element")
}
if len(nodes) != 1 {
t.Fatalf("Expected 1 message node, got %d", len(nodes))
}
if nodes[0].GetText() != "Greetings" {
t.Fatalf("Expected text 'Greetings', got '%s'", nodes[0].GetText())
}
}
func TestParsePartialXML(t *testing.T) {
// Test with unclosed tag as in the requirements
xml := `<key>Hello`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
nodes, ok := doc.DeepFind("key")
if !ok {
t.Fatal("Failed to find key element")
}
if len(nodes) != 1 {
t.Fatalf("Expected 1 key node, got %d", len(nodes))
}
if nodes[0].GetText() != "Hello" {
t.Fatalf("Expected text 'Hello', got '%s'", nodes[0].GetText())
}
}
func TestParseInvalidXML(t *testing.T) {
// Test with text before XML as in the requirements
xml := `Hello how are you
<name>James</name>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
// Check that the text is preserved
if doc.Root.Children[0].Type != TextNode {
t.Fatal("First child should be a text node")
}
if doc.Root.Children[0].Value != "Hello how are you\n" {
t.Fatalf("Expected text 'Hello how are you\\n', got '%s'", doc.Root.Children[0].Value)
}
// Check that the name element is found
nodes, ok := doc.DeepFind("name")
if !ok {
t.Fatal("Failed to find name element")
}
if len(nodes) != 1 {
t.Fatalf("Expected 1 name node, got %d", len(nodes))
}
if nodes[0].GetText() != "James" {
t.Fatalf("Expected text 'James', got '%s'", nodes[0].GetText())
}
}
func TestNestedElements(t *testing.T) {
// Test parsing nested elements
xml := `<outer><inner>Nested content</inner></outer>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
nodes, ok := doc.DeepFind("inner")
if !ok {
t.Fatal("Failed to find inner element")
}
if len(nodes) != 1 {
t.Fatalf("Expected 1 inner node, got %d", len(nodes))
}
if nodes[0].GetText() != "Nested content" {
t.Fatalf("Expected text 'Nested content', got '%s'", nodes[0].GetText())
}
}
func TestAttributes(t *testing.T) {
// Test parsing attributes
xml := `<user name="John" age="30" />`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
nodes, ok := doc.DeepFind("user")
if !ok {
t.Fatal("Failed to find user element")
}
if len(nodes) != 1 {
t.Fatalf("Expected 1 user node, got %d", len(nodes))
}
nameAttr, ok := nodes[0].GetAttribute("name")
if !ok {
t.Fatal("Failed to find name attribute")
}
if nameAttr != "John" {
t.Fatalf("Expected name attribute 'John', got '%s'", nameAttr)
}
ageAttr, ok := nodes[0].GetAttribute("age")
if !ok {
t.Fatal("Failed to find age attribute")
}
if ageAttr != "30" {
t.Fatalf("Expected age attribute '30', got '%s'", ageAttr)
}
}
func TestMixedContent(t *testing.T) {
// Test mixed content (text and elements)
xml := `Text before <tag>Inside tag</tag> Text after`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
if len(doc.Root.Children) != 3 {
t.Fatalf("Expected 3 children of root, got %d", len(doc.Root.Children))
}
if doc.Root.Children[0].Type != TextNode || doc.Root.Children[0].Value != "Text before " {
t.Fatalf("Expected first child to be text 'Text before ', got '%s'", doc.Root.Children[0].Value)
}
if doc.Root.Children[1].Type != ElementNode || doc.Root.Children[1].Name != "tag" {
t.Fatal("Expected second child to be tag element")
}
if doc.Root.Children[2].Type != TextNode || doc.Root.Children[2].Value != " Text after" {
t.Fatalf("Expected third child to be text ' Text after', got '%s'", doc.Root.Children[2].Value)
}
}
func TestComment(t *testing.T) {
// Test parsing comments
xml := `<!-- This is a comment --><data>Content</data>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
if len(doc.Root.Children) != 2 {
t.Fatalf("Expected 2 children of root, got %d", len(doc.Root.Children))
}
if doc.Root.Children[0].Type != CommentNode || doc.Root.Children[0].Value != " This is a comment " {
t.Fatalf("Expected first child to be comment ' This is a comment ', got '%s'", doc.Root.Children[0].Value)
}
nodes, ok := doc.DeepFind("data")
if !ok {
t.Fatal("Failed to find data element")
}
if nodes[0].GetText() != "Content" {
t.Fatalf("Expected text 'Content', got '%s'", nodes[0].GetText())
}
}
func TestProcessingInstruction(t *testing.T) {
// Test parsing processing instructions
xml := `<?xml version="1.0" encoding="UTF-8"?><data>Content</data>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
if len(doc.Root.Children) != 2 {
t.Fatalf("Expected 2 children of root, got %d", len(doc.Root.Children))
}
if doc.Root.Children[0].Type != ProcessingInstructionNode || doc.Root.Children[0].Name != "xml" {
t.Fatal("Expected first child to be xml processing instruction")
}
nodes, ok := doc.DeepFind("data")
if !ok {
t.Fatal("Failed to find data element")
}
if nodes[0].GetText() != "Content" {
t.Fatalf("Expected text 'Content', got '%s'", nodes[0].GetText())
}
}
func TestUnclosedTags(t *testing.T) {
// Test unclosed tags (where outer closes before inner)
xml := `<outer><inner>Content</outer>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
nodes, ok := doc.DeepFind("inner")
if !ok {
t.Fatal("Failed to find inner element")
}
if len(nodes) != 1 {
t.Fatalf("Expected 1 inner node, got %d", len(nodes))
}
if nodes[0].GetText() != "Content" {
t.Fatalf("Expected text 'Content', got '%s'", nodes[0].GetText())
}
}
func TestFindOne(t *testing.T) {
// Test FindOne method
xml := `<data><item>First</item><item>Second</item></data>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
node, ok := doc.FindOne("item")
if !ok {
t.Fatal("Failed to find item element")
}
if node.GetText() != "First" {
t.Fatalf("Expected text 'First', got '%s'", node.GetText())
}
}
func TestPerformance(t *testing.T) {
// Test performance with a large XML document
const elementCount = 1000
// Generate a large XML document
var xml strings.Builder
xml.WriteString("<root>")
for i := 0; i < elementCount; i++ {
xml.WriteString("<item id=\"")
xml.WriteByte('A' + byte(i%26))
xml.WriteString("\">Item ")
xml.WriteByte('A' + byte(i%26))
xml.WriteString("</item>")
}
xml.WriteString("</root>")
doc, err := Parse(xml.String())
if err != nil {
t.Fatalf("Parse error: %v", err)
}
nodes, ok := doc.DeepFind("item")
if !ok {
t.Fatal("Failed to find item elements")
}
if len(nodes) != elementCount {
t.Fatalf("Expected %d item nodes, got %d", elementCount, len(nodes))
}
}
func TestMultipleRootElements(t *testing.T) {
// Test with multiple root elements (invalid in standard XML)
xml := `<first>Element 1</first><second>Element 2</second>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
// Check both roots are found
first, ok := doc.DeepFind("first")
if !ok || len(first) != 1 {
t.Fatal("Failed to find first element")
}
second, ok := doc.DeepFind("second")
if !ok || len(second) != 1 {
t.Fatal("Failed to find second element")
}
if first[0].GetText() != "Element 1" {
t.Fatalf("Expected text 'Element 1', got '%s'", first[0].GetText())
}
if second[0].GetText() != "Element 2" {
t.Fatalf("Expected text 'Element 2', got '%s'", second[0].GetText())
}
}
func TestMalformedAttributes(t *testing.T) {
// Test with malformed attributes
xml := `<element attr1 attr2=value attr3="quoted value" attr4='single quotes'>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
elements, ok := doc.DeepFind("element")
if !ok || len(elements) != 1 {
t.Fatal("Failed to find element")
}
element := elements[0]
// Check attribute values
value, ok := element.GetAttribute("attr1")
if !ok {
t.Fatal("Failed to find attr1")
}
if value != "" {
t.Fatalf("Expected empty value for attr1, got '%s'", value)
}
value, ok = element.GetAttribute("attr2")
if !ok {
t.Fatal("Failed to find attr2")
}
if value != "value" {
t.Fatalf("Expected 'value' for attr2, got '%s'", value)
}
value, ok = element.GetAttribute("attr3")
if !ok {
t.Fatal("Failed to find attr3")
}
if value != "quoted value" {
t.Fatalf("Expected 'quoted value' for attr3, got '%s'", value)
}
value, ok = element.GetAttribute("attr4")
if !ok {
t.Fatal("Failed to find attr4")
}
if value != "single quotes" {
t.Fatalf("Expected 'single quotes' for attr4, got '%s'", value)
}
}
func TestSelfClosingTags(t *testing.T) {
// Test self-closing tags
xml := `<parent><selfClosing/><regular>Content</regular></parent>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
parent, ok := doc.DeepFind("parent")
if !ok || len(parent) != 1 {
t.Fatal("Failed to find parent element")
}
if len(parent[0].Children) != 2 {
t.Fatalf("Expected 2 children of parent, got %d", len(parent[0].Children))
}
if parent[0].Children[0].Type != ElementNode || parent[0].Children[0].Name != "selfClosing" {
t.Fatal("Expected first child to be selfClosing element")
}
if len(parent[0].Children[0].Children) != 0 {
t.Fatalf("Expected selfClosing to have no children, got %d", len(parent[0].Children[0].Children))
}
}
func TestUnexpectedEndOfInput(t *testing.T) {
// Test with unexpected end of input in various states
testCases := []string{
"<",
"<tag",
"<tag attribute=",
"<tag attribute=\"value",
"<!-- comment",
"<?xml",
}
for _, testCase := range testCases {
doc, _ := Parse(testCase)
// Even with errors, we should get a document back
if doc == nil {
t.Fatalf("Expected document for input %q, got nil", testCase)
}
}
}
func TestDeepNestedElements(t *testing.T) {
// Test deeply nested elements
xml := `<level1><level2><level3><level4><level5>Deep</level5></level4></level3></level2></level1>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
level5, ok := doc.DeepFind("level5")
if !ok || len(level5) != 1 {
t.Fatal("Failed to find deeply nested element")
}
if level5[0].GetText() != "Deep" {
t.Fatalf("Expected text 'Deep', got '%s'", level5[0].GetText())
}
}
func TestLLMUseCase(t *testing.T) {
// Test the LLM use case
xml := `<think>
To find the sum of the first 100 positive integers, I can use the formula:
Sum = n(n+1)/2, where n is the number of integers.
In this case, n = 100.
Substituting:
Sum = 100(100+1)/2
Sum = 100(101)/2
Sum = 10100/2
Sum = 5050
Let me double-check this. Another approach would be to pair numbers from opposite ends:
1 + 100 = 101
2 + 99 = 101
3 + 98 = 101
...
50 + 51 = 101
There are 50 such pairs, and each pair sums to 101.
So, total sum = 50 × 101 = 5050
Both methods yield the same result, so I'm confident the answer is 5050.
</think>
<output>The sum of the first 100 positive integers is 5050.
This can be calculated using the formula Sum = n(n+1)/2, where n is the number of integers we're adding.
For n = 100, we get:
Sum = 100(100+1)/2 = 100(101)/2 = 10100/2 = 5050</output>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
thinking, ok := doc.DeepFind("think")
if !ok || len(thinking) != 1 {
t.Fatal("Failed to find thinking element")
}
output, ok := doc.DeepFind("output")
if !ok || len(output) != 1 {
t.Fatal("Failed to find output element")
}
}
func TestStringMethod(t *testing.T) {
// Test the String method for serialization
xml := `<root><child attribute="value">Content</child></root>`
doc, err := Parse(xml)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
// The string representation should be parseable again
repr := doc.String()
doc2, err := Parse(repr)
if err != nil {
t.Fatalf("Failed to parse string representation: %v", err)
}
child, ok := doc2.DeepFind("child")
if !ok || len(child) != 1 {
t.Fatal("Failed to find child element in reparsed document")
}
attrValue, ok := child[0].GetAttribute("attribute")
if !ok || attrValue != "value" {
t.Fatalf("Expected attribute 'value', got '%s'", attrValue)
}
if child[0].GetText() != "Content" {
t.Fatalf("Expected text 'Content', got '%s'", child[0].GetText())
}
}