From 36ade8290ae8948a592da61fce919e36bfd43921 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sat, 15 Feb 2025 15:42:31 -0500 Subject: [PATCH] Added assert package and added test --- internal/assert/assert.go | 18 ++++++++++++ internal/server/templates.go | 8 ++++- internal/server/templates_test.go | 49 +++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 internal/assert/assert.go create mode 100644 internal/server/templates_test.go diff --git a/internal/assert/assert.go b/internal/assert/assert.go new file mode 100644 index 0000000..1417379 --- /dev/null +++ b/internal/assert/assert.go @@ -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 we’re 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) + } +} diff --git a/internal/server/templates.go b/internal/server/templates.go index 0014d77..fd57571 100644 --- a/internal/server/templates.go +++ b/internal/server/templates.go @@ -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 diff --git a/internal/server/templates_test.go b/internal/server/templates_test.go new file mode 100644 index 0000000..50e5ce7 --- /dev/null +++ b/internal/server/templates_test.go @@ -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) + }) + } + +}