// SPDX-License-Identifier: GPL-3.0-or-later package build import ( "testing" "time" "git.kmalloc.org/squid/motorino/internal/site" ) // dateLang returns a language with the strings that write a date. func dateLang(layout string, months ...string) site.Lang { l := site.Lang{Code: "xx", Strings: map[string]string{}} if layout != "" { l.Strings["date_long"] = layout } for i, m := range months { l.Strings["month_"+string(rune('0'+(i+1)/10))+string(rune('0'+(i+1)%10))] = m } return l } // allMonths lists twelve month names for a test language. var allMonths = []string{"gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"} // TestLongDate checks the long date in a language, as written in the date. func TestLongDate(t *testing.T) { date, err := time.Parse("2006-01-02T15:04:05-0700", "2026-11-05T23:30:00-0500") if err != nil { t.Fatal(err) } if got := longDate(date, dateLang("{d} {month} {y}", allMonths...)); got != "5 novembre 2026" { t.Errorf("longDate() = %q, want the day without a zero, the month name and the year", got) } if got := longDate(date, dateLang("{month} {d}, {y}", allMonths...)); got != "novembre 5, 2026" { t.Errorf("longDate() with another layout = %q", got) } } // TestLongDateFallsBack checks the ISO date when the language has no layout or a month. func TestLongDateFallsBack(t *testing.T) { date, _ := time.Parse("2006-01-02T15:04:05-0700", "2026-11-14T12:00:00+0100") if got := longDate(date, dateLang("")); got != "2026-11-14" { t.Errorf("no layout: longDate() = %q, want the ISO date", got) } if got := longDate(date, dateLang("{d} {month} {y}", "uno", "due")); got != "2026-11-14" { t.Errorf("a missing month name: longDate() = %q, want the ISO date", got) } } // TestMonthYear checks the label of an archive month. func TestMonthYear(t *testing.T) { l := dateLang("{d} {month} {y}", allMonths...) l.Strings["date_month_year"] = "{month} {y}" if got := monthYear(2026, 11, l); got != "novembre 2026" { t.Errorf("monthYear() = %q", got) } if got := monthYear(2026, 3, dateLang("")); got != "2026-03" { t.Errorf("monthYear() without strings = %q, want 2026-03", got) } } // TestReadingText checks the reading time sentence and its absence. func TestReadingText(t *testing.T) { l := site.Lang{Strings: map[string]string{"reading_time": "{n} min di lettura"}} if got := readingText(3, l); got != "3 min di lettura" { t.Errorf("readingText() = %q", got) } if got := readingText(3, site.Lang{Strings: map[string]string{}}); got != "" { t.Errorf("readingText() without a string = %q, want empty", got) } }