-
Notifications
You must be signed in to change notification settings - Fork 6
/
import_test.go
147 lines (138 loc) · 3.1 KB
/
import_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package collada
import (
"bytes"
"encoding/xml"
"io"
"os"
"reflect"
"strings"
"testing"
)
var emptyCollada string = `
<COLLADA version="1.5.0">
</COLLADA>
`
func TestEmptyDocument(t *testing.T) {
reader := strings.NewReader(emptyCollada)
collada, err := LoadDocumentFromReader(reader)
if err != nil {
t.Error(err)
t.FailNow()
}
if collada.Version != Version1_5_0 {
t.Error("wrong version", collada.Version)
}
}
//A simple scene with a cube
func TestCubeDocument(t *testing.T) {
compareColladaFile("cube.dae", t)
}
//A more complicated geometry
func TestScrewDocument(t *testing.T) {
compareColladaFile("screw.dae", t)
}
func compareColladaFile(filename string, t *testing.T) {
file, err := os.Open(filename)
if err != nil {
t.Error(err)
t.FailNow()
}
defer file.Close()
collada, err := LoadDocumentFromReader(file)
if err != nil {
t.Error(err)
t.FailNow()
}
buffer := &bytes.Buffer{}
err = collada.ExportToWriter(buffer)
if err != nil {
t.Error(err)
}
file.Seek(0, os.SEEK_SET)
reader := bytes.NewReader(buffer.Bytes())
CompareXml(file, reader, t)
}
func failCompare(t *testing.T, message string, a, b interface{}) {
t.Error(message, "\nExpected:", a, "\nActual :", b)
t.FailNow()
}
func nextToken(decoder *xml.Decoder) (xml.Token, error) {
t, err := decoder.Token()
for err == nil {
switch t := t.(type) {
case xml.CharData:
sa := strings.TrimSpace(string(t))
if len(sa) > 0 {
return t, err
}
default:
return t, err
}
t, err = decoder.Token()
}
return t, err
}
func CompareXml(ar, br io.Reader, t *testing.T) {
a := xml.NewDecoder(ar)
b := xml.NewDecoder(br)
var ea, eb error
for true {
var na, nb xml.Token
na, ea = nextToken(a)
nb, eb = nextToken(b)
if ea != nil || eb != nil {
break
}
switch na := na.(type) {
case xml.StartElement:
switch nb := nb.(type) {
case xml.StartElement:
if na.Name.Local != nb.Name.Local {
failCompare(t, "wrong node name", na.Name, nb.Name)
}
lenAttr := len(na.Attr)
if lenAttr != len(nb.Attr) {
failCompare(t, "wrong attribute length <"+na.Name.Local+">", na.Attr, nb.Attr)
t.FailNow()
}
aa := make(map[xml.Name]string, lenAttr)
ab := make(map[xml.Name]string, lenAttr)
for i := 0; i < lenAttr; i++ {
aa[na.Attr[i].Name] = na.Attr[i].Value
ab[nb.Attr[i].Name] = nb.Attr[i].Value
}
for k, v := range aa {
if v != ab[k] {
failCompare(t, "wrong attribute <"+na.Name.Local+">"+k.Local, v, ab[k])
}
}
for i := 0; i < lenAttr; i++ {
if na.Attr[i] != nb.Attr[i] {
}
}
default:
t.Error("\n", na, "\n", nb)
failCompare(t, "wrong token", na, reflect.TypeOf(nb))
t.FailNow()
}
case xml.CharData:
switch nb := nb.(type) {
case xml.CharData:
sa := strings.TrimSpace(string(na))
sb := strings.TrimSpace(string(nb))
if sa != sb {
failCompare(t, "wrong text", sa, sb)
}
default:
t.Error("\n", na, "\n", nb)
failCompare(t, "wrong token", reflect.TypeOf(na), reflect.TypeOf(nb))
}
}
}
if ea == nil {
t.Error("missing elements")
}
if eb == nil {
t.Error("extra elements")
}
}