1// Copyright (c) 2012 - Cloud Instruments Co., Ltd.
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this
9//    list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11//    this list of conditions and the following disclaimer in the documentation
12//    and/or other materials provided with the distribution.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25package seelog
26
27import (
28	"strings"
29	"testing"
30	//"fmt"
31	"reflect"
32)
33
34var testEnv *testing.T
35
36/*func TestWrapper(t *testing.T) {
37	testEnv = t
38
39	s := "<a d='a'><g m='a'></g><g h='t' j='kk'></g></a>"
40	reader := strings.NewReader(s)
41	config, err := unmarshalConfig(reader)
42	if err != nil {
43		testEnv.Error(err)
44		return
45	}
46
47	printXML(config, 0)
48}
49
50func printXML(node *xmlNode, level int) {
51	indent := strings.Repeat("\t", level)
52	fmt.Print(indent + node.name)
53	for key, value := range node.attributes {
54		fmt.Print(" " + key + "/" + value)
55	}
56	fmt.Println()
57
58	for _, child := range node.children {
59		printXML(child, level+1)
60	}
61}*/
62
63var xmlNodeTests []xmlNodeTest
64
65type xmlNodeTest struct {
66	testName      string
67	inputXML      string
68	expected      interface{}
69	errorExpected bool
70}
71
72func getXMLTests() []xmlNodeTest {
73	if xmlNodeTests == nil {
74		xmlNodeTests = make([]xmlNodeTest, 0)
75
76		testName := "Simple test"
77		testXML := `<a></a>`
78		testExpected := newNode()
79		testExpected.name = "a"
80		xmlNodeTests = append(xmlNodeTests, xmlNodeTest{testName, testXML, testExpected, false})
81
82		testName = "Multiline test"
83		testXML =
84			`
85<a>
86</a>
87`
88		testExpected = newNode()
89		testExpected.name = "a"
90		xmlNodeTests = append(xmlNodeTests, xmlNodeTest{testName, testXML, testExpected, false})
91
92		testName = "Multiline test #2"
93		testXML =
94			`
95
96
97<a>
98
99</a>
100
101`
102		testExpected = newNode()
103		testExpected.name = "a"
104		xmlNodeTests = append(xmlNodeTests, xmlNodeTest{testName, testXML, testExpected, false})
105
106		testName = "Incorrect names"
107		testXML = `< a     ><      /a >`
108		xmlNodeTests = append(xmlNodeTests, xmlNodeTest{testName, testXML, nil, true})
109
110		testName = "Comments"
111		testXML =
112			`<!-- <abcdef/> -->
113<a> <!-- 12345-->
114</a>
115`
116		testExpected = newNode()
117		testExpected.name = "a"
118		xmlNodeTests = append(xmlNodeTests, xmlNodeTest{testName, testXML, testExpected, false})
119
120		testName = "Multiple roots"
121		testXML = `<a></a><b></b>`
122		xmlNodeTests = append(xmlNodeTests, xmlNodeTest{testName, testXML, nil, true})
123
124		testName = "Multiple roots + incorrect xml"
125		testXML = `<a></a><b>`
126		xmlNodeTests = append(xmlNodeTests, xmlNodeTest{testName, testXML, nil, true})
127
128		testName = "Some unicode and data"
129		testXML = `<俄语>данные</俄语>`
130		testExpected = newNode()
131		testExpected.name = "俄语"
132		testExpected.value = "данные"
133		xmlNodeTests = append(xmlNodeTests, xmlNodeTest{testName, testXML, testExpected, false})
134
135		testName = "Values and children"
136		testXML = `<俄语>данные<and_a_child></and_a_child></俄语>`
137		testExpected = newNode()
138		testExpected.name = "俄语"
139		testExpected.value = "данные"
140		child := newNode()
141		child.name = "and_a_child"
142		testExpected.children = append(testExpected.children, child)
143		xmlNodeTests = append(xmlNodeTests, xmlNodeTest{testName, testXML, testExpected, false})
144
145		testName = "Just children"
146		testXML = `<俄语><and_a_child></and_a_child></俄语>`
147		testExpected = newNode()
148		testExpected.name = "俄语"
149		child = newNode()
150		child.name = "and_a_child"
151		testExpected.children = append(testExpected.children, child)
152		xmlNodeTests = append(xmlNodeTests, xmlNodeTest{testName, testXML, testExpected, false})
153
154		testName = "Mixed test"
155		testXML = `<俄语 a="1" b="2.13" c="abc"><child abc="bca"/><child abc="def"></child></俄语>`
156		testExpected = newNode()
157		testExpected.name = "俄语"
158		testExpected.attributes["a"] = "1"
159		testExpected.attributes["b"] = "2.13"
160		testExpected.attributes["c"] = "abc"
161		child = newNode()
162		child.name = "child"
163		child.attributes["abc"] = "bca"
164		testExpected.children = append(testExpected.children, child)
165		child = newNode()
166		child.name = "child"
167		child.attributes["abc"] = "def"
168		testExpected.children = append(testExpected.children, child)
169		xmlNodeTests = append(xmlNodeTests, xmlNodeTest{testName, testXML, testExpected, false})
170	}
171
172	return xmlNodeTests
173}
174
175func TestXmlNode(t *testing.T) {
176
177	for _, test := range getXMLTests() {
178
179		reader := strings.NewReader(test.inputXML)
180		parsedXML, err := unmarshalConfig(reader)
181
182		if (err != nil) != test.errorExpected {
183			t.Errorf("\n%s:\nXML input: %s\nExpected error:%t. Got error: %t\n", test.testName,
184				test.inputXML, test.errorExpected, (err != nil))
185			if err != nil {
186				t.Logf("%s\n", err.Error())
187			}
188			continue
189		}
190
191		if err == nil && !reflect.DeepEqual(parsedXML, test.expected) {
192			t.Errorf("\n%s:\nXML input: %s\nExpected: %s. \nGot: %s\n", test.testName,
193				test.inputXML, test.expected, parsedXML)
194		}
195	}
196}
197