1 /* 2 * Copyright 2024, Andrew Lindesay <apl@lindesay.co.nz>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "LocaleUtilsTest.h" 8 9 #include <String.h> 10 11 #include <cppunit/TestCaller.h> 12 #include <cppunit/TestSuite.h> 13 14 #include <string.h> 15 16 #include "LanguageRepository.h" 17 #include "LocaleUtils.h" 18 19 20 LocaleUtilsTest::LocaleUtilsTest() 21 { 22 } 23 24 25 LocaleUtilsTest::~LocaleUtilsTest() 26 { 27 } 28 29 30 void 31 LocaleUtilsTest::TestLanguageIsBeforeFalseAfter() 32 { 33 LanguageRef languageZh = LanguageRef(new Language("zh", "Chinese", true), true); 34 LanguageRef languageDeCh = LanguageRef(new Language("de_CH", "German (Swiss)", true), true); 35 36 // ---------------------- 37 bool actual = IsLanguageBefore(languageZh, languageDeCh); 38 // ---------------------- 39 40 CPPUNIT_ASSERT_EQUAL(false, actual); 41 } 42 43 44 void 45 LocaleUtilsTest::TestLanguageIsBeforeFalseEqual() 46 { 47 LanguageRef languageDeCh = LanguageRef(new Language("de_CH", "German (Swiss)", true), true); 48 49 // ---------------------- 50 bool actual = IsLanguageBefore(languageDeCh, languageDeCh); 51 // ---------------------- 52 53 CPPUNIT_ASSERT_EQUAL(false, actual); 54 } 55 56 57 void 58 LocaleUtilsTest::TestLanguageIsBeforeTrueBefore() 59 { 60 LanguageRef languageZh = LanguageRef(new Language("zh", "Chinese", true), true); 61 LanguageRef languageDeCh = LanguageRef(new Language("de_CH", "German (Swiss)", true), true); 62 63 // ---------------------- 64 bool actual = IsLanguageBefore(languageDeCh, languageZh); 65 // ---------------------- 66 67 CPPUNIT_ASSERT_EQUAL(true, actual); 68 } 69 70 71 void 72 LocaleUtilsTest::TestLanguageSorting() 73 { 74 LanguageRepository* repository = new LanguageRepository(); 75 76 LanguageRef languageZh = LanguageRef(new Language("zh", "Chinese", true), true); 77 LanguageRef languageDeCh = LanguageRef(new Language("de_CH", "German (Swiss)", true), true); 78 LanguageRef languageDe1996 = LanguageRef(new Language("de__1996", "German (1996)", true), true); 79 LanguageRef languageFr = LanguageRef(new Language("fr", "French", true), true); 80 LanguageRef languageEs = LanguageRef(new Language("es", "Spanish", true), true); 81 82 repository->AddLanguage(languageZh); 83 repository->AddLanguage(languageDeCh); 84 repository->AddLanguage(languageDe1996); 85 repository->AddLanguage(languageFr); 86 repository->AddLanguage(languageEs); 87 88 // ---------------------- 89 LanguageRef language0 = repository->LanguageAtIndex(0); 90 LanguageRef language1 = repository->LanguageAtIndex(1); 91 LanguageRef language2 = repository->LanguageAtIndex(2); 92 LanguageRef language3 = repository->LanguageAtIndex(3); 93 LanguageRef language4 = repository->LanguageAtIndex(4); 94 // ---------------------- 95 96 CPPUNIT_ASSERT_EQUAL(BString("de_CH"), BString(language0->ID())); 97 CPPUNIT_ASSERT_EQUAL(BString("de__1996"), BString(language1->ID())); 98 CPPUNIT_ASSERT_EQUAL(BString("es"), BString(language2->ID())); 99 CPPUNIT_ASSERT_EQUAL(BString("fr"), BString(language3->ID())); 100 CPPUNIT_ASSERT_EQUAL(BString("zh"), BString(language4->ID())); 101 102 CPPUNIT_ASSERT_EQUAL(0, language0->Compare(*languageDeCh.Get())); 103 CPPUNIT_ASSERT_EQUAL(0, language1->Compare(*languageDe1996.Get())); 104 CPPUNIT_ASSERT_EQUAL(0, language2->Compare(*languageEs.Get())); 105 CPPUNIT_ASSERT_EQUAL(0, language3->Compare(*languageFr.Get())); 106 CPPUNIT_ASSERT_EQUAL(0, language4->Compare(*languageZh.Get())); 107 108 delete repository; 109 } 110 111 112 void 113 LocaleUtilsTest::TestDeriveDefaultLanguageCodeOnly() 114 { 115 LanguageRepository* repository = new LanguageRepository(); 116 LocaleUtils::SetForcedSystemDefaultLanguageID("de"); 117 118 repository->AddLanguage(LanguageRef(new Language("zh", "Chinese", true), true)); 119 repository->AddLanguage(LanguageRef(new Language("de", "German", true), true)); 120 repository->AddLanguage(LanguageRef(new Language("fr", "French", true), true)); 121 122 // ---------------------- 123 LanguageRef defaultLanguage = LocaleUtils::DeriveDefaultLanguage(repository); 124 // ---------------------- 125 126 CPPUNIT_ASSERT_EQUAL(BString("de"), BString(defaultLanguage->Code())); 127 CPPUNIT_ASSERT(NULL == defaultLanguage->CountryCode()); 128 CPPUNIT_ASSERT(NULL == defaultLanguage->ScriptCode()); 129 130 LocaleUtils::SetForcedSystemDefaultLanguageID(""); 131 delete repository; 132 } 133 134 135 /*! This test has a more specific system default language selecting a less 136 specific supported language. 137 */ 138 void 139 LocaleUtilsTest::TestDeriveDefaultLanguageSystemDefaultMoreSpecific() 140 { 141 LanguageRepository* repository = new LanguageRepository(); 142 LocaleUtils::SetForcedSystemDefaultLanguageID("de_CH"); 143 144 repository->AddLanguage(LanguageRef(new Language("zh", "Chinese", true), true)); 145 repository->AddLanguage(LanguageRef(new Language("de", "German", true), true)); 146 repository->AddLanguage(LanguageRef(new Language("fr", "French", true), true)); 147 148 // ---------------------- 149 LanguageRef defaultLanguage = LocaleUtils::DeriveDefaultLanguage(repository); 150 // ---------------------- 151 152 CPPUNIT_ASSERT_EQUAL(BString("de"), BString(defaultLanguage->Code())); 153 CPPUNIT_ASSERT(NULL == defaultLanguage->CountryCode()); 154 CPPUNIT_ASSERT(NULL == defaultLanguage->ScriptCode()); 155 156 LocaleUtils::SetForcedSystemDefaultLanguageID(""); 157 delete repository; 158 } 159 160 161 /*! This test has a less specific system default language selecting a more 162 specific supported language. 163 */ 164 void 165 LocaleUtilsTest::TestDeriveDefaultLanguageSystemDefaultLessSpecific() 166 { 167 LanguageRepository* repository = new LanguageRepository(); 168 LocaleUtils::SetForcedSystemDefaultLanguageID("de"); 169 170 repository->AddLanguage(LanguageRef(new Language("zh", "Chinese", true), true)); 171 repository->AddLanguage(LanguageRef(new Language("de_CH", "German (Swiss)", true), true)); 172 repository->AddLanguage(LanguageRef(new Language("de__1996", "German (1996)", true), true)); 173 repository->AddLanguage(LanguageRef(new Language("fr", "French", true), true)); 174 repository->AddLanguage(LanguageRef(new Language("es", "Spanish", true), true)); 175 176 // ---------------------- 177 LanguageRef defaultLanguage = LocaleUtils::DeriveDefaultLanguage(repository); 178 // ---------------------- 179 180 CPPUNIT_ASSERT_EQUAL(BString("de"), BString(defaultLanguage->Code())); 181 CPPUNIT_ASSERT_EQUAL(BString("CH"), BString(defaultLanguage->CountryCode())); 182 CPPUNIT_ASSERT(NULL == defaultLanguage->ScriptCode()); 183 184 LocaleUtils::SetForcedSystemDefaultLanguageID(""); 185 delete repository; 186 } 187 188 189 /*static*/ void 190 LocaleUtilsTest::AddTests(BTestSuite& parent) 191 { 192 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("LocaleUtilsTest"); 193 194 suite.addTest( 195 new CppUnit::TestCaller<LocaleUtilsTest>("LocaleUtilsTest::TestLanguageIsBeforeFalseAfter", 196 &LocaleUtilsTest::TestLanguageIsBeforeFalseAfter)); 197 198 suite.addTest( 199 new CppUnit::TestCaller<LocaleUtilsTest>("LocaleUtilsTest::TestLanguageIsBeforeFalseEqual", 200 &LocaleUtilsTest::TestLanguageIsBeforeFalseEqual)); 201 202 suite.addTest( 203 new CppUnit::TestCaller<LocaleUtilsTest>("LocaleUtilsTest::TestLanguageIsBeforeTrueBefore", 204 &LocaleUtilsTest::TestLanguageIsBeforeTrueBefore)); 205 206 suite.addTest(new CppUnit::TestCaller<LocaleUtilsTest>("LocaleUtilsTest::TestLanguageSorting", 207 &LocaleUtilsTest::TestLanguageSorting)); 208 209 suite.addTest(new CppUnit::TestCaller<LocaleUtilsTest>( 210 "LocaleUtilsTest::TestDeriveDefaultLanguageCodeOnly", 211 &LocaleUtilsTest::TestDeriveDefaultLanguageCodeOnly)); 212 213 suite.addTest(new CppUnit::TestCaller<LocaleUtilsTest>( 214 "LocaleUtilsTest::TestDeriveDefaultLanguageSystemDefaultMoreSpecific", 215 &LocaleUtilsTest::TestDeriveDefaultLanguageSystemDefaultMoreSpecific)); 216 217 suite.addTest(new CppUnit::TestCaller<LocaleUtilsTest>( 218 "LocaleUtilsTest::TestDeriveDefaultLanguageSystemDefaultLessSpecific", 219 &LocaleUtilsTest::TestDeriveDefaultLanguageSystemDefaultLessSpecific)); 220 221 parent.addTest("LocaleUtilsTest", &suite); 222 } 223