From a82b12a31431b296f62d3d7424360424745d6e0d Mon Sep 17 00:00:00 2001 From: ebosas Date: Fri, 19 Nov 2021 09:20:18 +0200 Subject: [PATCH] Add timeutil test --- Makefile | 2 ++ internal/timeutil/timeutil_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 Makefile create mode 100644 internal/timeutil/timeutil_test.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..24a590e --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +test: + go test ./internal/timeutil \ No newline at end of file diff --git a/internal/timeutil/timeutil_test.go b/internal/timeutil/timeutil_test.go new file mode 100644 index 0000000..71b5eb3 --- /dev/null +++ b/internal/timeutil/timeutil_test.go @@ -0,0 +1,29 @@ +package timeutil + +import ( + "testing" + "time" +) + +func TestFormatDuration(t *testing.T) { + now := time.Now().Unix() * 1000 + var tests = []struct { + input int64 + want string + }{ + {now, "less than a minute"}, + {now - 1.4*60*1000, "1 minute"}, + {now - 1.5*60*1000, "2 minutes"}, + {now - 44*60*1000, "44 minutes"}, + {now - 2*60*60*1000, "about 2 hours"}, + {now - 1439*60*1000, "about 24 hours"}, + {now - 1440*60*1000, "1 day"}, + {now - 3*43200*60*1000, "3 months"}, + {now - 24*43200*60*1000, "about 2 years"}, + } + for _, test := range tests { + if got := FormatDuration(test.input); got != test.want { + t.Errorf("FormatDuration(%d) = %q, want %q", test.input, got, test.want) + } + } +}