[🤖] 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
+81
View File
@@ -0,0 +1,81 @@
package timeext
import (
"testing"
"time"
)
func TestGetIsoWeekCount(t *testing.T) {
// The implementation subtracts 1 from the ISO week numbers internally,
// so for a 53-week year it returns 52, and for a 52-week year it returns 51.
tests := []struct {
year int
want int
}{
{2020, 52}, // 2020 is a 53-week ISO year
{2021, 51},
{2022, 51},
{2023, 51},
{2024, 51},
}
for _, tt := range tests {
got := GetIsoWeekCount(tt.year)
if got != tt.want {
t.Errorf("GetIsoWeekCount(%d) = %d; want %d", tt.year, got, tt.want)
}
}
}
func TestGetAggregateIsoWeekCount_Year1900(t *testing.T) {
got := GetAggregateIsoWeekCount(1900)
if got != 0 {
t.Errorf("GetAggregateIsoWeekCount(1900) = %d; want 0", got)
}
}
func TestGetAggregateIsoWeekCount_Monotonic(t *testing.T) {
// The aggregate count must be strictly monotonically increasing year over year (for years > 1900)
prev := GetAggregateIsoWeekCount(1900)
for y := 1901; y <= 2030; y++ {
cur := GetAggregateIsoWeekCount(y)
if cur <= prev {
t.Errorf("GetAggregateIsoWeekCount(%d)=%d not greater than GetAggregateIsoWeekCount(%d)=%d", y, cur, y-1, prev)
}
prev = cur
}
}
func TestGetAggregateIsoWeekCount_BeforeBaseline(t *testing.T) {
// for years < 1900 the aggregate is negative
got := GetAggregateIsoWeekCount(1899)
if got >= 0 {
t.Errorf("GetAggregateIsoWeekCount(1899) = %d; want < 0", got)
}
}
func TestGetGlobalWeeknumber_Monotonic(t *testing.T) {
// Walking forward by one week should never decrease the global week number
t0 := time.Date(2020, 1, 6, 0, 0, 0, 0, TimezoneBerlin) // Monday, ISO week 2 of 2020
prev := GetGlobalWeeknumber(t0)
for i := 1; i < 200; i++ {
ti := t0.AddDate(0, 0, i*7)
cur := GetGlobalWeeknumber(ti)
if cur < prev {
t.Errorf("week number decreased at offset %d weeks: %d -> %d", i, prev, cur)
}
prev = cur
}
}
func TestGetGlobalWeeknumber_DifferentYearsDiffer(t *testing.T) {
w2020 := GetGlobalWeeknumber(time.Date(2020, 6, 1, 0, 0, 0, 0, TimezoneBerlin))
w2021 := GetGlobalWeeknumber(time.Date(2021, 6, 1, 0, 0, 0, 0, TimezoneBerlin))
if w2021 <= w2020 {
t.Errorf("expected w2021 > w2020, got %d and %d", w2021, w2020)
}
// Approximately 52 weeks apart
delta := w2021 - w2020
if delta < 50 || delta > 54 {
t.Errorf("expected ~52 week difference, got %d", delta)
}
}