1 /* 2 * Copyright 2014 Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "DateFormatTest.h" 8 9 #include <DateFormat.h> 10 #include <FormattingConventions.h> 11 #include <Language.h> 12 #include <TimeFormat.h> 13 14 #include <cppunit/TestCaller.h> 15 #include <cppunit/TestSuite.h> 16 17 18 DateFormatTest::DateFormatTest() 19 { 20 } 21 22 23 DateFormatTest::~DateFormatTest() 24 { 25 } 26 27 28 void 29 DateFormatTest::TestCustomFormat() 30 { 31 struct Test { 32 const char* language; 33 const char* formatting; 34 int32 fields; 35 36 BString expected; 37 BString force24; 38 BString force12; 39 }; 40 41 BString buffer; 42 43 const Test tests[] = { 44 { "en", "en_US", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE, 45 "10:21 PM", "22:21", "10:21 PM" }, 46 { "en", "en_US", 47 B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE | B_DATE_ELEMENT_SECOND, 48 "10:21:18 PM", "22:21:18", "10:21:18 PM" }, 49 // FIXME we need a way to set the timezone for this test to work 50 // reliably. 51 { "en", "en_US", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE 52 | B_DATE_ELEMENT_TIMEZONE, 53 "10:21 PM GMT+1", "22:21 GMT+1", "10:21 PM GMT+1" }, 54 { "en", "en_US", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE 55 | B_DATE_ELEMENT_SECOND | B_DATE_ELEMENT_TIMEZONE, 56 "10:21:18 PM GMT+1", "22:21:18 GMT+1", "10:21:18 PM GMT+1" }, 57 { "fr", "fr_FR", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE, 58 "22:21", "22:21", "10:21 PM" }, 59 { NULL } 60 }; 61 62 for (int i = 0; tests[i].language != NULL; i++) 63 { 64 NextSubTest(); 65 66 BLanguage language(tests[i].language); 67 BFormattingConventions formatting(tests[i].formatting); 68 status_t result; 69 70 // Test default for language/formatting 71 { 72 BDateTimeFormat format(language, formatting); 73 format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, 74 tests[i].fields); 75 76 result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT, 77 B_SHORT_TIME_FORMAT); 78 79 CPPUNIT_ASSERT_EQUAL(B_OK, result); 80 CPPUNIT_ASSERT_EQUAL(tests[i].expected, buffer); 81 } 82 83 // Test forced 24 hours 84 { 85 formatting.SetExplicitUse24HourClock(true); 86 BDateTimeFormat format(language, formatting); 87 format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, 88 tests[i].fields); 89 result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT, 90 B_SHORT_TIME_FORMAT); 91 92 CPPUNIT_ASSERT_EQUAL(B_OK, result); 93 CPPUNIT_ASSERT_EQUAL(tests[i].force24, buffer); 94 } 95 96 // Test forced 12 hours 97 { 98 formatting.SetExplicitUse24HourClock(false); 99 BDateTimeFormat format(language, formatting); 100 format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, 101 tests[i].fields); 102 result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT, 103 B_SHORT_TIME_FORMAT); 104 105 CPPUNIT_ASSERT_EQUAL(B_OK, result); 106 CPPUNIT_ASSERT_EQUAL(tests[i].force12, buffer); 107 } 108 } 109 } 110 111 112 void 113 DateFormatTest::TestFormat() 114 { 115 struct Value { 116 const char* language; 117 const char* convention; 118 time_t time; 119 const char* shortDate; 120 const char* longDate; 121 const char* shortTime; 122 const char* longTime; 123 const char* shortDateTime; 124 }; 125 126 static const Value values[] = { 127 {"en", "en_US", 12345, "1/1/70", "January 1, 1970", 128 "4:25 AM", "4:25:45 AM", "1/1/70, 4:25 AM"}, 129 {"fr", "fr_FR", 12345, "01/01/1970", "1 janvier 1970", 130 "04:25", "04:25:45", "01/01/1970 04:25"}, 131 {"fr", "fr_FR", 12345678, "23/05/1970", "23 mai 1970", 132 "22:21", "22:21:18", "23/05/1970 22:21"}, 133 {NULL} 134 }; 135 136 BString output; 137 status_t result; 138 139 for (int i = 0; values[i].language != NULL; i++) { 140 NextSubTest(); 141 142 BLanguage language(values[i].language); 143 BFormattingConventions formatting(values[i].convention); 144 BDateFormat dateFormat(language, formatting); 145 BTimeFormat timeFormat(language, formatting); 146 BDateTimeFormat dateTimeFormat(language, formatting); 147 148 result = dateFormat.Format(output, values[i].time, B_SHORT_DATE_FORMAT); 149 CPPUNIT_ASSERT_EQUAL(B_OK, result); 150 CPPUNIT_ASSERT_EQUAL(BString(values[i].shortDate), output); 151 152 result = dateFormat.Format(output, values[i].time, B_LONG_DATE_FORMAT); 153 CPPUNIT_ASSERT_EQUAL(B_OK, result); 154 CPPUNIT_ASSERT_EQUAL(BString(values[i].longDate), output); 155 156 result = timeFormat.Format(output, values[i].time, B_SHORT_TIME_FORMAT); 157 CPPUNIT_ASSERT_EQUAL(B_OK, result); 158 CPPUNIT_ASSERT_EQUAL(BString(values[i].shortTime), output); 159 160 result = timeFormat.Format(output, values[i].time, B_MEDIUM_TIME_FORMAT); 161 CPPUNIT_ASSERT_EQUAL(B_OK, result); 162 CPPUNIT_ASSERT_EQUAL(BString(values[i].longTime), output); 163 164 result = dateTimeFormat.Format(output, values[i].time, 165 B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT); 166 CPPUNIT_ASSERT_EQUAL(B_OK, result); 167 CPPUNIT_ASSERT_EQUAL(BString(values[i].shortDateTime), output); 168 } 169 } 170 171 172 void 173 DateFormatTest::TestFormatDate() 174 { 175 BLanguage language("en"); 176 BFormattingConventions formatting("en_US"); 177 BDateFormat format(language, formatting); 178 179 BString output; 180 status_t result; 181 182 BDate date(2014, 9, 29); 183 184 result = format.Format(output, date, B_LONG_DATE_FORMAT); 185 CPPUNIT_ASSERT_EQUAL(B_OK, result); 186 CPPUNIT_ASSERT_EQUAL(BString("September 29, 2014"), output); 187 188 // Test an invalid date - must return B_BAD_DATA 189 date.SetDate(2014, 29, 29); 190 result = format.Format(output, date, B_LONG_DATE_FORMAT); 191 CPPUNIT_ASSERT_EQUAL(B_BAD_DATA, result); 192 } 193 194 195 void 196 DateFormatTest::TestMonthNames() 197 { 198 BLanguage language("en"); 199 BFormattingConventions formatting("en_US"); 200 BDateFormat format(language, formatting); 201 202 BString buffer; 203 status_t result = format.GetMonthName(1, buffer); 204 205 CPPUNIT_ASSERT_EQUAL(BString("January"), buffer); 206 CPPUNIT_ASSERT_EQUAL(B_OK, result); 207 208 buffer.Truncate(0); 209 result = format.GetMonthName(12, buffer); 210 211 CPPUNIT_ASSERT_EQUAL(BString("December"), buffer); 212 CPPUNIT_ASSERT_EQUAL(B_OK, result); 213 } 214 215 216 std::ostream& operator<<(std::ostream& stream, const BDate& date) 217 { 218 stream << date.Year(); 219 stream << '-'; 220 stream << date.Month(); 221 stream << '-'; 222 stream << date.Day(); 223 224 return stream; 225 } 226 227 228 std::ostream& operator<<(std::ostream& stream, const BTime& date) 229 { 230 stream << date.Hour(); 231 stream << ':'; 232 stream << date.Minute(); 233 stream << ':'; 234 stream << date.Second(); 235 236 return stream; 237 } 238 239 240 void 241 DateFormatTest::TestParseDate() 242 { 243 BLanguage language("en"); 244 BFormattingConventions formatting("en_US"); 245 BDateFormat format(language, formatting); 246 BDate date; 247 status_t result; 248 249 struct Test { 250 const char* input; 251 BDate output; 252 }; 253 254 static const Test tests[] = { 255 {"01/01/1970", BDate(1970, 1, 1)}, 256 {"05/07/1988", BDate(1988, 5, 7)}, 257 {"07/31/2345", BDate(2345, 7, 31)}, 258 {NULL} 259 }; 260 261 for (int i = 0; tests[i].input != NULL; i++) { 262 NextSubTest(); 263 result = format.Parse(tests[i].input, B_SHORT_DATE_FORMAT, date); 264 265 CPPUNIT_ASSERT_EQUAL(tests[i].output, date); 266 CPPUNIT_ASSERT_EQUAL(B_OK, result); 267 } 268 } 269 270 271 void 272 DateFormatTest::TestParseTime() 273 { 274 BLanguage language("fr"); 275 BFormattingConventions formatting("fr_FR"); 276 BTimeFormat format(language, formatting); 277 BTime date; 278 status_t result; 279 280 struct Test { 281 const char* input; 282 BTime output; 283 }; 284 285 static const Test tests[] = { 286 {"03:25", BTime(3, 25, 0)}, 287 {"16:18", BTime(16, 18, 0)}, 288 {"23:59", BTime(23, 59, 0)}, 289 {NULL} 290 }; 291 292 for (int i = 0; tests[i].input != NULL; i++) { 293 NextSubTest(); 294 result = format.Parse(tests[i].input, B_SHORT_TIME_FORMAT, date); 295 296 CPPUNIT_ASSERT_EQUAL(tests[i].output, date); 297 CPPUNIT_ASSERT_EQUAL(B_OK, result); 298 } 299 } 300 301 302 /*static*/ void 303 DateFormatTest::AddTests(BTestSuite& parent) 304 { 305 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("DateFormatTest"); 306 307 suite.addTest(new CppUnit::TestCaller<DateFormatTest>( 308 "DateFormatTest::TestCustomFormat", &DateFormatTest::TestCustomFormat)); 309 suite.addTest(new CppUnit::TestCaller<DateFormatTest>( 310 "DateFormatTest::TestFormat", &DateFormatTest::TestFormat)); 311 suite.addTest(new CppUnit::TestCaller<DateFormatTest>( 312 "DateFormatTest::TestFormatDate", &DateFormatTest::TestFormatDate)); 313 suite.addTest(new CppUnit::TestCaller<DateFormatTest>( 314 "DateFormatTest::TestMonthNames", &DateFormatTest::TestMonthNames)); 315 suite.addTest(new CppUnit::TestCaller<DateFormatTest>( 316 "DateFormatTest::TestParseDate", &DateFormatTest::TestParseDate)); 317 suite.addTest(new CppUnit::TestCaller<DateFormatTest>( 318 "DateFormatTest::TestParseTime", &DateFormatTest::TestParseTime)); 319 320 parent.addTest("DateFormatTest", &suite); 321 } 322