...

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

Documentation: github.com/clbanning/mxj/v2

     1  // bulk_test.go - uses Handler and Writer functions to process some streams as a demo.
     2  
     3  package mxj
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  	"testing"
     9  )
    10  
    11  func TestBulkHeader(t *testing.T) {
    12  	fmt.Println("\n----------------  bulk_test.go ...")
    13  }
    14  
    15  var jsonWriter = new(bytes.Buffer)
    16  var xmlWriter = new(bytes.Buffer)
    17  
    18  var jsonErrLog = new(bytes.Buffer)
    19  var xmlErrLog = new(bytes.Buffer)
    20  
    21  func TestXmlReader(t *testing.T) {
    22  	// create Reader for xmldata
    23  	xmlReader := bytes.NewReader(xmldata)
    24  
    25  	// read XML from Readerand pass Map value with the raw XML to handler
    26  	err := HandleXmlReader(xmlReader, bxmaphandler, bxerrhandler)
    27  	if err != nil {
    28  		t.Fatal("err:", err.Error())
    29  	}
    30  
    31  	// get the JSON
    32  	j := make([]byte, jsonWriter.Len())
    33  	_, _ = jsonWriter.Read(j)
    34  
    35  	// get the errors
    36  	e := make([]byte, xmlErrLog.Len())
    37  	_, _ = xmlErrLog.Read(e)
    38  
    39  	// print the input
    40  	fmt.Println("XmlReader, xmldata:\n", string(xmldata))
    41  	// print the result
    42  	fmt.Println("XmlReader, result :\n", string(j))
    43  	// print the errors
    44  	fmt.Println("XmlReader, errors :\n", string(e))
    45  }
    46  
    47  func bxmaphandler(m Map) bool {
    48  	j, err := m.JsonIndent("", "  ", true)
    49  	if err != nil {
    50  		return false
    51  	}
    52  
    53  	_, _ = jsonWriter.Write(j)
    54  	// put in a NL to pretty up printing the Writer
    55  	_, _ = jsonWriter.Write([]byte("\n"))
    56  	return true
    57  }
    58  
    59  func bxerrhandler(err error) bool {
    60  	// write errors to file
    61  	_, _ = xmlErrLog.Write([]byte(err.Error()))
    62  	_, _ = xmlErrLog.Write([]byte("\n")) // pretty up
    63  	return true
    64  }
    65  
    66  func TestJsonReader(t *testing.T) {
    67  	jsonReader := bytes.NewReader(jsondata)
    68  
    69  	// read all the JSON
    70  	err := HandleJsonReader(jsonReader, bjmaphandler, bjerrhandler)
    71  	if err != nil {
    72  		t.Fatal("err:", err.Error())
    73  	}
    74  
    75  	// get the XML
    76  	x := make([]byte, xmlWriter.Len())
    77  	_, _ = xmlWriter.Read(x)
    78  
    79  	// get the errors
    80  	e := make([]byte, jsonErrLog.Len())
    81  	_, _ = jsonErrLog.Read(e)
    82  
    83  	// print the input
    84  	fmt.Println("JsonReader, jsondata:\n", string(jsondata))
    85  	// print the result
    86  	fmt.Println("JsonReader, result  :\n", string(x))
    87  	// print the errors
    88  	fmt.Println("JsonReader, errors :\n", string(e))
    89  }
    90  
    91  func bjmaphandler(m Map) bool {
    92  	x, err := m.XmlIndent("  ", "  ")
    93  	if err != nil {
    94  		return false
    95  	}
    96  	_, _ = xmlWriter.Write(x)
    97  	// put in a NL to pretty up printing the Writer
    98  	_, _ = xmlWriter.Write([]byte("\n"))
    99  	return true
   100  }
   101  
   102  func bjerrhandler(err error) bool {
   103  	// write errors to file
   104  	_, _ = jsonErrLog.Write([]byte(err.Error()))
   105  	_, _ = jsonErrLog.Write([]byte("\n")) // pretty up
   106  	return true
   107  }
   108  

View as plain text