diff --git a/src/tests/kits/locale/DateFormatTest.cpp b/src/tests/kits/locale/DateFormatTest.cpp new file mode 100644 index 0000000000..2cc1d4149f --- /dev/null +++ b/src/tests/kits/locale/DateFormatTest.cpp @@ -0,0 +1,100 @@ +/* + * Copyright 2014 Haiku, Inc. + * Distributed under the terms of the MIT License. + */ + + +#include "DateFormatTest.h" + +#include +#include +#include + +#include +#include + + +DateFormatTest::DateFormatTest() +{ +} + + +DateFormatTest::~DateFormatTest() +{ +} + + +void +DateFormatTest::TestFormat() +{ + struct Value { + char* language; + char* convention; + time_t time; + char* shortDate; + char* longDate; + }; + + static const Value values[] = { + {"en", "en_US", 12345, "1/1/70", "January 1, 1970"}, + {"fr", "fr_FR", 12345, "01/01/70", "1 janvier 1970"}, + {"fr", "fr_FR", 12345678, "23/05/70", "23 mai 1970"}, + {NULL} + }; + + BString output; + status_t result; + + for (int i = 0; values[i].language != NULL; i++) { + NextSubTest(); + + BLanguage language(values[i].language); + BFormattingConventions formatting(values[i].convention); + BDateFormat format(&language, &formatting); + + result = format.Format(output, values[i].time, B_SHORT_DATE_FORMAT); + CPPUNIT_ASSERT_EQUAL(B_OK, result); + CPPUNIT_ASSERT_EQUAL(BString(values[i].shortDate), output); + + result = format.Format(output, values[i].time, B_LONG_DATE_FORMAT); + CPPUNIT_ASSERT_EQUAL(B_OK, result); + CPPUNIT_ASSERT_EQUAL(BString(values[i].longDate), output); + } +} + + +void +DateFormatTest::TestFormatDate() +{ + BLanguage language("en"); + BFormattingConventions formatting("en_US"); + BDateFormat format(&language, &formatting); + + BString output; + status_t result; + + BDate date(2014, 9, 29); + + result = format.Format(output, date, B_LONG_DATE_FORMAT); + CPPUNIT_ASSERT_EQUAL(B_OK, result); + CPPUNIT_ASSERT_EQUAL(BString("September 29, 2014"), output); + + // Test an invalid date - must return B_BAD_DATA + date.SetDate(2014, 29, 29); + result = format.Format(output, date, B_LONG_DATE_FORMAT); + CPPUNIT_ASSERT_EQUAL(B_BAD_DATA, result); +} + + +/*static*/ void +DateFormatTest::AddTests(BTestSuite& parent) +{ + CppUnit::TestSuite& suite = *new CppUnit::TestSuite("DateFormatTest"); + + suite.addTest(new CppUnit::TestCaller( + "DateFormatTest::TestFormat", &DateFormatTest::TestFormat)); + suite.addTest(new CppUnit::TestCaller( + "DateFormatTest::TestFormatDate", &DateFormatTest::TestFormatDate)); + + parent.addTest("DateFormatTest", &suite); +} diff --git a/src/tests/kits/locale/DateFormatTest.h b/src/tests/kits/locale/DateFormatTest.h new file mode 100644 index 0000000000..d3a34c3310 --- /dev/null +++ b/src/tests/kits/locale/DateFormatTest.h @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Haiku, Inc. + * Distributed under the terms of the MIT License. + */ +#ifndef DATE_FORMAT_TEST_H +#define DATE_FORMAT_TEST_H + + +#include +#include + + +class DateFormatTest: public BTestCase { +public: + DateFormatTest(); + virtual ~DateFormatTest(); + + void TestFormat(); + void TestFormatDate(); + + static void AddTests(BTestSuite& suite); +}; + + +#endif diff --git a/src/tests/kits/locale/Jamfile b/src/tests/kits/locale/Jamfile index 8caf5c05d5..5e9af753a4 100644 --- a/src/tests/kits/locale/Jamfile +++ b/src/tests/kits/locale/Jamfile @@ -50,6 +50,7 @@ UnitTestLib localekittest.so : LocaleKitTestAddon.cpp CollatorTest.cpp + DateFormatTest.cpp LanguageTest.cpp UnicodeCharTest.cpp diff --git a/src/tests/kits/locale/LocaleKitTestAddon.cpp b/src/tests/kits/locale/LocaleKitTestAddon.cpp index 0877206a2d..93eb87f0a4 100644 --- a/src/tests/kits/locale/LocaleKitTestAddon.cpp +++ b/src/tests/kits/locale/LocaleKitTestAddon.cpp @@ -8,6 +8,7 @@ #include #include "CollatorTest.h" +#include "DateFormatTest.h" #include "LanguageTest.h" #include "UnicodeCharTest.h" @@ -18,6 +19,7 @@ getTestSuite() BTestSuite* suite = new BTestSuite("LocaleKit"); CollatorTest::AddTests(*suite); + DateFormatTest::AddTests(*suite); LanguageTest::AddTests(*suite); UnicodeCharTest::AddTests(*suite);