File: snakecase_test.go

package info (click to toggle)
golang-github-clbanning-mxj 2.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,200 kB
  • sloc: xml: 176; makefile: 4
file content (69 lines) | stat: -rw-r--r-- 1,803 bytes parent folder | download | duplicates (2)
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
package mxj

import (
	"fmt"
	"testing"
)

func TestStakeCase(t *testing.T) {
	PrependAttrWithHyphen(true)
	fmt.Println("\n----------- TestSnakeCase")
	CoerceKeysToSnakeCase()
	defer CoerceKeysToSnakeCase()

	data1 := `<xml-rpc><element-one attr-1="an attribute">something</element-one></xml-rpc>`
	data2 := `<xml_rpc><element_one attr_1="an attribute">something</element_one></xml_rpc>`

	m, err := NewMapXml([]byte(data1))
	if err != nil {
		t.Fatal(err)
	}

	x, err := m.Xml()
	if err != nil {
		t.Fatal(err)
	}
	if string(x) != data2 {
		t.Fatal(string(x), "!=", data2)
	}

	// Use-case from: https://github.com/clbanning/mxj/pull/33#issuecomment-273724506
	data1 = `<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/11.2R4/junos" message-id="97741fa3-99e8-46ba-b103-bab6b459d884">
<software-information>
<host-name>srx100</host-name>
<product-model>srx100b</product-model>
<product-name>srx100b</product-name>
<jsr/>
<package-information>
<name>junos</name>
<comment>JUNOS Software Release [11.2R4.3]</comment>
</package-information>
</software-information>
</rpc-reply>`
	data2 = `<rpc_reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/11.2R4/junos" message_id="97741fa3-99e8-46ba-b103-bab6b459d884">
<software_information>
<host_name>srx100</host_name>
<product_model>srx100b</product_model>
<product_name>srx100b</product_name>
<jsr/>
<package_information>
<name>junos</name>
<comment>JUNOS Software Release [11.2R4.3]</comment>
</package_information>
</software_information>
</rpc_reply>`

	ms, err := NewMapXmlSeq([]byte(data1))
	if err != nil {
		t.Fatal(err)
	}

	x, err = ms.XmlIndent("", "")
	if err != nil {
		t.Fatal(err)
	}
	if string(x) != data2 {
		t.Fatal(string(x), "!=", data2)
	}
}