-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaxis_test.go
More file actions
246 lines (219 loc) · 7.86 KB
/
Copy pathaxis_test.go
File metadata and controls
246 lines (219 loc) · 7.86 KB
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
package geom
import (
"encoding/json"
"fmt"
"testing"
"github.com/gravitton/assert"
)
func TestParseAxis(t *testing.T) {
t.Run("every name round-trips", func(t *testing.T) {
for _, axis := range Axes() {
parsed, err := ParseAxis(axis.String())
assert.NoError(t, err, axis.String()+": ")
assert.Equal(t, parsed, axis, axis.String()+": ")
}
})
t.Run("none", func(t *testing.T) {
parsed, err := ParseAxis("None")
assert.NoError(t, err)
assert.Equal(t, parsed, AxisNone)
})
t.Run("unknown is an error", func(t *testing.T) {
_, err := ParseAxis("Diagonal")
assert.Error(t, err)
})
}
func TestAxis_Cross(t *testing.T) {
t.Run("swaps the two axes", func(t *testing.T) {
assert.Equal(t, AxisHorizontal.Perpendicular(), AxisVertical)
assert.Equal(t, AxisVertical.Perpendicular(), AxisHorizontal)
})
t.Run("none has no cross", func(t *testing.T) {
assert.Equal(t, AxisNone.Perpendicular(), AxisNone)
assert.Equal(t, Axis(2).Perpendicular(), AxisNone)
})
}
func TestAxis_Direction(t *testing.T) {
t.Run("positive points right and down", func(t *testing.T) {
assert.Equal(t, AxisHorizontal.Direction(true), DirectionRight)
assert.Equal(t, AxisVertical.Direction(true), DirectionDown)
})
t.Run("negative points left and up", func(t *testing.T) {
assert.Equal(t, AxisHorizontal.Direction(false), DirectionLeft)
assert.Equal(t, AxisVertical.Direction(false), DirectionUp)
})
t.Run("none has no direction", func(t *testing.T) {
assert.Equal(t, AxisNone.Direction(true), DirectionNone)
assert.Equal(t, AxisNone.Direction(false), DirectionNone)
})
}
func TestAxis_Along(t *testing.T) {
size := Sz(10, 20)
t.Run("picks the extent on the axis", func(t *testing.T) {
assert.Equal(t, AxisHorizontal.Along(size), 10)
assert.Equal(t, AxisVertical.Along(size), 20)
})
t.Run("none has no extent", func(t *testing.T) {
assert.Equal(t, AxisNone.Along(size), 0)
})
}
func TestAxis_Across(t *testing.T) {
size := Sz(10, 20)
t.Run("picks the extent on the axis", func(t *testing.T) {
assert.Equal(t, AxisHorizontal.Across(size), 20)
assert.Equal(t, AxisVertical.Across(size), 10)
})
t.Run("none has no extent", func(t *testing.T) {
assert.Equal(t, AxisNone.Across(size), 0)
})
}
func TestAxis_Project(t *testing.T) {
t.Run("picks the component on the main axis", func(t *testing.T) {
assert.Equal(t, AxisHorizontal.Project(Vec(3, 4)), 3)
assert.Equal(t, AxisVertical.Project(Vec(3, 4)), 4)
})
t.Run("a perpendicular direction projects to zero", func(t *testing.T) {
assert.Equal(t, AxisVertical.Project(DirectionRight.Offset[int]()), 0)
assert.Equal(t, AxisVertical.Project(DirectionDown.Offset[int]()), 1)
})
t.Run("keeps the sign", func(t *testing.T) {
assert.Equal(t, AxisHorizontal.Project(Vec(-3, 2)), -3)
assert.Equal(t, AxisVertical.Project(Vec(3, -4.5)), -4.5)
})
t.Run("none projects to zero", func(t *testing.T) {
assert.Equal(t, AxisNone.Project(Vec(3, 4)), 0)
})
}
func TestAxis_ScaleAlong(t *testing.T) {
t.Run("leaves the cross axis alone", func(t *testing.T) {
AssertSize(t, AxisHorizontal.ScaleAlong(Sz(10, 20), 0.5), Sz(5, 20))
AssertSize(t, AxisVertical.ScaleAlong(Sz(10, 20), 0.5), Sz(10, 10))
AssertSize(t, AxisHorizontal.ScaleAlong(Sz(1.0, 2.0), 3), Sz(3.0, 2.0))
})
t.Run("none leaves the size unchanged", func(t *testing.T) {
AssertSize(t, AxisNone.ScaleAlong(Sz(10, 20), 0.5), Sz(10, 20))
})
}
func TestAxis_ScaleAcross(t *testing.T) {
t.Run("leaves the main axis alone", func(t *testing.T) {
AssertSize(t, AxisHorizontal.ScaleAcross(Sz(10, 20), 0.5), Sz(10, 10))
AssertSize(t, AxisVertical.ScaleAcross(Sz(10, 20), 0.5), Sz(5, 20))
AssertSize(t, AxisHorizontal.ScaleAcross(Sz(1.0, 2.0), 3), Sz(1.0, 6.0))
})
t.Run("none leaves the size unchanged", func(t *testing.T) {
AssertSize(t, AxisNone.ScaleAcross(Sz(10, 20), 0.5), Sz(10, 20))
})
}
func TestAxis_IsNone(t *testing.T) {
t.Run("the two axes", func(t *testing.T) {
assert.False(t, AxisHorizontal.IsNone())
assert.False(t, AxisVertical.IsNone())
assert.Equal(t, Axis(0), AxisHorizontal)
})
t.Run("anything else", func(t *testing.T) {
assert.True(t, AxisNone.IsNone())
assert.True(t, Axis(2).IsNone())
})
}
func TestAxis_Vector(t *testing.T) {
t.Run("horizontal keeps the order", func(t *testing.T) {
AssertVector(t, AxisHorizontal.Vector(3, 4), Vec(3, 4))
})
t.Run("vertical swaps it", func(t *testing.T) {
AssertVector(t, AxisVertical.Vector(3, 4), Vec(4, 3))
AssertVector(t, AxisVertical.Vector(1.5, 2.5), Vec(2.5, 1.5))
})
t.Run("none is the zero vector", func(t *testing.T) {
AssertVector(t, AxisNone.Vector(3, 4), Vec(0, 0))
})
}
func TestAxis_Size(t *testing.T) {
t.Run("horizontal keeps the order", func(t *testing.T) {
AssertSize(t, AxisHorizontal.Size(3, 4), Sz(3, 4))
})
t.Run("vertical swaps it", func(t *testing.T) {
AssertSize(t, AxisVertical.Size(3, 4), Sz(4, 3))
AssertSize(t, AxisVertical.Size(1.5, 2.5), Sz(2.5, 1.5))
})
t.Run("keeps the sign, like Sz", func(t *testing.T) {
AssertSize(t, AxisHorizontal.Size(-3, 4), Sz(-3, 4))
AssertSize(t, AxisHorizontal.ScaleAlong(Sz(10, 4), -1), Sz(10, 4).ScaleXY(-1, 1))
})
t.Run("none has no extent", func(t *testing.T) {
AssertSize(t, AxisNone.Size(3, 4), Sz(0, 0))
})
}
func TestAxis_String(t *testing.T) {
t.Run("named axes", func(t *testing.T) {
assert.Equal(t, AxisHorizontal.String(), "Horizontal")
assert.Equal(t, AxisVertical.String(), "Vertical")
})
t.Run("none and out of range", func(t *testing.T) {
assert.Equal(t, AxisNone.String(), "None")
assert.Equal(t, Axis(2).String(), "None")
})
}
func TestAxis_JSON(t *testing.T) {
t.Run("wire format is the name", func(t *testing.T) {
assert.JSON(t, AxisVertical, `"Vertical"`)
assert.JSON(t, AxisNone, `"None"`)
})
t.Run("round-trip", func(t *testing.T) {
axes := Axes()
for _, axis := range append(axes[:], AxisNone) {
data, err := json.Marshal(axis)
assert.NoError(t, err)
var decoded Axis
assert.NoError(t, json.Unmarshal(data, &decoded))
assert.Equal(t, decoded, axis, axis.String()+": ")
}
})
t.Run("an unknown name is an error", func(t *testing.T) {
var decoded Axis
assert.Error(t, json.Unmarshal([]byte(`"Diagonal"`), &decoded))
})
}
func TestAxis_Properties(t *testing.T) {
t.Run("cross is its own inverse", func(t *testing.T) {
for _, axis := range Axes() {
assert.Equal(t, axis.Perpendicular().Perpendicular(), axis, axis.String()+": ")
}
})
t.Run("along and across swap on the cross axis", func(t *testing.T) {
size := Sz(10.0, 20.0)
for _, axis := range Axes() {
AssertNumber(t, axis.Perpendicular().Along(size), axis.Across(size), axis.String()+": ")
AssertNumber(t, axis.Perpendicular().Across(size), axis.Along(size), axis.String()+": ")
}
})
t.Run("size and along are inverse", func(t *testing.T) {
for _, axis := range Axes() {
built := axis.Size(1.0, 2.0)
AssertNumber(t, axis.Along(built), 1.0, axis.String()+": ")
AssertNumber(t, axis.Across(built), 2.0, axis.String()+": ")
}
})
t.Run("direction round-trips through axis and sign", func(t *testing.T) {
for _, axis := range Axes() {
for _, positive := range []bool{true, false} {
direction := axis.Direction(positive)
assert.Equal(t, direction.Axis(), axis, direction.String()+": ")
assert.Equal(t, direction.IsPositive(), positive, direction.String()+": ")
}
}
})
t.Run("scale along and across swap on the cross axis", func(t *testing.T) {
size := Sz(10.0, 20.0)
for _, axis := range Axes() {
AssertSize(t, axis.Perpendicular().ScaleAlong(size, 0.5), axis.ScaleAcross(size, 0.5), axis.String()+": ")
AssertSize(t, axis.Perpendicular().ScaleAcross(size, 0.5), axis.ScaleAlong(size, 0.5), axis.String()+": ")
}
})
t.Run("project is the component of the vector", func(t *testing.T) {
for _, axis := range Axes() {
for _, vector := range vectorFixtures {
AssertNumber(t, axis.Project(vector), axis.Along(Sz(vector.X, vector.Y)), fmt.Sprintf("%s → %s: ", axis, vector))
}
}
})
}