quando/unit_test.go
Oliver Jakoubek 889e78da90 feat(quando-r1o): consolidate benchmarks and achieve 99.5% test coverage
Reorganized test suite with consolidated benchmarks and comprehensive
edge case coverage, exceeding 95% minimum coverage requirement.

Changes:
- Created benchmark_test.go with all 52 benchmarks organized by category
  (arithmetic, clock, date, diff, format, inspection, parse, snap, unit)
- Removed benchmarks from 9 individual test files for better organization
- Added 6 edge case tests to improve coverage:
  * Format() unknown type fallback
  * formatLong() empty lang default
  * Format.String() unknown value
  * isYearPrefix() edge cases
  * StartOf()/EndOf() unsupported units
- Added 3 DST transition tests:
  * Spring forward (23-hour day)
  * Fall back (25-hour day)
  * Multiple timezone preservation

Results:
- Test coverage: 97.7% → 99.5% (exceeds 95% target)
- All 52 benchmarks consolidated and verified
- All benchmarks meet performance targets
- No test regressions

Files modified:
- Created: benchmark_test.go (580 lines)
- Modified: 9 test files (removed benchmarks, added tests)
2026-02-11 21:13:09 +01:00

138 lines
3.1 KiB
Go

package quando
import (
"testing"
)
func TestUnitConstants(t *testing.T) {
// Verify that all constants are defined and have expected values
tests := []struct {
name string
unit Unit
expected int
str string
}{
{"Seconds", Seconds, 0, "seconds"},
{"Minutes", Minutes, 1, "minutes"},
{"Hours", Hours, 2, "hours"},
{"Days", Days, 3, "days"},
{"Weeks", Weeks, 4, "weeks"},
{"Months", Months, 5, "months"},
{"Quarters", Quarters, 6, "quarters"},
{"Years", Years, 7, "years"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Verify the numeric value
if int(tt.unit) != tt.expected {
t.Errorf("%s = %d, want %d", tt.name, int(tt.unit), tt.expected)
}
// Verify the string representation
if tt.unit.String() != tt.str {
t.Errorf("%s.String() = %q, want %q", tt.name, tt.unit.String(), tt.str)
}
})
}
}
func TestUnitOrdering(t *testing.T) {
// Verify that units are ordered from smallest to largest (except Quarters)
if Seconds >= Minutes {
t.Error("Seconds should be < Minutes")
}
if Minutes >= Hours {
t.Error("Minutes should be < Hours")
}
if Hours >= Days {
t.Error("Hours should be < Days")
}
if Days >= Weeks {
t.Error("Days should be < Weeks")
}
if Weeks >= Months {
t.Error("Weeks should be < Months")
}
if Months >= Quarters {
t.Error("Months should be < Quarters")
}
if Quarters >= Years {
t.Error("Quarters should be < Years")
}
}
func TestUnitString(t *testing.T) {
tests := []struct {
name string
unit Unit
expected string
}{
{"Seconds", Seconds, "seconds"},
{"Minutes", Minutes, "minutes"},
{"Hours", Hours, "hours"},
{"Days", Days, "days"},
{"Weeks", Weeks, "weeks"},
{"Months", Months, "months"},
{"Quarters", Quarters, "quarters"},
{"Years", Years, "years"},
{"Unknown", Unit(999), "unknown"},
{"Negative", Unit(-1), "unknown"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.unit.String()
if result != tt.expected {
t.Errorf("Unit(%d).String() = %q, want %q", tt.unit, result, tt.expected)
}
})
}
}
// TestUnitTypeSafety verifies that Unit provides compile-time type safety
func TestUnitTypeSafety(t *testing.T) {
// This test primarily exists to demonstrate type safety at compile time
// If this compiles, the type safety is working
var u Unit
u = Seconds
u = Minutes
u = Hours
u = Days
u = Weeks
u = Months
u = Quarters
u = Years
// Verify last assignment worked
if u != Years {
t.Error("Type safety test failed")
}
}
// TestUnitComparability verifies that Units can be compared
func TestUnitComparability(t *testing.T) {
if Seconds == Minutes {
t.Error("Seconds should not equal Minutes")
}
if Seconds == Seconds {
// This should be true
} else {
t.Error("Seconds should equal itself")
}
// Test in switch statement (common usage pattern)
u := Days
switch u {
case Seconds, Minutes, Hours:
t.Error("Days matched wrong case")
case Days:
// Expected
case Weeks, Months, Quarters, Years:
t.Error("Days matched wrong case")
default:
t.Error("Days didn't match any case")
}
}