1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Add more specs in the /wtf directory

This commit is contained in:
Chris Cummer 2019-08-04 08:37:52 -07:00
parent d3d6161c22
commit b0c175dd57
5 changed files with 447 additions and 28 deletions

View File

@ -47,6 +47,10 @@ func NewKeyboardWidget(app *tview.Application, pages *tview.Pages, settings *cfg
// widget.SetKeyboardChar("d", widget.deleteSelectedItem) // widget.SetKeyboardChar("d", widget.deleteSelectedItem)
// //
func (widget *KeyboardWidget) SetKeyboardChar(char string, fn func(), helpText string) { func (widget *KeyboardWidget) SetKeyboardChar(char string, fn func(), helpText string) {
if char == "" {
return
}
widget.charMap[char] = fn widget.charMap[char] = fn
widget.charHelp = append(widget.charHelp, helpItem{char, helpText}) 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) { func (widget *KeyboardWidget) SetKeyboardKey(key tcell.Key, fn func(), helpText string) {
widget.keyMap[key] = fn widget.keyMap[key] = fn
widget.keyHelp = append(widget.keyHelp, helpItem{tcell.KeyNames[key], helpText}) widget.keyHelp = append(widget.keyHelp, helpItem{tcell.KeyNames[key], helpText})
if len(tcell.KeyNames[key]) > widget.maxKey { if len(tcell.KeyNames[key]) > widget.maxKey {
widget.maxKey = len(tcell.KeyNames[key]) 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) // widget.View.SetInputCapture(widget.InputCapture)
// //
func (widget *KeyboardWidget) InputCapture(event *tcell.EventKey) *tcell.EventKey { func (widget *KeyboardWidget) InputCapture(event *tcell.EventKey) *tcell.EventKey {
if event == nil {
return nil
}
fn := widget.charMap[string(event.Rune())] fn := widget.charMap[string(event.Rune())]
if fn != nil { if fn != nil {
fn() fn()

159
wtf/keyboard_widget_test.go Normal file
View File

@ -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())
}
})
}
}

209
wtf/text_widget_test.go Normal file
View File

@ -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)
}
}

View File

@ -13,13 +13,17 @@ import (
"github.com/wtfutil/wtf/utils" "github.com/wtfutil/wtf/utils"
) )
const SimpleDateFormat = "Jan 2" const (
const SimpleTimeFormat = "15:04 MST" SimpleDateFormat = "Jan 2"
const MinimumTimeFormat = "15:04" SimpleTimeFormat = "15:04 MST"
const FullDateFormat = "Monday, Jan 2" MinimumTimeFormat = "15:04"
const FriendlyDateFormat = "Mon, Jan 2"
const FriendlyDateTimeFormat = "Mon, Jan 2, 15:04" FullDateFormat = "Monday, Jan 2"
const TimestampFormat = "2006-01-02T15:04:05-0700" FriendlyDateFormat = "Mon, Jan 2"
FriendlyDateTimeFormat = "Mon, Jan 2, 15:04"
TimestampFormat = "2006-01-02T15:04:05-0700"
)
var OpenFileUtil = "open" 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 // ExecuteCommand executes an external command on the local machine as the current user
func ExecuteCommand(cmd *exec.Cmd) string { func ExecuteCommand(cmd *exec.Cmd) string {
if cmd == nil {
return ""
}
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
return fmt.Sprintf("%v\n", err) return fmt.Sprintf("%v\n", err)

View File

@ -1,22 +1,53 @@
package wtf package wtf
import ( import (
"os/exec"
"testing" "testing"
. "github.com/stretchr/testify/assert" . "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", -9))
Equal(t, "cat", CenterText("cat", 0)) Equal(t, "cat", CenterText("cat", 0))
Equal(t, " cat ", CenterText("cat", 9)) 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 var result [][]string
expected := [][]string([][]string{[]string{"SSID: 7E5B5C", "7E5B5C"}}) expected := [][]string([][]string{[]string{"SSID: 7E5B5C", "7E5B5C"}})
@ -24,23 +55,17 @@ func TestFindMatch(t *testing.T) {
Equal(t, expected, result) Equal(t, expected, result)
} }
/* -------------------- Exclude() -------------------- */ func Test_ExcludeWhenTrue(t *testing.T) {
func TestExcludeWhenTrue(t *testing.T) {
Equal(t, true, Exclude([]string{"cat", "dog", "rat"}, "bat")) Equal(t, true, Exclude([]string{"cat", "dog", "rat"}, "bat"))
Equal(t, false, Exclude([]string{"cat", "dog", "rat"}, "dog")) Equal(t, false, Exclude([]string{"cat", "dog", "rat"}, "dog"))
} }
/* -------------------- NameFromEmail() -------------------- */ func Test_NameFromEmail(t *testing.T) {
func TestNameFromEmail(t *testing.T) {
Equal(t, "", NameFromEmail("")) Equal(t, "", NameFromEmail(""))
Equal(t, "Chris Cummer", NameFromEmail("chris.cummer@me.com")) Equal(t, "Chris Cummer", NameFromEmail("chris.cummer@me.com"))
} }
/* -------------------- NamesFromEmails() -------------------- */ func Test_NamesFromEmails(t *testing.T) {
func TestNamesFromEmails(t *testing.T) {
var result []string var result []string
result = NamesFromEmails([]string{}) result = NamesFromEmails([]string{})
@ -50,17 +75,28 @@ func TestNamesFromEmails(t *testing.T) {
Equal(t, []string{"Chris Cummer", "Chriscummer"}, result) Equal(t, []string{"Chris Cummer", "Chriscummer"}, result)
} }
/* -------------------- PadRow() -------------------- */ func Test_PadRow(t *testing.T) {
func TestPadRow(t *testing.T) {
Equal(t, "", PadRow(0, 0)) Equal(t, "", PadRow(0, 0))
Equal(t, "", PadRow(5, 2)) Equal(t, "", PadRow(5, 2))
Equal(t, " ", PadRow(1, 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} expected := []int{1, 2, 3}
source := make([]interface{}, len(expected)) source := make([]interface{}, len(expected))
@ -71,9 +107,7 @@ func TestToInts(t *testing.T) {
Equal(t, expected, ToInts(source)) Equal(t, expected, ToInts(source))
} }
/* -------------------- ToStrs() -------------------- */ func Test_ToStrs(t *testing.T) {
func TestToStrs(t *testing.T) {
expected := []string{"cat", "dog", "rat"} expected := []string{"cat", "dog", "rat"}
source := make([]interface{}, len(expected)) source := make([]interface{}, len(expected))