...

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

Documentation: github.com/clbanning/mxj/v2

     1  package mxj
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  )
     8  
     9  var whiteSpaceDataSeqTest2 = []byte(`<books>
    10     <book seq="1" ser="5">
    11        <author>William T. Gaddis </author>
    12        <title> The Recognitions </title>
    13        <review> One of the great seminal American novels of the 20th century.</review>
    14     </book>
    15     <book seq="2">
    16        <author>Austin Tappan Wright</author>
    17        <title>Islandia</title>
    18        <review>An example of earlier 20th century American utopian fiction.</review>
    19     </book>
    20     <book seq="3" ser="6">
    21        <author> John Hawkes </author>
    22        <title> The Beetle Leg </title>
    23        <review> A lyrical novel about the construction of Ft. Peck Dam in Montana. </review>
    24     </book>
    25  </books>`)
    26  
    27  func TestSetGlobalKeyMapPrefix(t *testing.T) {
    28  	prefixList := []struct {
    29  		name  string
    30  		value string
    31  	}{
    32  		{
    33  			name:  "Testing with % as Map Key Prefix",
    34  			value: "%",
    35  		},
    36  		{
    37  			name:  "Testing with _ as Map Key Prefix",
    38  			value: "_",
    39  		},
    40  		{
    41  			name:  "Testing with - as Map Key Prefix",
    42  			value: "-",
    43  		},
    44  		{
    45  			name:  "Testing with & as Map Key Prefix",
    46  			value: "&",
    47  		},
    48  	}
    49  
    50  	for _, prefix := range prefixList {
    51  		t.Run(prefix.name, func(t *testing.T) {
    52  
    53  			// Testing MapSeq(Ordering) with whitespace and byte equivalence
    54  			DisableTrimWhiteSpace(true)
    55  			SetGlobalKeyMapPrefix(prefix.value)
    56  
    57  			m, err := NewMapFormattedXmlSeq(whiteSpaceDataSeqTest2)
    58  			if err != nil {
    59  				t.Fatal(err)
    60  			}
    61  
    62  			m1 := MapSeq(m)
    63  			x, err := m1.XmlIndent("", "   ")
    64  			if err != nil {
    65  				t.Fatal(err)
    66  			}
    67  
    68  			if string(x) != string(whiteSpaceDataSeqTest2) {
    69  				t.Fatalf("expected\n'%s' \ngot \n'%s'", whiteSpaceDataSeqTest2, x)
    70  			}
    71  			DisableTrimWhiteSpace(false)
    72  
    73  			// Testing Map with whitespace and deep equivalence
    74  			DisableTrimWhiteSpace(true)
    75  			m3, err := NewMapXml(whiteSpaceDataSeqTest2)
    76  			if err != nil {
    77  				t.Fatal(err)
    78  			}
    79  
    80  			m4 := Map(m3)
    81  
    82  			if !cmp.Equal(m3, m4) {
    83  				t.Errorf("Maps unmatched using %s", prefix.value)
    84  			}
    85  			DisableTrimWhiteSpace(false)
    86  
    87  			// Testing MapSeq(Ordering) without whitespace and byte equivalence
    88  			m5, err := NewMapFormattedXmlSeq(whiteSpaceDataSeqTest2)
    89  			if err != nil {
    90  				t.Fatal(err)
    91  			}
    92  
    93  			m6 := MapSeq(m5)
    94  
    95  			if !cmp.Equal(m5, m6) {
    96  				t.Errorf("Maps unmatched using %s", prefix.value)
    97  			}
    98  
    99  			// Testing Map without whitespace and deep equivalence
   100  			m7, err := NewMapXml(whiteSpaceDataSeqTest2)
   101  			if err != nil {
   102  				t.Fatal(err)
   103  			}
   104  
   105  			m8 := Map(m7)
   106  
   107  			if !cmp.Equal(m7, m8) {
   108  				t.Errorf("Maps unmatched using %s", prefix.value)
   109  			}
   110  		})
   111  
   112  	}
   113  	SetGlobalKeyMapPrefix("#")
   114  
   115  }
   116  

View as plain text