-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawable_test.go
95 lines (73 loc) · 1.95 KB
/
drawable_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
package main
import (
"testing"
"github.com/hajimehoshi/ebiten/v2"
)
// MockDrawable is a mock implementation of the Drawable interface for testing purposes.
type MockDrawable struct {
zIndex int
drawn bool
}
func (m *MockDrawable) Draw(screen *ebiten.Image) {
m.drawn = true
}
func (m *MockDrawable) ZIndex() int {
return m.zIndex
}
func TestDrawHandlerAdd(t *testing.T) {
handler := &DrawHandler{}
// Add objects with different ZIndexes
obj1 := &MockDrawable{zIndex: 1}
obj2 := &MockDrawable{zIndex: 2}
obj3 := &MockDrawable{zIndex: 3}
handler.Add(obj1)
handler.Add(obj2)
handler.Add(obj3)
// Check the order
if len(handler.drawable) != 3 {
t.Errorf("expected 3 objects, got %d", len(handler.drawable))
}
if handler.drawable[0] != obj1 || handler.drawable[1] != obj2 || handler.drawable[2] != obj3 {
t.Errorf("objects are not sorted correctly by ZIndex")
}
}
func TestDrawHandlerRemove(t *testing.T) {
handler := &DrawHandler{}
obj1 := &MockDrawable{zIndex: 1}
obj2 := &MockDrawable{zIndex: 2}
handler.Add(obj1)
handler.Add(obj2)
handler.Remove(obj1)
// Check the remaining objects
if len(handler.drawable) != 1 {
t.Errorf("expected 1 object, got %d", len(handler.drawable))
}
if handler.drawable[0] != obj2 {
t.Errorf("unexpected object in the list")
}
}
func TestDrawHandlerDraw(t *testing.T) {
handler := &DrawHandler{}
screen := ebiten.NewImage(100, 100)
obj1 := &MockDrawable{zIndex: 1}
obj2 := &MockDrawable{zIndex: 2}
handler.Add(obj1)
handler.Add(obj2)
handler.HandleDraw(screen)
// Check if both objects were drawn
if !obj1.drawn || !obj2.drawn {
t.Errorf("expected objects to be drawn")
}
}
func TestDrawHandlerClear(t *testing.T) {
handler := &DrawHandler{}
obj1 := &MockDrawable{zIndex: 1}
obj2 := &MockDrawable{zIndex: 2}
handler.Add(obj1)
handler.Add(obj2)
handler.Clear()
// Check if the list is empty
if len(handler.drawable) != 0 {
t.Errorf("expected 0 objects, got %d", len(handler.drawable))
}
}