package web
import (
"testing"
"time"
)
const tickTestWidth = 700.0
// TestMonthTicksNeverCrowd holds the date axis to its one promise: on a
// window of any length, two labels are never closer than a label is
// wide, and there is always something to read.
func TestMonthTicksNeverCrowd(t *testing.T) {
for _, span := range []int{7, 30, 90, 180, 365, 730, 1500, 3650, 7057, 15000, 40000} {
for _, since := range []string{"2007-07-21", "2020-01-01", "2019-12-17"} {
ticks := monthTicks(since, span, tickTestWidth)
labels, last, lastYear := 0, -1e9, false
for i, tk := range ticks {
x := tickTestWidth * tk.day / float64(span)
if x < 0 || x > tickTestWidth {
t.Errorf("FAIL: span %d from %s: tick %d at %.1f is off the axis", span, since, i, x)
}
if i > 0 && tk.day <= ticks[i-1].day {
t.Errorf("FAIL: span %d from %s: tick %d is not after the one before", span, since, i)
}
if tk.major != (tk.label != "") {
t.Errorf("FAIL: span %d from %s: tick %d major=%v label=%q", span, since, i, tk.major, tk.label)
}
if !tk.major {
continue
}
_, err := time.Parse("2006", tk.label)
year := err == nil
room := 64.0
if year && lastYear {
room = 40
}
if x-last < room {
t.Errorf("FAIL: span %d from %s: labels %.1f apart, want %.0f", span, since, x-last, room)
}
labels, last, lastYear = labels+1, x, year
}
if labels == 0 {
t.Errorf("FAIL: span %d from %s: no label", span, since)
}
if len(ticks) > 200 {
t.Errorf("FAIL: span %d from %s: %d ticks", span, since, len(ticks))
}
}
}
if got := monthTicks("", 0, tickTestWidth); len(got) != 0 {
t.Errorf("FAIL: an empty window drew %d ticks", len(got))
}
if !t.Failed() {
t.Log("ok")
}
}