mirror of
https://github.com/walles/moar.git
synced 2024-09-11 20:17:13 +03:00
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
package twin
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"gotest.tools/v3/assert"
|
|
)
|
|
|
|
func assertEncode(t *testing.T, incomingString string, expectedEvent Event, expectedRemainder string) {
|
|
actualEvent, actualRemainder := consumeEncodedEvent(incomingString)
|
|
|
|
message := strings.Replace(incomingString, "\x1b", "ESC", -1)
|
|
message = strings.Replace(message, "\r", "RET", -1)
|
|
|
|
assert.Assert(t, actualEvent != nil,
|
|
"Input: %s Result: %#v Expected: %#v", message, "nil", expectedEvent)
|
|
assert.Equal(t, *actualEvent, expectedEvent,
|
|
"Input: %s Result: %#v Expected: %#v", message, *actualEvent, expectedEvent)
|
|
assert.Equal(t, actualRemainder, expectedRemainder, message)
|
|
}
|
|
|
|
func TestConsumeEncodedEvent(t *testing.T) {
|
|
assertEncode(t, "a", EventRune{rune: 'a'}, "")
|
|
assertEncode(t, "\r", EventKeyCode{keyCode: KeyEnter}, "")
|
|
assertEncode(t, "\x1b", EventKeyCode{keyCode: KeyEscape}, "")
|
|
|
|
// Implicitly test having a remaining rune at the end
|
|
assertEncode(t, "\x1b[Ax", EventKeyCode{keyCode: KeyUp}, "x")
|
|
|
|
assertEncode(t, "\x1b[<64;127;41M", EventMouse{buttons: MouseWheelUp}, "")
|
|
assertEncode(t, "\x1b[<65;127;41M", EventMouse{buttons: MouseWheelDown}, "")
|
|
|
|
// This happens when users paste.
|
|
//
|
|
// Ref: https://github.com/walles/moar/issues/73
|
|
assertEncode(t, "1234", EventRune{rune: '1'}, "234")
|
|
}
|
|
|
|
func TestConsumeEncodedEventWithUnsupportedEscapeCode(t *testing.T) {
|
|
event, remainder := consumeEncodedEvent("\x1bXXXXX")
|
|
assert.Assert(t, event == nil)
|
|
assert.Equal(t, remainder, "")
|
|
}
|
|
|
|
func TestConsumeEncodedEventWithNoInput(t *testing.T) {
|
|
event, remainder := consumeEncodedEvent("")
|
|
assert.Assert(t, event == nil)
|
|
assert.Equal(t, remainder, "")
|
|
}
|