...

Source file src/code.rocketnine.space/tslocum/cbind/key_test.go

Documentation: code.rocketnine.space/tslocum/cbind

     1  package cbind
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gdamore/tcell/v2"
     7  )
     8  
     9  type testCase struct {
    10  	mod     tcell.ModMask
    11  	key     tcell.Key
    12  	ch      rune
    13  	encoded string
    14  }
    15  
    16  var testCases = []testCase{
    17  	{mod: tcell.ModNone, key: tcell.KeyRune, ch: 'a', encoded: "a"},
    18  	{mod: tcell.ModNone, key: tcell.KeyRune, ch: '+', encoded: "+"},
    19  	{mod: tcell.ModNone, key: tcell.KeyRune, ch: ';', encoded: ";"},
    20  	{mod: tcell.ModNone, key: tcell.KeyTab, ch: rune(tcell.KeyTab), encoded: "Tab"},
    21  	{mod: tcell.ModNone, key: tcell.KeyEnter, ch: rune(tcell.KeyEnter), encoded: "Enter"},
    22  	{mod: tcell.ModNone, key: tcell.KeyPgDn, ch: 0, encoded: "PageDown"},
    23  	{mod: tcell.ModAlt, key: tcell.KeyRune, ch: 'a', encoded: "Alt+a"},
    24  	{mod: tcell.ModAlt, key: tcell.KeyRune, ch: '+', encoded: "Alt++"},
    25  	{mod: tcell.ModAlt, key: tcell.KeyRune, ch: ';', encoded: "Alt+;"},
    26  	{mod: tcell.ModAlt, key: tcell.KeyRune, ch: ' ', encoded: "Alt+Space"},
    27  	{mod: tcell.ModAlt, key: tcell.KeyRune, ch: '1', encoded: "Alt+1"},
    28  	{mod: tcell.ModAlt, key: tcell.KeyTab, ch: rune(tcell.KeyTab), encoded: "Alt+Tab"},
    29  	{mod: tcell.ModAlt, key: tcell.KeyEnter, ch: rune(tcell.KeyEnter), encoded: "Alt+Enter"},
    30  	{mod: tcell.ModAlt, key: tcell.KeyBackspace2, ch: rune(tcell.KeyBackspace2), encoded: "Alt+Backspace"},
    31  	{mod: tcell.ModCtrl, key: tcell.KeyCtrlC, ch: rune(tcell.KeyCtrlC), encoded: "Ctrl+C"},
    32  	{mod: tcell.ModCtrl, key: tcell.KeyCtrlD, ch: rune(tcell.KeyCtrlD), encoded: "Ctrl+D"},
    33  	{mod: tcell.ModCtrl, key: tcell.KeyCtrlSpace, ch: rune(tcell.KeyCtrlSpace), encoded: "Ctrl+Space"},
    34  	{mod: tcell.ModCtrl, key: tcell.KeyCtrlRightSq, ch: rune(tcell.KeyCtrlRightSq), encoded: "Ctrl+]"},
    35  	{mod: tcell.ModCtrl | tcell.ModAlt, key: tcell.KeyRune, ch: '+', encoded: "Ctrl+Alt++"},
    36  	{mod: tcell.ModCtrl | tcell.ModShift, key: tcell.KeyRune, ch: '+', encoded: "Ctrl+Shift++"},
    37  }
    38  
    39  func TestEncode(t *testing.T) {
    40  	t.Parallel()
    41  
    42  	for _, c := range testCases {
    43  		encoded, err := Encode(c.mod, c.key, c.ch)
    44  		if err != nil {
    45  			t.Errorf("failed to encode key %d %d %d: %s", c.mod, c.key, c.ch, err)
    46  		}
    47  		if encoded != c.encoded {
    48  			t.Errorf("failed to encode key %d %d %d: got %s, want %s", c.mod, c.key, c.ch, encoded, c.encoded)
    49  		}
    50  	}
    51  }
    52  
    53  func TestDecode(t *testing.T) {
    54  	t.Parallel()
    55  
    56  	for _, c := range testCases {
    57  		mod, key, ch, err := Decode(c.encoded)
    58  		if err != nil {
    59  			t.Errorf("failed to decode key %s: %s", c.encoded, err)
    60  		}
    61  		if mod != c.mod {
    62  			t.Errorf("failed to decode key %s: invalid modifiers: got %d, want %d", c.encoded, mod, c.mod)
    63  		}
    64  		if key != c.key {
    65  			t.Errorf("failed to decode key %s: invalid key: got %d, want %d", c.encoded, key, c.key)
    66  		}
    67  		if ch != c.ch {
    68  			t.Errorf("failed to decode key %s: invalid rune: got %d, want %d", c.encoded, ch, c.ch)
    69  		}
    70  	}
    71  }
    72  

View as plain text