...

Source file src/github.com/clbanning/mxj/v2/snakecase_test.go

Documentation: github.com/clbanning/mxj/v2

     1  package mxj
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestStakeCase(t *testing.T) {
     9  	PrependAttrWithHyphen(true)
    10  	fmt.Println("\n----------- TestSnakeCase")
    11  	CoerceKeysToSnakeCase()
    12  	defer CoerceKeysToSnakeCase()
    13  
    14  	data1 := `<xml-rpc><element-one attr-1="an attribute">something</element-one></xml-rpc>`
    15  	data2 := `<xml_rpc><element_one attr_1="an attribute">something</element_one></xml_rpc>`
    16  
    17  	m, err := NewMapXml([]byte(data1))
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  
    22  	x, err := m.Xml()
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	if string(x) != data2 {
    27  		t.Fatal(string(x), "!=", data2)
    28  	}
    29  
    30  	// Use-case from: https://github.com/clbanning/mxj/pull/33#issuecomment-273724506
    31  	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">
    32  <software-information>
    33  <host-name>srx100</host-name>
    34  <product-model>srx100b</product-model>
    35  <product-name>srx100b</product-name>
    36  <jsr/>
    37  <package-information>
    38  <name>junos</name>
    39  <comment>JUNOS Software Release [11.2R4.3]</comment>
    40  </package-information>
    41  </software-information>
    42  </rpc-reply>`
    43  	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">
    44  <software_information>
    45  <host_name>srx100</host_name>
    46  <product_model>srx100b</product_model>
    47  <product_name>srx100b</product_name>
    48  <jsr/>
    49  <package_information>
    50  <name>junos</name>
    51  <comment>JUNOS Software Release [11.2R4.3]</comment>
    52  </package_information>
    53  </software_information>
    54  </rpc_reply>`
    55  
    56  	ms, err := NewMapXmlSeq([]byte(data1))
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	x, err = ms.XmlIndent("", "")
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	if string(x) != data2 {
    66  		t.Fatal(string(x), "!=", data2)
    67  	}
    68  }
    69  
    70  

View as plain text