1 /* 2 * Copyright 2014-2021 Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "LanguageTest.h" 8 9 #include "Language.h" 10 11 #include <cppunit/TestCaller.h> 12 #include <cppunit/TestSuite.h> 13 14 15 LanguageTest::LanguageTest() 16 { 17 } 18 19 20 LanguageTest::~LanguageTest() 21 { 22 } 23 24 25 void 26 LanguageTest::TestLanguageParseJapanese() 27 { 28 // GIVEN 29 30 // WHEN 31 BLanguage language("jp"); 32 33 // THEN 34 CPPUNIT_ASSERT_EQUAL(BString("jp"), language.ID()); 35 CPPUNIT_ASSERT_EQUAL(BString("jp"), language.Code()); 36 CPPUNIT_ASSERT_EQUAL(BString(""), language.ScriptCode()); 37 CPPUNIT_ASSERT_EQUAL(BString(""), language.CountryCode()); 38 } 39 40 41 void 42 LanguageTest::TestLanguageParseFrenchWithCountry() 43 { 44 // GIVEN 45 46 // WHEN 47 BLanguage language("fr_FR"); 48 49 // THEN 50 CPPUNIT_ASSERT_EQUAL(BString("fr_FR"), language.ID()); 51 CPPUNIT_ASSERT_EQUAL(BString("fr"), language.Code()); 52 CPPUNIT_ASSERT_EQUAL(BString(""), language.ScriptCode()); 53 CPPUNIT_ASSERT_EQUAL(BString("FR"), language.CountryCode()); 54 } 55 56 57 void 58 LanguageTest::TestLanguageParseSerbianScriptAndCountry() 59 { 60 // GIVEN 61 62 // WHEN 63 BLanguage language("sr_Latn_BA"); 64 65 // THEN 66 CPPUNIT_ASSERT_EQUAL(BString("sr_Latn_BA"), language.ID()); 67 CPPUNIT_ASSERT_EQUAL(BString("sr"), language.Code()); 68 CPPUNIT_ASSERT_EQUAL(BString("BA"), language.CountryCode()); 69 CPPUNIT_ASSERT_EQUAL(BString("Latn"), language.ScriptCode()); 70 } 71 72 73 void 74 LanguageTest::TestLanguageParseSerbianScriptAndCountryHyphens() 75 { 76 // GIVEN 77 78 // WHEN 79 BLanguage language("sr-Latn-BA"); 80 81 // THEN 82 CPPUNIT_ASSERT_EQUAL(BString("sr_Latn_BA"), language.ID()); 83 CPPUNIT_ASSERT_EQUAL(BString("sr"), language.Code()); 84 CPPUNIT_ASSERT_EQUAL(BString("BA"), language.CountryCode()); 85 CPPUNIT_ASSERT_EQUAL(BString("Latn"), language.ScriptCode()); 86 } 87 88 89 void 90 LanguageTest::TestLanguageNameFrenchInEnglish() 91 { 92 // GIVEN 93 BLanguage languageFrench("fr_FR"); 94 BLanguage languageEnglish("en_US"); 95 BString name; 96 97 // WHEN 98 languageFrench.GetName(name, &languageEnglish); 99 // get the name of French in English 100 101 // THEN 102 CPPUNIT_ASSERT_EQUAL(BString("French (France)"), name); 103 } 104 105 106 void 107 LanguageTest::TestLanguageNameFrenchInFrench() 108 { 109 // GIVEN 110 BLanguage languageFrench("fr_FR"); 111 BString name; 112 113 // WHEN 114 languageFrench.GetName(name, &languageFrench); 115 // get the name of French in French 116 117 // THEN 118 CPPUNIT_ASSERT_EQUAL(BString("français (France)"), name); 119 } 120 121 122 void 123 LanguageTest::TestLanguagePropertiesFrench() 124 { 125 // GIVEN 126 BLanguage language("fr_FR"); 127 128 // WHEN 129 130 // THEN 131 CPPUNIT_ASSERT_EQUAL(BString("fr"), language.Code()); 132 CPPUNIT_ASSERT(language.Direction() == B_LEFT_TO_RIGHT); 133 } 134 135 136 /*static*/ void 137 LanguageTest::AddTests(BTestSuite& parent) 138 { 139 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("LanguageTest"); 140 141 suite.addTest(new CppUnit::TestCaller<LanguageTest>( 142 "LanguageTest::TestLanguageParseJapanese", 143 &LanguageTest::TestLanguageParseJapanese)); 144 suite.addTest(new CppUnit::TestCaller<LanguageTest>( 145 "LanguageTest::TestLanguageParseFrenchWithCountry", 146 &LanguageTest::TestLanguageParseFrenchWithCountry)); 147 suite.addTest(new CppUnit::TestCaller<LanguageTest>( 148 "LanguageTest::TestLanguageParseSerbianScriptAndCountry", 149 &LanguageTest::TestLanguageParseSerbianScriptAndCountry)); 150 suite.addTest(new CppUnit::TestCaller<LanguageTest>( 151 "LanguageTest::TestLanguageParseSerbianScriptAndCountryHyphens", 152 &LanguageTest::TestLanguageParseSerbianScriptAndCountryHyphens)); 153 154 suite.addTest(new CppUnit::TestCaller<LanguageTest>( 155 "LanguageTest::TestLanguageNameFrenchInEnglish", 156 &LanguageTest::TestLanguageNameFrenchInEnglish)); 157 suite.addTest(new CppUnit::TestCaller<LanguageTest>( 158 "LanguageTest::TestLanguageNameFrenchInFrench", 159 &LanguageTest::TestLanguageNameFrenchInFrench)); 160 suite.addTest(new CppUnit::TestCaller<LanguageTest>( 161 "LanguageTest::TestLanguagePropertiesFrench", 162 &LanguageTest::TestLanguagePropertiesFrench)); 163 164 parent.addTest("LanguageTest", &suite); 165 } 166