...

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

Documentation: github.com/clbanning/mxj/v2

     1  package mxj
     2  
     3  import (
     4  	"encoding/json"
     5  	"encoding/xml"
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  	"testing"
    10  )
    11  
    12  func TestXml2Header(t *testing.T) {
    13  	fmt.Println("\n----------------  xml2_test.go ...")
    14  }
    15  
    16  func TestNewMapXml4(t *testing.T) {
    17  	x := []byte(`<doc> 
    18     <books>
    19        <book seq="1">
    20           <author>William T. Gaddis</author>
    21           <title>The Recognitions</title>
    22           <review>One of the great seminal American novels of the 20th century.</review>
    23        </book>
    24        <book seq="2">
    25           <author>Austin Tappan Wright</author>
    26           <title>Islandia</title>
    27           <review>An example of earlier 20th century American utopian fiction.</review>
    28        </book>
    29        <book seq="3">
    30           <author>John Hawkes</author>
    31           <title>The Beetle Leg</title>
    32           <review>A lyrical novel about the construction of Ft. Peck Dam in Montana.</review>
    33        </book>
    34        <book seq="4"> 
    35           <author>
    36              <first_name>T.E.</first_name>
    37              <last_name>Porter</last_name>
    38           </author>
    39           <title>King's Day</title>
    40           <review>A magical novella.</review>
    41        </book>
    42     </books>
    43  </doc>`)
    44  
    45  	m, err := NewMapXml(x)
    46  	if err != nil && err != io.EOF {
    47  		t.Fatal("err:", err.Error())
    48  	}
    49  	fmt.Println("NewMapXml4, x:\n", string(x))
    50  	fmt.Println("NewMapXml4, m:\n", m)
    51  	fmt.Println("NewMapXml4, s:\n", m.StringIndent())
    52  	b, err := m.XmlIndent("", "  ")
    53  	if err != nil {
    54  		t.Fatal("err:", err)
    55  	}
    56  	fmt.Println("NewMapXml4, b:\n", string(b))
    57  }
    58  
    59  func TestNewMapXml5(t *testing.T) {
    60  	fh, err := os.Open("songtext.xml")
    61  	if err != nil {
    62  		t.Fatal("err:", err.Error())
    63  	}
    64  	defer fh.Close()
    65  
    66  	m, raw, err := NewMapXmlReaderRaw(fh)
    67  	if err != nil && err != io.EOF {
    68  		t.Fatal("err:", err.Error())
    69  	}
    70  	fmt.Println("NewMapXml5, raw:\n", string(raw))
    71  	fmt.Println("NewMapXml5, m:\n", m)
    72  	fmt.Println("NewMapXml5, s:\n", m.StringIndent())
    73  	b, err := m.Xml()
    74  	if err != nil {
    75  		t.Fatal("err:", err)
    76  	}
    77  	fmt.Println("NewMapXml5, b:\n", string(b))
    78  	b, err = m.XmlIndent("", "  ")
    79  	if err != nil {
    80  		t.Fatal("err:", err)
    81  	}
    82  	fmt.Println("NewMapXml5, b:\n", string(b))
    83  }
    84  
    85  func TestNewMapXml6(t *testing.T) {
    86  	fh, err := os.Open("atomFeedString.xml")
    87  	if err != nil {
    88  		t.Fatal("err:", err.Error())
    89  	}
    90  	defer fh.Close()
    91  
    92  	m, raw, err := NewMapXmlReaderRaw(fh)
    93  	if err != nil && err != io.EOF {
    94  		t.Fatal("err:", err.Error())
    95  	}
    96  	fmt.Println("NewMapXml6, raw:\n", string(raw))
    97  	fmt.Println("NewMapXml6, m:\n", m)
    98  	fmt.Println("NewMapXml6, s:\n", m.StringIndent())
    99  	b, err := m.Xml()
   100  	if err != nil {
   101  		t.Fatal("err:", err)
   102  	}
   103  	fmt.Println("NewMapXml6, b:\n", string(b))
   104  	b, err = m.XmlIndent("", "  ")
   105  	if err != nil {
   106  		t.Fatal("err:", err)
   107  	}
   108  	fmt.Println("NewMapXml6, b:\n", string(b))
   109  }
   110  
   111  // ===================================== benchmarks ============================
   112  
   113  var smallxml = []byte(`
   114  	<doc>
   115  		<words>
   116  			<word1>this</word1>
   117  			<word2>is</word2>
   118  			<word3>the</word3>
   119  			<word4>end</word4>
   120  		</words>
   121  	</doc>
   122  `)
   123  
   124  var smalljson = []byte(`{"doc":{"words":{"word1":"this","word2":"is","word3":"the","word4":"end"}}}`)
   125  
   126  type words struct {
   127  	Word1 string `xml:"word1"`
   128  	Word2 string `xml:"word2"`
   129  	Word3 string `xml:"word3"`
   130  	Word4 string `xml:"word4"`
   131  }
   132  
   133  type xmldoc struct {
   134  	Words words `xml:"words"`
   135  }
   136  
   137  type jsondoc struct {
   138  	Doc xmldoc
   139  }
   140  
   141  func BenchmarkNewMapXml(b *testing.B) {
   142  	// var m Map
   143  	var err error
   144  	for i := 0; i < b.N; i++ {
   145  		if _, err = NewMapXml(smallxml); err != nil {
   146  			b.Fatal("err:", err)
   147  		}
   148  	}
   149  	// fmt.Println("m Map:", m)
   150  }
   151  
   152  func BenchmarkNewStructXml(b *testing.B) {
   153  	var s *xmldoc
   154  	var err error
   155  	for i := 0; i < b.N; i++ {
   156  		s = new(xmldoc)
   157  		if err = xml.Unmarshal(smallxml, s); err != nil {
   158  			b.Fatal("err:", err)
   159  		}
   160  	}
   161  	// fmt.Println("s xmldoc:", *s)
   162  }
   163  
   164  func BenchmarkNewMapJson(b *testing.B) {
   165  	var m map[string]interface{}
   166  	var err error
   167  	for i := 0; i < b.N; i++ {
   168  		m = make(map[string]interface{})
   169  		if err = json.Unmarshal(smalljson, &m); err != nil {
   170  			b.Fatal("err:", err)
   171  		}
   172  	}
   173  	// fmt.Println("m map:", m)
   174  }
   175  
   176  func BenchmarkNewStructJson(b *testing.B) {
   177  	var s *jsondoc
   178  	var err error
   179  	for i := 0; i < b.N; i++ {
   180  		s = new(jsondoc)
   181  		if err = json.Unmarshal(smalljson, s); err != nil {
   182  			b.Fatal("err:", err)
   183  		}
   184  	}
   185  	// fmt.Println("s jsondoc:", *s)
   186  }
   187  
   188  // ================== something with a little more content ... ===================
   189  
   190  var xmlbooks = []byte(`
   191  <doc> 
   192     <books>
   193        <book seq="1">
   194           <author>William T. Gaddis</author>
   195           <title>The Recognitions</title>
   196           <review>One of the great seminal American novels of the 20th century.</review>
   197        </book>
   198        <book seq="2">
   199           <author>Austin Tappan Wright</author>
   200           <title>Islandia</title>
   201           <review>An example of earlier 20th century American utopian fiction.</review>
   202        </book>
   203        <book seq="3">
   204           <author>John Hawkes</author>
   205           <title>The Beetle Leg</title>
   206           <review>A lyrical novel set during the construction of Ft. Peck Dam in Montana.</review>
   207        </book>
   208        <book seq="4"> 
   209           <author>
   210              <first_name>T.E.</first_name>
   211              <last_name>Porter</last_name>
   212           </author>
   213           <title>King's Day</title>
   214           <review>A magical novella.</review>
   215        </book>
   216     </books>
   217  </doc>
   218  `)
   219  
   220  var jsonbooks = []byte(`
   221  {"doc":
   222  	{"books":
   223  		{"book":[
   224  			{	"author":"William T. Gaddis",
   225  				"title":"The Recognitions",
   226  				"review":"One of the great seminal American novels of the 20th century."
   227  			},
   228  			{	"author":"Austin Tappan Wright",
   229  				"title":"Islandia",
   230  				"review":"An example of earlier 20th century American utopian fiction."
   231  			},
   232  			{	"author":"John Hawkes",
   233  				"title":"The Beetle Leg",
   234  				"review":"A lyrical novel set during the construction of Ft. Peck Dam in Montana."
   235  			},
   236  			{	"author":{"first_name":"T.E.", "last_name":"Porter"},
   237  				"title":"King's Day",
   238  				"review":"A magical novella."
   239  			}]
   240  		}
   241  	}
   242  }
   243  `)
   244  
   245  type book struct {
   246  	Author string `xml:"author"`
   247  	Title  string `xml:"title"`
   248  	Review string `xml:"review"`
   249  }
   250  
   251  type books struct {
   252  	Book []book `xml:"book"`
   253  }
   254  
   255  type doc struct {
   256  	Books books `xml:"books"`
   257  }
   258  
   259  type jsonbook struct {
   260  	Author json.RawMessage
   261  	Title  string
   262  	Review string
   263  }
   264  
   265  type jsonbooks2 struct {
   266  	Book []jsonbook
   267  }
   268  
   269  type jsondoc1 struct {
   270  	Books jsonbooks2
   271  }
   272  
   273  type jsondoc2 struct {
   274  	Doc jsondoc1
   275  }
   276  
   277  func BenchmarkNewMapXmlBooks(b *testing.B) {
   278  	// var m Map
   279  	var err error
   280  	for i := 0; i < b.N; i++ {
   281  		if _, err = NewMapXml(xmlbooks); err != nil {
   282  			b.Fatal("err:", err)
   283  		}
   284  	}
   285  	// fmt.Println("m Map:", m)
   286  }
   287  
   288  func BenchmarkNewStructXmlBooks(b *testing.B) {
   289  	var s *doc
   290  	var err error
   291  	for i := 0; i < b.N; i++ {
   292  		s = new(doc)
   293  		if err = xml.Unmarshal(xmlbooks, s); err != nil {
   294  			b.Fatal("err:", err)
   295  		}
   296  	}
   297  	// fmt.Println("s doc:", *s)
   298  }
   299  
   300  func BenchmarkNewMapJsonBooks(b *testing.B) {
   301  	var m map[string]interface{}
   302  	var err error
   303  	for i := 0; i < b.N; i++ {
   304  		m = make(map[string]interface{})
   305  		if err = json.Unmarshal(jsonbooks, &m); err != nil {
   306  			b.Fatal("err:", err)
   307  		}
   308  	}
   309  	// fmt.Println("m map:", m)
   310  }
   311  
   312  func BenchmarkNewStructJsonBooks(b *testing.B) {
   313  	var s *jsondoc2
   314  	var err error
   315  	for i := 0; i < b.N; i++ {
   316  		s = new(jsondoc2)
   317  		if err = json.Unmarshal(jsonbooks, s); err != nil {
   318  			b.Fatal("err:", err)
   319  		}
   320  	}
   321  	// fmt.Println("s jsondoc2:", *s)
   322  }
   323  

View as plain text