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/span-eval.h"
18
19 #include "utils/flatbuffers/flatbuffers.h"
20 #include "utils/grammar/semantics/expression_generated.h"
21 #include "utils/grammar/testing/utils.h"
22 #include "utils/grammar/testing/value_generated.h"
23 #include "utils/grammar/types.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 SpanTextEvaluatorTest : public GrammarTest {};
32
TEST_F(SpanTextEvaluatorTest,CreatesSpanTextValues)33 TEST_F(SpanTextEvaluatorTest, CreatesSpanTextValues) {
34 OwnedFlatbuffer<SemanticExpression> expression =
35 CreateExpression(SpanAsStringExpressionT());
36 SpanAsStringEvaluator span_eval;
37 TextContext text = TextContextForText("This a test.");
38 ParseTree derivation(/*lhs=*/kUnassignedNonterm, CodepointSpan{5, 11},
39 /*match_offset=*/0, /*type=*/ParseTree::Type::kDefault);
40
41 StatusOr<const SemanticValue*> result = span_eval.Apply(
42 /*context=*/{&text, &derivation}, expression.get(), &arena_);
43
44 ASSERT_TRUE(result.ok());
45 EXPECT_EQ(result.ValueOrDie()->Value<StringPiece>().ToString(), "a test");
46 }
47
48 } // namespace
49 } // namespace libtextclassifier3::grammar
50