1 /*
2  * Copyright (C) 2018 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 #ifndef LIBTEXTCLASSIFIER_ANNOTATOR_TEST_UTILS_H_
18 #define LIBTEXTCLASSIFIER_ANNOTATOR_TEST_UTILS_H_
19 
20 #include "annotator/types.h"
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 
24 namespace libtextclassifier3 {
25 
26 using ::testing::Value;
27 
28 MATCHER_P3(IsAnnotatedSpan, start, end, best_class, "") {
29   const std::string first_result = arg.classification.empty()
30                                        ? "<INVALID RESULTS>"
31                                        : arg.classification[0].collection;
32   return Value(arg.span, CodepointSpan(start, end)) &&
33          Value(first_result, best_class);
34 }
35 
36 MATCHER_P(IsAnnotationWithType, best_class, "") {
37   const std::string first_result = arg.classification.empty()
38                                        ? "<INVALID RESULTS>"
39                                        : arg.classification[0].collection;
40   return Value(first_result, best_class);
41 }
42 
43 MATCHER_P2(IsDateResult, time_ms_utc, granularity, "") {
44   return Value(arg.collection, "date") &&
45          Value(arg.datetime_parse_result.time_ms_utc, time_ms_utc) &&
46          Value(arg.datetime_parse_result.granularity, granularity);
47 }
48 
49 MATCHER_P2(IsDatetimeResult, time_ms_utc, granularity, "") {
50   return Value(arg.collection, "datetime") &&
51          Value(arg.datetime_parse_result.time_ms_utc, time_ms_utc) &&
52          Value(arg.datetime_parse_result.granularity, granularity);
53 }
54 
55 MATCHER_P3(IsDurationSpan, start, end, duration_ms, "") {
56   if (arg.classification.empty()) {
57     return false;
58   }
59   return ExplainMatchResult(IsAnnotatedSpan(start, end, "duration"), arg,
60                             result_listener) &&
61          arg.classification[0].duration_ms == duration_ms;
62 }
63 
64 MATCHER_P4(IsDatetimeSpan, start, end, time_ms_utc, granularity, "") {
65   if (arg.classification.empty()) {
66     return false;
67   }
68   return ExplainMatchResult(IsAnnotatedSpan(start, end, "datetime"), arg,
69                             result_listener) &&
70          arg.classification[0].datetime_parse_result.time_ms_utc ==
71              time_ms_utc &&
72          arg.classification[0].datetime_parse_result.granularity == granularity;
73 }
74 
75 MATCHER_P2(IsBetween, low, high, "") { return low < arg && arg < high; }
76 
77 }  // namespace libtextclassifier3
78 
79 #endif  // LIBTEXTCLASSIFIER_ANNOTATOR_TEST_UTILS_H_
80