Skip to content

Commit 69e5ac8

Browse files
committed
test: add pikey unit tests
1 parent 63d288f commit 69e5ac8

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

pikey/pikey_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2025 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package pikey_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
11+
"github.com/elgopher/pi"
12+
"github.com/elgopher/pi/pikey"
13+
)
14+
15+
func TestDuration(t *testing.T) {
16+
t.Run("should return 0 by default", func(t *testing.T) {
17+
assert.Equal(t, 0, pikey.Duration(pikey.A))
18+
})
19+
20+
pikey.Target().Publish(aDownEvent)
21+
22+
t.Run("should return 1 when key was down in the current frame", func(t *testing.T) {
23+
assert.Equal(t, 1, pikey.Duration(pikey.A))
24+
})
25+
26+
pi.Frame++
27+
28+
t.Run("should return 2 when key has been down since the previous frame", func(t *testing.T) {
29+
assert.Equal(t, 2, pikey.Duration(pikey.A))
30+
})
31+
32+
pikey.Target().Publish(aUpEvent)
33+
34+
t.Run("should return 0 when key is up", func(t *testing.T) {
35+
assert.Equal(t, 0, pikey.Duration(pikey.A))
36+
})
37+
}

pikey/shortcut_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2025 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package pikey_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/elgopher/pi/pikey"
10+
"github.com/elgopher/pi/piloop"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
var (
16+
aDownEvent = pikey.Event{Type: pikey.EventDown, Key: pikey.A}
17+
aUpEvent = pikey.Event{Type: pikey.EventUp, Key: pikey.A}
18+
ctrlDownEvent = pikey.Event{Type: pikey.EventDown, Key: pikey.Ctrl}
19+
)
20+
21+
func TestRegisterShortcut(t *testing.T) {
22+
t.Run("single key", func(t *testing.T) {
23+
executionTimes := 0
24+
shortcut := pikey.RegisterShortcut(func() {
25+
executionTimes++
26+
}, pikey.A)
27+
require.NotNil(t, shortcut)
28+
29+
pikey.Target().Publish(aDownEvent)
30+
piloop.Target().Publish(piloop.EventLateUpdate)
31+
32+
t.Run("should execute callback when key is pressed", func(t *testing.T) {
33+
assert.Equal(t, 1, executionTimes)
34+
})
35+
36+
executionTimes = 0
37+
38+
piloop.Target().Publish(piloop.EventLateUpdate)
39+
40+
t.Run("should not execute callback again on next frame when key is still pressed", func(t *testing.T) {
41+
assert.Equal(t, 0, executionTimes)
42+
})
43+
44+
shortcut.Unregister()
45+
46+
pikey.Target().Publish(aDownEvent)
47+
piloop.Target().Publish(piloop.EventLateUpdate)
48+
49+
t.Run("should not execute callback after shortcut is unregistered", func(t *testing.T) {
50+
assert.Equal(t, 0, executionTimes)
51+
})
52+
})
53+
54+
t.Run("multiple keys", func(t *testing.T) {
55+
executionTimes := 0
56+
shortcut := pikey.RegisterShortcut(func() {
57+
executionTimes++
58+
}, pikey.A, pikey.Ctrl)
59+
require.NotNil(t, shortcut)
60+
// when
61+
pikey.Target().Publish(aDownEvent)
62+
pikey.Target().Publish(ctrlDownEvent)
63+
piloop.Target().Publish(piloop.EventLateUpdate)
64+
// then
65+
assert.Equal(t, 1, executionTimes)
66+
})
67+
68+
t.Run("should not run callback when all keys are not pressed simultaneously, but was down before", func(t *testing.T) {
69+
executionTimes := 0
70+
shortcut := pikey.RegisterShortcut(func() {
71+
executionTimes++
72+
}, pikey.A, pikey.Ctrl)
73+
require.NotNil(t, shortcut)
74+
75+
pikey.Target().Publish(aDownEvent)
76+
piloop.Target().Publish(piloop.EventLateUpdate)
77+
require.Equal(t, 0, executionTimes)
78+
// when
79+
pikey.Target().Publish(ctrlDownEvent)
80+
pikey.Target().Publish(aUpEvent) // "A" no longer down
81+
piloop.Target().Publish(piloop.EventLateUpdate)
82+
// then
83+
assert.Equal(t, 0, executionTimes)
84+
})
85+
}

0 commit comments

Comments
 (0)