1
1
package keys_test
2
2
3
3
import (
4
+ "fmt"
4
5
"testing"
5
6
6
7
"github.com/mishamyrt/nuga-lib/features/keys"
7
8
"github.com/mishamyrt/nuga-lib/layout"
8
9
)
9
10
11
+ func ref [T any ](v T ) * T {
12
+ return & v
13
+ }
14
+
10
15
var testTemplate = layout.Template {
11
16
layout .KeyM : 0 ,
12
17
layout .KeyK : 1 ,
13
18
layout .KeyBrightnessUp : 2 ,
19
+ layout .KeyY : 3 ,
20
+ layout .KeyR : 4 ,
14
21
}
15
22
16
23
var testKeyMap = keys.KeyMap {
@@ -34,6 +41,13 @@ var testKeyMap = keys.KeyMap{
34
41
Name : layout .KeyBrightnessUp ,
35
42
},
36
43
},
44
+ layout .KeyY : {
45
+ Type : keys .ActionMacro ,
46
+ MacroIndex : ref (uint8 (1 )),
47
+ },
48
+ layout .KeyR : {
49
+ Type : keys .ActionNone ,
50
+ },
37
51
}
38
52
39
53
func TestApply (t * testing.T ) {
@@ -47,6 +61,8 @@ func TestApply(t *testing.T) {
47
61
)},
48
62
{layout .KeyK , layout .Keys [layout .KeyK ].Code },
49
63
{layout .KeyBrightnessUp , layout .Keys [layout .KeyBrightnessUp ].Code },
64
+ {layout .KeyY , keys .IndexToMacro (1 )},
65
+ {layout .KeyR , layout .Keys [layout .KeyNone ].Code },
50
66
}
51
67
source := make ([]uint32 , len (tests ))
52
68
err := testKeyMap .Apply (source , & testTemplate )
@@ -68,6 +84,8 @@ func TestParseKeyMap(t *testing.T) {
68
84
keys .ApplyModifiers (layout .Keys [layout .KeyM ].Code , & keys.Modifiers {Ctrl : true }),
69
85
layout .Keys [layout .KeyK ].Code ,
70
86
layout .Keys [layout .KeyBrightnessUp ].Code ,
87
+ keys .IndexToMacro (1 ),
88
+ layout .Keys [layout .KeyNone ].Code ,
71
89
}
72
90
keyMap , err := keys .ParseKeyMap (values , & testTemplate )
73
91
if err != nil {
@@ -77,29 +95,40 @@ func TestParseKeyMap(t *testing.T) {
77
95
if len (* keyMap ) != len (testKeyMap ) {
78
96
t .Errorf ("Expected keyMap length to be %d, got %d" , len (testKeyMap ), len (* keyMap ))
79
97
}
80
-
81
98
for k , v := range testKeyMap {
82
- if (* keyMap )[k ].Keystroke .Name != v .Keystroke .Name {
83
- t .Errorf ("Expected keyMap[%s].Name to be %#v, got %#v" , k , v .Keystroke .Name , (* keyMap )[k ].Keystroke .Name )
84
- }
85
- if v .Keystroke .Modifiers == nil || (* keyMap )[k ].Keystroke .Modifiers == nil {
86
- if v .Keystroke .Modifiers == nil && (* keyMap )[k ].Keystroke .Modifiers == nil {
87
- continue
99
+ switch v .Type {
100
+ case keys .ActionKeystroke :
101
+ if (* keyMap )[k ].Keystroke .Name != v .Keystroke .Name {
102
+ t .Errorf ("Expected keyMap[%s].Name to be %#v, got %#v" , k , v .Keystroke .Name , (* keyMap )[k ].Keystroke .Name )
103
+ }
104
+ if v .Keystroke .Modifiers == nil || (* keyMap )[k ].Keystroke .Modifiers == nil {
105
+ if v .Keystroke .Modifiers == nil && (* keyMap )[k ].Keystroke .Modifiers == nil {
106
+ continue
107
+ } else {
108
+ t .Errorf (
109
+ "Unexpected one modifier to be nil: %v, %v" ,
110
+ v .Keystroke .Modifiers ,
111
+ (* keyMap )[k ].Keystroke .Modifiers ,
112
+ )
113
+ }
114
+ }
115
+ if v .Keystroke .Modifiers == nil && (* keyMap )[k ].Keystroke .Modifiers != nil {
116
+ t .Errorf ("Expected keyMap[%s].Modifiers to be nil, got %#v" , k , (* keyMap )[k ].Keystroke .Modifiers )
88
117
} else {
89
- t . Errorf (
90
- "Unexpected one modifier to be nil: %v, %v" ,
91
- v . Keystroke . Modifiers ,
92
- ( * keyMap )[ k ]. Keystroke . Modifiers ,
93
- )
118
+ expectedModifiers := * v . Keystroke . Modifiers
119
+ gotModifiers := * (( * keyMap )[ k ]. Keystroke . Modifiers )
120
+ if expectedModifiers != gotModifiers {
121
+ t . Errorf ( "Expected keyMap[%s]. Modifiers to be %#v, got %#v" , k , expectedModifiers , gotModifiers )
122
+ }
94
123
}
95
- }
96
- if v . Keystroke . Modifiers == nil && (* keyMap )[k ].Keystroke . Modifiers != nil {
97
- t .Errorf ("Expected keyMap[%s].Modifiers to be nil , got %#v" , k , (* keyMap )[k ].Keystroke . Modifiers )
98
- } else {
99
- expectedModifiers := * v . Keystroke . Modifiers
100
- gotModifiers := * (( * keyMap )[k ].Keystroke . Modifiers )
101
- if expectedModifiers != gotModifiers {
102
- t .Errorf ("Expected keyMap[%s].Modifiers to be %#v , got %#v" , k , expectedModifiers , gotModifiers )
124
+ case keys . ActionMacro :
125
+ if ( * keyMap )[ k ]. MacroIndex == nil || * (* keyMap )[k ].MacroIndex != 1 {
126
+ t .Errorf ("Expected keyMap[%s].MacroIndex to be 1 , got %#v" , k , (* keyMap )[k ].MacroIndex )
127
+ }
128
+ case keys . ActionNone :
129
+ if ( * keyMap )[k ].Type != keys . ActionNone {
130
+ fmt . Println ( * ( * keyMap )[ k ]. Keystroke )
131
+ t .Errorf ("Expected keyMap[%s].Type to be ActionNone , got %#v" , k , ( * keyMap )[ k ]. Type )
103
132
}
104
133
}
105
134
0 commit comments