Added assert package and added test

main
Drew Bednar 2 months ago
parent 514c24a768
commit 36ade8290a

@ -0,0 +1,18 @@
package assert
import "testing"
// Equal a generic function to test equivalence between two values
// of the same type
func Equal[T comparable](t *testing.T, actual, expected T) {
//The t.Helper() function that were using in the code above indicates
// to the Go test runner that our Equal() function is a test helper.
// This means that when t.Errorf() is called from our Equal() function,
// the Go test runner will report the filename and line number of the
// code which called our Equal() function in the output.
t.Helper()
if actual != expected {
t.Errorf("got: %v; want %v", actual, expected)
}
}

@ -48,7 +48,13 @@ func newTemplateData(r *http.Request, sm *scs.SessionManager) templateData {
// Create a humanDate function which returns a nicely formatted string
// representation of a time.Time object.
func humanDate(t time.Time) string {
return t.Format("02 Jan 2006 at 15:04")
if t.IsZero() {
return ""
}
// return t.Format("02 Jan 2006 at 15:04")
// always return in UTC
return t.UTC().Format("02 Jan 2006 at 15:04")
}
// TEMPLATE FILTERS

@ -0,0 +1,49 @@
package server
import (
"testing"
"time"
"git.runcible.io/learning/ratchet/internal/assert"
)
func TestHumanDate(t *testing.T) {
// Table driven test
tests := []struct {
name string
tm time.Time
want string
}{
{
name: "UTC",
tm: time.Date(2077, time.April, 12, 23, 0, 0, 0, time.UTC),
want: "12 Apr 2077 at 23:00",
},
{
name: "UTC",
tm: time.Date(2025, time.March, 3, 2, 31, 0, 0, time.UTC),
want: "03 Mar 2025 at 02:31",
},
{
name: "Empty",
tm: time.Time{},
want: "",
},
// CET is one hour ahead of UTC but we print in UTC
{
name: "CET",
tm: time.Date(2024, 3, 17, 10, 15, 0, 0, time.FixedZone("CET", 1*60*60)),
want: "17 Mar 2024 at 09:15",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := humanDate(test.tm)
assert.Equal(t, got, test.want)
})
}
}
Loading…
Cancel
Save