| 1 |
// Copyright 2026 Bjørn Erik Pedersen |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package internal |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
// TestMergeIntoPartialMonths verifies that mergeInto fills empty slots in a |
| 11 |
// partially-populated array from the parent, without overwriting non-empty |
| 12 |
// child values. |
| 13 |
func TestMergeIntoPartialMonths(t *testing.T) { |
| 14 |
parent := LocaleConfig{ |
| 15 |
MonthsConfig: MonthsConfig{ |
| 16 |
MonthsWide: Months{ |
| 17 |
"janeiro", "fevereiro", "março", "abril", |
| 18 |
"maio", "junho", "julho", "agosto", |
| 19 |
"setembro", "outubro", "novembro", "dezembro", |
| 20 |
}, |
| 21 |
MonthsAbbreviated: Months{ |
| 22 |
"jan.", "fev.", "mar.", "abr.", |
| 23 |
"mai.", "jun.", "jul.", "ago.", |
| 24 |
"set.", "out.", "nov.", "dez.", |
| 25 |
}, |
| 26 |
}, |
| 27 |
WeekDaysConfig: WeekDaysConfig{ |
| 28 |
WeekDaysWide: Days{ |
| 29 |
"domingo", "segunda-feira", "terça-feira", "quarta-feira", |
| 30 |
"quinta-feira", "sexta-feira", "sábado", |
| 31 |
}, |
| 32 |
}, |
| 33 |
} |
| 34 |
|
| 35 |
// Child (e.g. pt-BR) overrides only the first four wide month names |
| 36 |
// and provides no abbreviated names. |
| 37 |
child := LocaleConfig{ |
| 38 |
MonthsConfig: MonthsConfig{ |
| 39 |
MonthsWide: Months{ |
| 40 |
"janeiro (BR)", "fevereiro (BR)", "março (BR)", "abril (BR)", |
| 41 |
"", "", "", "", |
| 42 |
"", "", "", "", |
| 43 |
}, |
| 44 |
}, |
| 45 |
} |
| 46 |
|
| 47 |
mergeInto(&child, &parent) |
| 48 |
|
| 49 |
// First four wide months are from the child. |
| 50 |
for i := 0; i < 4; i++ { |
| 51 |
if child.MonthsWide[i] == parent.MonthsWide[i] { |
| 52 |
t.Errorf("MonthsWide[%d]: expected child override %q, got parent value %q", |
| 53 |
i, child.MonthsWide[i], parent.MonthsWide[i]) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// Remaining eight wide months must come from the parent, not stay empty. |
| 58 |
for i := 4; i < 12; i++ { |
| 59 |
if child.MonthsWide[i] == "" { |
| 60 |
t.Errorf("MonthsWide[%d]: expected parent value %q, got empty string", |
| 61 |
i, parent.MonthsWide[i]) |
| 62 |
} |
| 63 |
if child.MonthsWide[i] != parent.MonthsWide[i] { |
| 64 |
t.Errorf("MonthsWide[%d]: expected parent value %q, got %q", |
| 65 |
i, parent.MonthsWide[i], child.MonthsWide[i]) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// Abbreviated months were absent in the child, so all are inherited from the parent. |
| 70 |
for i := 0; i < 12; i++ { |
| 71 |
if child.MonthsAbbreviated[i] != parent.MonthsAbbreviated[i] { |
| 72 |
t.Errorf("MonthsAbbreviated[%d]: expected %q from parent, got %q", |
| 73 |
i, parent.MonthsAbbreviated[i], child.MonthsAbbreviated[i]) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Weekdays were absent in the child, so all are inherited from the parent. |
| 78 |
for i := 0; i < 7; i++ { |
| 79 |
if child.WeekDaysWide[i] != parent.WeekDaysWide[i] { |
| 80 |
t.Errorf("WeekDaysWide[%d]: expected %q from parent, got %q", |
| 81 |
i, parent.WeekDaysWide[i], child.WeekDaysWide[i]) |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// TestMergeIntoChildPreserved verifies that non-empty child values are never |
| 87 |
// overwritten by the parent. |
| 88 |
func TestMergeIntoChildPreserved(t *testing.T) { |
| 89 |
parent := LocaleConfig{ |
| 90 |
MonthsConfig: MonthsConfig{ |
| 91 |
MonthsWide: Months{ |
| 92 |
"January", "February", "March", "April", |
| 93 |
"May", "June", "July", "August", |
| 94 |
"September", "October", "November", "December", |
| 95 |
}, |
| 96 |
}, |
| 97 |
NumberConfig: NumberConfig{ |
| 98 |
Decimal: ".", |
| 99 |
Group: ",", |
| 100 |
}, |
| 101 |
} |
| 102 |
|
| 103 |
child := LocaleConfig{ |
| 104 |
MonthsConfig: MonthsConfig{ |
| 105 |
MonthsWide: Months{ |
| 106 |
"Jan-child", "Feb-child", "March", "April", |
| 107 |
"May", "June", "July", "August", |
| 108 |
"September", "October", "November", "December", |
| 109 |
}, |
| 110 |
}, |
| 111 |
NumberConfig: NumberConfig{ |
| 112 |
Decimal: ",", |
| 113 |
Group: " ", |
| 114 |
}, |
| 115 |
} |
| 116 |
|
| 117 |
mergeInto(&child, &parent) |
| 118 |
|
| 119 |
if child.MonthsWide[0] != "Jan-child" { |
| 120 |
t.Errorf("MonthsWide[0]: child value should be preserved, got %q", child.MonthsWide[0]) |
| 121 |
} |
| 122 |
if child.MonthsWide[1] != "Feb-child" { |
| 123 |
t.Errorf("MonthsWide[1]: child value should be preserved, got %q", child.MonthsWide[1]) |
| 124 |
} |
| 125 |
if child.Decimal != "," { |
| 126 |
t.Errorf("Decimal: child value should be preserved, got %q", child.Decimal) |
| 127 |
} |
| 128 |
if child.Group != " " { |
| 129 |
t.Errorf("Group: child value should be preserved, got %q", child.Group) |
| 130 |
} |
| 131 |
} |