-
Notifications
You must be signed in to change notification settings - Fork 14
/
tilepix_test.go
138 lines (128 loc) · 2.62 KB
/
tilepix_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
package tilepix_test
import (
"bytes"
"encoding/xml"
"io"
"io/ioutil"
"testing"
"github.com/bcvery1/tilepix"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetOutput(ioutil.Discard)
}
func TestReadFile(t *testing.T) {
tests := []struct {
name string
filepath string
want *tilepix.Map
wantErr bool
}{
{
name: "base64",
filepath: "testdata/base64.tmx",
want: nil,
wantErr: false,
},
{
name: "base64-zlib",
filepath: "testdata/base64-zlib.tmx",
want: nil,
wantErr: false,
},
{
name: "base64-gzip",
filepath: "testdata/base64-gzip.tmx",
want: nil,
wantErr: false,
},
{
name: "csv",
filepath: "testdata/csv.tmx",
want: nil,
wantErr: false,
},
{
name: "xml",
filepath: "testdata/xml.tmx",
want: nil,
wantErr: false,
},
{
name: "missing file",
filepath: "testdata/foo.tmx",
want: nil,
wantErr: true,
},
{
name: "map is infinite",
filepath: "testdata/infinite.tmx",
want: nil,
wantErr: true,
},
{
name: "external tileset",
filepath: "testdata/external_tileset.tmx",
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tilepix.ReadFile(tt.filepath)
if !tt.wantErr && err != nil {
t.Errorf("tmx.ReadFile(): got unexpected error: %v", err)
}
if tt.wantErr && err == nil {
t.Errorf("tmx.ReadFile(): expected error but not nil")
}
})
}
}
func TestRead(t *testing.T) {
tests := []struct {
name string
input io.Reader
dir string
want *tilepix.Map
wantErr bool
expectedErr string
}{
{
name: "Missing columns parameter in tileset",
input: getInput(),
dir: "testdata",
want: nil,
wantErr: true,
expectedErr: "Tileset columns value not valid",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tilepix.Read(tt.input, tt.dir, nil)
if tt.wantErr && err == nil {
t.Errorf("tsx.Read: expected error but not nil")
}
if tt.wantErr && err != nil && err.Error() != tt.expectedErr {
t.Errorf("tsx.Read: expected error '%s' but not '%s'", tt.expectedErr, err.Error())
}
})
}
}
func getInput() io.Reader {
m := tilepix.Map{
Version: "1.2",
Orientation: "orthogonal",
Width: 640,
Height: 480,
TileWidth: 32,
TileHeight: 32,
Tilesets: []*tilepix.Tileset{
&tilepix.Tileset{
Source: "tileset_no_columns.tsx",
},
},
}
byteMap, _ := xml.Marshal(m)
return bytes.NewReader(byteMap)
}