|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package mpr |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 9 | +) |
| 10 | + |
| 11 | +// TestParseLanguageSettings_Languages verifies that Languages array items stored |
| 12 | +// as primitive.D (the BSON decoded type) are correctly parsed via extractBsonMap. |
| 13 | +// This is the fix for issue #480: bare .(map[string]any) assertions always fail |
| 14 | +// on primitive.D values, so extractBsonMap must be used instead. |
| 15 | +func TestParseLanguageSettings_Languages(t *testing.T) { |
| 16 | + raw := map[string]any{ |
| 17 | + "$ID": "settings-lang-1", |
| 18 | + "$Type": "Settings$LanguageSettings", |
| 19 | + "DefaultLanguageCode": "en_US", |
| 20 | + "Languages": primitive.A{ |
| 21 | + int32(2), |
| 22 | + primitive.D{ |
| 23 | + {Key: "$ID", Value: "lang-1"}, |
| 24 | + {Key: "$Type", Value: "Texts$Language"}, |
| 25 | + {Key: "Code", Value: "en_US"}, |
| 26 | + {Key: "CheckCompleteness", Value: true}, |
| 27 | + {Key: "CustomDateFormat", Value: "MM/dd/yyyy"}, |
| 28 | + {Key: "CustomDateTimeFormat", Value: "MM/dd/yyyy HH:mm"}, |
| 29 | + {Key: "CustomTimeFormat", Value: "HH:mm"}, |
| 30 | + }, |
| 31 | + primitive.D{ |
| 32 | + {Key: "$ID", Value: "lang-2"}, |
| 33 | + {Key: "$Type", Value: "Texts$Language"}, |
| 34 | + {Key: "Code", Value: "fr_FR"}, |
| 35 | + {Key: "CheckCompleteness", Value: false}, |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + ls := parseLanguageSettings(raw) |
| 41 | + |
| 42 | + if ls.DefaultLanguageCode != "en_US" { |
| 43 | + t.Errorf("DefaultLanguageCode = %q, want %q", ls.DefaultLanguageCode, "en_US") |
| 44 | + } |
| 45 | + if len(ls.Languages) != 2 { |
| 46 | + t.Fatalf("len(Languages) = %d, want 2", len(ls.Languages)) |
| 47 | + } |
| 48 | + |
| 49 | + en := ls.Languages[0] |
| 50 | + if en.Code != "en_US" { |
| 51 | + t.Errorf("Languages[0].Code = %q, want %q", en.Code, "en_US") |
| 52 | + } |
| 53 | + if !en.CheckCompleteness { |
| 54 | + t.Errorf("Languages[0].CheckCompleteness = false, want true") |
| 55 | + } |
| 56 | + if en.CustomDateFormat != "MM/dd/yyyy" { |
| 57 | + t.Errorf("Languages[0].CustomDateFormat = %q, want %q", en.CustomDateFormat, "MM/dd/yyyy") |
| 58 | + } |
| 59 | + if en.CustomDateTimeFormat != "MM/dd/yyyy HH:mm" { |
| 60 | + t.Errorf("Languages[0].CustomDateTimeFormat = %q, want %q", en.CustomDateTimeFormat, "MM/dd/yyyy HH:mm") |
| 61 | + } |
| 62 | + if en.CustomTimeFormat != "HH:mm" { |
| 63 | + t.Errorf("Languages[0].CustomTimeFormat = %q, want %q", en.CustomTimeFormat, "HH:mm") |
| 64 | + } |
| 65 | + |
| 66 | + fr := ls.Languages[1] |
| 67 | + if fr.Code != "fr_FR" { |
| 68 | + t.Errorf("Languages[1].Code = %q, want %q", fr.Code, "fr_FR") |
| 69 | + } |
| 70 | + if fr.CheckCompleteness { |
| 71 | + t.Errorf("Languages[1].CheckCompleteness = true, want false") |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// TestParseLanguageSettings_EmptyLanguages verifies that an absent or empty |
| 76 | +// Languages array results in a nil/empty slice without panicking. |
| 77 | +func TestParseLanguageSettings_EmptyLanguages(t *testing.T) { |
| 78 | + raw := map[string]any{ |
| 79 | + "$ID": "settings-lang-2", |
| 80 | + "$Type": "Settings$LanguageSettings", |
| 81 | + "DefaultLanguageCode": "en_US", |
| 82 | + "Languages": primitive.A{int32(2)}, |
| 83 | + } |
| 84 | + |
| 85 | + ls := parseLanguageSettings(raw) |
| 86 | + if len(ls.Languages) != 0 { |
| 87 | + t.Errorf("len(Languages) = %d, want 0", len(ls.Languages)) |
| 88 | + } |
| 89 | +} |
0 commit comments