xref: /haiku/src/tests/kits/locale/DurationFormatTest.cpp (revision b247f935d133a42c427cad8a759a1bf2f65bc290)
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 		BDurationFormat format(englishLanguage, englishFormat,
76 			":", B_TIME_UNIT_ABBREVIATED);
77 		status_t result = format.Format(buffer, 0, 800000000000ll);
78 
79 		expected << "1 wk:2 days:6 hr:13 min:20 sec";
80 		CPPUNIT_ASSERT_EQUAL(B_OK, result);
81 		CPPUNIT_ASSERT_EQUAL(expected, buffer);
82 	}
83 
84 	{
85 		BDurationFormat format(frenchLanguage, frenchFormat,
86 			":", B_TIME_UNIT_ABBREVIATED);
87 		result = format.Format(buffer, 0, 800000000000ll);
88 
89 		expected << "1 sem.:2 j:6 h:13 min:20 s";
90 		CPPUNIT_ASSERT_EQUAL(B_OK, result);
91 		CPPUNIT_ASSERT_EQUAL(expected, buffer);
92 	}
93 }
94 
95 
96 void
97 DurationFormatTest::TestTimeUnit()
98 {
99 	BString buffer;
100 	status_t result;
101 
102 	BFormattingConventions englishFormat("en_US");
103 	BLanguage englishLanguage("en");
104 
105 	BFormattingConventions frenchFormat("fr_FR");
106 	BLanguage frenchLanguage("fr");
107 
108 	{
109 		BTimeUnitFormat format(englishLanguage, englishFormat);
110 		result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
111 
112 		CPPUNIT_ASSERT_EQUAL(B_OK, result);
113 		CPPUNIT_ASSERT_EQUAL(BString("5 hours"), buffer);
114 	}
115 
116 	{
117 		BTimeUnitFormat format(frenchLanguage, frenchFormat);
118 		result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
119 
120 		CPPUNIT_ASSERT_EQUAL(B_OK, result);
121 		// We check that the passed BString is not truncated. This makes it easy
122 		// to append several units to the same string, as BDurationFormat does.
123 		CPPUNIT_ASSERT_EQUAL(BString("5 hours5 heures"), buffer);
124 	}
125 }
126 
127 
128 /*static*/ void
129 DurationFormatTest::AddTests(BTestSuite& parent)
130 {
131 	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("DurationFormatTest");
132 
133 	suite.addTest(new CppUnit::TestCaller<DurationFormatTest>(
134 		"DurationFormatTest::TestDefault", &DurationFormatTest::TestDefault));
135 	suite.addTest(new CppUnit::TestCaller<DurationFormatTest>(
136 		"DurationFormatTest::TestDuration", &DurationFormatTest::TestDuration));
137 	suite.addTest(new CppUnit::TestCaller<DurationFormatTest>(
138 		"DurationFormatTest::TestTimeUnit", &DurationFormatTest::TestTimeUnit));
139 
140 	parent.addTest("DurationFormatTest", &suite);
141 }
142