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/flatbuffers/reflection.h"
18
19 #include "utils/flatbuffers/flatbuffers_generated.h"
20 #include "utils/flatbuffers/test-utils.h"
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "flatbuffers/reflection_generated.h"
24
25 namespace libtextclassifier3 {
26 namespace {
27
TEST(ReflectionTest,ResolvesFieldOffsets)28 TEST(ReflectionTest, ResolvesFieldOffsets) {
29 std::string metadata_buffer = LoadTestMetadata();
30 const reflection::Schema* schema =
31 flatbuffers::GetRoot<reflection::Schema>(metadata_buffer.data());
32 FlatbufferFieldPathT path;
33 path.field.emplace_back(new FlatbufferFieldT);
34 path.field.back()->field_name = "flight_number";
35 path.field.emplace_back(new FlatbufferFieldT);
36 path.field.back()->field_name = "carrier_code";
37
38 EXPECT_TRUE(SwapFieldNamesForOffsetsInPath(schema, &path));
39
40 EXPECT_THAT(path.field[0]->field_name, testing::IsEmpty());
41 EXPECT_EQ(14, path.field[0]->field_offset);
42 EXPECT_THAT(path.field[1]->field_name, testing::IsEmpty());
43 EXPECT_EQ(4, path.field[1]->field_offset);
44 }
45
TEST(ReflectionTest,ParseEnumValuesByName)46 TEST(ReflectionTest, ParseEnumValuesByName) {
47 std::string metadata_buffer = LoadTestMetadata();
48 const reflection::Schema* schema =
49 flatbuffers::GetRoot<reflection::Schema>(metadata_buffer.data());
50 const reflection::Field* field =
51 GetFieldOrNull(schema->root_table(), "enum_value");
52
53 Variant enum_value_0 = ParseEnumValue(schema, field->type(), "VALUE_0");
54 Variant enum_value_1 = ParseEnumValue(schema, field->type(), "VALUE_1");
55 Variant enum_no_value = ParseEnumValue(schema, field->type(), "NO_VALUE");
56
57 EXPECT_TRUE(enum_value_0.HasValue());
58 EXPECT_EQ(enum_value_0.GetType(), Variant::TYPE_INT_VALUE);
59 EXPECT_EQ(enum_value_0.Value<int>(), 0);
60
61 EXPECT_TRUE(enum_value_1.HasValue());
62 EXPECT_EQ(enum_value_1.GetType(), Variant::TYPE_INT_VALUE);
63 EXPECT_EQ(enum_value_1.Value<int>(), 1);
64
65 EXPECT_FALSE(enum_no_value.HasValue());
66 }
67
68 } // namespace
69 } // namespace libtextclassifier3
70