xref: /haiku/src/tests/kits/locale/RelativeDateTimeFormatTest.cpp (revision cf911230410f2d40ba1a118ad6be5085a49470f4)
1 /*
2  * Copyright 2017, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
7  */
8 
9 #include "RelativeDateTimeFormatTest.h"
10 
11 #include <time.h>
12 
13 #include <Locale.h>
14 #include <RelativeDateTimeFormat.h>
15 
16 #include <cppunit/TestCaller.h>
17 #include <cppunit/TestSuite.h>
18 
19 
20 const int kMonthsPerYear = 12;
21 const int kMaxDaysPerMonth = 31;
22 const int kDaysPerWeek = 7;
23 const int kHoursPerDay = 24;
24 const int kMinutesPerHour = 60;
25 const int kSecondsPerMinute = 60;
26 
27 
RelativeDateTimeFormatTest()28 RelativeDateTimeFormatTest::RelativeDateTimeFormatTest()
29 {
30 }
31 
32 
~RelativeDateTimeFormatTest()33 RelativeDateTimeFormatTest::~RelativeDateTimeFormatTest()
34 {
35 }
36 
37 
38 void
TestDefault()39 RelativeDateTimeFormatTest::TestDefault()
40 {
41 	BRelativeDateTimeFormat format;
42 	BString buffer;
43 	BString expected;
44 
45 	status_t result = format.Format(buffer, time(NULL));
46 	CPPUNIT_ASSERT_EQUAL(B_OK, result);
47 	// The exact format and language used depends on the locale settings, but
48 	// we can assume that whatever they are, it should put something in the
49 	// string.
50 	CPPUNIT_ASSERT(buffer.Length() > 0);
51 }
52 
53 
54 void
TestFormat()55 RelativeDateTimeFormatTest::TestFormat()
56 {
57 	struct Value {
58 		const char* language;
59 		const char* convention;
60 		time_t timeDelta;
61 		const char* relativeDate;
62 	};
63 
64 	static const Value values[] = {
65 		{"en", "en_US", 0, "in 0 seconds"},
66 		{"en", "en_US", 20, "in 20 seconds"},
67 		{"en", "en_US", -20, "20 seconds ago"},
68 		{"en", "en_US", 5 * kSecondsPerMinute, "in 5 minutes"},
69 		{"en", "en_US", -5 * kSecondsPerMinute, "5 minutes ago"},
70 		{"en", "en_US", 2 * kMinutesPerHour * kSecondsPerMinute, "in 2 hours"},
71 		{"en", "en_US", -2 * kMinutesPerHour * kSecondsPerMinute, "2 hours ago"},
72 		{"fr", "fr_FR", 2 * kMinutesPerHour * kSecondsPerMinute, "dans 2 heures"},
73 		{"fr", "fr_FR", -2 * kMinutesPerHour * kSecondsPerMinute, "il y a 2 heures"},
74 		{"en", "en_US", 5 * kHoursPerDay * kMinutesPerHour * kSecondsPerMinute,
75 			"in 5 days"},
76 		{"en", "en_US", -5 * kHoursPerDay * kMinutesPerHour * kSecondsPerMinute,
77 			"5 days ago"},
78 		{"de", "de_DE", 5 * kHoursPerDay * kMinutesPerHour * kSecondsPerMinute,
79 			"in 5 Tagen"},
80 		{"de", "de_DE", -5 * kHoursPerDay * kMinutesPerHour * kSecondsPerMinute,
81 			"vor 5 Tagen"},
82 		{"en", "en_US", 3 * kDaysPerWeek * kHoursPerDay * kMinutesPerHour
83 			* kSecondsPerMinute, "in 3 weeks"},
84 		{"en", "en_US", -3 * kDaysPerWeek * kHoursPerDay * kMinutesPerHour
85 			* kSecondsPerMinute, "3 weeks ago"},
86 		{"en", "en_US", 4 * kMaxDaysPerMonth * kHoursPerDay * kMinutesPerHour
87 			* kSecondsPerMinute, "in 4 months"},
88 		{"en", "en_US", -4 * kMaxDaysPerMonth * kHoursPerDay * kMinutesPerHour
89 			* kSecondsPerMinute, "4 months ago"},
90 		{"en", "en_US", 2 * kMonthsPerYear * kMaxDaysPerMonth * kHoursPerDay
91 			* kMinutesPerHour * kSecondsPerMinute, "in 2 years"},
92 		{"en", "en_US", -2 * kMonthsPerYear * kMaxDaysPerMonth * kHoursPerDay
93 			* kMinutesPerHour * kSecondsPerMinute, "2 years ago"},
94 		{NULL}
95 	};
96 
97 	status_t result;
98 
99 	for (int i = 0; values[i].language != NULL; i++) {
100 		NextSubTest();
101 
102 		BString output;
103 		BLanguage language(values[i].language);
104 		BFormattingConventions formatting(values[i].convention);
105 		BRelativeDateTimeFormat format(language, formatting);
106 
107 		result = format.Format(output, time(NULL) + values[i].timeDelta);
108 		CPPUNIT_ASSERT_EQUAL(B_OK, result);
109 		CPPUNIT_ASSERT_EQUAL(BString(values[i].relativeDate), output);
110 	}
111 }
112 
113 
114 /*static*/ void
AddTests(BTestSuite & parent)115 RelativeDateTimeFormatTest::AddTests(BTestSuite& parent)
116 {
117 	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("RelativeDateTimeFormatTest");
118 
119 	suite.addTest(new CppUnit::TestCaller<RelativeDateTimeFormatTest>(
120 		"RelativeDateTimeFormatTest::TestDefault", &RelativeDateTimeFormatTest::TestDefault));
121 	suite.addTest(new CppUnit::TestCaller<RelativeDateTimeFormatTest>(
122 		"RelativeDateTimeFormatTest::TestFormat", &RelativeDateTimeFormatTest::TestFormat));
123 	parent.addTest("RelativeDateTimeFormatTest", &suite);
124 }
125