-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_parser_test.go
More file actions
530 lines (444 loc) · 14.5 KB
/
Copy pathxml_parser_test.go
File metadata and controls
530 lines (444 loc) · 14.5 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
526
527
528
529
530
package parser
import (
"log/slog"
"os"
"testing"
)
func TestXMLParserWithAttributes(t *testing.T) {
xmlContent := `<user id="123" name="John Doe" active="true">
<email type="primary">john@example.com</email>
<email type="secondary">john.doe@work.com</email>
<profile>
<age>30</age>
<city>New York</city>
</profile>
</user>`
result, err := parseXMLToGeneric(xmlContent)
if err != nil {
t.Fatalf("Failed to parse XML: %v", err)
}
// Check that we have the expected flattened structure
// Check user attributes
if result["user/id"] != "123" {
t.Errorf("Expected user/id to be '123', got %v", result["user/id"])
}
if result["user/name"] != "John Doe" {
t.Errorf("Expected user/name to be 'John Doe', got %v", result["user/name"])
}
if result["user/active"] != "true" {
t.Errorf("Expected user/active to be 'true', got %v", result["user/active"])
}
// Check email attributes
emailTypes := []string{"primary", "secondary"}
for i, expectedType := range emailTypes {
emailTypeKey := "user/email/type"
emailType := result[emailTypeKey]
// Handle array of email types
if emailArray, ok := emailType.([]interface{}); ok {
if i < len(emailArray) {
if emailArray[i] != expectedType {
t.Errorf("Expected email[%d] type to be '%s', got %v", i, expectedType, emailArray[i])
}
}
} else if i == 0 {
// Single email case
if emailType != expectedType {
t.Errorf("Expected first email type to be '%s', got %v", expectedType, emailType)
}
}
}
// Check email text content
expectedEmails := []string{"john@example.com", "john.doe@work.com"}
emailText := result["user/email"]
if emailArray, ok := emailText.([]interface{}); ok {
for i, expectedEmail := range expectedEmails {
if i < len(emailArray) {
if emailArray[i] != expectedEmail {
t.Errorf("Expected email[%d] to be '%s', got %v", i, expectedEmail, emailArray[i])
}
}
}
}
// Check profile content
if result["user/profile/age"] != "30" {
t.Errorf("Expected age to be '30', got %v", result["user/profile/age"])
}
if result["user/profile/city"] != "New York" {
t.Errorf("Expected city to be 'New York', got %v", result["user/profile/city"])
}
// Check that user element exists as nested structure
userMap, ok := result["user"].(map[string]interface{})
if !ok {
t.Fatal("Expected user to be a map")
}
// Check nested structure contains expected elements
if userMap["email"] == nil {
t.Error("Expected user map to contain email element")
}
if userMap["profile"] == nil {
t.Error("Expected user map to contain profile element")
}
t.Logf("XML parsing result: %+v", result)
}
func TestXMLHelperFunctions(t *testing.T) {
xmlContent := `<product id="456" name="Widget" available="true">
<price currency="USD">29.99</price>
<description>A useful widget</description>
</product>`
result, err := parseXMLToGeneric(xmlContent)
if err != nil {
t.Fatalf("Failed to parse XML: %v", err)
}
helper := XMLHelper{}
// Test GetXMLAttribute for product attributes
id := helper.GetXMLAttribute(result, "product", "id")
if id != "456" {
t.Errorf("Expected product id '456', got '%s'", id)
}
name := helper.GetXMLAttribute(result, "product", "name")
if name != "Widget" {
t.Errorf("Expected product name 'Widget', got '%s'", name)
}
// Test GetXMLAttribute for price currency
currency := helper.GetXMLAttribute(result, "product/price", "currency")
if currency != "USD" {
t.Errorf("Expected price currency 'USD', got '%s'", currency)
}
// Test GetXMLText for elements with text content
description := helper.GetXMLText(result, "product/description")
if description != "A useful widget" {
t.Errorf("Expected description 'A useful widget', got '%s'", description)
}
price := helper.GetXMLText(result, "product/price")
if price != "29.99" {
t.Errorf("Expected price '29.99', got '%s'", price)
}
// Test HasXMLAttribute
if !helper.HasXMLAttribute(result, "product", "id") {
t.Error("Expected product to have 'id' attribute")
}
if helper.HasXMLAttribute(result, "product", "nonexistent") {
t.Error("Expected product not to have 'nonexistent' attribute")
}
// Test HasXMLElement
if !helper.HasXMLElement(result, "product") {
t.Error("Expected to have 'product' element")
}
if !helper.HasXMLElement(result, "product/price") {
t.Error("Expected to have 'product/price' element")
}
if helper.HasXMLElement(result, "nonexistent") {
t.Error("Expected not to have 'nonexistent' element")
}
// Test ListXMLAttributes for product
attrs := helper.ListXMLAttributes(result, "product")
expectedAttrs := []string{"id", "name", "available"}
if len(attrs) != len(expectedAttrs) {
t.Logf("Got attributes: %v", attrs)
// Check if the basic expected attributes are present
hasId := false
hasName := false
hasAvailable := false
for _, attr := range attrs {
switch attr {
case "id":
hasId = true
case "name":
hasName = true
case "available":
hasAvailable = true
}
}
if !hasId || !hasName || !hasAvailable {
t.Errorf("Expected product to have at least 'id', 'name', and 'available' attributes")
}
}
// Test ListXMLElements
elements := helper.ListXMLElements(result)
if len(elements) < 1 { // Should have at least product
t.Errorf("Expected at least 1 elements, got %d: %v", len(elements), elements)
}
t.Logf("XML parsing result: %+v", result)
}
func TestXMLParsingEdgeCases(t *testing.T) {
// Test empty XML
_, err := parseXMLToGeneric("")
if err == nil {
t.Error("Expected error for empty XML")
}
// Test whitespace only
_, err = parseXMLToGeneric(" \n\t ")
if err == nil {
t.Error("Expected error for whitespace-only XML")
}
// Test simple text node
result, err := parseXMLToGeneric("<note>Simple note</note>")
if err != nil {
t.Fatalf("Failed to parse simple XML: %v", err)
}
// In our flattened structure, simple elements should be accessible directly by their path
if result["note"] == nil {
t.Error("Expected 'note' element to exist")
}
// The note value should be accessible as text from the nested structure
noteMap, ok := result["note"].(map[string]interface{})
if !ok {
t.Fatalf("Expected note to be a map, got %T", result["note"])
}
if noteMap["note"] != "Simple note" {
t.Errorf("Expected note text to be 'Simple note', got %v", noteMap["note"])
}
// Test self-closing tag
result, err = parseXMLToGeneric(`<img src="test.jpg" alt="Test" />`)
if err != nil {
t.Fatalf("Failed to parse self-closing XML: %v", err)
}
if result["img/src"] != "test.jpg" {
t.Errorf("Expected src 'test.jpg', got %v", result["img/src"])
}
if result["img/alt"] != "Test" {
t.Errorf("Expected alt 'Test', got %v", result["img/alt"])
}
// Check that img element exists as empty in flattened structure
if result["img"] == nil {
t.Error("Expected 'img' element to exist")
}
}
func TestXMLParsingWithDuplicateChildren(t *testing.T) {
xmlContent := `<items>
<item id="1">First</item>
<item id="2">Second</item>
<item id="3">Third</item>
</items>`
result, err := parseXMLToGeneric(xmlContent)
if err != nil {
t.Fatalf("Failed to parse XML: %v", err)
}
// Check that we have arrays for duplicate items in flattened structure
itemTexts := result["items/item"]
itemArray, ok := itemTexts.([]interface{})
if !ok {
t.Fatalf("Expected items/item to be an array, got %T", itemTexts)
}
if len(itemArray) != 3 {
t.Errorf("Expected 3 items, got %d", len(itemArray))
}
// Check item text values
expectedTexts := []string{"First", "Second", "Third"}
for i, expectedText := range expectedTexts {
if i < len(itemArray) {
if itemArray[i] != expectedText {
t.Errorf("Expected item[%d] text to be '%s', got %v", i, expectedText, itemArray[i])
}
}
}
// Check item attributes array
itemIds := result["items/item/id"]
idArray, ok := itemIds.([]interface{})
if !ok {
t.Fatalf("Expected items/item/id to be an array, got %T", itemIds)
}
expectedIds := []string{"1", "2", "3"}
for i, expectedId := range expectedIds {
if i < len(idArray) {
if idArray[i] != expectedId {
t.Errorf("Expected item[%d] id to be '%s', got %v", i, expectedId, idArray[i])
}
}
}
// Check that items element exists as nested structure
itemsMap, ok := result["items"].(map[string]interface{})
if !ok {
t.Fatal("Expected items to be a map")
}
// Check nested structure contains item array
if itemsMap["item"] == nil {
t.Error("Expected items map to contain item element")
}
t.Logf("Successfully parsed XML with duplicate children: %+v", result)
}
func TestXMLParsingInvalidXML(t *testing.T) {
// Test malformed XML
_, err := parseXMLToGeneric("<invalid><unclosed>")
if err == nil {
t.Error("Expected error for malformed XML")
}
// Test invalid characters
_, err = parseXMLToGeneric("<test>Invalid\x00character</test>")
if err == nil {
t.Error("Expected error for invalid characters")
}
}
// TestFlattenedXMLStructure tests the new flattened XML structure format
func TestFlattenedXMLStructure(t *testing.T) {
// Set debug logging
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
})))
xmlContent := `<xml>
<key attr1="1" attr2="2">
<value1 attr="x"/>
<value2 attr="y"/>
<value3 attr="z">
<child1 attr="c"/>
</value3>
</key>
<tag attr1="a" attr2="b">tags</tag>
</xml>`
result, err := parseXMLToGeneric(xmlContent)
if err != nil {
t.Fatalf("Failed to parse XML: %v", err)
}
// Debug: print all keys in result
t.Logf("XML parsing result keys: %v", result)
for k, v := range result {
t.Logf("Key: %s, Value: %v", k, v)
}
// Verify key element attributes
if result["xml/key/attr1"] != "1" {
t.Errorf("Expected xml/key/attr1 to be '1', got '%v'", result["xml/key/attr1"])
}
if result["xml/key/attr2"] != "2" {
t.Errorf("Expected xml/key/attr2 to be '2', got '%v'", result["xml/key/attr2"])
}
// Verify tag element and attributes
if result["xml/tag"] != "tags" {
t.Errorf("Expected xml/tag to be 'tags', got '%v'", result["xml/tag"])
}
if result["xml/tag/attr1"] != "a" {
t.Errorf("Expected xml/tag/attr1 to be 'a', got '%v'", result["xml/tag/attr1"])
}
if result["xml/tag/attr2"] != "b" {
t.Errorf("Expected xml/tag/attr2 to be 'b', got '%v'", result["xml/tag/attr2"])
}
// Verify key has nested structure
keyValue, keyExists := result["xml/key"]
if !keyExists {
t.Fatal("Expected 'xml/key' element to exist")
}
keyMap, isMap := keyValue.(map[string]interface{})
if !isMap {
t.Fatalf("Expected 'key' to be a map, got %T", keyValue)
}
// Check nested attributes
if keyMap["value1/attr"] != "x" {
t.Errorf("Expected value1/attr to be 'x', got '%v'", keyMap["value1/attr"])
}
if keyMap["value2/attr"] != "y" {
t.Errorf("Expected value2/attr to be 'y', got '%v'", keyMap["value2/attr"])
}
if keyMap["value3/attr"] != "z" {
t.Errorf("Expected value3/attr to be 'z', got '%v'", keyMap["value3/attr"])
}
// Check deeply nested structure
value3, value3Exists := keyMap["value3"]
if !value3Exists {
t.Fatal("Expected 'value3' element to exist in key")
}
value3Map, isMap := value3.(map[string]interface{})
if !isMap {
t.Fatalf("Expected 'value3' to be a map, got %T", value3)
}
if value3Map["child1/attr"] != "c" {
t.Errorf("Expected child1/attr to be 'c', got '%v'", value3Map["child1/attr"])
}
t.Logf("Successfully parsed XML structure: %+v", result)
}
// TestXMLArrayHandling tests that multiple children with same name become arrays
func TestXMLArrayHandling(t *testing.T) {
xmlContent := `<xml>
<items>
<item id="1">First Item</item>
<item id="2">Second Item</item>
<item id="3">Third Item</item>
</items>
<category name="electronics">Electronics</category>
<category name="books">Books</category>
</xml>`
result, err := parseXMLToGeneric(xmlContent)
if err != nil {
t.Fatalf("Failed to parse XML: %v", err)
}
// Verify items contains an array of item elements in flattened structure
itemsValue, itemsExists := result["xml/items"]
if !itemsExists {
t.Fatal("Expected 'xml/items' element to exist")
}
itemsMap, isMap := itemsValue.(map[string]interface{})
if !isMap {
t.Fatalf("Expected 'xml/items' to be a map, got %T", itemsValue)
}
// Check that item is an array
itemArray, itemExists := itemsMap["item"]
if !itemExists {
t.Fatal("Expected 'item' elements to exist in items")
}
itemSlice, isSlice := itemArray.([]interface{})
if !isSlice {
t.Fatalf("Expected 'item' to be an array, got %T", itemArray)
}
if len(itemSlice) != 3 {
t.Errorf("Expected 3 item elements, got %d", len(itemSlice))
}
// Check item values using flattened paths
itemTexts := result["xml/items/item"]
itemTextSlice, isSlice := itemTexts.([]interface{})
if !isSlice {
t.Fatalf("Expected 'xml/items/item' to be an array, got %T", itemTexts)
}
expectedValues := []string{"First Item", "Second Item", "Third Item"}
for i, expectedValue := range expectedValues {
if i < len(itemTextSlice) {
if itemTextSlice[i] != expectedValue {
t.Errorf("Expected item[%d] to be '%s', got '%v'", i, expectedValue, itemTextSlice[i])
}
}
}
// Check that item attributes are also arrays
itemIdArray, idExists := result["xml/items/item/id"]
if !idExists {
t.Fatal("Expected 'xml/items/item/id' attributes to exist")
}
idSlice, isSlice := itemIdArray.([]interface{})
if !isSlice {
t.Fatalf("Expected 'xml/items/item/id' to be an array, got %T", itemIdArray)
}
expectedIds := []string{"1", "2", "3"}
for i, expectedId := range expectedIds {
if i < len(idSlice) {
if idSlice[i] != expectedId {
t.Errorf("Expected item/id[%d] to be '%s', got '%v'", i, expectedId, idSlice[i])
}
}
}
// Check that category elements at root level are also arrays
categoryArray, categoryExists := result["xml/category"]
if !categoryExists {
t.Fatal("Expected 'xml/category' elements to exist")
}
catSlice, isSlice := categoryArray.([]interface{})
if !isSlice {
t.Fatalf("Expected 'xml/category' to be an array, got %T", categoryArray)
}
if len(catSlice) != 2 {
t.Errorf("Expected 2 category elements, got %d", len(catSlice))
}
// Check category attributes are arrays
categoryNameArray, nameExists := result["xml/category/name"]
if !nameExists {
t.Fatal("Expected 'xml/category/name' attributes to exist")
}
nameSlice, isSlice := categoryNameArray.([]interface{})
if !isSlice {
t.Fatalf("Expected 'xml/category/name' to be an array, got %T", categoryNameArray)
}
expectedNames := []string{"electronics", "books"}
for i, expectedName := range expectedNames {
if i < len(nameSlice) {
if nameSlice[i] != expectedName {
t.Errorf("Expected category/name[%d] to be '%s', got '%v'", i, expectedName, nameSlice[i])
}
}
}
t.Logf("Successfully parsed XML with arrays: %+v", result)
}