streamdeck-plugin/streamdesk_test.go

73 lines
1.3 KiB
Go
Raw Normal View History

2024-06-27 20:10:18 +09:30
package streamdeck
import (
"testing"
2024-06-28 17:51:57 +09:30
"github.com/tardisx/streamdeck-plugin/events"
2024-06-27 20:10:18 +09:30
)
type testLogger struct {
t *testing.T
}
func (tl testLogger) Info(s string, x ...any) { tl.t.Log(s, x) }
func (tl testLogger) Debug(s string, x ...any) { tl.t.Log(s, x) }
func (tl testLogger) Error(s string, x ...any) { tl.t.Log(s, x) }
func TestReflection(t *testing.T) {
c := NewWithLogger(testLogger{t: t})
// incoming
2024-06-28 17:51:57 +09:30
in := events.ERDidReceiveSettingsPayload{}
2024-06-27 20:10:18 +09:30
ranHandler := false
2024-06-28 17:51:57 +09:30
c.RegisterHandler(func(event events.ERDidReceiveSettingsPayload) {
2024-06-27 20:10:18 +09:30
ranHandler = true
})
c.handle(in)
if !ranHandler {
t.Error("did not run handler")
}
}
func TestUmmarshal(t *testing.T) {
b := []byte(`
{
"action": "com.elgato.example.action1",
"event": "keyUp",
"context": "ABC123",
"device": "DEF456",
"payload": {
"settings": {},
"coordinates": {
"column": 3,
"row": 1
},
"state": 0,
"userDesiredState": 1,
"isInMultiAction": false
}
}`)
c := NewWithLogger(testLogger{t: t})
2024-06-28 17:51:57 +09:30
e, _ := events.TypeForEvent("keyUp")
keyUp, err := c.unmarshalToConcrete(e, b)
2024-06-27 20:10:18 +09:30
if err != nil {
t.Error(err)
}
2024-06-28 17:51:57 +09:30
realKeyUp, ok := keyUp.(events.ERKeyUp)
2024-06-27 20:10:18 +09:30
if !ok {
t.Errorf("wrong type (is %T)", keyUp)
}
if realKeyUp.Context != "ABC123" {
t.Error("wrong value")
}
}