-
Notifications
You must be signed in to change notification settings - Fork 6
/
grid_test.go
208 lines (184 loc) · 5.07 KB
/
grid_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
package ganim8_test
import (
"fmt"
"image"
"testing"
"github.com/stretchr/testify/require"
"github.com/yohamta/ganim8/v2"
)
func assertEqualRect(a, b *image.Rectangle) bool {
return a.Eq(*b)
}
func assertEqualRects(a, b []*image.Rectangle) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if !assertEqualRect(a[i], b[i]) {
return false
}
}
return true
}
func TestWith2Integers(t *testing.T) {
grid := ganim8.NewGrid(16, 16, 64, 64)
nr := func(x, y int) *image.Rectangle {
r := image.Rect(x, y, x+16, y+16)
return &r
}
var tests = []struct {
name string
args []interface{}
want []*image.Rectangle
}{
{"returns a single frame", []interface{}{1, 1}, []*image.Rectangle{nr(0, 0)}},
{"another single frame", []interface{}{3, 2}, []*image.Rectangle{nr(32, 16)}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := _TestGetFrames(grid, tt.args)
if assertEqualRects(got, tt.want) == false {
t.Errorf("%s: got %v; want %v", tt.name, got, tt.want)
}
})
}
}
func TestWithFloat64(t *testing.T) {
grid := ganim8.NewGrid(16, 16, 64, 64)
nr := func(x, y int) *image.Rectangle {
r := image.Rect(x, y, x+16, y+16)
return &r
}
got := _TestGetFrames(grid, []interface{}{1.2, 1.3})
if assertEqualRects(got, []*image.Rectangle{nr(0, 0)}) == false {
t.Errorf("got %v; want %v", got, []*image.Rectangle{nr(0, 0)})
}
}
func TestWithSeveralPairsOfIntegers(t *testing.T) {
grid := ganim8.NewGrid(16, 16, 64, 64)
gridWithOffesets := ganim8.NewGrid(16, 16, 64, 64, 10, 20)
nr := func(x, y int) *image.Rectangle {
r := image.Rect(x, y, x+16, y+16)
return &r
}
var tests = []struct {
name string
args []interface{}
want []*image.Rectangle
grid *ganim8.Grid
}{
{"returns a list of frames", []interface{}{1, 3, 2, 2, 3, 1}, []*image.Rectangle{nr(0, 32), nr(16, 16), nr(32, 0)}, grid},
{"takes into account left and top", []interface{}{1, 3, 2, 2, 3, 1}, []*image.Rectangle{nr(10, 52), nr(26, 36), nr(42, 20)}, gridWithOffesets},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := _TestGetFrames(tt.grid, tt.args)
if assertEqualRects(got, tt.want) == false {
t.Errorf("%s: got %v; want %v", tt.name, got, tt.want)
}
})
}
}
func TestWithStringAndAIntegry(t *testing.T) {
grid := ganim8.NewGrid(16, 16, 64, 64)
nr := func(x, y int) *image.Rectangle {
r := image.Rect(x, y, x+16, y+16)
return &r
}
var tests = []struct {
name string
args []interface{}
want []*image.Rectangle
grid *ganim8.Grid
}{
{"returns a list of frames", []interface{}{"1-2", 2}, []*image.Rectangle{nr(0, 16), nr(16, 16)}, grid},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := _TestGetFrames(tt.grid, tt.args)
if assertEqualRects(got, tt.want) == false {
t.Errorf("%s: got %v; want %v", tt.name, got, tt.want)
}
})
}
}
func TestWithSeveralStrings(t *testing.T) {
grid := ganim8.NewGrid(16, 16, 64, 64)
nr := func(x, y int) *image.Rectangle {
r := image.Rect(x, y, x+16, y+16)
return &r
}
var tests = []struct {
name string
args []interface{}
want []*image.Rectangle
grid *ganim8.Grid
}{
{"returns a list of frames", []interface{}{"1-2", 2, 3, 2}, []*image.Rectangle{nr(0, 16), nr(16, 16), nr(32, 16)}, grid},
{"parses rows first, then columns", []interface{}{"1-3", "1-3"}, []*image.Rectangle{
nr(0, 0), nr(16, 0), nr(32, 0),
nr(0, 16), nr(16, 16), nr(32, 16),
nr(0, 32), nr(16, 32), nr(32, 32),
}, grid},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := _TestGetFrames(tt.grid, tt.args)
if assertEqualRects(got, tt.want) == false {
t.Errorf("%s: got %v; want %v", tt.name, got, tt.want)
}
})
}
}
func TestWithoutArgs(t *testing.T) {
grid := ganim8.NewGrid(16, 16, 64, 64)
nr := func(x, y int) *image.Rectangle {
r := image.Rect(x, y, x+16, y+16)
return &r
}
var tests = []struct {
name string
args []interface{}
want []*image.Rectangle
}{
{"returns all frames", []interface{}{}, []*image.Rectangle{
nr(0, 0), nr(16, 0), nr(32, 0), nr(48, 0),
nr(0, 16), nr(16, 16), nr(32, 16), nr(48, 16),
nr(0, 32), nr(16, 32), nr(32, 32), nr(48, 32),
nr(0, 48), nr(16, 48), nr(32, 48), nr(48, 48),
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := _TestGetFrames(grid, tt.args)
if assertEqualRects(got, tt.want) == false {
t.Errorf("%s: got %v; want %v", tt.name, got, tt.want)
}
})
}
}
func TestFramesWithBorder(t *testing.T) {
gs := ganim8.NewGrid(32, 32, 1024, 1025, 0, 0, 1)
f := gs.Frames("1-3", 1, "3-1", 1)
require.Equal(t, 6, len(f))
expectedFrames := []image.Rectangle{
image.Rect(1, 1, 33, 33),
image.Rect(34, 1, 66, 33),
image.Rect(67, 1, 99, 33),
}
for i, want := range []image.Rectangle{
expectedFrames[0],
expectedFrames[1],
expectedFrames[2],
expectedFrames[2],
expectedFrames[1],
expectedFrames[0],
} {
t.Run(fmt.Sprintf("frame-%d", i), func(t *testing.T) {
require.Equal(t, want, f[i].Bounds())
})
}
}
func _TestGetFrames(g *ganim8.Grid, args []interface{}) []*image.Rectangle {
return g.GetFrames(args...)
}