Skip to content

Commit

Permalink
test: initial unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
akyrey committed Apr 29, 2024
1 parent 8268143 commit c041300
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
11 changes: 11 additions & 0 deletions internal/assert/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package assert

import "testing"

func Equal[T comparable](t *testing.T, actual, expected T) {
t.Helper()

if actual != expected {
t.Errorf("got: %v; want: %v", actual, expected)
}
}
5 changes: 4 additions & 1 deletion internal/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ type templateData struct {
}

func humanDate(t time.Time) string {
return t.Format("02 Jan 2006 at 15:04")
if t.IsZero() {
return ""
}
return t.UTC().Format("02 Jan 2006 at 15:04")
}

var functions = template.FuncMap{
Expand Down
39 changes: 39 additions & 0 deletions internal/templates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package internal

import (
"testing"
"time"

"github.com/akyrey/snippetbox/internal/assert"
)

func TestHumanDate(t *testing.T) {
tests := []struct {
name string
tm time.Time
want string
}{
{
name: "UTC",
tm: time.Date(2024, 04, 29, 22, 9, 0, 0, time.UTC),
want: "29 Apr 2024 at 22:09",
},
{
name: "Empty",
tm: time.Time{},
want: "",
},
{
name: "CET",
tm: time.Date(2024, 04, 29, 22, 9, 0, 0, time.FixedZone("CET", 1*60*60)),
want: "29 Apr 2024 at 21:09",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := humanDate(tt.tm)
assert.Equal(t, got, tt.want)
})
}
}

0 comments on commit c041300

Please sign in to comment.