• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 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 "utils/config_file.h"
16 
17 #include <sstream>
18 #include <string>
19 #include <vector>
20 
21 #include <gtest/gtest.h>
22 
23 
24 namespace header_checker {
25 namespace utils {
26 
27 
TEST(ConfigFileTest,Parse)28 TEST(ConfigFileTest, Parse) {
29   std::stringstream stream(R"(
30 // Comment line starts with slash
31 /* embedded comment */
32 {
33   "global": {
34     "ignore_linker_set_keys": [
35       "set_key1",
36       "set_key3",
37     ],
38     "flags": {
39       "key1": true,
40       "key2": false,
41     },
42   },
43   "library1": [
44     {
45       "target_version": "current",
46       "ignore_linker_set_keys": [
47         "set_key1",
48         "set_key2",
49       ],
50       "flags": {
51         "key1": true,
52         "key2": false,
53       },
54     },
55     {
56       "target_version": "34",
57       "flags": {
58         "key1": false,
59         "key2": true,
60       },
61     },
62   ],
63 }
64 )");
65 
66   ConfigFile cfg;
67   cfg.Load(stream);
68   EXPECT_TRUE(cfg.HasSection("global", ""));
69   EXPECT_TRUE(cfg.HasSection("library1", "current"));
70   EXPECT_FALSE(cfg.HasSection("library1", "35"));
71   EXPECT_FALSE(cfg.HasSection("library2", "current"));
72 
73   auto &&section1 = cfg.GetSection("global", "");
74   EXPECT_TRUE(section1.HasProperty("key1"));
75   EXPECT_TRUE(section1.GetProperty("key1"));
76   EXPECT_TRUE(section1.HasProperty("key2"));
77   EXPECT_FALSE(section1.GetProperty("key2"));
78 
79   EXPECT_FALSE(section1.HasProperty("key3"));
80   EXPECT_FALSE(section1.GetProperty("key3"));
81   EXPECT_EQ(std::vector<std::string>({"set_key1", "set_key3"}), section1.GetIgnoredLinkerSetKeys());
82 
83   auto &&section2 = cfg.GetSection("library1", "current");
84   EXPECT_TRUE(section2.HasProperty("key1"));
85   EXPECT_TRUE(section2.GetProperty("key1"));
86   EXPECT_TRUE(section2.HasProperty("key2"));
87   EXPECT_FALSE(section2.GetProperty("key2"));
88   EXPECT_EQ(std::vector<std::string>({"set_key1", "set_key2"}), section2.GetIgnoredLinkerSetKeys());
89 
90   EXPECT_TRUE(cfg.GetProperty("global", "", "key1"));
91   EXPECT_TRUE(cfg.GetProperty("library1", "34", "key2"));
92 
93   EXPECT_TRUE(section1["key1"]);
94   EXPECT_FALSE(section2["key2"]);
95 }
96 
TEST(ConfigFileTest,BadJsonFormat)97 TEST(ConfigFileTest, BadJsonFormat) {
98   std::stringstream stream(R"(
99 {
100   "section1: {
101   }
102 }
103 )");
104 
105   ConfigFile cfg;
106   bool result = cfg.Load(stream);
107 
108   EXPECT_FALSE(result);
109 }
110 
111 
112 }  // namespace utils
113 }  // namespace header_checker
114