Add tests for RuleTargetAxis.

This commit is contained in:
Anna Rose Wiggins 2025-07-10 16:46:01 -04:00
parent 8f3b8f4b47
commit 6646044d28
8 changed files with 164 additions and 9 deletions

1
go.mod
View file

@ -12,5 +12,6 @@ require (
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

2
go.sum
View file

@ -6,6 +6,8 @@ github.com/holoplot/go-evdev v0.0.0-20240306072622-217e18f17db1 h1:92OsBIf5KB1Ta
github.com/holoplot/go-evdev v0.0.0-20240306072622-217e18f17db1/go.mod h1:iHAf8OIncO2gcQ8XOjS7CMJ2aPbX2Bs0wl5pZyanEqk= github.com/holoplot/go-evdev v0.0.0-20240306072622-217e18f17db1/go.mod h1:iHAf8OIncO2gcQ8XOjS7CMJ2aPbX2Bs0wl5pZyanEqk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o= golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=

View file

@ -26,3 +26,10 @@ type RuleTarget interface {
// for most implementations. // for most implementations.
CreateEvent(int32, *string) *evdev.InputEvent CreateEvent(int32, *string) *evdev.InputEvent
} }
// RuleTargetDevice is an interface abstraction on top of evdev.InputDevice, implementing
// only the methods we need in this package. This is used for testing, and the
// RuleTargetDevice can be safely cast to an *evdev.InputDevice when necessary.
type RuleTargetDevice interface {
AbsInfos() (map[evdev.EvCode]evdev.AbsInfo, error)
}

View file

@ -23,5 +23,6 @@ func (rule *MappingRuleAxis) MatchEvent(device *evdev.InputDevice, event *evdev.
return nil, nil return nil, nil
} }
return rule.Output.Device, rule.Output.CreateEvent(rule.Input.NormalizeValue(event.Value), mode) // The cast here is safe because the interface is only ever different for unit tests
return rule.Output.Device.(*evdev.InputDevice), rule.Output.CreateEvent(rule.Input.NormalizeValue(event.Value), mode)
} }

View file

@ -80,6 +80,6 @@ func (t *MappingRuleButtonTests) TestMatchEventInverted() {
t.EqualValues(expected, event) t.EqualValues(expected, event)
} }
func TestRunnerMatching(t *testing.T) { func TestRunnerMappingRuleButtonTests(t *testing.T) {
suite.Run(t, new(MappingRuleButtonTests)) suite.Run(t, new(MappingRuleButtonTests))
} }

View file

@ -13,3 +13,13 @@ func AbsInt[T constraints.Integer](value T) T {
func LerpInt[T constraints.Integer](min, max T, t float64) T { func LerpInt[T constraints.Integer](min, max T, t float64) T {
return T((1-t)*float64(min) + t*float64(max)) return T((1-t)*float64(min) + t*float64(max))
} }
func ClampInt[T constraints.Integer](value, min, max T) T {
if value < min {
value = min
}
if value > max {
value = max
}
return value
}

View file

@ -9,12 +9,11 @@ import (
type RuleTargetAxis struct { type RuleTargetAxis struct {
DeviceName string DeviceName string
Device *evdev.InputDevice Device RuleTargetDevice
Axis evdev.EvCode Axis evdev.EvCode
Inverted bool Inverted bool
DeadzoneStart int32 DeadzoneStart int32
DeadzoneEnd int32 DeadzoneEnd int32
Sensitivity float64 // TODO: is this even a value that makes sense?
axisSize int32 axisSize int32
deadzoneSize int32 deadzoneSize int32
} }
@ -25,7 +24,7 @@ const (
) )
func NewRuleTargetAxis(device_name string, func NewRuleTargetAxis(device_name string,
device *evdev.InputDevice, device RuleTargetDevice,
axis evdev.EvCode, axis evdev.EvCode,
inverted bool, inverted bool,
deadzoneStart int32, deadzoneStart int32,
@ -83,8 +82,7 @@ func (target *RuleTargetAxis) NormalizeValue(value int32) int32 {
} }
func (target *RuleTargetAxis) CreateEvent(value int32, mode *string) *evdev.InputEvent { func (target *RuleTargetAxis) CreateEvent(value int32, mode *string) *evdev.InputEvent {
// TODO: we can use the axis begin/end to decide whether to emit the event value = ClampInt(value, MinAxisValue, MaxAxisValue)
// TODO: oh no we need center deadzones actually...
return &evdev.InputEvent{ return &evdev.InputEvent{
Type: evdev.EV_ABS, Type: evdev.EV_ABS,
Code: target.Axis, Code: target.Axis,
@ -92,9 +90,9 @@ func (target *RuleTargetAxis) CreateEvent(value int32, mode *string) *evdev.Inpu
} }
} }
func (target *RuleTargetAxis) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent) bool { func (target *RuleTargetAxis) MatchEvent(device RuleTargetDevice, event *evdev.InputEvent) bool {
return device == target.Device && return device == target.Device &&
event.Type == evdev.EV_ABS && event.Type == evdev.EV_ABS &&
event.Code == target.Axis && event.Code == target.Axis &&
(event.Value <= target.DeadzoneStart || event.Value >= target.DeadzoneEnd) (event.Value < target.DeadzoneStart || event.Value > target.DeadzoneEnd)
} }

View file

@ -0,0 +1,136 @@
package mappingrules
import (
"testing"
"github.com/holoplot/go-evdev"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)
type RuleTargetAxisTests struct {
suite.Suite
mock *InputDeviceMock
call *mock.Call
}
type InputDeviceMock struct {
mock.Mock
}
func (m *InputDeviceMock) AbsInfos() (map[evdev.EvCode]evdev.AbsInfo, error) {
args := m.Called()
return args.Get(0).(map[evdev.EvCode]evdev.AbsInfo), args.Error(1)
}
func (t *RuleTargetAxisTests) SetupSuite() {
t.mock = new(InputDeviceMock)
t.call = t.mock.On("AbsInfos").Return(map[evdev.EvCode]evdev.AbsInfo{
evdev.ABS_X: {
Minimum: 0,
Maximum: 10000,
},
evdev.ABS_Y: {
Minimum: -10000,
Maximum: 10000,
},
}, nil)
}
func (t *RuleTargetAxisTests) TearDownSuite() {
t.call.Unset()
}
func (t *RuleTargetAxisTests) TestNewRuleTargetAxis() {
// RuleTargets should get created
ruleTarget, err := NewRuleTargetAxis("", t.mock, evdev.ABS_X, false, 0, 0)
t.Nil(err)
t.EqualValues(10000, ruleTarget.axisSize)
ruleTarget, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, 0, 0)
t.Nil(err)
t.EqualValues(20000, ruleTarget.axisSize)
// Creating a rule with a deadzone should work and reduce the axisSize
ruleTarget, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, -500, 500)
t.Nil(err)
t.EqualValues(19000, ruleTarget.axisSize)
t.EqualValues(-500, ruleTarget.DeadzoneStart)
t.EqualValues(500, ruleTarget.DeadzoneEnd)
// Creating a rule with a deadzone should fail if end > start
_, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, 500, -500)
t.NotNil(err)
// Creating a rule on a non-existent axis should err
_, err = NewRuleTargetAxis("", t.mock, evdev.ABS_Z, false, 0, 0)
t.NotNil(err)
}
func (t *RuleTargetAxisTests) TestNormalizeValue() {
// Basic normalization should work
ruleTarget, _ := NewRuleTargetAxis("", t.mock, evdev.ABS_X, false, 0, 0)
t.Equal(MaxAxisValue, ruleTarget.NormalizeValue(int32(10000)))
t.Equal(MinAxisValue, ruleTarget.NormalizeValue(int32(0)))
t.EqualValues(0, ruleTarget.NormalizeValue(int32(5000)))
// Normalization with a deadzone should work
ruleTarget, _ = NewRuleTargetAxis("", t.mock, evdev.ABS_X, false, 0, 5000)
t.Equal(MaxAxisValue, ruleTarget.NormalizeValue(int32(10000)))
t.True(ruleTarget.NormalizeValue(int32(5001)) < int32(-31000))
t.EqualValues(0, ruleTarget.NormalizeValue(int32(7500)))
// Normalization on an inverted axis should work
ruleTarget, _ = NewRuleTargetAxis("", t.mock, evdev.ABS_X, true, 0, 0)
t.Equal(MaxAxisValue, ruleTarget.NormalizeValue(int32(0)))
t.Equal(MinAxisValue, ruleTarget.NormalizeValue(int32(10000)))
}
func (t *RuleTargetAxisTests) TestMatchEvent() {
ruleTarget, _ := NewRuleTargetAxis("", t.mock, evdev.ABS_Y, false, -500, 500)
validEvent := &evdev.InputEvent{
Type: evdev.EV_ABS,
Code: evdev.ABS_Y,
Value: 800,
}
deadzoneEvent := &evdev.InputEvent{
Type: evdev.EV_ABS,
Code: evdev.ABS_Y,
Value: 200,
}
// An event on the correct device and axis should match
t.True(ruleTarget.MatchEvent(t.mock, validEvent))
// A value on the wrong device should not match
t.False(ruleTarget.MatchEvent(&evdev.InputDevice{}, validEvent))
// A value in the deadzone should not match
t.False(ruleTarget.MatchEvent(t.mock, deadzoneEvent))
}
func (t *RuleTargetAxisTests) TestCreateEvent() {
ruleTarget, _ := NewRuleTargetAxis("", t.mock, evdev.ABS_X, false, 0, 0)
expected := &evdev.InputEvent{
Type: evdev.EV_ABS,
Code: evdev.ABS_X,
}
// Basic event creation
testValue := int32(3928) // Arbitrarily chosen test value
expected.Value = testValue
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
// Validate axis clamping
testValue = int32(64000)
expected.Value = MaxAxisValue
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
testValue = int32(-64000)
expected.Value = MinAxisValue
t.EqualValues(expected, ruleTarget.CreateEvent(testValue, nil))
}
func TestRunnerRuleTargetAxisTests(t *testing.T) {
suite.Run(t, new(RuleTargetAxisTests))
}