forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils_test.go
126 lines (115 loc) · 3.09 KB
/
Utils_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
package giu
import (
"image"
"image/color"
"testing"
imgui "github.com/AllenDang/cimgui-go"
"github.com/stretchr/testify/assert"
)
func Test_ToVec4(t *testing.T) {
tests := []struct {
name string
source color.Color
expected imgui.Vec4
}{
{
name: "Red - RGBA",
source: &color.RGBA{R: 255, G: 0, B: 0, A: 255},
expected: imgui.Vec4{X: 1, Y: 0, Z: 0, W: 1},
},
{
name: "Purple - RGBA",
source: &color.RGBA{R: 158, G: 0, B: 173, A: 255},
expected: imgui.Vec4{X: 0.61960787, Y: 0, Z: 0.6784314, W: 1},
},
{
name: "Purple - CMYK",
source: &color.CMYK{C: 22, M: 255, Y: 0, K: 82},
expected: imgui.Vec4{X: 0.6198978, Y: 0, Z: 0.6784314, W: 1},
},
}
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
assert.Equal(tt, test.expected, ToVec4Color(test.source), "Unexpected result")
})
}
}
func Test_ToVec2(t *testing.T) {
tests := []struct {
name string
source image.Point
expected imgui.Vec2
}{
{"Point 0,0", image.Pt(0, 0), imgui.Vec2{X: 0, Y: 0}},
{"Random point 1", image.Pt(80, 209), imgui.Vec2{X: 80, Y: 209}},
{"Random point 2", image.Pt(200, 128), imgui.Vec2{X: 200, Y: 128}},
}
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
assert.Equal(tt, test.expected, ToVec2(test.source), "Unexpected result")
})
}
}
func Test_Vec4ToRGBA(t *testing.T) {
tests := []struct {
name string
source imgui.Vec4
expected color.RGBA
}{
{
name: "Red",
source: imgui.Vec4{X: 1, Y: 0, Z: 0, W: 1},
expected: color.RGBA{R: 255, G: 0, B: 0, A: 255},
},
{
name: "Red - with 20% alpha",
source: imgui.Vec4{X: 1, Y: 0, Z: 0, W: 0.2},
expected: color.RGBA{R: 255, G: 0, B: 0, A: 51},
},
{
name: "Purple - RGBA",
source: imgui.Vec4{X: 0.61960787, Y: 0, Z: 0.6784314, W: 1},
expected: color.RGBA{R: 158, G: 0, B: 173, A: 255},
},
}
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
assert.Equal(tt, test.expected, Vec4ToRGBA(test.source), "Unexpected result")
})
}
}
func Test_Assert(t *testing.T) {
tests := []struct {
name string
condition bool
shouldPanic bool
}{
{"expected behavior - no panic", true, false},
{"something happened - panic", false, true},
}
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
if test.shouldPanic {
assert.Panics(tt, func() { Assert(test.condition, "somewidget", "somemethod", "panics") }, "unexpected behavior")
} else {
assert.NotPanics(tt, func() { Assert(test.condition, "somewidget", "somemethod", "panics") }, "unexpected behavior")
}
})
}
}
func TestUintToColor(t *testing.T) {
tests := []struct {
name string
col uint32
want *color.RGBA
}{
{"full red full alpha", 0xFF0000FF, &color.RGBA{R: 255, G: 0, B: 0, A: 255}},
{"full red 0 alpha", 0xFF000000, &color.RGBA{R: 255, G: 0, B: 0, A: 0}},
{"full green", 0x00FF00FF, &color.RGBA{R: 0, G: 255, B: 0, A: 255}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, UintToColor(tt.col), "UintToColor(%v)", tt.col)
})
}
}