From 8bb91155389ee733d29f89653c32ed415b9b2221 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Fri, 18 May 2018 11:54:39 -0700 Subject: [PATCH] A very basic start to adding some unit tests --- wtf/config_watcher.rb | 13 ----- wtf/position.go | 11 ++++ wtf/text_widget.go | 13 ++--- wtf_tests/position_test.go | 27 ++++++++++ wtf_tests/utils_test.go | 107 +++++++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+), 19 deletions(-) delete mode 100644 wtf/config_watcher.rb create mode 100644 wtf_tests/position_test.go create mode 100644 wtf_tests/utils_test.go diff --git a/wtf/config_watcher.rb b/wtf/config_watcher.rb deleted file mode 100644 index d9d32412..00000000 --- a/wtf/config_watcher.rb +++ /dev/null @@ -1,13 +0,0 @@ -package wtf - -import( -"time" -) - -type ConfigWatcher struct { - UpdatedAt *time.Time -} - -func (watcher *ConfigWatcher) Watch() { - -} diff --git a/wtf/position.go b/wtf/position.go index 73fc5584..bc48dead 100644 --- a/wtf/position.go +++ b/wtf/position.go @@ -9,6 +9,17 @@ type Position struct { height int } +func NewPosition(top, left, width, height int) Position { + pos := Position{ + top: 0, + left: 1, + width: 2, + height: 3, + } + + return pos +} + func (pos *Position) Top() int { return pos.top } diff --git a/wtf/text_widget.go b/wtf/text_widget.go index 7ee3200d..5bf030af 100644 --- a/wtf/text_widget.go +++ b/wtf/text_widget.go @@ -29,14 +29,15 @@ func NewTextWidget(name string, configKey string, focusable bool) TextWidget { Name: name, RefreshInt: Config.UInt(fmt.Sprintf("wtf.mods.%s.refreshInterval", configKey)), - Position: Position{ - top: Config.UInt(fmt.Sprintf("wtf.mods.%s.position.top", configKey)), - left: Config.UInt(fmt.Sprintf("wtf.mods.%s.position.left", configKey)), - height: Config.UInt(fmt.Sprintf("wtf.mods.%s.position.height", configKey)), - width: Config.UInt(fmt.Sprintf("wtf.mods.%s.position.width", configKey)), - }, } + widget.Position = NewPosition( + Config.UInt(fmt.Sprintf("wtf.mods.%s.position.top", configKey)), + Config.UInt(fmt.Sprintf("wtf.mods.%s.position.left", configKey)), + Config.UInt(fmt.Sprintf("wtf.mods.%s.position.height", configKey)), + Config.UInt(fmt.Sprintf("wtf.mods.%s.position.width", configKey)), + ) + widget.addView() return widget diff --git a/wtf_tests/position_test.go b/wtf_tests/position_test.go new file mode 100644 index 00000000..370a3373 --- /dev/null +++ b/wtf_tests/position_test.go @@ -0,0 +1,27 @@ +package wtf_tests + +import ( + "testing" + + . "github.com/senorprogrammer/wtf/wtf" +) + +func TestPosition(t *testing.T) { + pos := NewPosition(0, 1, 2, 3) + + if pos.Top() != 0 { + t.Fatalf("Expected 0 but got %d", pos.Top()) + } + + if pos.Left() != 1 { + t.Fatalf("Expected 1 but got %d", pos.Left()) + } + + if pos.Width() != 2 { + t.Fatalf("Expected 2 but got %d", pos.Width()) + } + + if pos.Height() != 3 { + t.Fatalf("Expected 3 but got %d", pos.Height()) + } +} diff --git a/wtf_tests/utils_test.go b/wtf_tests/utils_test.go new file mode 100644 index 00000000..9a6c005c --- /dev/null +++ b/wtf_tests/utils_test.go @@ -0,0 +1,107 @@ +package wtf_tests + +import ( + "testing" + + . "github.com/senorprogrammer/wtf/wtf" + "github.com/go-test/deep" +) + +/* -------------------- Exclude() -------------------- */ + +func TestExcludeWhenTrue(t *testing.T) { + if Exclude([]string{"cat", "dog", "rat"}, "bat") != true { + t.Fatalf("Expected true but got false") + } +} + +func TestExcludeWhenFalse(t *testing.T) { + if Exclude([]string{"cat", "dog", "rat"}, "dog") != false { + t.Fatalf("Expected false but got true") + } +} + +/* -------------------- NameFromEmail() -------------------- */ + +func TestNameFromEmailWhenEmpty(t *testing.T) { + expected := "" + actual := NameFromEmail("") + + if expected != actual { + t.Fatalf("Expected %s but got %s", expected, actual) + } +} + +func TestNameFromEmailWithEmail(t *testing.T) { + expected := "Chris Cummer" + actual := NameFromEmail("chris.cummer@me.com") + + if expected != actual { + t.Fatalf("Expected %s but got %s", expected, actual) + } +} + +/* -------------------- NamesFromEmails() -------------------- */ + +func TestNamesFromEmailsWhenEmpty(t *testing.T) { + expected := []string{} + actual := NamesFromEmails([]string{}) + + if diff := deep.Equal(expected, actual); diff != nil { + t.Fatalf("Expected %s but got %s", expected, actual) + } +} + +func TestNamesFromEmailsWithEmails(t *testing.T) { + expected := []string{"Chris Cummer", "Chriscummer"} + actual := NamesFromEmails([]string{"chris.cummer@me.com", "chriscummer@me.com"}) + + if diff := deep.Equal(expected, actual); diff != nil { + t.Fatalf("Expected %s but got %s", expected, actual) + } +} + +/* -------------------- ToInts() -------------------- */ + +func TestToInts(t *testing.T) { + expected := []int{1, 2, 3} + + source := make([]interface{}, len(expected)) + for idx, val := range expected { + source[idx] = val + } + + actual := ToInts(source) + + if diff := deep.Equal(expected, actual); diff != nil { + t.Fatalf("Expected %s but got %s", expected, actual) + } +} + +/* -------------------- ToStrs() -------------------- */ + +func TestToStrs(t *testing.T) { + expected := []string{"cat", "dog", "rat"} + + source := make([]interface{}, len(expected)) + for idx, val := range expected { + source[idx] = val + } + + actual := ToStrs(source) + + if diff := deep.Equal(expected, actual); diff != nil { + t.Fatalf("Expected %s but got %s", expected, actual) + } +} + +/* -------------------- PrettyDate() -------------------- */ + +func TestPrettyDate(t *testing.T) { + expected := "Oct 21, 1999" + actual := PrettyDate("1999-10-21") + + if expected != actual { + t.Fatalf("Expected %s but got %s", expected, actual) + } +}