github.com/zhongdalu/gf@v1.0.0/g/encoding/gxml/gxml_test.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  package gxml_test
     8  
     9  import (
    10  	"bytes"
    11  	"github.com/zhongdalu/gf/g/encoding/gcharset"
    12  	"github.com/zhongdalu/gf/g/encoding/gparser"
    13  	"github.com/zhongdalu/gf/g/encoding/gxml"
    14  	"github.com/zhongdalu/gf/g/test/gtest"
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  var testData = []struct {
    20  	utf8, other, otherEncoding string
    21  }{
    22  	{"Hello 常用國字標準字體表", "Hello \xb1`\xa5\u03b0\xea\xa6r\xbc\u0437\u01e6r\xc5\xe9\xaa\xed", "big5"},
    23  	{"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gbk"},
    24  	{"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gb18030"},
    25  }
    26  
    27  var testErrData = []struct {
    28  	utf8, other, otherEncoding string
    29  }{
    30  	{"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gbk"},
    31  }
    32  
    33  func buildXml(charset string, str string) (string, string) {
    34  	head := `<?xml version="1.0" encoding="UTF-8"?>`
    35  	srcXml := strings.Replace(head, "UTF-8", charset, -1)
    36  
    37  	srcParser := gparser.New(nil)
    38  	srcParser.Set("name", str)
    39  	srcParser.Set("age", "12")
    40  
    41  	s, err := srcParser.ToXml()
    42  	if err != nil {
    43  		return "", ""
    44  	}
    45  
    46  	srcXml = srcXml + string(s)
    47  	srcXml, err = gcharset.UTF8To(charset, srcXml)
    48  	if err != nil {
    49  		return "", ""
    50  	}
    51  
    52  	dstXml := head + string(s)
    53  
    54  	return srcXml, dstXml
    55  }
    56  
    57  //测试XML中字符集的转换
    58  func Test_XmlToJson(t *testing.T) {
    59  	for _, v := range testData {
    60  		srcXml, dstXml := buildXml(v.otherEncoding, v.utf8)
    61  		if len(srcXml) == 0 && len(dstXml) == 0 {
    62  			t.Errorf("build xml string error. srcEncoding:%s, src:%s, utf8:%s", v.otherEncoding, v.other, v.utf8)
    63  		}
    64  
    65  		srcJson, err := gxml.ToJson([]byte(srcXml))
    66  		if err != nil {
    67  			t.Errorf("gxml.ToJson error. %s", srcXml)
    68  		}
    69  
    70  		dstJson, err := gxml.ToJson([]byte(dstXml))
    71  		if err != nil {
    72  			t.Errorf("dstXml to json error. %s", dstXml)
    73  		}
    74  
    75  		if bytes.Compare(srcJson, dstJson) != 0 {
    76  			t.Errorf("convert to json error. srcJson:%s, dstJson:%s", string(srcJson), string(dstJson))
    77  		}
    78  
    79  	}
    80  }
    81  
    82  func Test_Decode(t *testing.T) {
    83  	for _, v := range testData {
    84  		srcXml, dstXml := buildXml(v.otherEncoding, v.utf8)
    85  		if len(srcXml) == 0 && len(dstXml) == 0 {
    86  			t.Errorf("build xml string error. srcEncoding:%s, src:%s, utf8:%s", v.otherEncoding, v.other, v.utf8)
    87  		}
    88  
    89  		srcMap, err := gxml.Decode([]byte(srcXml))
    90  		if err != nil {
    91  			t.Errorf("gxml.Decode error. %s", srcXml)
    92  		}
    93  
    94  		dstMap, err := gxml.Decode([]byte(dstXml))
    95  		if err != nil {
    96  			t.Errorf("gxml decode error. %s", dstXml)
    97  		}
    98  		s := srcMap["doc"].(map[string]interface{})
    99  		d := dstMap["doc"].(map[string]interface{})
   100  		for kk, vv := range s {
   101  			if vv.(string) != d[kk].(string) {
   102  				t.Errorf("convert to map error. src:%v, dst:%v", vv, d[kk])
   103  			}
   104  		}
   105  	}
   106  }
   107  
   108  func Test_Encode(t *testing.T) {
   109  	m := make(map[string]interface{})
   110  	v := map[string]interface{}{
   111  		"string": "hello world",
   112  		"int":    123,
   113  		"float":  100.92,
   114  		"bool":   true,
   115  	}
   116  	m["root"] = interface{}(v)
   117  
   118  	xmlStr, err := gxml.Encode(m)
   119  	if err != nil {
   120  		t.Errorf("encode error.")
   121  	}
   122  	//t.Logf("%s\n", string(xmlStr))
   123  
   124  	res := `<root><bool>true</bool><float>100.92</float><int>123</int><string>hello world</string></root>`
   125  	if string(xmlStr) != res {
   126  		t.Errorf("encode error. result: [%s], expect:[%s]", string(xmlStr), res)
   127  	}
   128  }
   129  
   130  func Test_EncodeIndent(t *testing.T) {
   131  	m := make(map[string]interface{})
   132  	v := map[string]interface{}{
   133  		"string": "hello world",
   134  		"int":    123,
   135  		"float":  100.92,
   136  		"bool":   true,
   137  	}
   138  	m["root"] = interface{}(v)
   139  
   140  	_, err := gxml.EncodeWithIndent(m, "xml")
   141  	if err != nil {
   142  		t.Errorf("encodeWithIndent error.")
   143  	}
   144  
   145  	//t.Logf("%s\n", string(xmlStr))
   146  
   147  }
   148  
   149  func TestErrXml(t *testing.T) {
   150  	for _, v := range testErrData {
   151  		srcXml, dstXml := buildXml(v.otherEncoding, v.utf8)
   152  		if len(srcXml) == 0 && len(dstXml) == 0 {
   153  			t.Errorf("build xml string error. srcEncoding:%s, src:%s, utf8:%s", v.otherEncoding, v.other, v.utf8)
   154  		}
   155  
   156  		srcXml = strings.Replace(srcXml, "gbk", "XXX", -1)
   157  		_, err := gxml.ToJson([]byte(srcXml))
   158  		if err == nil {
   159  			t.Errorf("srcXml to json should be failed. %s", srcXml)
   160  		}
   161  
   162  	}
   163  }
   164  
   165  func TestErrCase(t *testing.T) {
   166  	gtest.Case(t, func() {
   167  		errXml := `<root><bool>true</bool><float>100.92</float><int>123</int><string>hello world</string>`
   168  		_, err := gxml.ToJson([]byte(errXml))
   169  		if err == nil {
   170  			t.Errorf("unexpected value: nil")
   171  		}
   172  	})
   173  
   174  	gtest.Case(t, func() {
   175  		errXml := `<root><bool>true</bool><float>100.92</float><int>123</int><string>hello world</string>`
   176  		_, err := gxml.Decode([]byte(errXml))
   177  		if err == nil {
   178  			t.Errorf("unexpected value: nil")
   179  		}
   180  	})
   181  }