• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/constituent-eval.h"
18 
19 #include "utils/flatbuffers/flatbuffers.h"
20 #include "utils/grammar/semantics/eval-context.h"
21 #include "utils/grammar/semantics/expression_generated.h"
22 #include "utils/grammar/testing/utils.h"
23 #include "utils/grammar/testing/value_generated.h"
24 #include "gmock/gmock.h"
25 #include "gtest/gtest.h"
26 #include "flatbuffers/flatbuffers.h"
27 
28 namespace libtextclassifier3::grammar {
29 namespace {
30 
31 class ConstituentEvaluatorTest : public GrammarTest {
32  protected:
ConstituentEvaluatorTest()33   explicit ConstituentEvaluatorTest() {}
34 
CreateConstituentExpression(const int id)35   OwnedFlatbuffer<SemanticExpression> CreateConstituentExpression(
36       const int id) {
37     ConstituentExpressionT constituent_expression;
38     constituent_expression.id = id;
39     return CreateExpression(constituent_expression);
40   }
41 
42   const ConstituentEvaluator constituent_eval_;
43 };
44 
TEST_F(ConstituentEvaluatorTest,HandlesNotDefinedConstituents)45 TEST_F(ConstituentEvaluatorTest, HandlesNotDefinedConstituents) {
46   OwnedFlatbuffer<SemanticExpression> expression =
47       CreateConstituentExpression(/*id=*/42);
48 
49   StatusOr<const SemanticValue*> result = constituent_eval_.Apply(
50       /*context=*/{}, expression.get(), /*arena=*/nullptr);
51 
52   EXPECT_TRUE(result.ok());
53   EXPECT_EQ(result.ValueOrDie(), nullptr);
54 }
55 
TEST_F(ConstituentEvaluatorTest,ForwardsConstituentSemanticValues)56 TEST_F(ConstituentEvaluatorTest, ForwardsConstituentSemanticValues) {
57   // Create example values for constituents.
58   EvalContext context;
59   TestValueT value_0;
60   value_0.test_string = "constituent 0 value";
61   context.rule_constituents[0] = CreateSemanticValue(value_0);
62 
63   TestValueT value_42;
64   value_42.test_string = "constituent 42 value";
65   context.rule_constituents[42] = CreateSemanticValue(value_42);
66 
67   OwnedFlatbuffer<SemanticExpression> expression =
68       CreateConstituentExpression(/*id=*/42);
69 
70   StatusOr<const SemanticValue*> result =
71       constituent_eval_.Apply(context, expression.get(), /*arena=*/nullptr);
72 
73   EXPECT_TRUE(result.ok());
74   const TestValue* result_value = result.ValueOrDie()->Table<TestValue>();
75   EXPECT_EQ(result_value->test_string()->str(), "constituent 42 value");
76 }
77 
78 }  // namespace
79 }  // namespace libtextclassifier3::grammar
80