Skip to content

Commit 65a599b

Browse files
committed
fix: correctly set none action type
1 parent 6f8dd2f commit 65a599b

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

features/keys/keymap_test.go

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package keys_test
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/mishamyrt/nuga-lib/features/keys"
78
"github.com/mishamyrt/nuga-lib/layout"
89
)
910

11+
func ref[T any](v T) *T {
12+
return &v
13+
}
14+
1015
var testTemplate = layout.Template{
1116
layout.KeyM: 0,
1217
layout.KeyK: 1,
1318
layout.KeyBrightnessUp: 2,
19+
layout.KeyY: 3,
20+
layout.KeyR: 4,
1421
}
1522

1623
var testKeyMap = keys.KeyMap{
@@ -34,6 +41,13 @@ var testKeyMap = keys.KeyMap{
3441
Name: layout.KeyBrightnessUp,
3542
},
3643
},
44+
layout.KeyY: {
45+
Type: keys.ActionMacro,
46+
MacroIndex: ref(uint8(1)),
47+
},
48+
layout.KeyR: {
49+
Type: keys.ActionNone,
50+
},
3751
}
3852

3953
func TestApply(t *testing.T) {
@@ -47,6 +61,8 @@ func TestApply(t *testing.T) {
4761
)},
4862
{layout.KeyK, layout.Keys[layout.KeyK].Code},
4963
{layout.KeyBrightnessUp, layout.Keys[layout.KeyBrightnessUp].Code},
64+
{layout.KeyY, keys.IndexToMacro(1)},
65+
{layout.KeyR, layout.Keys[layout.KeyNone].Code},
5066
}
5167
source := make([]uint32, len(tests))
5268
err := testKeyMap.Apply(source, &testTemplate)
@@ -68,6 +84,8 @@ func TestParseKeyMap(t *testing.T) {
6884
keys.ApplyModifiers(layout.Keys[layout.KeyM].Code, &keys.Modifiers{Ctrl: true}),
6985
layout.Keys[layout.KeyK].Code,
7086
layout.Keys[layout.KeyBrightnessUp].Code,
87+
keys.IndexToMacro(1),
88+
layout.Keys[layout.KeyNone].Code,
7189
}
7290
keyMap, err := keys.ParseKeyMap(values, &testTemplate)
7391
if err != nil {
@@ -77,29 +95,40 @@ func TestParseKeyMap(t *testing.T) {
7795
if len(*keyMap) != len(testKeyMap) {
7896
t.Errorf("Expected keyMap length to be %d, got %d", len(testKeyMap), len(*keyMap))
7997
}
80-
8198
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)
88117
} 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+
}
94123
}
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)
103132
}
104133
}
105134

features/keys/keys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func IsMacroKey(code uint32) bool {
77

88
// IsRegularKey checks if code is regular
99
func IsRegularKey(code uint32) bool {
10-
return byte(code&0xFF) == 0x00
10+
return code != 0 && byte(code&0xFF) == 0x00
1111
}
1212

1313
// IndexToMacro converts index to macro

features/keys/keys_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestIsRegularKey(t *testing.T) {
5353
code uint32
5454
expected bool
5555
}{
56-
{"Empty", 0x0000, true},
56+
{"None", 0x0000, false},
5757
{"Regular large value", 0x9900, true},
5858
{"Empty not regular", 0x0001, false},
5959
{"Large not regular", 0x101b, false},

0 commit comments

Comments
 (0)