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:
		
							parent
							
								
									dc32346301
								
							
						
					
					
						commit
						2fec736935
					
				
							
								
								
									
										35
									
								
								wtf/datetime.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								wtf/datetime.go
									
									
									
									
									
										Normal 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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								wtf/utils.go
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								wtf/utils.go
									
									
									
									
									
								
							@ -6,7 +6,6 @@ import (
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/rivo/tview"
 | 
			
		||||
)
 | 
			
		||||
@ -18,6 +17,10 @@ const FriendlyDateTimeFormat = "Mon, Jan 2, 15:04"
 | 
			
		||||
const TimestampFormat = "2006-01-02T15:04:05-0700"
 | 
			
		||||
 | 
			
		||||
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))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -151,34 +154,3 @@ func ToStrs(slice []interface{}) []string {
 | 
			
		||||
 | 
			
		||||
	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
									
								
							
							
						
						
									
										14
									
								
								wtf_tests/colors_test.go
									
									
									
									
									
										Normal 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"))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								wtf_tests/datetime_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								wtf_tests/datetime_test.go
									
									
									
									
									
										Normal 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"))
 | 
			
		||||
}
 | 
			
		||||
@ -3,62 +3,60 @@ package wtf_tests
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-test/deep"
 | 
			
		||||
	. "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() -------------------- */
 | 
			
		||||
 | 
			
		||||
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")
 | 
			
		||||
	}
 | 
			
		||||
	Equal(t, true, Exclude([]string{"cat", "dog", "rat"}, "bat"))
 | 
			
		||||
	Equal(t, false, Exclude([]string{"cat", "dog", "rat"}, "dog"))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* -------------------- 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)
 | 
			
		||||
	}
 | 
			
		||||
func TestNameFromEmail(t *testing.T) {
 | 
			
		||||
	Equal(t, "", NameFromEmail(""))
 | 
			
		||||
	Equal(t, "Chris Cummer", NameFromEmail("chris.cummer@me.com"))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* -------------------- NamesFromEmails() -------------------- */
 | 
			
		||||
 | 
			
		||||
func TestNamesFromEmailsWhenEmpty(t *testing.T) {
 | 
			
		||||
	expected := []string{}
 | 
			
		||||
	actual := NamesFromEmails([]string{})
 | 
			
		||||
func TestNamesFromEmails(t *testing.T) {
 | 
			
		||||
	var result []string
 | 
			
		||||
 | 
			
		||||
	if diff := deep.Equal(expected, actual); diff != nil {
 | 
			
		||||
		t.Fatalf("Expected %s but got %s", expected, actual)
 | 
			
		||||
	}
 | 
			
		||||
	result = NamesFromEmails([]string{})
 | 
			
		||||
	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) {
 | 
			
		||||
	expected := []string{"Chris Cummer", "Chriscummer"}
 | 
			
		||||
	actual := NamesFromEmails([]string{"chris.cummer@me.com", "chriscummer@me.com"})
 | 
			
		||||
/* -------------------- PadRow() -------------------- */
 | 
			
		||||
 | 
			
		||||
	if diff := deep.Equal(expected, actual); diff != nil {
 | 
			
		||||
		t.Fatalf("Expected %s but got %s", expected, actual)
 | 
			
		||||
	}
 | 
			
		||||
func TestPadRow(t *testing.T) {
 | 
			
		||||
	Equal(t, "", PadRow(0, 0))
 | 
			
		||||
	Equal(t, "", PadRow(5, 2))
 | 
			
		||||
	Equal(t, " ", PadRow(1, 2))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* -------------------- ToInts() -------------------- */
 | 
			
		||||
@ -71,11 +69,7 @@ func TestToInts(t *testing.T) {
 | 
			
		||||
		source[idx] = val
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	actual := ToInts(source)
 | 
			
		||||
 | 
			
		||||
	if diff := deep.Equal(expected, actual); diff != nil {
 | 
			
		||||
		t.Fatalf("Expected %v but got %v", expected, actual)
 | 
			
		||||
	}
 | 
			
		||||
	Equal(t, expected, ToInts(source))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* -------------------- ToStrs() -------------------- */
 | 
			
		||||
@ -88,20 +82,5 @@ func TestToStrs(t *testing.T) {
 | 
			
		||||
		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)
 | 
			
		||||
	}
 | 
			
		||||
	Equal(t, expected, ToStrs(source))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user