1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/intl/Calendar.h"
#include "mozilla/intl/DateTimeFormat.h"
#include "mozilla/Span.h"
#include "TestBuffer.h"
namespace mozilla::intl {
// Firefox 1.0 release date.
const double CALENDAR_DATE = 1032800850000.0;
TEST(IntlCalendar, GetLegacyKeywordValuesForLocale)
{
bool hasGregorian = false;
bool hasIslamic = false;
auto gregorian = MakeStringSpan("gregorian");
auto islamic = MakeStringSpan("islamic");
auto keywords = Calendar::GetLegacyKeywordValuesForLocale("en-US").unwrap();
for (auto name : keywords) {
// Check a few keywords, as this list may not be stable between ICU updates.
if (name.unwrap() == gregorian) {
hasGregorian = true;
}
if (name.unwrap() == islamic) {
hasIslamic = true;
}
}
ASSERT_TRUE(hasGregorian);
ASSERT_TRUE(hasIslamic);
}
TEST(IntlCalendar, GetBcp47KeywordValuesForLocale)
{
bool hasGregory = false;
bool hasIslamic = false;
auto gregory = MakeStringSpan("gregory");
auto islamic = MakeStringSpan("islamic");
auto keywords = Calendar::GetBcp47KeywordValuesForLocale("en-US").unwrap();
for (auto name : keywords) {
// Check a few keywords, as this list may not be stable between ICU updates.
if (name.unwrap() == gregory) {
hasGregory = true;
}
if (name.unwrap() == islamic) {
hasIslamic = true;
}
}
ASSERT_TRUE(hasGregory);
ASSERT_TRUE(hasIslamic);
}
TEST(IntlCalendar, GetBcp47KeywordValuesForLocaleCommonlyUsed)
{
bool hasGregory = false;
bool hasIslamic = false;
auto gregory = MakeStringSpan("gregory");
auto islamic = MakeStringSpan("islamic");
auto keywords = Calendar::GetBcp47KeywordValuesForLocale(
"en-US", Calendar::CommonlyUsed::Yes)
.unwrap();
for (auto name : keywords) {
// Check a few keywords, as this list may not be stable between ICU updates.
if (name.unwrap() == gregory) {
hasGregory = true;
}
if (name.unwrap() == islamic) {
hasIslamic = true;
}
}
ASSERT_TRUE(hasGregory);
ASSERT_FALSE(hasIslamic);
}
TEST(IntlCalendar, GetBcp47Type)
{
auto calendar =
Calendar::TryCreate("en-US", Some(MakeStringSpan(u"GMT+3"))).unwrap();
ASSERT_STREQ(calendar->GetBcp47Type().unwrap().data(), "gregory");
}
TEST(IntlCalendar, SetTimeInMs)
{
auto calendar =
Calendar::TryCreate("en-US", Some(MakeStringSpan(u"GMT+3"))).unwrap();
// There is no way to verify the results. Unwrap the results to ensure it
// doesn't fail, but don't check the values.
calendar->SetTimeInMs(CALENDAR_DATE).unwrap();
}
TEST(IntlCalendar, CloneFrom)
{
DateTimeFormat::StyleBag style;
style.date = Some(DateTimeFormat::Style::Medium);
style.time = Some(DateTimeFormat::Style::Medium);
auto dtFormat = DateTimeFormat::TryCreateFromStyle(
MakeStringSpan("en-US"), style,
// It's ok to pass nullptr here, as it will cause format
// operations to fail, but this test is only checking
// calendar cloning.
nullptr, Some(MakeStringSpan(u"America/Chicago")))
.unwrap();
dtFormat->CloneCalendar(CALENDAR_DATE).unwrap();
}
TEST(IntlCalendar, GetWeekend)
{
auto calendar_en_US = Calendar::TryCreate("en-US").unwrap();
auto weekend_en_US = calendar_en_US->GetWeekend().unwrap();
ASSERT_EQ(weekend_en_US, EnumSet({Weekday::Saturday, Weekday::Sunday}));
auto calendar_de_DE = Calendar::TryCreate("de-DE").unwrap();
auto weekend_de_DE = calendar_de_DE->GetWeekend().unwrap();
ASSERT_EQ(weekend_de_DE, EnumSet({Weekday::Saturday, Weekday::Sunday}));
auto calendar_ar_EG = Calendar::TryCreate("ar-EG").unwrap();
auto weekend_ar_EG = calendar_ar_EG->GetWeekend().unwrap();
ASSERT_EQ(weekend_ar_EG, EnumSet({Weekday::Friday, Weekday::Saturday}));
}
TEST(IntlCalendar, GetFirstDayOfWeek)
{
auto calendar_en_US = Calendar::TryCreate("en-US").unwrap();
auto firstDayOfWeek_en_US = calendar_en_US->GetFirstDayOfWeek();
ASSERT_EQ(firstDayOfWeek_en_US, Weekday::Sunday);
auto calendar_de_DE = Calendar::TryCreate("de-DE").unwrap();
auto firstDayOfWeek_de_DE = calendar_de_DE->GetFirstDayOfWeek();
ASSERT_EQ(firstDayOfWeek_de_DE, Weekday::Monday);
auto calendar_ar_EG = Calendar::TryCreate("ar-EG").unwrap();
auto firstDayOfWeek_ar_EG = calendar_ar_EG->GetFirstDayOfWeek();
ASSERT_EQ(firstDayOfWeek_ar_EG, Weekday::Saturday);
}
TEST(IntlCalendar, GetMinimalDaysInFirstWeek)
{
auto calendar_en_US = Calendar::TryCreate("en-US").unwrap();
auto minimalDays_en_US = calendar_en_US->GetMinimalDaysInFirstWeek();
ASSERT_EQ(minimalDays_en_US, 1);
auto calendar_de_DE = Calendar::TryCreate("de-DE").unwrap();
auto minimalDays_de_DE = calendar_de_DE->GetMinimalDaysInFirstWeek();
ASSERT_EQ(minimalDays_de_DE, 4);
auto calendar_ar_EG = Calendar::TryCreate("ar-EG").unwrap();
auto minimalDays_ar_EG = calendar_ar_EG->GetMinimalDaysInFirstWeek();
ASSERT_EQ(minimalDays_ar_EG, 1);
}
} // namespace mozilla::intl
|