1 // Copyright (C) 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "include/instruction_test.h"
16
17 #include <dirent.h>
18
19 #include <ditto/parser.h>
20
string_has_suffix(const std::string & input,const std::string & suffix)21 bool string_has_suffix(const std::string& input, const std::string& suffix) {
22 if (input.length() < suffix.length()) {
23 return false;
24 }
25 return (!input.compare(input.length() - suffix.length(), suffix.length(), suffix));
26 }
27
28 /**
29 * For compatibility with C++ versions prior to C++17 that do not support
30 * `recursive_directory_iterator`, recurse the directory manually.
31 */
__ditto_files_paths(std::vector<std::string> * ditto_paths,const std::string & base_path)32 void __ditto_files_paths(std::vector<std::string>* ditto_paths, const std::string& base_path) {
33 struct dirent* entry;
34 DIR* directory = opendir(base_path.c_str());
35
36 std::cout << "Recursing in: " << base_path << std::endl;
37 while ((entry = readdir(directory)) != nullptr) {
38 std::string path = base_path + "/" + entry->d_name;
39 std::cout << "Found: " << path << std::endl;
40 switch (entry->d_type) {
41 case DT_REG: {
42 if (string_has_suffix(entry->d_name, ".ditto")) {
43 ditto_paths->push_back(path);
44 }
45 break;
46 }
47 case DT_DIR: {
48 if (std::string(".").compare(entry->d_name) && std::string("..").compare(entry->d_name)) {
49 __ditto_files_paths(ditto_paths, path);
50 }
51 break;
52 }
53 }
54 }
55
56 closedir(directory);
57 }
58
ditto_files_paths()59 std::vector<std::string> ditto_files_paths() {
60 std::string base_path;
61 std::vector<std::string> paths;
62
63 if (absolute_path.empty()) {
64 base_path = "example";
65 } else {
66 base_path = absolute_path + "/example";
67 }
68
69 __ditto_files_paths(&paths, base_path);
70 return paths;
71 }
72
73 class ExampleTest : public testing::TestWithParam<std::string> {};
74
75 // Test each .ditto file inside example/ folder to make sure that all of them follow the schema
76 // If .ditto is parsed successfully, exit with code 0 is expected
TEST_P(ExampleTest,IsParsable)77 TEST_P(ExampleTest, IsParsable) {
78 std::string file_path = GetParam();
79 std::cout << "Testing file path: " << GetParam() << std::endl;
80 #ifndef __ANDROID__
81 if (std::string::npos != file_path.rfind("example/android/")) {
82 GTEST_SKIP();
83 }
84 #else
85 EXPECT_EXIT(
86 {
87 dittosuite::Parser::GetParser().ParseFile(file_path, {});
88 exit(0);
89 },
90 testing::ExitedWithCode(0), "");
91 #endif
92 }
93
94 INSTANTIATE_TEST_SUITE_P(DittoFile, ExampleTest, testing::ValuesIn(ditto_files_paths()));
95