1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <time.h>
18 #include <fstream>
19 #include <iostream>
20 #include <memory>
21 #include <string>
22
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25
26 #include "datetime/parser.h"
27 #include "model_generated.h"
28 #include "text-classifier.h"
29 #include "types-test-util.h"
30
31 using testing::ElementsAreArray;
32
33 namespace libtextclassifier2 {
34 namespace {
35
GetModelPath()36 std::string GetModelPath() {
37 return LIBTEXTCLASSIFIER_TEST_DATA_DIR;
38 }
39
ReadFile(const std::string & file_name)40 std::string ReadFile(const std::string& file_name) {
41 std::ifstream file_stream(file_name);
42 return std::string(std::istreambuf_iterator<char>(file_stream), {});
43 }
44
FormatMillis(int64 time_ms_utc)45 std::string FormatMillis(int64 time_ms_utc) {
46 long time_seconds = time_ms_utc / 1000; // NOLINT
47 // Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
48 char buffer[512];
49 strftime(buffer, sizeof(buffer), "%a %Y-%m-%d %H:%M:%S %Z",
50 localtime(&time_seconds));
51 return std::string(buffer);
52 }
53
54 class ParserTest : public testing::Test {
55 public:
SetUp()56 void SetUp() override {
57 model_buffer_ = ReadFile(GetModelPath() + "test_model.fb");
58 classifier_ = TextClassifier::FromUnownedBuffer(
59 model_buffer_.data(), model_buffer_.size(), &unilib_);
60 TC_CHECK(classifier_);
61 parser_ = classifier_->DatetimeParserForTests();
62 }
63
HasNoResult(const std::string & text,bool anchor_start_end=false,const std::string & timezone="Europe/Zurich")64 bool HasNoResult(const std::string& text, bool anchor_start_end = false,
65 const std::string& timezone = "Europe/Zurich") {
66 std::vector<DatetimeParseResultSpan> results;
67 if (!parser_->Parse(text, 0, timezone, /*locales=*/"", ModeFlag_ANNOTATION,
68 anchor_start_end, &results)) {
69 TC_LOG(ERROR) << text;
70 TC_CHECK(false);
71 }
72 return results.empty();
73 }
74
ParsesCorrectly(const std::string & marked_text,const int64 expected_ms_utc,DatetimeGranularity expected_granularity,bool anchor_start_end=false,const std::string & timezone="Europe/Zurich",const std::string & locales="en-US")75 bool ParsesCorrectly(const std::string& marked_text,
76 const int64 expected_ms_utc,
77 DatetimeGranularity expected_granularity,
78 bool anchor_start_end = false,
79 const std::string& timezone = "Europe/Zurich",
80 const std::string& locales = "en-US") {
81 const UnicodeText marked_text_unicode =
82 UTF8ToUnicodeText(marked_text, /*do_copy=*/false);
83 auto brace_open_it =
84 std::find(marked_text_unicode.begin(), marked_text_unicode.end(), '{');
85 auto brace_end_it =
86 std::find(marked_text_unicode.begin(), marked_text_unicode.end(), '}');
87 TC_CHECK(brace_open_it != marked_text_unicode.end());
88 TC_CHECK(brace_end_it != marked_text_unicode.end());
89
90 std::string text;
91 text +=
92 UnicodeText::UTF8Substring(marked_text_unicode.begin(), brace_open_it);
93 text += UnicodeText::UTF8Substring(std::next(brace_open_it), brace_end_it);
94 text += UnicodeText::UTF8Substring(std::next(brace_end_it),
95 marked_text_unicode.end());
96
97 std::vector<DatetimeParseResultSpan> results;
98
99 if (!parser_->Parse(text, 0, timezone, locales, ModeFlag_ANNOTATION,
100 anchor_start_end, &results)) {
101 TC_LOG(ERROR) << text;
102 TC_CHECK(false);
103 }
104 if (results.empty()) {
105 TC_LOG(ERROR) << "No results.";
106 return false;
107 }
108
109 const int expected_start_index =
110 std::distance(marked_text_unicode.begin(), brace_open_it);
111 // The -1 bellow is to account for the opening bracket character.
112 const int expected_end_index =
113 std::distance(marked_text_unicode.begin(), brace_end_it) - 1;
114
115 std::vector<DatetimeParseResultSpan> filtered_results;
116 for (const DatetimeParseResultSpan& result : results) {
117 if (SpansOverlap(result.span,
118 {expected_start_index, expected_end_index})) {
119 filtered_results.push_back(result);
120 }
121 }
122
123 const std::vector<DatetimeParseResultSpan> expected{
124 {{expected_start_index, expected_end_index},
125 {expected_ms_utc, expected_granularity},
126 /*target_classification_score=*/1.0,
127 /*priority_score=*/0.0}};
128 const bool matches =
129 testing::Matches(ElementsAreArray(expected))(filtered_results);
130 if (!matches) {
131 TC_LOG(ERROR) << "Expected: " << expected[0] << " which corresponds to: "
132 << FormatMillis(expected[0].data.time_ms_utc);
133 for (int i = 0; i < filtered_results.size(); ++i) {
134 TC_LOG(ERROR) << "Actual[" << i << "]: " << filtered_results[i]
135 << " which corresponds to: "
136 << FormatMillis(filtered_results[i].data.time_ms_utc);
137 }
138 }
139 return matches;
140 }
141
ParsesCorrectlyGerman(const std::string & marked_text,const int64 expected_ms_utc,DatetimeGranularity expected_granularity)142 bool ParsesCorrectlyGerman(const std::string& marked_text,
143 const int64 expected_ms_utc,
144 DatetimeGranularity expected_granularity) {
145 return ParsesCorrectly(marked_text, expected_ms_utc, expected_granularity,
146 /*anchor_start_end=*/false,
147 /*timezone=*/"Europe/Zurich", /*locales=*/"de");
148 }
149
150 protected:
151 std::string model_buffer_;
152 std::unique_ptr<TextClassifier> classifier_;
153 const DatetimeParser* parser_;
154 UniLib unilib_;
155 };
156
157 // Test with just a few cases to make debugging of general failures easier.
TEST_F(ParserTest,ParseShort)158 TEST_F(ParserTest, ParseShort) {
159 EXPECT_TRUE(
160 ParsesCorrectly("{January 1, 1988}", 567990000000, GRANULARITY_DAY));
161 EXPECT_TRUE(ParsesCorrectly("{three days ago}", -262800000, GRANULARITY_DAY));
162 }
163
TEST_F(ParserTest,Parse)164 TEST_F(ParserTest, Parse) {
165 EXPECT_TRUE(
166 ParsesCorrectly("{January 1, 1988}", 567990000000, GRANULARITY_DAY));
167 EXPECT_TRUE(
168 ParsesCorrectly("{january 31 2018}", 1517353200000, GRANULARITY_DAY));
169 EXPECT_TRUE(ParsesCorrectly("lorem {1 january 2018} ipsum", 1514761200000,
170 GRANULARITY_DAY));
171 EXPECT_TRUE(ParsesCorrectly("{09/Mar/2004 22:02:40}", 1078866160000,
172 GRANULARITY_SECOND));
173 EXPECT_TRUE(ParsesCorrectly("{Dec 2, 2010 2:39:58 AM}", 1291253998000,
174 GRANULARITY_SECOND));
175 EXPECT_TRUE(ParsesCorrectly("{Jun 09 2011 15:28:14}", 1307626094000,
176 GRANULARITY_SECOND));
177 EXPECT_TRUE(
178 ParsesCorrectly("{Mar 16 08:12:04}", 6419524000, GRANULARITY_SECOND));
179 EXPECT_TRUE(ParsesCorrectly("{2010-06-26 02:31:29},573", 1277512289000,
180 GRANULARITY_SECOND));
181 EXPECT_TRUE(ParsesCorrectly("{2006/01/22 04:11:05}", 1137899465000,
182 GRANULARITY_SECOND));
183 EXPECT_TRUE(ParsesCorrectly("{11:42:35}", 38555000, GRANULARITY_SECOND));
184 EXPECT_TRUE(ParsesCorrectly("{11:42:35}.173", 38555000, GRANULARITY_SECOND));
185 EXPECT_TRUE(
186 ParsesCorrectly("{23/Apr 11:42:35},173", 9715355000, GRANULARITY_SECOND));
187 EXPECT_TRUE(ParsesCorrectly("{23/Apr/2015 11:42:35}", 1429782155000,
188 GRANULARITY_SECOND));
189 EXPECT_TRUE(ParsesCorrectly("{23-Apr-2015 11:42:35}", 1429782155000,
190 GRANULARITY_SECOND));
191 EXPECT_TRUE(ParsesCorrectly("{23-Apr-2015 11:42:35}.883", 1429782155000,
192 GRANULARITY_SECOND));
193 EXPECT_TRUE(ParsesCorrectly("{23 Apr 2015 11:42:35}", 1429782155000,
194 GRANULARITY_SECOND));
195 EXPECT_TRUE(ParsesCorrectly("{23 Apr 2015 11:42:35}.883", 1429782155000,
196 GRANULARITY_SECOND));
197 EXPECT_TRUE(ParsesCorrectly("{04/23/15 11:42:35}", 1429782155000,
198 GRANULARITY_SECOND));
199 EXPECT_TRUE(ParsesCorrectly("{04/23/2015 11:42:35}", 1429782155000,
200 GRANULARITY_SECOND));
201 EXPECT_TRUE(ParsesCorrectly("{04/23/2015 11:42:35}.883", 1429782155000,
202 GRANULARITY_SECOND));
203 EXPECT_TRUE(ParsesCorrectly("{9/28/2011 2:23:15 PM}", 1317212595000,
204 GRANULARITY_SECOND));
205 EXPECT_TRUE(ParsesCorrectly(
206 "Are sentiments apartments decisively the especially alteration. "
207 "Thrown shy denote ten ladies though ask saw. Or by to he going "
208 "think order event music. Incommode so intention defective at "
209 "convinced. Led income months itself and houses you. After nor "
210 "you leave might share court balls. {19/apr/2010 06:36:15} Are "
211 "sentiments apartments decisively the especially alteration. "
212 "Thrown shy denote ten ladies though ask saw. Or by to he going "
213 "think order event music. Incommode so intention defective at "
214 "convinced. Led income months itself and houses you. After nor "
215 "you leave might share court balls. ",
216 1271651775000, GRANULARITY_SECOND));
217 EXPECT_TRUE(ParsesCorrectly("{january 1 2018 at 4:30}", 1514777400000,
218 GRANULARITY_MINUTE));
219 EXPECT_TRUE(ParsesCorrectly("{january 1 2018 at 4:30 am}", 1514777400000,
220 GRANULARITY_MINUTE));
221 EXPECT_TRUE(ParsesCorrectly("{january 1 2018 at 4pm}", 1514818800000,
222 GRANULARITY_HOUR));
223
224 EXPECT_TRUE(ParsesCorrectly("{today}", -3600000, GRANULARITY_DAY));
225 EXPECT_TRUE(ParsesCorrectly("{today}", -57600000, GRANULARITY_DAY,
226 /*anchor_start_end=*/false,
227 "America/Los_Angeles"));
228 EXPECT_TRUE(ParsesCorrectly("{next week}", 255600000, GRANULARITY_WEEK));
229 EXPECT_TRUE(ParsesCorrectly("{next day}", 82800000, GRANULARITY_DAY));
230 EXPECT_TRUE(ParsesCorrectly("{in three days}", 255600000, GRANULARITY_DAY));
231 EXPECT_TRUE(
232 ParsesCorrectly("{in three weeks}", 1465200000, GRANULARITY_WEEK));
233 EXPECT_TRUE(ParsesCorrectly("{tomorrow}", 82800000, GRANULARITY_DAY));
234 EXPECT_TRUE(
235 ParsesCorrectly("{tomorrow at 4:00}", 97200000, GRANULARITY_MINUTE));
236 EXPECT_TRUE(ParsesCorrectly("{tomorrow at 4}", 97200000, GRANULARITY_HOUR));
237 EXPECT_TRUE(ParsesCorrectly("{next wednesday}", 514800000, GRANULARITY_DAY));
238 EXPECT_TRUE(
239 ParsesCorrectly("{next wednesday at 4}", 529200000, GRANULARITY_HOUR));
240 EXPECT_TRUE(ParsesCorrectly("last seen {today at 9:01 PM}", 72060000,
241 GRANULARITY_MINUTE));
242 EXPECT_TRUE(ParsesCorrectly("{Three days ago}", -262800000, GRANULARITY_DAY));
243 EXPECT_TRUE(ParsesCorrectly("{three days ago}", -262800000, GRANULARITY_DAY));
244 }
245
TEST_F(ParserTest,ParseWithAnchor)246 TEST_F(ParserTest, ParseWithAnchor) {
247 EXPECT_TRUE(ParsesCorrectly("{January 1, 1988}", 567990000000,
248 GRANULARITY_DAY, /*anchor_start_end=*/false));
249 EXPECT_TRUE(ParsesCorrectly("{January 1, 1988}", 567990000000,
250 GRANULARITY_DAY, /*anchor_start_end=*/true));
251 EXPECT_TRUE(ParsesCorrectly("lorem {1 january 2018} ipsum", 1514761200000,
252 GRANULARITY_DAY, /*anchor_start_end=*/false));
253 EXPECT_TRUE(HasNoResult("lorem 1 january 2018 ipsum",
254 /*anchor_start_end=*/true));
255 }
256
TEST_F(ParserTest,ParseGerman)257 TEST_F(ParserTest, ParseGerman) {
258 EXPECT_TRUE(
259 ParsesCorrectlyGerman("{Januar 1 2018}", 1514761200000, GRANULARITY_DAY));
260 EXPECT_TRUE(
261 ParsesCorrectlyGerman("{1 2 2018}", 1517439600000, GRANULARITY_DAY));
262 EXPECT_TRUE(ParsesCorrectlyGerman("lorem {1 Januar 2018} ipsum",
263 1514761200000, GRANULARITY_DAY));
264 EXPECT_TRUE(ParsesCorrectlyGerman("{19/Apr/2010:06:36:15}", 1271651775000,
265 GRANULARITY_SECOND));
266 EXPECT_TRUE(ParsesCorrectlyGerman("{09/März/2004 22:02:40}", 1078866160000,
267 GRANULARITY_SECOND));
268 EXPECT_TRUE(ParsesCorrectlyGerman("{Dez 2, 2010 2:39:58}", 1291253998000,
269 GRANULARITY_SECOND));
270 EXPECT_TRUE(ParsesCorrectlyGerman("{Juni 09 2011 15:28:14}", 1307626094000,
271 GRANULARITY_SECOND));
272 EXPECT_TRUE(ParsesCorrectlyGerman("{März 16 08:12:04}", 6419524000,
273 GRANULARITY_SECOND));
274 EXPECT_TRUE(ParsesCorrectlyGerman("{2010-06-26 02:31:29},573", 1277512289000,
275 GRANULARITY_SECOND));
276 EXPECT_TRUE(ParsesCorrectlyGerman("{2006/01/22 04:11:05}", 1137899465000,
277 GRANULARITY_SECOND));
278 EXPECT_TRUE(
279 ParsesCorrectlyGerman("{11:42:35}", 38555000, GRANULARITY_SECOND));
280 EXPECT_TRUE(
281 ParsesCorrectlyGerman("{11:42:35}.173", 38555000, GRANULARITY_SECOND));
282 EXPECT_TRUE(ParsesCorrectlyGerman("{23/Apr 11:42:35},173", 9715355000,
283 GRANULARITY_SECOND));
284 EXPECT_TRUE(ParsesCorrectlyGerman("{23/Apr/2015:11:42:35}", 1429782155000,
285 GRANULARITY_SECOND));
286 EXPECT_TRUE(ParsesCorrectlyGerman("{23/Apr/2015 11:42:35}", 1429782155000,
287 GRANULARITY_SECOND));
288 EXPECT_TRUE(ParsesCorrectlyGerman("{23-Apr-2015 11:42:35}", 1429782155000,
289 GRANULARITY_SECOND));
290 EXPECT_TRUE(ParsesCorrectlyGerman("{23-Apr-2015 11:42:35}.883", 1429782155000,
291 GRANULARITY_SECOND));
292 EXPECT_TRUE(ParsesCorrectlyGerman("{23 Apr 2015 11:42:35}", 1429782155000,
293 GRANULARITY_SECOND));
294 EXPECT_TRUE(ParsesCorrectlyGerman("{23 Apr 2015 11:42:35}.883", 1429782155000,
295 GRANULARITY_SECOND));
296 EXPECT_TRUE(ParsesCorrectlyGerman("{04/23/15 11:42:35}", 1429782155000,
297 GRANULARITY_SECOND));
298 EXPECT_TRUE(ParsesCorrectlyGerman("{04/23/2015 11:42:35}", 1429782155000,
299 GRANULARITY_SECOND));
300 EXPECT_TRUE(ParsesCorrectlyGerman("{04/23/2015 11:42:35}.883", 1429782155000,
301 GRANULARITY_SECOND));
302 EXPECT_TRUE(ParsesCorrectlyGerman("{19/apr/2010:06:36:15}", 1271651775000,
303 GRANULARITY_SECOND));
304 EXPECT_TRUE(ParsesCorrectlyGerman("{januar 1 2018 um 4:30}", 1514777400000,
305 GRANULARITY_MINUTE));
306 EXPECT_TRUE(ParsesCorrectlyGerman("{januar 1 2018 um 4:30 nachm}",
307 1514820600000, GRANULARITY_MINUTE));
308 EXPECT_TRUE(ParsesCorrectlyGerman("{januar 1 2018 um 4 nachm}", 1514818800000,
309 GRANULARITY_HOUR));
310 EXPECT_TRUE(
311 ParsesCorrectlyGerman("{14.03.2017}", 1489446000000, GRANULARITY_DAY));
312 EXPECT_TRUE(ParsesCorrectlyGerman("{heute}", -3600000, GRANULARITY_DAY));
313 EXPECT_TRUE(
314 ParsesCorrectlyGerman("{nächste Woche}", 342000000, GRANULARITY_WEEK));
315 EXPECT_TRUE(
316 ParsesCorrectlyGerman("{nächsten Tag}", 82800000, GRANULARITY_DAY));
317 EXPECT_TRUE(
318 ParsesCorrectlyGerman("{in drei Tagen}", 255600000, GRANULARITY_DAY));
319 EXPECT_TRUE(
320 ParsesCorrectlyGerman("{in drei Wochen}", 1551600000, GRANULARITY_WEEK));
321 EXPECT_TRUE(
322 ParsesCorrectlyGerman("{vor drei Tagen}", -262800000, GRANULARITY_DAY));
323 EXPECT_TRUE(ParsesCorrectlyGerman("{morgen}", 82800000, GRANULARITY_DAY));
324 EXPECT_TRUE(
325 ParsesCorrectlyGerman("{morgen um 4:00}", 97200000, GRANULARITY_MINUTE));
326 EXPECT_TRUE(
327 ParsesCorrectlyGerman("{morgen um 4}", 97200000, GRANULARITY_HOUR));
328 EXPECT_TRUE(
329 ParsesCorrectlyGerman("{nächsten Mittwoch}", 514800000, GRANULARITY_DAY));
330 EXPECT_TRUE(ParsesCorrectlyGerman("{nächsten Mittwoch um 4}", 529200000,
331 GRANULARITY_HOUR));
332 EXPECT_TRUE(
333 ParsesCorrectlyGerman("{Vor drei Tagen}", -262800000, GRANULARITY_DAY));
334 EXPECT_TRUE(
335 ParsesCorrectlyGerman("{in einer woche}", 342000000, GRANULARITY_WEEK));
336 EXPECT_TRUE(
337 ParsesCorrectlyGerman("{in einer tag}", 82800000, GRANULARITY_DAY));
338 }
339
TEST_F(ParserTest,ParseNonUs)340 TEST_F(ParserTest, ParseNonUs) {
341 EXPECT_TRUE(ParsesCorrectly("{1/5/15}", 1430431200000, GRANULARITY_DAY,
342 /*anchor_start_end=*/false,
343 /*timezone=*/"Europe/Zurich",
344 /*locales=*/"en-GB"));
345 EXPECT_TRUE(ParsesCorrectly("{1/5/15}", 1430431200000, GRANULARITY_DAY,
346 /*anchor_start_end=*/false,
347 /*timezone=*/"Europe/Zurich", /*locales=*/"en"));
348 }
349
TEST_F(ParserTest,ParseUs)350 TEST_F(ParserTest, ParseUs) {
351 EXPECT_TRUE(ParsesCorrectly("{1/5/15}", 1420412400000, GRANULARITY_DAY,
352 /*anchor_start_end=*/false,
353 /*timezone=*/"Europe/Zurich",
354 /*locales=*/"en-US"));
355 EXPECT_TRUE(ParsesCorrectly("{1/5/15}", 1420412400000, GRANULARITY_DAY,
356 /*anchor_start_end=*/false,
357 /*timezone=*/"Europe/Zurich",
358 /*locales=*/"es-US"));
359 }
360
TEST_F(ParserTest,ParseUnknownLanguage)361 TEST_F(ParserTest, ParseUnknownLanguage) {
362 EXPECT_TRUE(ParsesCorrectly("bylo to {31. 12. 2015} v 6 hodin", 1451516400000,
363 GRANULARITY_DAY,
364 /*anchor_start_end=*/false,
365 /*timezone=*/"Europe/Zurich", /*locales=*/"xx"));
366 }
367
368 class ParserLocaleTest : public testing::Test {
369 public:
370 void SetUp() override;
371 bool HasResult(const std::string& input, const std::string& locales);
372
373 protected:
374 UniLib unilib_;
375 flatbuffers::FlatBufferBuilder builder_;
376 std::unique_ptr<DatetimeParser> parser_;
377 };
378
AddPattern(const std::string & regex,int locale,std::vector<std::unique_ptr<DatetimeModelPatternT>> * patterns)379 void AddPattern(const std::string& regex, int locale,
380 std::vector<std::unique_ptr<DatetimeModelPatternT>>* patterns) {
381 patterns->emplace_back(new DatetimeModelPatternT);
382 patterns->back()->regexes.emplace_back(new DatetimeModelPattern_::RegexT);
383 patterns->back()->regexes.back()->pattern = regex;
384 patterns->back()->regexes.back()->groups.push_back(
385 DatetimeGroupType_GROUP_UNUSED);
386 patterns->back()->locales.push_back(locale);
387 }
388
SetUp()389 void ParserLocaleTest::SetUp() {
390 DatetimeModelT model;
391 model.use_extractors_for_locating = false;
392 model.locales.clear();
393 model.locales.push_back("en-US");
394 model.locales.push_back("en-CH");
395 model.locales.push_back("zh-Hant");
396 model.locales.push_back("en-*");
397 model.locales.push_back("zh-Hant-*");
398 model.locales.push_back("*-CH");
399 model.locales.push_back("default");
400 model.default_locales.push_back(6);
401
402 AddPattern(/*regex=*/"en-US", /*locale=*/0, &model.patterns);
403 AddPattern(/*regex=*/"en-CH", /*locale=*/1, &model.patterns);
404 AddPattern(/*regex=*/"zh-Hant", /*locale=*/2, &model.patterns);
405 AddPattern(/*regex=*/"en-all", /*locale=*/3, &model.patterns);
406 AddPattern(/*regex=*/"zh-Hant-all", /*locale=*/4, &model.patterns);
407 AddPattern(/*regex=*/"all-CH", /*locale=*/5, &model.patterns);
408 AddPattern(/*regex=*/"default", /*locale=*/6, &model.patterns);
409
410 builder_.Finish(DatetimeModel::Pack(builder_, &model));
411 const DatetimeModel* model_fb =
412 flatbuffers::GetRoot<DatetimeModel>(builder_.GetBufferPointer());
413 ASSERT_TRUE(model_fb);
414
415 parser_ = DatetimeParser::Instance(model_fb, unilib_,
416 /*decompressor=*/nullptr);
417 ASSERT_TRUE(parser_);
418 }
419
HasResult(const std::string & input,const std::string & locales)420 bool ParserLocaleTest::HasResult(const std::string& input,
421 const std::string& locales) {
422 std::vector<DatetimeParseResultSpan> results;
423 EXPECT_TRUE(parser_->Parse(input, /*reference_time_ms_utc=*/0,
424 /*reference_timezone=*/"", locales,
425 ModeFlag_ANNOTATION, false, &results));
426 return results.size() == 1;
427 }
428
TEST_F(ParserLocaleTest,English)429 TEST_F(ParserLocaleTest, English) {
430 EXPECT_TRUE(HasResult("en-US", /*locales=*/"en-US"));
431 EXPECT_FALSE(HasResult("en-CH", /*locales=*/"en-US"));
432 EXPECT_FALSE(HasResult("en-US", /*locales=*/"en-CH"));
433 EXPECT_TRUE(HasResult("en-CH", /*locales=*/"en-CH"));
434 EXPECT_TRUE(HasResult("default", /*locales=*/"en-CH"));
435 }
436
TEST_F(ParserLocaleTest,TraditionalChinese)437 TEST_F(ParserLocaleTest, TraditionalChinese) {
438 EXPECT_TRUE(HasResult("zh-Hant-all", /*locales=*/"zh-Hant"));
439 EXPECT_TRUE(HasResult("zh-Hant-all", /*locales=*/"zh-Hant-TW"));
440 EXPECT_TRUE(HasResult("zh-Hant-all", /*locales=*/"zh-Hant-SG"));
441 EXPECT_FALSE(HasResult("zh-Hant-all", /*locales=*/"zh-SG"));
442 EXPECT_FALSE(HasResult("zh-Hant-all", /*locales=*/"zh"));
443 EXPECT_TRUE(HasResult("default", /*locales=*/"zh"));
444 EXPECT_TRUE(HasResult("default", /*locales=*/"zh-Hant-SG"));
445 }
446
TEST_F(ParserLocaleTest,SwissEnglish)447 TEST_F(ParserLocaleTest, SwissEnglish) {
448 EXPECT_TRUE(HasResult("all-CH", /*locales=*/"de-CH"));
449 EXPECT_TRUE(HasResult("all-CH", /*locales=*/"en-CH"));
450 EXPECT_TRUE(HasResult("en-all", /*locales=*/"en-CH"));
451 EXPECT_FALSE(HasResult("all-CH", /*locales=*/"de-DE"));
452 EXPECT_TRUE(HasResult("default", /*locales=*/"de-CH"));
453 EXPECT_TRUE(HasResult("default", /*locales=*/"en-CH"));
454 }
455
456 } // namespace
457 } // namespace libtextclassifier2
458