[🤖] Add Unit-Tests
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m34s

This commit is contained in:
2026-04-27 10:46:08 +02:00
parent dad0e3240d
commit 02d6894ec6
116 changed files with 18795 additions and 1 deletions
+145
View File
@@ -0,0 +1,145 @@
package timeext
import (
"testing"
"time"
)
func TestFromNanoseconds(t *testing.T) {
if got := FromNanoseconds(0); got != 0 {
t.Errorf("expected 0, got %v", got)
}
if got := FromNanoseconds(1); got != time.Nanosecond {
t.Errorf("expected 1ns, got %v", got)
}
if got := FromNanoseconds(1000); got != 1000*time.Nanosecond {
t.Errorf("expected 1000ns, got %v", got)
}
if got := FromNanoseconds(int64(123456789)); got != time.Duration(123456789) {
t.Errorf("expected 123456789ns, got %v", got)
}
}
func TestFromMicroseconds(t *testing.T) {
if got := FromMicroseconds(1); got != time.Microsecond {
t.Errorf("expected 1us, got %v", got)
}
if got := FromMicroseconds(1000); got != time.Millisecond {
t.Errorf("expected 1ms, got %v", got)
}
if got := FromMicroseconds(2.5); got != time.Microsecond*2+time.Nanosecond*500 {
t.Errorf("expected 2.5us, got %v", got)
}
}
func TestFromMilliseconds(t *testing.T) {
if got := FromMilliseconds(1); got != time.Millisecond {
t.Errorf("expected 1ms, got %v", got)
}
if got := FromMilliseconds(1000); got != time.Second {
t.Errorf("expected 1s, got %v", got)
}
}
func TestFromSeconds(t *testing.T) {
if got := FromSeconds(1); got != time.Second {
t.Errorf("expected 1s, got %v", got)
}
if got := FromSeconds(60); got != time.Minute {
t.Errorf("expected 1min, got %v", got)
}
if got := FromSeconds(0.5); got != 500*time.Millisecond {
t.Errorf("expected 0.5s, got %v", got)
}
}
func TestFromMinutes(t *testing.T) {
if got := FromMinutes(1); got != time.Minute {
t.Errorf("expected 1min, got %v", got)
}
if got := FromMinutes(60); got != time.Hour {
t.Errorf("expected 1h, got %v", got)
}
}
func TestFromHours(t *testing.T) {
if got := FromHours(1); got != time.Hour {
t.Errorf("expected 1h, got %v", got)
}
if got := FromHours(24); got != 24*time.Hour {
t.Errorf("expected 24h, got %v", got)
}
}
func TestFromDays(t *testing.T) {
if got := FromDays(1); got != 24*time.Hour {
t.Errorf("expected 1d, got %v", got)
}
if got := FromDays(7); got != 7*24*time.Hour {
t.Errorf("expected 7d, got %v", got)
}
if got := FromDays(0); got != 0 {
t.Errorf("expected 0, got %v", got)
}
}
func TestFormatNaturalDurationEnglish(t *testing.T) {
tests := []struct {
dur time.Duration
want string
}{
{time.Second, "1 second ago"},
{2 * time.Second, "2 seconds ago"},
{30 * time.Second, "30 seconds ago"},
{179 * time.Second, "179 seconds ago"},
{180 * time.Second, "3 minutes ago"},
{30 * time.Minute, "30 minutes ago"},
{179 * time.Minute, "179 minutes ago"},
{180 * time.Minute, "3 hours ago"},
{24 * time.Hour, "24 hours ago"},
{71 * time.Hour, "71 hours ago"},
{72 * time.Hour, "3 days ago"},
{20 * 24 * time.Hour, "20 days ago"},
{21 * 24 * time.Hour, "3 weeks ago"},
{11 * 7 * 24 * time.Hour, "11 weeks ago"},
// The months tier divides hours by (24*7*30); the actual boundaries are unusual
// but we capture the current observable behavior:
{12 * 7 * 24 * time.Hour, "0 months ago"},
{90 * 7 * 24 * time.Hour, "3 months ago"},
}
for _, tt := range tests {
got := FormatNaturalDurationEnglish(tt.dur)
if got != tt.want {
t.Errorf("FormatNaturalDurationEnglish(%v) = %q; want %q", tt.dur, got, tt.want)
}
}
}
func TestFormatDurationGerman(t *testing.T) {
tests := []struct {
dur time.Duration
want string
}{
{time.Second, "1s"},
{30 * time.Second, "30s"},
{179 * time.Second, "179s"},
{180 * time.Second, "3min"},
{30 * time.Minute, "30min"},
{179 * time.Minute, "179min"},
{180 * time.Minute, "3h"},
{24 * time.Hour, "24h"},
{71 * time.Hour, "71h"},
{72 * time.Hour, "3 Tage"},
{20 * 24 * time.Hour, "20 Tage"},
{21 * 24 * time.Hour, "3 Wochen"},
{11 * 7 * 24 * time.Hour, "11 Wochen"},
{12 * 7 * 24 * time.Hour, "0 Monate"},
{90 * 7 * 24 * time.Hour, "3 Monate"},
}
for _, tt := range tests {
got := FormatDurationGerman(tt.dur)
if got != tt.want {
t.Errorf("FormatDurationGerman(%v) = %q; want %q", tt.dur, got, tt.want)
}
}
}