From 814f2b4b7c65b7a171429defa98fe24f8f2ff471 Mon Sep 17 00:00:00 2001
From: Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
Date: Sat, 26 Aug 2017 15:22:09 +0530
Subject: [PATCH] Fix BDateFormatTest: Consider timezone in test cases.
* Issue: A time_t value of say '12345678' results into different timestamps
in different timezones. So the expected output will not match the result of
BDateFormat::Format() if the calendar's timezone is different from the
timezone of the expected output, and the tests 'TestFormat' and
'TestCustomFormat' will fail.
* Fix: Add timezone information in the test cases and pass the timezone
while calling BDateFormat::Format() in order to set the calendar's
timezone same as that of the expected output.
---
src/tests/kits/locale/DateFormatTest.cpp | 43 +++++++++++++++++++-------------
1 file changed, 25 insertions(+), 18 deletions(-)
diff --git a/src/tests/kits/locale/DateFormatTest.cpp b/src/tests/kits/locale/DateFormatTest.cpp
index 574a3ab..856ecd4 100644
a
|
b
|
|
10 | 10 | #include <FormattingConventions.h> |
11 | 11 | #include <Language.h> |
12 | 12 | #include <TimeFormat.h> |
| 13 | #include <TimeZone.h> |
13 | 14 | |
14 | 15 | #include <cppunit/TestCaller.h> |
15 | 16 | #include <cppunit/TestSuite.h> |
… |
… |
DateFormatTest::TestCustomFormat()
|
31 | 32 | struct Test { |
32 | 33 | const char* language; |
33 | 34 | const char* formatting; |
| 35 | const char* timeZone; |
34 | 36 | int32 fields; |
35 | 37 | |
36 | 38 | BString expected; |
… |
… |
DateFormatTest::TestCustomFormat()
|
41 | 43 | BString buffer; |
42 | 44 | |
43 | 45 | const Test tests[] = { |
44 | | { "en", "en_US", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE, |
| 46 | { "en", "en_US", "GMT+1", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE, |
45 | 47 | "10:21 PM", "22:21", "10:21 PM" }, |
46 | | { "en", "en_US", |
| 48 | { "en", "en_US", "GMT+1", |
47 | 49 | B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE | B_DATE_ELEMENT_SECOND, |
48 | 50 | "10:21:18 PM", "22:21:18", "10:21:18 PM" }, |
49 | | // FIXME we need a way to set the timezone for this test to work |
50 | | // reliably. |
51 | | { "en", "en_US", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE |
| 51 | { "en", "en_US", "GMT+1", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE |
52 | 52 | | B_DATE_ELEMENT_TIMEZONE, |
53 | 53 | "10:21 PM GMT+1", "22:21 GMT+1", "10:21 PM GMT+1" }, |
54 | | { "en", "en_US", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE |
| 54 | { "en", "en_US", "GMT+1", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE |
55 | 55 | | B_DATE_ELEMENT_SECOND | B_DATE_ELEMENT_TIMEZONE, |
56 | 56 | "10:21:18 PM GMT+1", "22:21:18 GMT+1", "10:21:18 PM GMT+1" }, |
57 | | { "fr", "fr_FR", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE, |
| 57 | { "fr", "fr_FR", "GMT+1", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE, |
58 | 58 | "22:21", "22:21", "10:21 PM" }, |
59 | 59 | { NULL } |
60 | 60 | }; |
… |
… |
DateFormatTest::TestCustomFormat()
|
65 | 65 | |
66 | 66 | BLanguage language(tests[i].language); |
67 | 67 | BFormattingConventions formatting(tests[i].formatting); |
| 68 | BTimeZone timeZone(tests[i].timeZone); |
68 | 69 | status_t result; |
69 | 70 | |
70 | 71 | // Test default for language/formatting |
… |
… |
DateFormatTest::TestCustomFormat()
|
74 | 75 | tests[i].fields); |
75 | 76 | |
76 | 77 | result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT, |
77 | | B_SHORT_TIME_FORMAT); |
| 78 | B_SHORT_TIME_FORMAT, &timeZone); |
78 | 79 | |
79 | 80 | CPPUNIT_ASSERT_EQUAL(B_OK, result); |
80 | 81 | CPPUNIT_ASSERT_EQUAL(tests[i].expected, buffer); |
… |
… |
DateFormatTest::TestCustomFormat()
|
87 | 88 | format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, |
88 | 89 | tests[i].fields); |
89 | 90 | result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT, |
90 | | B_SHORT_TIME_FORMAT); |
| 91 | B_SHORT_TIME_FORMAT, &timeZone); |
91 | 92 | |
92 | 93 | CPPUNIT_ASSERT_EQUAL(B_OK, result); |
93 | 94 | CPPUNIT_ASSERT_EQUAL(tests[i].force24, buffer); |
… |
… |
DateFormatTest::TestCustomFormat()
|
100 | 101 | format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, |
101 | 102 | tests[i].fields); |
102 | 103 | result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT, |
103 | | B_SHORT_TIME_FORMAT); |
| 104 | B_SHORT_TIME_FORMAT, &timeZone); |
104 | 105 | |
105 | 106 | CPPUNIT_ASSERT_EQUAL(B_OK, result); |
106 | 107 | CPPUNIT_ASSERT_EQUAL(tests[i].force12, buffer); |
… |
… |
DateFormatTest::TestFormat()
|
115 | 116 | struct Value { |
116 | 117 | const char* language; |
117 | 118 | const char* convention; |
| 119 | const char* timeZone; |
118 | 120 | time_t time; |
119 | 121 | const char* shortDate; |
120 | 122 | const char* longDate; |
… |
… |
DateFormatTest::TestFormat()
|
124 | 126 | }; |
125 | 127 | |
126 | 128 | static const Value values[] = { |
127 | | {"en", "en_US", 12345, "1/1/70", "January 1, 1970", |
| 129 | {"en", "en_US", "GMT+1", 12345, "1/1/70", "January 1, 1970", |
128 | 130 | "4:25 AM", "4:25:45 AM", "1/1/70, 4:25 AM"}, |
129 | | {"fr", "fr_FR", 12345, "01/01/1970", "1 janvier 1970", |
| 131 | {"fr", "fr_FR", "GMT+1", 12345, "01/01/1970", "1 janvier 1970", |
130 | 132 | "04:25", "04:25:45", "01/01/1970 04:25"}, |
131 | | {"fr", "fr_FR", 12345678, "23/05/1970", "23 mai 1970", |
| 133 | {"fr", "fr_FR", "GMT+1", 12345678, "23/05/1970", "23 mai 1970", |
132 | 134 | "22:21", "22:21:18", "23/05/1970 22:21"}, |
133 | 135 | {NULL} |
134 | 136 | }; |
… |
… |
DateFormatTest::TestFormat()
|
141 | 143 | |
142 | 144 | BLanguage language(values[i].language); |
143 | 145 | BFormattingConventions formatting(values[i].convention); |
| 146 | BTimeZone timeZone(values[i].timeZone); |
144 | 147 | BDateFormat dateFormat(language, formatting); |
145 | 148 | BTimeFormat timeFormat(language, formatting); |
146 | 149 | BDateTimeFormat dateTimeFormat(language, formatting); |
147 | 150 | |
148 | | result = dateFormat.Format(output, values[i].time, B_SHORT_DATE_FORMAT); |
| 151 | result = dateFormat.Format(output, values[i].time, B_SHORT_DATE_FORMAT, |
| 152 | &timeZone); |
149 | 153 | CPPUNIT_ASSERT_EQUAL(B_OK, result); |
150 | 154 | CPPUNIT_ASSERT_EQUAL(BString(values[i].shortDate), output); |
151 | 155 | |
152 | | result = dateFormat.Format(output, values[i].time, B_LONG_DATE_FORMAT); |
| 156 | result = dateFormat.Format(output, values[i].time, B_LONG_DATE_FORMAT, |
| 157 | &timeZone); |
153 | 158 | CPPUNIT_ASSERT_EQUAL(B_OK, result); |
154 | 159 | CPPUNIT_ASSERT_EQUAL(BString(values[i].longDate), output); |
155 | 160 | |
156 | | result = timeFormat.Format(output, values[i].time, B_SHORT_TIME_FORMAT); |
| 161 | result = timeFormat.Format(output, values[i].time, B_SHORT_TIME_FORMAT, |
| 162 | &timeZone); |
157 | 163 | CPPUNIT_ASSERT_EQUAL(B_OK, result); |
158 | 164 | CPPUNIT_ASSERT_EQUAL(BString(values[i].shortTime), output); |
159 | 165 | |
160 | | result = timeFormat.Format(output, values[i].time, B_MEDIUM_TIME_FORMAT); |
| 166 | result = timeFormat.Format(output, values[i].time, B_MEDIUM_TIME_FORMAT, |
| 167 | &timeZone); |
161 | 168 | CPPUNIT_ASSERT_EQUAL(B_OK, result); |
162 | 169 | CPPUNIT_ASSERT_EQUAL(BString(values[i].longTime), output); |
163 | 170 | |
164 | 171 | result = dateTimeFormat.Format(output, values[i].time, |
165 | | B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT); |
| 172 | B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, &timeZone); |
166 | 173 | CPPUNIT_ASSERT_EQUAL(B_OK, result); |
167 | 174 | CPPUNIT_ASSERT_EQUAL(BString(values[i].shortDateTime), output); |
168 | 175 | } |