From b0c175dd57dbfc7a6c0807b01e4a37bfd7f6ea4a Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sun, 4 Aug 2019 08:37:52 -0700 Subject: [PATCH] Add more specs in the /wtf directory --- wtf/keyboard_widget.go | 9 ++ wtf/keyboard_widget_test.go | 159 +++++++++++++++++++++++++++ wtf/text_widget_test.go | 209 ++++++++++++++++++++++++++++++++++++ wtf/utils.go | 22 ++-- wtf/utils_test.go | 76 +++++++++---- 5 files changed, 447 insertions(+), 28 deletions(-) create mode 100644 wtf/keyboard_widget_test.go create mode 100644 wtf/text_widget_test.go diff --git a/wtf/keyboard_widget.go b/wtf/keyboard_widget.go index 4ea06749..4e09e0ab 100644 --- a/wtf/keyboard_widget.go +++ b/wtf/keyboard_widget.go @@ -47,6 +47,10 @@ func NewKeyboardWidget(app *tview.Application, pages *tview.Pages, settings *cfg // widget.SetKeyboardChar("d", widget.deleteSelectedItem) // func (widget *KeyboardWidget) SetKeyboardChar(char string, fn func(), helpText string) { + if char == "" { + return + } + widget.charMap[char] = fn widget.charHelp = append(widget.charHelp, helpItem{char, helpText}) } @@ -59,6 +63,7 @@ func (widget *KeyboardWidget) SetKeyboardChar(char string, fn func(), helpText s func (widget *KeyboardWidget) SetKeyboardKey(key tcell.Key, fn func(), helpText string) { widget.keyMap[key] = fn widget.keyHelp = append(widget.keyHelp, helpItem{tcell.KeyNames[key], helpText}) + if len(tcell.KeyNames[key]) > widget.maxKey { widget.maxKey = len(tcell.KeyNames[key]) } @@ -70,6 +75,10 @@ func (widget *KeyboardWidget) SetKeyboardKey(key tcell.Key, fn func(), helpText // widget.View.SetInputCapture(widget.InputCapture) // func (widget *KeyboardWidget) InputCapture(event *tcell.EventKey) *tcell.EventKey { + if event == nil { + return nil + } + fn := widget.charMap[string(event.Rune())] if fn != nil { fn() diff --git a/wtf/keyboard_widget_test.go b/wtf/keyboard_widget_test.go new file mode 100644 index 00000000..1eade042 --- /dev/null +++ b/wtf/keyboard_widget_test.go @@ -0,0 +1,159 @@ +package wtf + +import ( + "testing" + + "github.com/gdamore/tcell" + "github.com/rivo/tview" +) + +func test() {} + +func testKeyboardWidget() KeyboardWidget { + keyWid := NewKeyboardWidget( + tview.NewApplication(), + tview.NewPages(), + nil, + ) + return keyWid +} + +func Test_SetKeyboardChar(t *testing.T) { + tests := []struct { + name string + char string + fn func() + helpText string + mapChar string + expected bool + }{ + { + name: "with blank char", + char: "", + fn: test, + helpText: "help", + mapChar: "", + expected: false, + }, + { + name: "with undefined char", + char: "d", + fn: test, + helpText: "help", + mapChar: "m", + expected: false, + }, + { + name: "with defined char", + char: "d", + fn: test, + helpText: "help", + mapChar: "d", + expected: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + keyWid := testKeyboardWidget() + keyWid.SetKeyboardChar(tt.char, tt.fn, tt.helpText) + + actual := keyWid.charMap[tt.mapChar] + + if tt.expected != (actual != nil) { + t.Errorf("\nexpected: %s\n got: %T", "actual != nil", actual) + } + }) + } +} + +func Test_SetKeyboardKey(t *testing.T) { + tests := []struct { + name string + key tcell.Key + fn func() + helpText string + mapKey tcell.Key + expected bool + }{ + { + name: "with undefined key", + key: tcell.KeyCtrlA, + fn: test, + helpText: "help", + mapKey: tcell.KeyCtrlZ, + expected: false, + }, + { + name: "with defined key", + key: tcell.KeyCtrlA, + fn: test, + helpText: "help", + mapKey: tcell.KeyCtrlA, + expected: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + keyWid := testKeyboardWidget() + keyWid.SetKeyboardKey(tt.key, tt.fn, tt.helpText) + + actual := keyWid.keyMap[tt.mapKey] + + if tt.expected != (actual != nil) { + t.Errorf("\nexpected: %s\n got: %T", "actual != nil", actual) + } + }) + } +} + +func Test_InputCapture(t *testing.T) { + tests := []struct { + name string + before func(keyWid KeyboardWidget) KeyboardWidget + event *tcell.EventKey + expected *tcell.EventKey + }{ + { + name: "with nil event", + before: func(keyWid KeyboardWidget) KeyboardWidget { return keyWid }, + event: nil, + expected: nil, + }, + { + name: "with undefined event", + before: func(keyWid KeyboardWidget) KeyboardWidget { return keyWid }, + event: tcell.NewEventKey(tcell.KeyRune, 'a', tcell.ModNone), + expected: tcell.NewEventKey(tcell.KeyRune, 'a', tcell.ModNone), + }, + { + name: "with defined event", + before: func(keyWid KeyboardWidget) KeyboardWidget { + keyWid.SetKeyboardChar("a", test, "help") + return keyWid + }, + event: tcell.NewEventKey(tcell.KeyRune, 'a', tcell.ModNone), + expected: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + keyWid := testKeyboardWidget() + keyWid = tt.before(keyWid) + actual := keyWid.InputCapture(tt.event) + + if tt.expected == nil { + if actual != nil { + t.Errorf("\nexpected: %v\n got: %v", tt.expected, actual.Rune()) + } + return + } + + if tt.expected.Rune() != actual.Rune() { + t.Errorf("\nexpected: %v\n got: %v", tt.expected.Rune(), actual.Rune()) + } + }) + } +} diff --git a/wtf/text_widget_test.go b/wtf/text_widget_test.go new file mode 100644 index 00000000..4e2baa64 --- /dev/null +++ b/wtf/text_widget_test.go @@ -0,0 +1,209 @@ +package wtf + +import ( + "testing" + + "github.com/rivo/tview" + "github.com/wtfutil/wtf/cfg" +) + +func testTextWidget() TextWidget { + txtWid := NewTextWidget( + tview.NewApplication(), + &cfg.Common{ + Module: cfg.Module{ + Name: "test widget", + }, + }, + true, + ) + return txtWid +} + +func Test_Bordered(t *testing.T) { + tests := []struct { + name string + before func(txtWid TextWidget) TextWidget + expected bool + }{ + { + name: "without border", + before: func(txtWid TextWidget) TextWidget { + txtWid.bordered = false + return txtWid + }, + expected: false, + }, + { + name: "with border", + before: func(txtWid TextWidget) TextWidget { + txtWid.bordered = true + return txtWid + }, + expected: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + txtWid := testTextWidget() + txtWid = tt.before(txtWid) + actual := txtWid.Bordered() + + if tt.expected != actual { + t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) + } + }) + } +} + +func Test_Disabled(t *testing.T) { + tests := []struct { + name string + before func(txtWid TextWidget) TextWidget + expected bool + }{ + { + name: "when not enabled", + before: func(txtWid TextWidget) TextWidget { + txtWid.enabled = false + return txtWid + }, + expected: true, + }, + { + name: "when enabled", + before: func(txtWid TextWidget) TextWidget { + txtWid.enabled = true + return txtWid + }, + expected: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + txtWid := testTextWidget() + txtWid = tt.before(txtWid) + actual := txtWid.Disabled() + + if tt.expected != actual { + t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) + } + }) + } +} + +func Test_Enabled(t *testing.T) { + tests := []struct { + name string + before func(txtWid TextWidget) TextWidget + expected bool + }{ + { + name: "when not enabled", + before: func(txtWid TextWidget) TextWidget { + txtWid.enabled = false + return txtWid + }, + expected: false, + }, + { + name: "when enabled", + before: func(txtWid TextWidget) TextWidget { + txtWid.enabled = true + return txtWid + }, + expected: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + txtWid := testTextWidget() + txtWid = tt.before(txtWid) + actual := txtWid.Enabled() + + if tt.expected != actual { + t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) + } + }) + } +} + +func Test_Focusable(t *testing.T) { + tests := []struct { + name string + before func(txtWid TextWidget) TextWidget + expected bool + }{ + { + name: "when not focusable", + before: func(txtWid TextWidget) TextWidget { + txtWid.enabled = false + txtWid.focusable = false + return txtWid + }, + expected: false, + }, + { + name: "when not focusable", + before: func(txtWid TextWidget) TextWidget { + txtWid.enabled = false + txtWid.focusable = true + return txtWid + }, + expected: false, + }, + { + name: "when not focusable", + before: func(txtWid TextWidget) TextWidget { + txtWid.enabled = true + txtWid.focusable = false + return txtWid + }, + expected: false, + }, + { + name: "when focusable", + before: func(txtWid TextWidget) TextWidget { + txtWid.enabled = true + txtWid.focusable = true + return txtWid + }, + expected: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + txtWid := testTextWidget() + txtWid = tt.before(txtWid) + actual := txtWid.Focusable() + + if tt.expected != actual { + t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) + } + }) + } +} + +func Test_Name(t *testing.T) { + txtWid := testTextWidget() + actual := txtWid.Name() + expected := "test widget" + + if expected != actual { + t.Errorf("\nexpected: %s\n got: %s", expected, actual) + } +} + +func Test_String(t *testing.T) { + txtWid := testTextWidget() + actual := txtWid.String() + expected := "test widget" + + if expected != actual { + t.Errorf("\nexpected: %s\n got: %s", expected, actual) + } +} diff --git a/wtf/utils.go b/wtf/utils.go index fa5ea96e..4c64da19 100644 --- a/wtf/utils.go +++ b/wtf/utils.go @@ -13,13 +13,17 @@ import ( "github.com/wtfutil/wtf/utils" ) -const SimpleDateFormat = "Jan 2" -const SimpleTimeFormat = "15:04 MST" -const MinimumTimeFormat = "15:04" -const FullDateFormat = "Monday, Jan 2" -const FriendlyDateFormat = "Mon, Jan 2" -const FriendlyDateTimeFormat = "Mon, Jan 2, 15:04" -const TimestampFormat = "2006-01-02T15:04:05-0700" +const ( + SimpleDateFormat = "Jan 2" + SimpleTimeFormat = "15:04 MST" + MinimumTimeFormat = "15:04" + + FullDateFormat = "Monday, Jan 2" + FriendlyDateFormat = "Mon, Jan 2" + FriendlyDateTimeFormat = "Mon, Jan 2, 15:04" + + TimestampFormat = "2006-01-02T15:04:05-0700" +) var OpenFileUtil = "open" @@ -46,6 +50,10 @@ func CenterText(str string, width int) string { // ExecuteCommand executes an external command on the local machine as the current user func ExecuteCommand(cmd *exec.Cmd) string { + if cmd == nil { + return "" + } + stdout, err := cmd.StdoutPipe() if err != nil { return fmt.Sprintf("%v\n", err) diff --git a/wtf/utils_test.go b/wtf/utils_test.go index 22a8180f..3e5b2aed 100644 --- a/wtf/utils_test.go +++ b/wtf/utils_test.go @@ -1,22 +1,53 @@ package wtf import ( + "os/exec" "testing" . "github.com/stretchr/testify/assert" ) -/* -------------------- CenterText() -------------------- */ +func Test_Init(t *testing.T) { + Init("cats") + Equal(t, OpenFileUtil, "cats") +} -func TestCenterText(t *testing.T) { +func Test_CenterText(t *testing.T) { Equal(t, "cat", CenterText("cat", -9)) Equal(t, "cat", CenterText("cat", 0)) Equal(t, " cat ", CenterText("cat", 9)) } -/* -------------------- FindMatch() -------------------- */ +func Test_ExecuteCommand(t *testing.T) { + tests := []struct { + name string + cmd *exec.Cmd + expected string + }{ + { + name: "with nil command", + cmd: nil, + expected: "", + }, + { + name: "with defined command", + cmd: exec.Command("echo", "cats"), + expected: "cats\n", + }, + } -func TestFindMatch(t *testing.T) { + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual := ExecuteCommand(tt.cmd) + + if tt.expected != actual { + t.Errorf("\nexpected: %s\n got: %s", tt.expected, actual) + } + }) + } +} + +func Test_FindMatch(t *testing.T) { var result [][]string expected := [][]string([][]string{[]string{"SSID: 7E5B5C", "7E5B5C"}}) @@ -24,23 +55,17 @@ func TestFindMatch(t *testing.T) { Equal(t, expected, result) } -/* -------------------- Exclude() -------------------- */ - -func TestExcludeWhenTrue(t *testing.T) { +func Test_ExcludeWhenTrue(t *testing.T) { Equal(t, true, Exclude([]string{"cat", "dog", "rat"}, "bat")) Equal(t, false, Exclude([]string{"cat", "dog", "rat"}, "dog")) } -/* -------------------- NameFromEmail() -------------------- */ - -func TestNameFromEmail(t *testing.T) { +func Test_NameFromEmail(t *testing.T) { Equal(t, "", NameFromEmail("")) Equal(t, "Chris Cummer", NameFromEmail("chris.cummer@me.com")) } -/* -------------------- NamesFromEmails() -------------------- */ - -func TestNamesFromEmails(t *testing.T) { +func Test_NamesFromEmails(t *testing.T) { var result []string result = NamesFromEmails([]string{}) @@ -50,17 +75,28 @@ func TestNamesFromEmails(t *testing.T) { Equal(t, []string{"Chris Cummer", "Chriscummer"}, result) } -/* -------------------- PadRow() -------------------- */ - -func TestPadRow(t *testing.T) { +func Test_PadRow(t *testing.T) { Equal(t, "", PadRow(0, 0)) Equal(t, "", PadRow(5, 2)) Equal(t, " ", PadRow(1, 2)) } -/* -------------------- ToInts() -------------------- */ +func Test_MapToStrs(t *testing.T) { + expected := map[string]string{ + "a": "a", + "b": "b", + "c": "c", + } -func TestToInts(t *testing.T) { + source := make(map[string]interface{}) + for _, val := range expected { + source[val] = val + } + + Equal(t, expected, MapToStrs(source)) +} + +func Test_ToInts(t *testing.T) { expected := []int{1, 2, 3} source := make([]interface{}, len(expected)) @@ -71,9 +107,7 @@ func TestToInts(t *testing.T) { Equal(t, expected, ToInts(source)) } -/* -------------------- ToStrs() -------------------- */ - -func TestToStrs(t *testing.T) { +func Test_ToStrs(t *testing.T) { expected := []string{"cat", "dog", "rat"} source := make([]interface{}, len(expected))