2021-02-17 15:23:08 +08:00
|
|
|
package anko
|
2021-02-15 20:28:06 +08:00
|
|
|
|
|
|
|
import (
|
2021-02-17 17:08:39 +08:00
|
|
|
"fmt"
|
2021-02-15 20:28:06 +08:00
|
|
|
"testing"
|
|
|
|
|
2021-02-17 17:08:39 +08:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2021-02-15 20:28:06 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-02-15 21:46:33 +08:00
|
|
|
func TestDefine(t *testing.T) {
|
2021-02-17 15:23:08 +08:00
|
|
|
a := NewAnko()
|
2021-03-21 16:05:23 +08:00
|
|
|
err := a.DefineGlobal("x", 12)
|
2021-02-15 21:46:33 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSet(t *testing.T) {
|
2021-02-17 15:23:08 +08:00
|
|
|
a := NewAnko()
|
2021-03-21 16:05:23 +08:00
|
|
|
err := a.DefineGlobal("x", 12)
|
2021-02-15 21:46:33 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = a.Set("x", 12)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
2021-02-17 15:23:08 +08:00
|
|
|
a := NewAnko()
|
2021-02-15 21:46:33 +08:00
|
|
|
|
|
|
|
expect := 12
|
2021-03-21 16:05:23 +08:00
|
|
|
err := a.DefineGlobal("x", expect)
|
2021-02-15 21:46:33 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := a.Get("x")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expect, got)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetInt(t *testing.T) {
|
|
|
|
expect := 10
|
2021-02-17 15:23:08 +08:00
|
|
|
a := NewAnko()
|
2021-02-15 21:46:33 +08:00
|
|
|
|
|
|
|
_, err := a.Execute(`x = 10`)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := a.GetInt("x")
|
|
|
|
assert.Equal(t, expect, got)
|
|
|
|
|
2021-02-18 10:20:37 +08:00
|
|
|
_, err = a.Execute(`module S { x = 10 }`)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got = a.GetInt("S.x")
|
|
|
|
assert.Equal(t, expect, got)
|
|
|
|
|
|
|
|
got = a.GetInt("S.y")
|
|
|
|
assert.Equal(t, 0, got)
|
|
|
|
|
2021-03-21 16:05:23 +08:00
|
|
|
a.DefineGlobal("z", expect)
|
2021-02-15 21:46:33 +08:00
|
|
|
val := a.GetInt("z")
|
|
|
|
|
|
|
|
assert.Equal(t, expect, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetString(t *testing.T) {
|
|
|
|
expect := "bruhh"
|
2021-02-17 15:23:08 +08:00
|
|
|
a := NewAnko()
|
2021-02-15 21:46:33 +08:00
|
|
|
|
|
|
|
_, err := a.Execute(`x = "bruhh"`)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2021-02-15 20:28:06 +08:00
|
|
|
|
2021-02-15 21:46:33 +08:00
|
|
|
got := a.GetString("x")
|
|
|
|
assert.Equal(t, expect, got)
|
2021-02-15 20:28:06 +08:00
|
|
|
|
2021-02-18 10:20:37 +08:00
|
|
|
_, err = a.Execute(`module S { x = "bruhh" }`)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got = a.GetString("S.x")
|
|
|
|
assert.Equal(t, expect, got)
|
|
|
|
|
|
|
|
got = a.GetString("S.y")
|
|
|
|
assert.Equal(t, "", got)
|
|
|
|
|
2021-03-21 16:05:23 +08:00
|
|
|
a.DefineGlobal("z", expect)
|
2021-02-15 21:46:33 +08:00
|
|
|
val := a.GetString("z")
|
|
|
|
|
|
|
|
assert.Equal(t, expect, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBool(t *testing.T) {
|
2021-02-15 20:28:06 +08:00
|
|
|
expect := true
|
2021-02-17 15:23:08 +08:00
|
|
|
a := NewAnko()
|
2021-03-21 16:05:23 +08:00
|
|
|
a.DefineGlobal("x", expect)
|
2021-02-15 20:28:06 +08:00
|
|
|
|
2021-02-18 10:20:37 +08:00
|
|
|
_, err := a.Execute(`module S { x = true }`)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := a.GetBool("S.x")
|
|
|
|
assert.Equal(t, expect, got)
|
|
|
|
|
|
|
|
got = a.GetBool("S.y")
|
|
|
|
assert.Equal(t, false, got)
|
|
|
|
|
2021-02-15 20:28:06 +08:00
|
|
|
result := a.GetBool("x")
|
|
|
|
assert.Equal(t, expect, result)
|
|
|
|
}
|
2021-02-15 21:46:33 +08:00
|
|
|
|
|
|
|
func TestExecute(t *testing.T) {
|
|
|
|
expect := 12
|
2021-02-17 15:23:08 +08:00
|
|
|
a := NewAnko()
|
2021-02-15 21:46:33 +08:00
|
|
|
|
|
|
|
_, err := a.Execute(`x = 6 + 6`)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := a.GetInt("x")
|
|
|
|
assert.Equal(t, expect, got)
|
|
|
|
}
|
2021-02-16 11:06:41 +08:00
|
|
|
|
2021-02-17 15:23:08 +08:00
|
|
|
func TestExtractCtrlRune(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in string
|
|
|
|
out rune
|
Fix panic on Ctrl+Arrow
I often hit fn+arrow up / down to page up / down. If I accidentally
hit ctrl gomu panics.
Investigated it today and the issue is the rune extraction -- it assumes
we'll always have Ctrl+<rune> but in the case of an arrow key we have
Ctrl+Up or Ctrl+Down.
Essentially this simply checks if the regex found any matches, if not we
return not result akin to a failed map lookup. I considered raising an
error, but that would be just as annoying as a panic when I fat finger
Ctrl :)
```golang
panic: runtime error: index out of range [0] with length 0 [recovered]
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1()
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:243 +0x4d
panic({0xadb840, 0xc00076a000})
/usr/local/go/src/runtime/panic.go:1038 +0x215
github.com/issadarkthing/gomu/anko.extractCtrlRune({0xc00003cc90, 0x9})
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:216 +0x75
github.com/issadarkthing/gomu/anko.(*Anko).KeybindExists(0xb23243, {0xb2591b, 0x6}, 0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:158 +0x78
main.start.func4(0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:516 +0x14b
github.com/rivo/tview.(*Application).Run(0xc00023e2a0)
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:318 +0x6d2
main.start(0xc00023e2a0, {0xc00028e840, 0xc00003d4b6, 0xc00028e850, 0xc00003d4b7})
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:546 +0xceb
main.main()
/home/augustus/go/src/github.com/issadarkthing/gomu/main.go:21 +0x10e
```
2022-07-02 17:07:55 +01:00
|
|
|
ok bool
|
2021-02-17 15:23:08 +08:00
|
|
|
}{
|
Fix panic on Ctrl+Arrow
I often hit fn+arrow up / down to page up / down. If I accidentally
hit ctrl gomu panics.
Investigated it today and the issue is the rune extraction -- it assumes
we'll always have Ctrl+<rune> but in the case of an arrow key we have
Ctrl+Up or Ctrl+Down.
Essentially this simply checks if the regex found any matches, if not we
return not result akin to a failed map lookup. I considered raising an
error, but that would be just as annoying as a panic when I fat finger
Ctrl :)
```golang
panic: runtime error: index out of range [0] with length 0 [recovered]
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1()
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:243 +0x4d
panic({0xadb840, 0xc00076a000})
/usr/local/go/src/runtime/panic.go:1038 +0x215
github.com/issadarkthing/gomu/anko.extractCtrlRune({0xc00003cc90, 0x9})
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:216 +0x75
github.com/issadarkthing/gomu/anko.(*Anko).KeybindExists(0xb23243, {0xb2591b, 0x6}, 0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:158 +0x78
main.start.func4(0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:516 +0x14b
github.com/rivo/tview.(*Application).Run(0xc00023e2a0)
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:318 +0x6d2
main.start(0xc00023e2a0, {0xc00028e840, 0xc00003d4b6, 0xc00028e850, 0xc00003d4b7})
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:546 +0xceb
main.main()
/home/augustus/go/src/github.com/issadarkthing/gomu/main.go:21 +0x10e
```
2022-07-02 17:07:55 +01:00
|
|
|
{in: "Ctrl+x", out: 'x', ok: true},
|
|
|
|
{in: "Ctrl+]", out: ']', ok: true},
|
|
|
|
{in: "Ctrl+%", out: '%', ok: true},
|
|
|
|
{in: "Ctrl+^", out: '^', ok: true},
|
|
|
|
{in: "Ctrl+7", out: '7', ok: true},
|
|
|
|
{in: "Ctrl+B", out: 'B', ok: true},
|
|
|
|
{in: "Ctrl+Down", out: ' ', ok: false},
|
|
|
|
{in: "Ctrl+Left", out: ' ', ok: false},
|
2021-02-16 11:06:41 +08:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:23:08 +08:00
|
|
|
for _, test := range tests {
|
Fix panic on Ctrl+Arrow
I often hit fn+arrow up / down to page up / down. If I accidentally
hit ctrl gomu panics.
Investigated it today and the issue is the rune extraction -- it assumes
we'll always have Ctrl+<rune> but in the case of an arrow key we have
Ctrl+Up or Ctrl+Down.
Essentially this simply checks if the regex found any matches, if not we
return not result akin to a failed map lookup. I considered raising an
error, but that would be just as annoying as a panic when I fat finger
Ctrl :)
```golang
panic: runtime error: index out of range [0] with length 0 [recovered]
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1()
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:243 +0x4d
panic({0xadb840, 0xc00076a000})
/usr/local/go/src/runtime/panic.go:1038 +0x215
github.com/issadarkthing/gomu/anko.extractCtrlRune({0xc00003cc90, 0x9})
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:216 +0x75
github.com/issadarkthing/gomu/anko.(*Anko).KeybindExists(0xb23243, {0xb2591b, 0x6}, 0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:158 +0x78
main.start.func4(0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:516 +0x14b
github.com/rivo/tview.(*Application).Run(0xc00023e2a0)
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:318 +0x6d2
main.start(0xc00023e2a0, {0xc00028e840, 0xc00003d4b6, 0xc00028e850, 0xc00003d4b7})
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:546 +0xceb
main.main()
/home/augustus/go/src/github.com/issadarkthing/gomu/main.go:21 +0x10e
```
2022-07-02 17:07:55 +01:00
|
|
|
got, ok := extractCtrlRune(test.in)
|
2021-02-17 15:23:08 +08:00
|
|
|
assert.Equal(t, test.out, got)
|
Fix panic on Ctrl+Arrow
I often hit fn+arrow up / down to page up / down. If I accidentally
hit ctrl gomu panics.
Investigated it today and the issue is the rune extraction -- it assumes
we'll always have Ctrl+<rune> but in the case of an arrow key we have
Ctrl+Up or Ctrl+Down.
Essentially this simply checks if the regex found any matches, if not we
return not result akin to a failed map lookup. I considered raising an
error, but that would be just as annoying as a panic when I fat finger
Ctrl :)
```golang
panic: runtime error: index out of range [0] with length 0 [recovered]
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1()
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:243 +0x4d
panic({0xadb840, 0xc00076a000})
/usr/local/go/src/runtime/panic.go:1038 +0x215
github.com/issadarkthing/gomu/anko.extractCtrlRune({0xc00003cc90, 0x9})
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:216 +0x75
github.com/issadarkthing/gomu/anko.(*Anko).KeybindExists(0xb23243, {0xb2591b, 0x6}, 0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:158 +0x78
main.start.func4(0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:516 +0x14b
github.com/rivo/tview.(*Application).Run(0xc00023e2a0)
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:318 +0x6d2
main.start(0xc00023e2a0, {0xc00028e840, 0xc00003d4b6, 0xc00028e850, 0xc00003d4b7})
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:546 +0xceb
main.main()
/home/augustus/go/src/github.com/issadarkthing/gomu/main.go:21 +0x10e
```
2022-07-02 17:07:55 +01:00
|
|
|
assert.Equal(t, test.ok, ok)
|
2021-02-16 11:06:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:23:08 +08:00
|
|
|
func TestExtractAltRune(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in string
|
|
|
|
out rune
|
Fix panic on Ctrl+Arrow
I often hit fn+arrow up / down to page up / down. If I accidentally
hit ctrl gomu panics.
Investigated it today and the issue is the rune extraction -- it assumes
we'll always have Ctrl+<rune> but in the case of an arrow key we have
Ctrl+Up or Ctrl+Down.
Essentially this simply checks if the regex found any matches, if not we
return not result akin to a failed map lookup. I considered raising an
error, but that would be just as annoying as a panic when I fat finger
Ctrl :)
```golang
panic: runtime error: index out of range [0] with length 0 [recovered]
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1()
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:243 +0x4d
panic({0xadb840, 0xc00076a000})
/usr/local/go/src/runtime/panic.go:1038 +0x215
github.com/issadarkthing/gomu/anko.extractCtrlRune({0xc00003cc90, 0x9})
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:216 +0x75
github.com/issadarkthing/gomu/anko.(*Anko).KeybindExists(0xb23243, {0xb2591b, 0x6}, 0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:158 +0x78
main.start.func4(0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:516 +0x14b
github.com/rivo/tview.(*Application).Run(0xc00023e2a0)
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:318 +0x6d2
main.start(0xc00023e2a0, {0xc00028e840, 0xc00003d4b6, 0xc00028e850, 0xc00003d4b7})
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:546 +0xceb
main.main()
/home/augustus/go/src/github.com/issadarkthing/gomu/main.go:21 +0x10e
```
2022-07-02 17:07:55 +01:00
|
|
|
ok bool
|
2021-02-17 15:23:08 +08:00
|
|
|
}{
|
Fix panic on Ctrl+Arrow
I often hit fn+arrow up / down to page up / down. If I accidentally
hit ctrl gomu panics.
Investigated it today and the issue is the rune extraction -- it assumes
we'll always have Ctrl+<rune> but in the case of an arrow key we have
Ctrl+Up or Ctrl+Down.
Essentially this simply checks if the regex found any matches, if not we
return not result akin to a failed map lookup. I considered raising an
error, but that would be just as annoying as a panic when I fat finger
Ctrl :)
```golang
panic: runtime error: index out of range [0] with length 0 [recovered]
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1()
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:243 +0x4d
panic({0xadb840, 0xc00076a000})
/usr/local/go/src/runtime/panic.go:1038 +0x215
github.com/issadarkthing/gomu/anko.extractCtrlRune({0xc00003cc90, 0x9})
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:216 +0x75
github.com/issadarkthing/gomu/anko.(*Anko).KeybindExists(0xb23243, {0xb2591b, 0x6}, 0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:158 +0x78
main.start.func4(0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:516 +0x14b
github.com/rivo/tview.(*Application).Run(0xc00023e2a0)
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:318 +0x6d2
main.start(0xc00023e2a0, {0xc00028e840, 0xc00003d4b6, 0xc00028e850, 0xc00003d4b7})
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:546 +0xceb
main.main()
/home/augustus/go/src/github.com/issadarkthing/gomu/main.go:21 +0x10e
```
2022-07-02 17:07:55 +01:00
|
|
|
{in: "Alt+Rune[x]", out: 'x', ok: true},
|
|
|
|
{in: "Alt+Rune[]]", out: ']', ok: true},
|
|
|
|
{in: "Alt+Rune[%]", out: '%', ok: true},
|
|
|
|
{in: "Alt+Rune[^]", out: '^', ok: true},
|
|
|
|
{in: "Alt+Rune[7]", out: '7', ok: true},
|
|
|
|
{in: "Alt+Rune[B]", out: 'B', ok: true},
|
2021-02-16 11:06:41 +08:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:23:08 +08:00
|
|
|
for _, test := range tests {
|
Fix panic on Ctrl+Arrow
I often hit fn+arrow up / down to page up / down. If I accidentally
hit ctrl gomu panics.
Investigated it today and the issue is the rune extraction -- it assumes
we'll always have Ctrl+<rune> but in the case of an arrow key we have
Ctrl+Up or Ctrl+Down.
Essentially this simply checks if the regex found any matches, if not we
return not result akin to a failed map lookup. I considered raising an
error, but that would be just as annoying as a panic when I fat finger
Ctrl :)
```golang
panic: runtime error: index out of range [0] with length 0 [recovered]
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1()
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:243 +0x4d
panic({0xadb840, 0xc00076a000})
/usr/local/go/src/runtime/panic.go:1038 +0x215
github.com/issadarkthing/gomu/anko.extractCtrlRune({0xc00003cc90, 0x9})
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:216 +0x75
github.com/issadarkthing/gomu/anko.(*Anko).KeybindExists(0xb23243, {0xb2591b, 0x6}, 0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:158 +0x78
main.start.func4(0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:516 +0x14b
github.com/rivo/tview.(*Application).Run(0xc00023e2a0)
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:318 +0x6d2
main.start(0xc00023e2a0, {0xc00028e840, 0xc00003d4b6, 0xc00028e850, 0xc00003d4b7})
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:546 +0xceb
main.main()
/home/augustus/go/src/github.com/issadarkthing/gomu/main.go:21 +0x10e
```
2022-07-02 17:07:55 +01:00
|
|
|
got, ok := extractAltRune(test.in)
|
2021-02-17 15:23:08 +08:00
|
|
|
assert.Equal(t, test.out, got)
|
Fix panic on Ctrl+Arrow
I often hit fn+arrow up / down to page up / down. If I accidentally
hit ctrl gomu panics.
Investigated it today and the issue is the rune extraction -- it assumes
we'll always have Ctrl+<rune> but in the case of an arrow key we have
Ctrl+Up or Ctrl+Down.
Essentially this simply checks if the regex found any matches, if not we
return not result akin to a failed map lookup. I considered raising an
error, but that would be just as annoying as a panic when I fat finger
Ctrl :)
```golang
panic: runtime error: index out of range [0] with length 0 [recovered]
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1()
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:243 +0x4d
panic({0xadb840, 0xc00076a000})
/usr/local/go/src/runtime/panic.go:1038 +0x215
github.com/issadarkthing/gomu/anko.extractCtrlRune({0xc00003cc90, 0x9})
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:216 +0x75
github.com/issadarkthing/gomu/anko.(*Anko).KeybindExists(0xb23243, {0xb2591b, 0x6}, 0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/anko/anko.go:158 +0x78
main.start.func4(0xc00007e040)
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:516 +0x14b
github.com/rivo/tview.(*Application).Run(0xc00023e2a0)
/home/augustus/go/pkg/mod/github.com/rivo/tview@v0.0.0-20210312174852-ae9464cc3598/application.go:318 +0x6d2
main.start(0xc00023e2a0, {0xc00028e840, 0xc00003d4b6, 0xc00028e850, 0xc00003d4b7})
/home/augustus/go/src/github.com/issadarkthing/gomu/start.go:546 +0xceb
main.main()
/home/augustus/go/src/github.com/issadarkthing/gomu/main.go:21 +0x10e
```
2022-07-02 17:07:55 +01:00
|
|
|
assert.Equal(t, test.ok, ok)
|
2021-02-16 11:06:41 +08:00
|
|
|
}
|
2021-02-17 15:23:08 +08:00
|
|
|
}
|
2021-02-16 11:06:41 +08:00
|
|
|
|
2021-02-17 15:23:08 +08:00
|
|
|
func TestKeybindExists(t *testing.T) {
|
2021-02-16 11:06:41 +08:00
|
|
|
|
2021-02-17 17:08:39 +08:00
|
|
|
tests := []struct {
|
|
|
|
panel string
|
|
|
|
key *tcell.EventKey
|
|
|
|
exists bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
panel: "global",
|
|
|
|
key: tcell.NewEventKey(tcell.KeyRune, 'b', tcell.ModNone),
|
|
|
|
exists: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
panel: "global",
|
|
|
|
key: tcell.NewEventKey(tcell.KeyRune, 'x', tcell.ModNone),
|
|
|
|
exists: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
panel: "global",
|
|
|
|
key: tcell.NewEventKey(tcell.KeyRune, ']', tcell.ModNone),
|
|
|
|
exists: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
panel: "global",
|
|
|
|
key: tcell.NewEventKey(tcell.KeyRune, '[', tcell.ModNone),
|
|
|
|
exists: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
panel: "global",
|
|
|
|
key: tcell.NewEventKey(tcell.KeyCtrlB, 'b', tcell.ModCtrl),
|
|
|
|
exists: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
panel: "global",
|
|
|
|
key: tcell.NewEventKey(tcell.KeyCtrlC, 'c', tcell.ModCtrl),
|
|
|
|
exists: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
panel: "playlist",
|
|
|
|
key: tcell.NewEventKey(tcell.KeyRune, '!', tcell.ModAlt),
|
|
|
|
exists: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
panel: "playlist",
|
|
|
|
key: tcell.NewEventKey(tcell.KeyRune, '>', tcell.ModAlt),
|
|
|
|
exists: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
panel: "playlist",
|
|
|
|
key: tcell.NewEventKey(tcell.KeyCtrlCarat, '^', tcell.ModCtrl),
|
|
|
|
exists: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
panel: "queue",
|
|
|
|
key: tcell.NewEventKey(tcell.KeyRune, '>', tcell.ModAlt),
|
|
|
|
exists: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
panel: "queue",
|
|
|
|
key: tcell.NewEventKey(tcell.KeyEnter, 0, tcell.ModNone),
|
|
|
|
exists: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
src := `
|
|
|
|
module Keybinds {
|
|
|
|
global = {}
|
|
|
|
playlist = {}
|
|
|
|
queue = {}
|
|
|
|
|
|
|
|
global["b"] = func() { return 0 }
|
|
|
|
global["]"] = func() { return 0 }
|
|
|
|
global["ctrl_b"] = func() { return 0 }
|
|
|
|
global["alt_b"] = func() { return 0 }
|
|
|
|
|
|
|
|
playlist["alt_!"] = func() { return 0 }
|
|
|
|
playlist["ctrl_^"] = func() { return 0 }
|
|
|
|
|
|
|
|
queue["alt_>"] = func() { return 0 }
|
|
|
|
queue["enter"] = func() { return 0 }
|
|
|
|
}
|
|
|
|
`
|
|
|
|
a := NewAnko()
|
|
|
|
|
|
|
|
_, err := a.Execute(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
got := a.KeybindExists(test.panel, test.key)
|
|
|
|
msg := fmt.Sprintf("error on test %d", i+1)
|
|
|
|
assert.Equal(t, test.exists, got, msg)
|
|
|
|
}
|
2021-02-16 11:06:41 +08:00
|
|
|
}
|