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 // Common test utils.
18 
19 #ifndef LIBTEXTCLASSIFIER_UTILS_FLATBUFFERS_TEST_UTILS_H_
20 #define LIBTEXTCLASSIFIER_UTILS_FLATBUFFERS_TEST_UTILS_H_
21 
22 #include <fstream>
23 #include <string>
24 
25 #include "utils/flatbuffers/flatbuffers.h"
26 #include "utils/flatbuffers/flatbuffers_generated.h"
27 #include "utils/test-data-test-utils.h"
28 #include "gtest/gtest.h"
29 
30 namespace libtextclassifier3 {
31 
LoadTestMetadata()32 inline std::string LoadTestMetadata() {
33   std::ifstream test_config_stream(
34       GetTestDataPath("utils/flatbuffers/flatbuffers_test_extended.bfbs"));
35   return std::string((std::istreambuf_iterator<char>(test_config_stream)),
36                      (std::istreambuf_iterator<char>()));
37 }
38 
39 // Creates a flatbuffer field path from a dot separated field path string.
CreateUnpackedFieldPath(const std::vector<std::string> & fields)40 inline std::unique_ptr<FlatbufferFieldPathT> CreateUnpackedFieldPath(
41     const std::vector<std::string>& fields) {
42   std::unique_ptr<FlatbufferFieldPathT> path(new FlatbufferFieldPathT);
43   for (const std::string& field : fields) {
44     path->field.emplace_back(new FlatbufferFieldT);
45     path->field.back()->field_name = field;
46   }
47   return path;
48 }
49 
CreateFieldPath(const std::vector<std::string> & fields)50 inline OwnedFlatbuffer<FlatbufferFieldPath, std::string> CreateFieldPath(
51     const std::vector<std::string>& fields) {
52   std::unique_ptr<FlatbufferFieldPathT> path = CreateUnpackedFieldPath(fields);
53   return OwnedFlatbuffer<FlatbufferFieldPath, std::string>(
54       PackFlatbuffer<FlatbufferFieldPath>(path.get()));
55 }
56 
57 }  // namespace libtextclassifier3
58 
59 #endif  // LIBTEXTCLASSIFIER_UTILS_FLATBUFFERS_TEST_UTILS_H_
60