| 1 |
// Copyright 2026 Bjørn Erik Pedersen |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package golocales |
| 5 |
|
| 6 |
import ( |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
qt "github.com/frankban/quicktest" |
| 12 |
) |
| 13 |
|
| 14 |
func TestTranslator(t *testing.T) { |
| 15 |
c := qt.New(t) |
| 16 |
tr := New("nn") |
| 17 |
c.Assert(tr, qt.Not(qt.IsNil)) |
| 18 |
|
| 19 |
// Date formatting. |
| 20 |
loc, err := time.LoadLocation("America/Toronto") |
| 21 |
c.Assert(err, qt.IsNil) |
| 22 |
fixed := time.FixedZone("OTHER", -4) |
| 23 |
timeInLoc1 := time.Date(2016, 0o2, 0o3, 9, 5, 1, 0, loc) |
| 24 |
timeInLoc2 := time.Date(2016, 0o2, 0o3, 20, 5, 1, 0, loc) |
| 25 |
timeInFixed := time.Date(2016, 0o2, 0o3, 20, 5, 1, 0, fixed) |
| 26 |
|
| 27 |
c.Assert(tr.FormatTimeFull(timeInLoc1), qt.Equals, "09:05:01 normaltid for den nordamerikanske austkysten") |
| 28 |
c.Assert(tr.FormatTimeFull(timeInFixed), qt.Equals, "20:05:01 OTHER") |
| 29 |
c.Assert(tr.FormatTimeLong(timeInLoc1), qt.Equals, "09:05:01 EST") |
| 30 |
c.Assert(tr.FormatTimeLong(timeInLoc2), qt.Equals, "20:05:01 EST") |
| 31 |
c.Assert(tr.FormatTimeMedium(timeInLoc1), qt.Equals, "09:05:01") |
| 32 |
c.Assert(tr.FormatTimeMedium(timeInLoc2), qt.Equals, "20:05:01") |
| 33 |
c.Assert(tr.FormatTimeShort(timeInLoc1), qt.Equals, "09:05") |
| 34 |
c.Assert(tr.FormatTimeShort(timeInLoc2), qt.Equals, "20:05") |
| 35 |
|
| 36 |
c.Assert(tr.FormatDateFull(timeInLoc1), qt.Equals, "onsdag 3. februar 2016") |
| 37 |
c.Assert(tr.FormatDateLong(timeInLoc1), qt.Equals, "3. februar 2016") |
| 38 |
c.Assert(tr.FormatDateMedium(timeInLoc1), qt.Equals, "3. feb. 2016") |
| 39 |
c.Assert(tr.FormatDateShort(timeInLoc1), qt.Equals, "03.02.16") |
| 40 |
|
| 41 |
// Week days. |
| 42 |
c.Assert(tr.WeekdaysAbbreviated(), qt.DeepEquals, []string{"sø.", "må.", "ty.", "on.", "to.", "fr.", "la."}) |
| 43 |
c.Assert(tr.WeekdaysWide(), qt.DeepEquals, []string{"søndag", "måndag", "tysdag", "onsdag", "torsdag", "fredag", "laurdag"}) |
| 44 |
|
| 45 |
// Months. |
| 46 |
c.Assert(tr.MonthsAbbreviated(), qt.DeepEquals, []string{"jan.", "feb.", "mars", "apr.", "mai", "juni", "juli", "aug.", "sep.", "okt.", "nov.", "des."}) |
| 47 |
c.Assert(tr.MonthsWide(), qt.DeepEquals, []string{"januar", "februar", "mars", "april", "mai", "juni", "juli", "august", "september", "oktober", "november", "desember"}) |
| 48 |
|
| 49 |
// Number formatting. |
| 50 |
pi := 3.14159 |
| 51 |
c.Assert(tr.FormatNumber(pi, 3), qt.Equals, "3,142") |
| 52 |
|
| 53 |
c.Assert(tr.FormatNumber(1123456.5643, 2), qt.Equals, "1\u00a0123\u00a0456,56") |
| 54 |
c.Assert(tr.FormatAccounting(550.5643, 2, "USD"), qt.Equals, "550,56\u00a0USD") |
| 55 |
c.Assert(tr.FormatAccounting(123.567, 2, "NOK"), qt.Equals, "123,57\u00a0kr") |
| 56 |
c.Assert(tr.FormatAccounting(123.567, 2, "NoK"), qt.Equals, "123,57\u00a0kr") |
| 57 |
c.Assert(tr.FormatPercent(15, 0), qt.Equals, "15\u00a0%") |
| 58 |
c.Assert(tr.FormatCurrency(1123456.5643, 2, "USD"), qt.Equals, "1\u00a0123\u00a0456,56\u00a0USD") |
| 59 |
c.Assert(tr.FormatCurrency(1123456.5643, 2, "NOK"), qt.Equals, "1\u00a0123\u00a0456,56\u00a0kr") |
| 60 |
c.Assert(tr.FormatCurrency(1123456.5643, 2, "nok"), qt.Equals, "1\u00a0123\u00a0456,56\u00a0kr") |
| 61 |
} |
| 62 |
|
| 63 |
func TestNew(t *testing.T) { |
| 64 |
c := qt.New(t) |
| 65 |
|
| 66 |
checkOne := func(locale string) { |
| 67 |
tr := New(locale) |
| 68 |
c.Assert(tr, qt.Not(qt.IsNil)) |
| 69 |
} |
| 70 |
|
| 71 |
for _, locale := range []string{"en", "de", "fr", "zh", "no", "nb", "nb_NO", "nn", "nn_NO"} { |
| 72 |
checkOne(locale) |
| 73 |
checkOne(strings.ToUpper(locale)) |
| 74 |
checkOne(strings.ReplaceAll(locale, "_", "-")) |
| 75 |
} |
| 76 |
|
| 77 |
tr := New("nonexistent") |
| 78 |
c.Assert(tr, qt.IsNil) |
| 79 |
} |
| 80 |
|
| 81 |
func TestTranslatorQuotedLiterals(t *testing.T) { |
| 82 |
c := qt.New(t) |
| 83 |
// fr_CA uses patterns like: HH 'h' mm 'min' ss 's' |
| 84 |
tr := New("fr_CA") |
| 85 |
c.Assert(tr, qt.Not(qt.IsNil)) |
| 86 |
|
| 87 |
loc, err := time.LoadLocation("America/Toronto") |
| 88 |
c.Assert(err, qt.IsNil) |
| 89 |
tm := time.Date(2024, 3, 15, 14, 30, 45, 0, loc) |
| 90 |
|
| 91 |
// TimeMedium: HH 'h' mm 'min' ss 's' → "14 h 30 min 45 s" |
| 92 |
c.Assert(tr.FormatTimeMedium(tm), qt.Equals, "14 h 30 min 45 s") |
| 93 |
|
| 94 |
// TimeShort: HH 'h' mm → "14 h 30" |
| 95 |
c.Assert(tr.FormatTimeShort(tm), qt.Equals, "14 h 30") |
| 96 |
} |
| 97 |
|
| 98 |
func TestTranslatorEnglish12h(t *testing.T) { |
| 99 |
c := qt.New(t) |
| 100 |
tr := New("en") |
| 101 |
c.Assert(tr, qt.Not(qt.IsNil)) |
| 102 |
|
| 103 |
loc, err := time.LoadLocation("America/New_York") |
| 104 |
c.Assert(err, qt.IsNil) |
| 105 |
|
| 106 |
// Morning time (AM). |
| 107 |
morning := time.Date(2024, 7, 4, 9, 5, 1, 0, loc) |
| 108 |
// Afternoon time (PM). |
| 109 |
afternoon := time.Date(2024, 7, 4, 15, 30, 45, 0, loc) |
| 110 |
// Midnight (12 AM). |
| 111 |
midnight := time.Date(2024, 1, 1, 0, 0, 0, 0, loc) |
| 112 |
|
| 113 |
// TimeFull uses h:mm:ss a zzzz pattern — covers 'h' (12h), 'a' (AM/PM), 'zzzz' (long tz). |
| 114 |
c.Assert(tr.FormatTimeFull(morning), qt.Equals, "9:05:01\u202fam Eastern Daylight Time") |
| 115 |
c.Assert(tr.FormatTimeFull(afternoon), qt.Equals, "3:30:45\u202fpm Eastern Daylight Time") |
| 116 |
|
| 117 |
// Midnight: h=12, AM. |
| 118 |
c.Assert(tr.FormatTimeFull(midnight), qt.Equals, "12:00:00\u202fam Eastern Standard Time") |
| 119 |
|
| 120 |
// DateFull covers EEEE (wide weekday) and MMMM (wide month). |
| 121 |
c.Assert(tr.FormatDateFull(morning), qt.Equals, "Thursday, July 4, 2024") |
| 122 |
|
| 123 |
// DateShort covers single-digit M and d. |
| 124 |
c.Assert(tr.FormatDateShort(morning), qt.Equals, "7/4/24") |
| 125 |
} |
| 126 |
|
| 127 |
func TestFormatNumberEdgeCases(t *testing.T) { |
| 128 |
c := qt.New(t) |
| 129 |
tr := New("en") |
| 130 |
c.Assert(tr, qt.Not(qt.IsNil)) |
| 131 |
|
| 132 |
// Negative number. |
| 133 |
c.Assert(tr.FormatNumber(-1234.56, 2), qt.Equals, "-1,234.56") |
| 134 |
|
| 135 |
// Small number (no grouping needed). |
| 136 |
c.Assert(tr.FormatNumber(42, 0), qt.Equals, "42") |
| 137 |
|
| 138 |
// Zero precision. |
| 139 |
c.Assert(tr.FormatNumber(1234567, 0), qt.Equals, "1,234,567") |
| 140 |
|
| 141 |
// Zero. |
| 142 |
c.Assert(tr.FormatNumber(0, 2), qt.Equals, "0.00") |
| 143 |
} |
| 144 |
|
| 145 |
func TestFormatNumberRounding(t *testing.T) { |
| 146 |
c := qt.New(t) |
| 147 |
tr := New("en") |
| 148 |
c.Assert(tr, qt.Not(qt.IsNil)) |
| 149 |
|
| 150 |
// FormatNumber rounding. |
| 151 |
c.Assert(tr.FormatNumber(3.14159, 3), qt.Equals, "3.142") |
| 152 |
c.Assert(tr.FormatNumber(2.5, 0), qt.Equals, "3") |
| 153 |
c.Assert(tr.FormatNumber(1.005, 2), qt.Equals, "1.00") // IEEE 754: 1.005 is actually 1.00499... in float64. |
| 154 |
c.Assert(tr.FormatNumber(9.9999, 2), qt.Equals, "10.00") |
| 155 |
|
| 156 |
// FormatCurrency rounding. |
| 157 |
c.Assert(tr.FormatCurrency(123.567, 2, "USD"), qt.Equals, "$123.57") |
| 158 |
c.Assert(tr.FormatCurrency(99.995, 2, "USD"), qt.Equals, "$100.00") |
| 159 |
|
| 160 |
// FormatAccounting rounding. |
| 161 |
c.Assert(tr.FormatAccounting(123.567, 2, "USD"), qt.Equals, "$123.57") |
| 162 |
|
| 163 |
// FormatPercent rounding. |
| 164 |
c.Assert(tr.FormatPercent(33.456, 1), qt.Equals, "33.5%") |
| 165 |
c.Assert(tr.FormatPercent(99.99, 0), qt.Equals, "100%") |
| 166 |
} |
| 167 |
|
| 168 |
func TestFormatCurrencyFallbackToCode(t *testing.T) { |
| 169 |
c := qt.New(t) |
| 170 |
tr := New("en") |
| 171 |
c.Assert(tr, qt.Not(qt.IsNil)) |
| 172 |
|
| 173 |
// Use a currency code that has no symbol override and no default symbol. |
| 174 |
result := tr.FormatCurrency(100, 2, "XYZ") |
| 175 |
c.Assert(result, qt.Contains, "XYZ") |
| 176 |
c.Assert(result, qt.Contains, "100.00") |
| 177 |
} |
| 178 |
|
| 179 |
func TestFormatEmptyPatternFallbacks(t *testing.T) { |
| 180 |
c := qt.New(t) |
| 181 |
// "aa" locale has empty currency/percent patterns. |
| 182 |
tr := New("aa") |
| 183 |
c.Assert(tr, qt.Not(qt.IsNil)) |
| 184 |
|
| 185 |
// Currency with empty pattern falls back to "num symbol". |
| 186 |
result := tr.FormatCurrency(100, 2, "USD") |
| 187 |
c.Assert(result, qt.Contains, "100") |
| 188 |
|
| 189 |
// Accounting with empty pattern. |
| 190 |
result = tr.FormatAccounting(100, 2, "USD") |
| 191 |
c.Assert(result, qt.Contains, "100") |
| 192 |
|
| 193 |
// Percent with empty pattern. |
| 194 |
result = tr.FormatPercent(50, 0) |
| 195 |
c.Assert(result, qt.Contains, "50") |
| 196 |
c.Assert(result, qt.Contains, "%") |
| 197 |
} |
| 198 |
|
| 199 |
func TestFormatAccountingAndPercent(t *testing.T) { |
| 200 |
c := qt.New(t) |
| 201 |
tr := New("en") |
| 202 |
c.Assert(tr, qt.Not(qt.IsNil)) |
| 203 |
|
| 204 |
// Accounting format. |
| 205 |
result := tr.FormatAccounting(1234.56, 2, "USD") |
| 206 |
c.Assert(result, qt.Contains, "$") |
| 207 |
c.Assert(result, qt.Contains, "1,234.56") |
| 208 |
|
| 209 |
// Percent format. |
| 210 |
pct := tr.FormatPercent(75.5, 1) |
| 211 |
c.Assert(pct, qt.Contains, "75.5") |
| 212 |
c.Assert(pct, qt.Contains, "%") |
| 213 |
} |
| 214 |
|
| 215 |
func BenchmarkAll(b *testing.B) { |
| 216 |
tr := New("en") |
| 217 |
loc, _ := time.LoadLocation("America/New_York") |
| 218 |
tm := time.Date(2024, 7, 4, 15, 30, 45, 0, loc) |
| 219 |
|
| 220 |
b.Run("New", func(b *testing.B) { |
| 221 |
for range b.N { |
| 222 |
New("en") |
| 223 |
} |
| 224 |
}) |
| 225 |
b.Run("FormatDateFull", func(b *testing.B) { |
| 226 |
for range b.N { |
| 227 |
tr.FormatDateFull(tm) |
| 228 |
} |
| 229 |
}) |
| 230 |
b.Run("FormatDateShort", func(b *testing.B) { |
| 231 |
for range b.N { |
| 232 |
tr.FormatDateShort(tm) |
| 233 |
} |
| 234 |
}) |
| 235 |
b.Run("FormatTimeFull", func(b *testing.B) { |
| 236 |
for range b.N { |
| 237 |
tr.FormatTimeFull(tm) |
| 238 |
} |
| 239 |
}) |
| 240 |
b.Run("FormatTimeShort", func(b *testing.B) { |
| 241 |
for range b.N { |
| 242 |
tr.FormatTimeShort(tm) |
| 243 |
} |
| 244 |
}) |
| 245 |
b.Run("FormatNumber", func(b *testing.B) { |
| 246 |
for range b.N { |
| 247 |
tr.FormatNumber(1234567.89, 2) |
| 248 |
} |
| 249 |
}) |
| 250 |
b.Run("FormatCurrency", func(b *testing.B) { |
| 251 |
for range b.N { |
| 252 |
tr.FormatCurrency(1234567.89, 2, "USD") |
| 253 |
} |
| 254 |
}) |
| 255 |
b.Run("FormatPercent", func(b *testing.B) { |
| 256 |
for range b.N { |
| 257 |
tr.FormatPercent(75.5, 1) |
| 258 |
} |
| 259 |
}) |
| 260 |
b.Run("WeekdaysWide", func(b *testing.B) { |
| 261 |
for range b.N { |
| 262 |
tr.WeekdaysWide() |
| 263 |
} |
| 264 |
}) |
| 265 |
b.Run("MonthsWide", func(b *testing.B) { |
| 266 |
for range b.N { |
| 267 |
tr.MonthsWide() |
| 268 |
} |
| 269 |
}) |
| 270 |
} |