xref: /haiku/src/tests/kits/locale/DurationFormatTest.cpp (revision 4e3137c085bae361922078f123dceb92da700640)
1 /*
2  * Copyright 2014 Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "DurationFormatTest.h"
8 
9 #include <DurationFormat.h>
10 #include <Locale.h>
11 
12 #include <cppunit/TestCaller.h>
13 #include <cppunit/TestSuite.h>
14 
15 
16 DurationFormatTest::DurationFormatTest()
17 {
18 }
19 
20 
21 DurationFormatTest::~DurationFormatTest()
22 {
23 }
24 
25 
26 void
27 DurationFormatTest::TestDefault()
28 {
29 	BDurationFormat format;
30 	BString buffer;
31 	BString expected;
32 
33 	status_t result = format.Format(buffer, 0, 800000000000ll);
34 	CPPUNIT_ASSERT_EQUAL(B_OK, result);
35 	// The exact format and language used depends on the locale settings, but
36 	// we can assume that whatever they are, it should put something in the
37 	// string.
38 	CPPUNIT_ASSERT(buffer.Length() > 0);
39 }
40 
41 
42 void
43 DurationFormatTest::TestDuration()
44 {
45 	BString buffer;
46 	BString expected;
47 	status_t result;
48 
49 	BFormattingConventions englishFormat("en_US");
50 	BLanguage englishLanguage("en");
51 
52 	BFormattingConventions frenchFormat("fr_FR");
53 	BLanguage frenchLanguage("fr");
54 
55 	{
56 		BDurationFormat format(englishLanguage, englishFormat);
57 		status_t result = format.Format(buffer, 0, 800000000000ll);
58 
59 		expected << "1 week, 2 days, 6 hours, 13 minutes, 20 seconds";
60 		CPPUNIT_ASSERT_EQUAL(B_OK, result);
61 		CPPUNIT_ASSERT_EQUAL(expected, buffer);
62 	}
63 
64 	{
65 		BDurationFormat format(frenchLanguage, frenchFormat);
66 		result = format.Format(buffer, 0, 800000000000ll);
67 
68 		// We check that the passed BString is not truncated.
69 		expected << "1 semaine, 2 jours, 6 heures, 13 minutes, 20 secondes";
70 		CPPUNIT_ASSERT_EQUAL(B_OK, result);
71 		CPPUNIT_ASSERT_EQUAL(expected, buffer);
72 	}
73 }
74 
75 
76 void
77 DurationFormatTest::TestTimeUnit()
78 {
79 	BString buffer;
80 	status_t result;
81 
82 	BFormattingConventions englishFormat("en_US");
83 	BLanguage englishLanguage("en");
84 
85 	BFormattingConventions frenchFormat("fr_FR");
86 	BLanguage frenchLanguage("fr");
87 
88 	{
89 		BTimeUnitFormat format(englishLanguage, englishFormat);
90 		result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
91 
92 		CPPUNIT_ASSERT_EQUAL(B_OK, result);
93 		CPPUNIT_ASSERT_EQUAL(BString("5 hours"), buffer);
94 	}
95 
96 	{
97 		BTimeUnitFormat format(frenchLanguage, frenchFormat);
98 		result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
99 
100 		CPPUNIT_ASSERT_EQUAL(B_OK, result);
101 		// We check that the passed BString is not truncated. This makes it easy
102 		// to append several units to the same string, as BDurationFormat does.
103 		CPPUNIT_ASSERT_EQUAL(BString("5 hours5 heures"), buffer);
104 	}
105 }
106 
107 
108 /*static*/ void
109 DurationFormatTest::AddTests(BTestSuite& parent)
110 {
111 	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("DurationFormatTest");
112 
113 	suite.addTest(new CppUnit::TestCaller<DurationFormatTest>(
114 		"DurationFormatTest::TestDefault", &DurationFormatTest::TestDefault));
115 	suite.addTest(new CppUnit::TestCaller<DurationFormatTest>(
116 		"DurationFormatTest::TestDuration", &DurationFormatTest::TestDuration));
117 	suite.addTest(new CppUnit::TestCaller<DurationFormatTest>(
118 		"DurationFormatTest::TestTimeUnit", &DurationFormatTest::TestTimeUnit));
119 
120 	parent.addTest("DurationFormatTest", &suite);
121 }
122