From cedd1e158b898956252bfb746bff6bc072e7e8b9 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Wed, 1 Oct 2014 15:47:54 +0200 Subject: [PATCH] Add tests for BDurationFormat and BTimeUnitFormat. --- src/tests/kits/locale/DurationFormatTest.cpp | 99 ++++++++++++++++++++ src/tests/kits/locale/DurationFormatTest.h | 25 +++++ src/tests/kits/locale/Jamfile | 1 + src/tests/kits/locale/LocaleKitTestAddon.cpp | 2 + 4 files changed, 127 insertions(+) create mode 100644 src/tests/kits/locale/DurationFormatTest.cpp create mode 100644 src/tests/kits/locale/DurationFormatTest.h diff --git a/src/tests/kits/locale/DurationFormatTest.cpp b/src/tests/kits/locale/DurationFormatTest.cpp new file mode 100644 index 0000000000..b7f283f3c3 --- /dev/null +++ b/src/tests/kits/locale/DurationFormatTest.cpp @@ -0,0 +1,99 @@ +/* + * Copyright 2014 Haiku, Inc. + * Distributed under the terms of the MIT License. + */ + + +#include "DurationFormatTest.h" + +#include +#include + +#include +#include + + +DurationFormatTest::DurationFormatTest() +{ +} + + +DurationFormatTest::~DurationFormatTest() +{ +} + + +void +DurationFormatTest::TestDuration() +{ + BDurationFormat format; + BString buffer; + BString expected; + + BFormattingConventions englishFormat("en_US"); + BLanguage englishLanguage("en"); + + BFormattingConventions frenchFormat("fr_FR"); + BLanguage frenchLanguage("fr"); + + format.SetFormattingConventions(englishFormat); + format.SetLanguage(englishLanguage); + status_t result = format.Format(0, 800000000000ll, &buffer); + + expected << "1 week, 2 days, 6 hours, 13 minutes, 20 seconds"; + CPPUNIT_ASSERT_EQUAL(B_OK, result); + CPPUNIT_ASSERT_EQUAL(expected, buffer); + + format.SetFormattingConventions(frenchFormat); + format.SetLanguage(frenchLanguage); + result = format.Format(0, 800000000000ll, &buffer); + + // We check that the passed BString is not truncated. + expected << "1 semaine, 2 jours, 6 heures, 13 minutes, 20 secondes"; + CPPUNIT_ASSERT_EQUAL(B_OK, result); + CPPUNIT_ASSERT_EQUAL(expected, buffer); +} + + +void +DurationFormatTest::TestTimeUnit() +{ + BTimeUnitFormat format; + BString buffer; + + BFormattingConventions englishFormat("en_US"); + BLanguage englishLanguage("en"); + + BFormattingConventions frenchFormat("fr_FR"); + BLanguage frenchLanguage("fr"); + + format.SetFormattingConventions(englishFormat); + format.SetLanguage(englishLanguage); + status_t result = format.Format(5, B_TIME_UNIT_HOUR, &buffer); + + CPPUNIT_ASSERT_EQUAL(B_OK, result); + CPPUNIT_ASSERT_EQUAL(BString("5 hours"), buffer); + + format.SetFormattingConventions(frenchFormat); + format.SetLanguage(frenchLanguage); + result = format.Format(5, B_TIME_UNIT_HOUR, &buffer); + + CPPUNIT_ASSERT_EQUAL(B_OK, result); + // We check that the passed BString is not truncated. This makes it easy + // to append several units to the same string, as BDurationFormat does. + CPPUNIT_ASSERT_EQUAL(BString("5 hours5 heures"), buffer); +} + + +/*static*/ void +DurationFormatTest::AddTests(BTestSuite& parent) +{ + CppUnit::TestSuite& suite = *new CppUnit::TestSuite("DurationFormatTest"); + + suite.addTest(new CppUnit::TestCaller( + "DurationFormatTest::TestDuration", &DurationFormatTest::TestDuration)); + suite.addTest(new CppUnit::TestCaller( + "DurationFormatTest::TestTimeUnit", &DurationFormatTest::TestTimeUnit)); + + parent.addTest("DurationFormatTest", &suite); +} diff --git a/src/tests/kits/locale/DurationFormatTest.h b/src/tests/kits/locale/DurationFormatTest.h new file mode 100644 index 0000000000..80683dc227 --- /dev/null +++ b/src/tests/kits/locale/DurationFormatTest.h @@ -0,0 +1,25 @@ +/* + * Copyright 2014 Haiku, Inc. + * Distributed under the terms of the MIT License. + */ +#ifndef DURATION_FORMAT_TEST_H +#define DURATION_FORMAT_TEST_H + + +#include +#include + + +class DurationFormatTest: public BTestCase { +public: + DurationFormatTest(); + virtual ~DurationFormatTest(); + + void TestDuration(); + void TestTimeUnit(); + + static void AddTests(BTestSuite& suite); +}; + + +#endif diff --git a/src/tests/kits/locale/Jamfile b/src/tests/kits/locale/Jamfile index 5e9af753a4..0de09ffb58 100644 --- a/src/tests/kits/locale/Jamfile +++ b/src/tests/kits/locale/Jamfile @@ -51,6 +51,7 @@ UnitTestLib localekittest.so : CollatorTest.cpp DateFormatTest.cpp + DurationFormatTest.cpp LanguageTest.cpp UnicodeCharTest.cpp diff --git a/src/tests/kits/locale/LocaleKitTestAddon.cpp b/src/tests/kits/locale/LocaleKitTestAddon.cpp index 93eb87f0a4..460f32f725 100644 --- a/src/tests/kits/locale/LocaleKitTestAddon.cpp +++ b/src/tests/kits/locale/LocaleKitTestAddon.cpp @@ -9,6 +9,7 @@ #include "CollatorTest.h" #include "DateFormatTest.h" +#include "DurationFormatTest.h" #include "LanguageTest.h" #include "UnicodeCharTest.h" @@ -20,6 +21,7 @@ getTestSuite() CollatorTest::AddTests(*suite); DateFormatTest::AddTests(*suite); + DurationFormatTest::AddTests(*suite); LanguageTest::AddTests(*suite); UnicodeCharTest::AddTests(*suite);