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

Add spec coverage to wtf/utils

This commit is contained in:
Chris Cummer 2018-06-23 13:26:36 -07:00
parent dc32346301
commit 2fec736935
5 changed files with 112 additions and 91 deletions

35
wtf/datetime.go Normal file
View File

@ -0,0 +1,35 @@
package wtf
import (
"fmt"
"time"
)
// DateFormat defines the format we expect to receive dates from BambooHR in
const DateFormat = "2006-01-02"
const TimeFormat = "15:04"
func IsToday(date time.Time) bool {
now := Now()
return (date.Year() == now.Year()) &&
(date.Month() == now.Month()) &&
(date.Day() == now.Day())
}
func Now() time.Time {
return time.Now().Local()
}
func PrettyDate(dateStr string) string {
newTime, _ := time.Parse(DateFormat, dateStr)
return fmt.Sprint(newTime.Format("Jan 2, 2006"))
}
func Tomorrow() time.Time {
return Now().AddDate(0, 0, 1)
}
func UnixTime(unix int64) time.Time {
return time.Unix(unix, 0)
}

View File

@ -6,7 +6,6 @@ import (
"os/exec" "os/exec"
"regexp" "regexp"
"strings" "strings"
"time"
"github.com/rivo/tview" "github.com/rivo/tview"
) )
@ -18,6 +17,10 @@ const FriendlyDateTimeFormat = "Mon, Jan 2, 15:04"
const TimestampFormat = "2006-01-02T15:04:05-0700" const TimestampFormat = "2006-01-02T15:04:05-0700"
func CenterText(str string, width int) string { func CenterText(str string, width int) string {
if width < 0 {
width = 0
}
return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str)) return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str))
} }
@ -151,34 +154,3 @@ func ToStrs(slice []interface{}) []string {
return results return results
} }
/* -------------------- Date/Time -------------------- */
// DateFormat defines the format we expect to receive dates from BambooHR in
const DateFormat = "2006-01-02"
const TimeFormat = "15:04"
func IsToday(date time.Time) bool {
now := Now()
return (date.Year() == now.Year()) &&
(date.Month() == now.Month()) &&
(date.Day() == now.Day())
}
func Now() time.Time {
return time.Now().Local()
}
func PrettyDate(dateStr string) string {
newTime, _ := time.Parse(DateFormat, dateStr)
return fmt.Sprint(newTime.Format("Jan 2, 2006"))
}
func Tomorrow() time.Time {
return Now().AddDate(0, 0, 1)
}
func UnixTime(unix int64) time.Time {
return time.Unix(unix, 0)
}

14
wtf_tests/colors_test.go Normal file
View File

@ -0,0 +1,14 @@
package wtf_tests
import (
"testing"
"github.com/gdamore/tcell"
. "github.com/senorprogrammer/wtf/wtf"
. "github.com/stretchr/testify/assert"
)
func TestColorFor(t *testing.T) {
Equal(t, tcell.ColorRed, ColorFor("red"))
Equal(t, tcell.ColorGreen, ColorFor("cat"))
}

View File

@ -0,0 +1,21 @@
package wtf_tests
import (
"testing"
"time"
. "github.com/senorprogrammer/wtf/wtf"
. "github.com/stretchr/testify/assert"
)
func TestIsToday(t *testing.T) {
Equal(t, true, IsToday(time.Now().Local()))
Equal(t, false, IsToday(time.Now().AddDate(0, 0, -1)))
Equal(t, false, IsToday(time.Now().AddDate(0, 0, +1)))
}
/* -------------------- PrettyDate() -------------------- */
func TestPrettyDate(t *testing.T) {
Equal(t, "Oct 21, 1999", PrettyDate("1999-10-21"))
}

View File

@ -3,62 +3,60 @@ package wtf_tests
import ( import (
"testing" "testing"
"github.com/go-test/deep"
. "github.com/senorprogrammer/wtf/wtf" . "github.com/senorprogrammer/wtf/wtf"
. "github.com/stretchr/testify/assert"
) )
/* -------------------- CenterText() -------------------- */
func TestCenterText(t *testing.T) {
Equal(t, "cat", CenterText("cat", -9))
Equal(t, "cat", CenterText("cat", 0))
Equal(t, " cat ", CenterText("cat", 9))
}
/* -------------------- FindMatch() -------------------- */
func TestFindMatch(t *testing.T) {
var result [][]string
expected := [][]string([][]string{[]string{"SSID: 7E5B5C", "7E5B5C"}})
result = FindMatch(`s*SSID: (.+)s*`, "SSID: 7E5B5C")
Equal(t, expected, result)
}
/* -------------------- Exclude() -------------------- */ /* -------------------- Exclude() -------------------- */
func TestExcludeWhenTrue(t *testing.T) { func TestExcludeWhenTrue(t *testing.T) {
if Exclude([]string{"cat", "dog", "rat"}, "bat") != true { Equal(t, true, Exclude([]string{"cat", "dog", "rat"}, "bat"))
t.Fatalf("Expected true but got false") Equal(t, false, Exclude([]string{"cat", "dog", "rat"}, "dog"))
}
}
func TestExcludeWhenFalse(t *testing.T) {
if Exclude([]string{"cat", "dog", "rat"}, "dog") != false {
t.Fatalf("Expected false but got true")
}
} }
/* -------------------- NameFromEmail() -------------------- */ /* -------------------- NameFromEmail() -------------------- */
func TestNameFromEmailWhenEmpty(t *testing.T) { func TestNameFromEmail(t *testing.T) {
expected := "" Equal(t, "", NameFromEmail(""))
actual := NameFromEmail("") Equal(t, "Chris Cummer", NameFromEmail("chris.cummer@me.com"))
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() -------------------- */ /* -------------------- NamesFromEmails() -------------------- */
func TestNamesFromEmailsWhenEmpty(t *testing.T) { func TestNamesFromEmails(t *testing.T) {
expected := []string{} var result []string
actual := NamesFromEmails([]string{})
if diff := deep.Equal(expected, actual); diff != nil { result = NamesFromEmails([]string{})
t.Fatalf("Expected %s but got %s", expected, actual) Equal(t, []string{}, result)
}
result = NamesFromEmails([]string{"chris.cummer@me.com", "chriscummer@me.com"})
Equal(t, []string{"Chris Cummer", "Chriscummer"}, result)
} }
func TestNamesFromEmailsWithEmails(t *testing.T) { /* -------------------- PadRow() -------------------- */
expected := []string{"Chris Cummer", "Chriscummer"}
actual := NamesFromEmails([]string{"chris.cummer@me.com", "chriscummer@me.com"})
if diff := deep.Equal(expected, actual); diff != nil { func TestPadRow(t *testing.T) {
t.Fatalf("Expected %s but got %s", expected, actual) Equal(t, "", PadRow(0, 0))
} Equal(t, "", PadRow(5, 2))
Equal(t, " ", PadRow(1, 2))
} }
/* -------------------- ToInts() -------------------- */ /* -------------------- ToInts() -------------------- */
@ -71,11 +69,7 @@ func TestToInts(t *testing.T) {
source[idx] = val source[idx] = val
} }
actual := ToInts(source) Equal(t, expected, ToInts(source))
if diff := deep.Equal(expected, actual); diff != nil {
t.Fatalf("Expected %v but got %v", expected, actual)
}
} }
/* -------------------- ToStrs() -------------------- */ /* -------------------- ToStrs() -------------------- */
@ -88,20 +82,5 @@ func TestToStrs(t *testing.T) {
source[idx] = val source[idx] = val
} }
actual := ToStrs(source) Equal(t, expected, 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)
}
} }