forked from lafriks/go-tiled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiled_test.go
442 lines (380 loc) · 10.2 KB
/
tiled_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
Copyright (c) 2017 Lauris Bukšis-Haberkorns <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package tiled
import (
"bytes"
"embed"
"encoding/xml"
"image/color"
"io/fs"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
func GetAssetsDirectory() string {
_, filename, _, _ := runtime.Caller(0)
return filepath.Join(filepath.Dir(filename), "assets")
}
func TestLoadFromFileWangSet(t *testing.T) {
m, err := LoadFile(filepath.Join(GetAssetsDirectory(), "test_wangsets_map.tmx"))
assert.NoError(t, err)
assert.NotNil(t, m)
// Test WangSets
assert.NotNil(t, m.Tilesets[0].WangSets)
assert.NotNil(t, m.Tilesets[0].WangSets[0].WangColors)
}
func TestLoadReader(t *testing.T) {
r := bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.2.1" orientation="orthogonal" renderorder="right-down" width="4" height="4" tilewidth="16" tileheight="16" infinite="0" nextlayerid="2" nextobjectid="2">
<layer id="1" name="Tile Layer 1" width="4" height="4">
<data encoding="csv">
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0
</data>
</layer>
</map>`)
m, err := LoadReader(GetAssetsDirectory(), r)
assert.NoError(t, err)
assert.NotNil(t, m)
assert.Len(t, m.Layers, 1)
}
func TestLoadReaderError(t *testing.T) {
r := bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.2.1" orientation="orthogonal" renderorder="right-down" width="4" height="4" tilewidth="16" tileheight="16" infinite="0" nextlayerid="2" nextobjectid="2">
<layer id="1" name="Tile Layer 1" width="4" height="4">
<data encoding="csv">
</layer>
</map>`)
m, err := LoadReader(GetAssetsDirectory(), r)
assert.Error(t, err)
assert.Nil(t, m)
}
func TestLoadFile(t *testing.T) {
m, err := LoadFile(filepath.Join(GetAssetsDirectory(), "test.tmx"))
assert.NoError(t, err)
assert.NotNil(t, m)
assert.Len(t, m.Layers, 1)
assert.Equal(t, uint32(1), m.Layers[0].ID)
// Test ObjectGroups.Visible defaults to true
assert.Len(t, m.ObjectGroups, 1)
assert.Equal(t, uint32(2), m.ObjectGroups[0].ID)
assert.Equal(t, true, m.ObjectGroups[0].Visible)
// Test Object.Visible defaults to true
assert.Len(t, m.ObjectGroups[0].Objects, 1)
assert.Equal(t, uint32(2), m.ObjectGroups[0].Objects[0].ID)
assert.Equal(t, true, m.ObjectGroups[0].Objects[0].Visible)
}
func TestLoadFileError(t *testing.T) {
m, err := LoadFile(filepath.Join(GetAssetsDirectory(), "invalid.tmx"))
assert.Error(t, err)
assert.Nil(t, m)
}
func TestExternalTilesetImageLoaded(t *testing.T) {
m, err := LoadFile(filepath.Join(GetAssetsDirectory(), "test2.tmx"))
assert.NoError(t, err)
assert.NotNil(t, m)
for _, layer := range m.Layers {
var tileset *Tileset
for _, tile := range layer.Tiles {
if tile.ID > 0 {
tileset = tile.Tileset
assert.NotNil(t, tileset)
if tileset != nil {
assert.NotNil(t, tileset.Image)
assert.Equal(t, "ProjectUtumno_full.png", tileset.Image.Source)
assert.Contains(t, m.Tilesets, tileset)
}
}
}
}
}
func TestImageLayer(t *testing.T) {
m, err := LoadFile(filepath.Join(GetAssetsDirectory(), "imagelayer.tmx"))
assert.NoError(t, err)
assert.NotNil(t, m)
assert.Len(t, m.ImageLayers, 1)
layer := m.ImageLayers[0]
assert.Equal(t, uint32(2), layer.ID)
assert.Equal(t, "Image Layer", layer.Name)
assert.Equal(t, 0, layer.OffsetX)
assert.Equal(t, 0, layer.OffsetY)
assert.Equal(t, float32(1.0), layer.Opacity)
assert.Equal(t, true, layer.Visible)
image := layer.Image
assert.NotNil(t, image)
assert.Equal(t, image.Source, "background.jpg", image.Source)
}
func TestGroup(t *testing.T) {
m, err := LoadFile(filepath.Join(GetAssetsDirectory(), "groups.tmx"))
assert.NoError(t, err)
assert.NotNil(t, m)
assert.Len(t, m.Layers, 1)
assert.Len(t, m.Groups, 1)
a := m.Groups[0]
assert.Equal(t, uint32(2), a.ID)
assert.Equal(t, "A", a.Name)
assert.Len(t, a.ImageLayers, 1)
assert.Len(t, a.Groups, 1)
b := a.Groups[0]
assert.Equal(t, uint32(4), b.ID)
assert.Equal(t, "B", b.Name)
assert.Len(t, b.Layers, 1)
assert.Len(t, b.Groups, 1)
bL := b.Layers[0]
assert.Equal(t, uint32(3), bL.ID)
assert.Equal(t, bL.Name, "Tile Layer 2")
assert.Len(t, bL.Tiles, 400)
c := b.Groups[0]
assert.Equal(t, uint32(8), c.ID)
assert.Equal(t, "C", c.Name)
assert.Len(t, c.ObjectGroups, 1)
assert.Len(t, c.Groups, 0)
}
func TestFont(t *testing.T) {
m, err := LoadFile(filepath.Join(GetAssetsDirectory(), "font.tmx"))
assert.NoError(t, err)
assert.NotNil(t, m)
if assert.Len(t, m.ObjectGroups, 1) {
if assert.Len(t, m.ObjectGroups[0].Objects, 1) {
if assert.NotNil(t, m.ObjectGroups[0].Objects[0].Text) {
text := m.ObjectGroups[0].Objects[0].Text
assert.Equal(t, "Hello World", text.Text)
assert.Equal(t, "sans-serif", text.FontFamily)
assert.Equal(t, 16, text.Size)
assert.Equal(t, true, text.Wrap)
assert.Equal(t, &HexColor{}, text.Color)
assert.Equal(t, false, text.Bold)
assert.Equal(t, false, text.Italic)
assert.Equal(t, false, text.Underline)
assert.Equal(t, false, text.Strikethrough)
assert.Equal(t, true, text.Kerning)
assert.Equal(t, "left", text.HAlign)
assert.Equal(t, "top", text.VAlign)
}
}
}
}
type testFileSystem struct {
AttemptedOpen []string
}
func (t *testFileSystem) Open(filename string) (fs.File, error) {
t.AttemptedOpen = append(t.AttemptedOpen, filename)
if filepath.Base(filename) == "loader.tmx" {
return os.Open(filepath.Join(GetAssetsDirectory(), "loader.tmx"))
}
return nil, os.ErrNotExist
}
func TestLoader(t *testing.T) {
fs := &testFileSystem{}
loader := &loader{
FileSystem: fs,
}
mapFile := filepath.Join(GetAssetsDirectory(), "loader.tmx")
m, err := loader.LoadFile(mapFile)
if assert.Error(t, err) {
assert.True(t, os.IsNotExist(err), "expecting no exist error")
}
assert.Nil(t, m)
assert.Equal(t, []string{mapFile, filepath.Join(GetAssetsDirectory(), "..", "README.md")}, fs.AttemptedOpen)
}
//go:embed assets/**
var assetsFS embed.FS
func TestEmbeddedLoader(t *testing.T) {
tcs := []struct {
name string
load func() (*Map, error)
}{
{
name: "LoadReader",
load: func() (*Map, error) {
file, err := assetsFS.Open("assets/test2.tmx")
if err != nil {
return nil, err
}
return LoadReader("assets", file, WithFileSystem(assetsFS))
},
},
{
name: "LoadFile",
load: func() (*Map, error) {
return LoadFile("assets/test2.tmx", WithFileSystem(assetsFS))
},
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
m, err := tc.load()
assert.NoError(t, err)
assert.Equal(t, m.Version, "1.2")
assert.Equal(t, m.TiledVersion, "1.2.3")
assert.Len(t, m.Tilesets, 1)
tileset := m.Tilesets[0]
assert.Equal(t, tileset.Version, "1.2")
assert.Equal(t, tileset.TiledVersion, "1.2.3")
})
}
}
func TestLoadFileMissingFile(t *testing.T) {
_, err := LoadFile("missing-file.tmx")
if err == nil {
t.Fatal("expected LoadFile to return err, but no error was returned")
}
}
func TestParseHexColor(t *testing.T) {
type test struct {
name string
attr xml.Attr
color color.RGBA
}
cases := []test{
{
name: "Wihout alpha",
attr: xml.Attr{
Value: "#aabbcc",
},
color: color.RGBA{
R: 170,
G: 187,
B: 204,
A: 255,
},
},
{
name: "With alpha",
attr: xml.Attr{
Value: "#aabbccdd",
},
color: color.RGBA{
R: 187,
G: 204,
B: 221,
A: 170,
},
},
{
name: "Simplified",
attr: xml.Attr{
Value: "#fff",
},
color: color.RGBA{
R: 255,
G: 255,
B: 255,
A: 255,
},
},
{
name: "Simplified with alpha",
attr: xml.Attr{
Value: "#ffff",
},
color: color.RGBA{
R: 255,
G: 255,
B: 255,
A: 255,
},
},
{
name: "Different values",
attr: xml.Attr{
Value: "#010203",
},
color: color.RGBA{
R: 1,
G: 2,
B: 3,
A: 255,
},
},
{
name: "uppercase",
attr: xml.Attr{
Value: "#ABCDEF",
},
color: color.RGBA{
R: 171,
G: 205,
B: 239,
A: 255,
},
},
}
for _, c := range cases {
color := &HexColor{}
assert.NoError(t, color.UnmarshalXMLAttr(c.attr))
assert.Equal(t, c.color, color.c, c.name)
}
}
func TestFormatHexColor(t *testing.T) {
type test struct {
name string
color HexColor
hex string
}
cases := []test{
{
name: "alpha ignored at 255",
color: NewHexColor(255, 255, 255, 255),
hex: "#ffffff",
},
{
name: "transparent black",
color: NewHexColor(255, 255, 255, 0),
hex: "#00ffffff",
},
{
name: "50% transparency",
color: NewHexColor(255, 255, 255, 128),
hex: "#80ffffff",
},
{
name: "values encoded with 2 numbers",
color: NewHexColor(16, 32, 48, 255),
hex: "#102030",
},
{
name: "values encoded with 1 numbers",
color: NewHexColor(1, 2, 3, 255),
hex: "#010203",
},
{
name: "zero",
color: NewHexColor(0, 0, 0, 0),
hex: "#00000000",
},
}
for _, c := range cases {
assert.Equal(t, c.hex, c.color.String(), c.name)
}
}
func TestVersions(t *testing.T) {
m, err := LoadFile(filepath.Join(GetAssetsDirectory(), "test2.tmx"))
assert.Nil(t, err)
assert.Equal(t, m.Version, "1.2")
assert.Equal(t, m.TiledVersion, "1.2.3")
assert.Len(t, m.Tilesets, 1)
tileset := m.Tilesets[0]
assert.Equal(t, tileset.Version, "1.2")
assert.Equal(t, tileset.TiledVersion, "1.2.3")
}