Files

41 lines
1.6 KiB
Go

package nvda2cthulhu
import (
"testing"
)
func TestParseRequestMatchesPythonCompatibilityProtocol(t *testing.T) {
tests := []struct {
name string
value any
want bridgeRequest
}{
{name: "text", value: "Game text", want: bridgeRequest{kind: "SpeakText", text: "Game text"}},
{name: "cancel string", value: "CancelSpeech", want: bridgeRequest{kind: "CancelSpeech"}},
{name: "speech map", value: map[any]any{"SpeakText": "Map text"}, want: bridgeRequest{kind: "SpeakText", text: "Map text"}},
{name: "braille map", value: map[string]any{"BrailleText": "Braille"}, want: bridgeRequest{kind: "BrailleText", text: "Braille"}},
{name: "cancel map", value: map[any]any{"CancelSpeech": nil}, want: bridgeRequest{kind: "CancelSpeech"}},
{name: "speech array", value: []any{"SpeakText", "Array text"}, want: bridgeRequest{kind: "SpeakText", text: "Array text"}},
{name: "cancel array", value: []any{"CancelSpeech"}, want: bridgeRequest{kind: "CancelSpeech"}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := parseRequest(test.value)
if err != nil {
t.Fatalf("parseRequest() error = %v", err)
}
if got.kind != test.want.kind || got.text != test.want.text {
t.Fatalf("parseRequest() = %+v, want %+v", got, test.want)
}
})
}
}
func TestParseRequestRejectsUnknownAndMalformedPayloads(t *testing.T) {
for _, value := range []any{"", map[any]any{}, map[any]any{"Two": "values", "SpeakText": "text"}, []any{}, []any{"Unknown", "value"}} {
if request, err := parseRequest(value); err == nil {
t.Fatalf("parseRequest(%#v) = %+v, want error", value, request)
}
}
}