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 #include "utils/grammar/semantics/evaluators/merge-values-eval.h"
18
19 #include <vector>
20
21 #include "utils/base/statusor.h"
22 #include "utils/flatbuffers/flatbuffers.h"
23 #include "utils/flatbuffers/reflection.h"
24 #include "utils/flatbuffers/test-utils.h"
25 #include "utils/grammar/semantics/evaluator.h"
26 #include "utils/grammar/semantics/evaluators/const-eval.h"
27 #include "utils/grammar/semantics/expression_generated.h"
28 #include "utils/grammar/testing/utils.h"
29 #include "utils/grammar/testing/value_generated.h"
30 #include "gmock/gmock.h"
31 #include "gtest/gtest.h"
32 #include "flatbuffers/flatbuffers.h"
33
34 namespace libtextclassifier3::grammar {
35 namespace {
36
37 class MergeValuesEvaluatorTest : public GrammarTest {
38 protected:
MergeValuesEvaluatorTest()39 explicit MergeValuesEvaluatorTest()
40 : const_eval_(semantic_values_schema_.get()) {}
41
42 // Evaluator that just returns a constant value.
43 ConstEvaluator const_eval_;
44 };
45
TEST_F(MergeValuesEvaluatorTest,MergeSemanticValues)46 TEST_F(MergeValuesEvaluatorTest, MergeSemanticValues) {
47 // Setup the data
48 TestDateT date_value_day;
49 date_value_day.day = 23;
50 TestDateT date_value_month;
51 date_value_month.month = 9;
52 TestDateT date_value_year;
53 date_value_year.year = 2019;
54
55 OwnedFlatbuffer<SemanticExpression> expression =
56 CreateAndPackMergeValuesExpression(
57 {date_value_day, date_value_month, date_value_year});
58
59 MergeValuesEvaluator merge_values_eval(&const_eval_,
60 semantic_values_schema_.get());
61
62 StatusOr<const SemanticValue*> result =
63 merge_values_eval.Apply(/*context=*/{}, expression.get(), &arena_);
64
65 EXPECT_TRUE(result.ok());
66 const SemanticValue* result_value = result.ValueOrDie();
67 ASSERT_NE(result_value, nullptr);
68 EXPECT_EQ(result_value->type()->name()->str(),
69 "libtextclassifier3.grammar.TestDate");
70 const TestDate* result_test_date = result_value->Table<TestDate>();
71 EXPECT_EQ(result_test_date->day(), 23);
72 EXPECT_EQ(result_test_date->month(), 9);
73 EXPECT_EQ(result_test_date->year(), 2019);
74 }
75
76 } // namespace
77 } // namespace libtextclassifier3::grammar
78