• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "gn/metadata.h"
6 #include "gn/test_with_scope.h"
7 #include "util/test/test.h"
8 
TEST(MetadataTest,SetContents)9 TEST(MetadataTest, SetContents) {
10   Metadata metadata;
11 
12   ASSERT_TRUE(metadata.contents().empty());
13 
14   Value a_expected(nullptr, Value::LIST);
15   a_expected.list_value().push_back(Value(nullptr, "foo"));
16   Value b_expected(nullptr, Value::LIST);
17   b_expected.list_value().push_back(Value(nullptr, true));
18 
19   Metadata::Contents contents;
20   contents.insert(std::pair<std::string_view, Value>("a", a_expected));
21   contents.insert(std::pair<std::string_view, Value>("b", b_expected));
22 
23   metadata.set_contents(std::move(contents));
24 
25   ASSERT_EQ(metadata.contents().size(), 2);
26   auto a_actual = metadata.contents().find("a");
27   auto b_actual = metadata.contents().find("b");
28   ASSERT_FALSE(a_actual == metadata.contents().end());
29   ASSERT_FALSE(b_actual == metadata.contents().end());
30   ASSERT_EQ(a_actual->second, a_expected);
31   ASSERT_EQ(b_actual->second, b_expected);
32 }
33 
TEST(MetadataTest,Walk)34 TEST(MetadataTest, Walk) {
35   TestWithScope setup;
36   Metadata metadata;
37   metadata.set_source_dir(SourceDir("/usr/home/files/"));
38 
39   Value a_expected(nullptr, Value::LIST);
40   a_expected.list_value().push_back(Value(nullptr, "foo.cpp"));
41   a_expected.list_value().push_back(Value(nullptr, "bar.h"));
42 
43   metadata.contents().insert(
44       std::pair<std::string_view, Value>("a", a_expected));
45 
46   std::vector<std::string> data_keys;
47   data_keys.emplace_back("a");
48   std::vector<std::string> walk_keys;
49   std::vector<Value> next_walk_keys;
50   std::vector<Value> results;
51 
52   std::vector<Value> expected;
53   expected.emplace_back(Value(nullptr, "foo.cpp"));
54   expected.emplace_back(Value(nullptr, "bar.h"));
55 
56   std::vector<Value> expected_walk_keys;
57   expected_walk_keys.emplace_back(nullptr, "");
58 
59   Err err;
60   EXPECT_TRUE(metadata.WalkStep(setup.settings()->build_settings(), data_keys,
61                                 walk_keys, SourceDir(), &next_walk_keys,
62                                 &results, &err));
63   EXPECT_FALSE(err.has_error());
64   EXPECT_EQ(next_walk_keys, expected_walk_keys);
65   EXPECT_EQ(results, expected);
66 }
67 
TEST(MetadataTest,WalkWithRebase)68 TEST(MetadataTest, WalkWithRebase) {
69   TestWithScope setup;
70   Metadata metadata;
71   metadata.set_source_dir(SourceDir("/usr/home/files/"));
72 
73   Value a_expected(nullptr, Value::LIST);
74   a_expected.list_value().push_back(Value(nullptr, "foo.cpp"));
75   a_expected.list_value().push_back(Value(nullptr, "foo/bar.h"));
76 
77   metadata.contents().insert(
78       std::pair<std::string_view, Value>("a", a_expected));
79 
80   std::vector<std::string> data_keys;
81   data_keys.emplace_back("a");
82   std::vector<std::string> walk_keys;
83   std::vector<Value> next_walk_keys;
84   std::vector<Value> results;
85 
86   std::vector<Value> expected;
87   expected.emplace_back(Value(nullptr, "../home/files/foo.cpp"));
88   expected.emplace_back(Value(nullptr, "../home/files/foo/bar.h"));
89 
90   std::vector<Value> expected_walk_keys;
91   expected_walk_keys.emplace_back(nullptr, "");
92 
93   Err err;
94   EXPECT_TRUE(metadata.WalkStep(setup.settings()->build_settings(), data_keys,
95                                 walk_keys, SourceDir("/usr/foo_dir/"),
96                                 &next_walk_keys, &results, &err));
97   EXPECT_FALSE(err.has_error());
98   EXPECT_EQ(next_walk_keys, expected_walk_keys);
99   EXPECT_EQ(results, expected);
100 }
101 
TEST(MetadataTest,WalkWithRebaseNonString)102 TEST(MetadataTest, WalkWithRebaseNonString) {
103   TestWithScope setup;
104   Metadata metadata;
105   metadata.set_source_dir(SourceDir("/usr/home/files/"));
106 
107   Value a(nullptr, Value::LIST);
108   Value inner_list(nullptr, Value::LIST);
109   Value inner_scope(nullptr, Value::SCOPE);
110   inner_list.list_value().push_back(Value(nullptr, "foo.cpp"));
111   inner_list.list_value().push_back(Value(nullptr, "foo/bar.h"));
112   a.list_value().push_back(inner_list);
113 
114   std::unique_ptr<Scope> scope(new Scope(setup.settings()));
115   scope->SetValue("a1", Value(nullptr, "foo2.cpp"), nullptr);
116   scope->SetValue("a2", Value(nullptr, "foo/bar2.h"), nullptr);
117   inner_scope.SetScopeValue(std::move(scope));
118   a.list_value().push_back(inner_scope);
119 
120   metadata.contents().insert(std::pair<std::string_view, Value>("a", a));
121   std::vector<std::string> data_keys;
122   data_keys.emplace_back("a");
123   std::vector<std::string> walk_keys;
124   std::vector<Value> next_walk_keys;
125   std::vector<Value> results;
126 
127   std::vector<Value> expected;
128   Value inner_list_expected(nullptr, Value::LIST);
129   Value inner_scope_expected(nullptr, Value::SCOPE);
130   inner_list_expected.list_value().push_back(
131       Value(nullptr, "../home/files/foo.cpp"));
132   inner_list_expected.list_value().push_back(
133       Value(nullptr, "../home/files/foo/bar.h"));
134   expected.push_back(inner_list_expected);
135 
136   std::unique_ptr<Scope> scope_expected(new Scope(setup.settings()));
137   scope_expected->SetValue("a1", Value(nullptr, "../home/files/foo2.cpp"),
138                            nullptr);
139   scope_expected->SetValue("a2", Value(nullptr, "../home/files/foo/bar2.h"),
140                            nullptr);
141   inner_scope_expected.SetScopeValue(std::move(scope_expected));
142   expected.push_back(inner_scope_expected);
143 
144   std::vector<Value> expected_walk_keys;
145   expected_walk_keys.emplace_back(nullptr, "");
146 
147   Err err;
148   EXPECT_TRUE(metadata.WalkStep(setup.settings()->build_settings(), data_keys,
149                                 walk_keys, SourceDir("/usr/foo_dir/"),
150                                 &next_walk_keys, &results, &err));
151   EXPECT_FALSE(err.has_error());
152   EXPECT_EQ(next_walk_keys, expected_walk_keys);
153   EXPECT_EQ(results, expected);
154 }
155 
TEST(MetadataTest,WalkKeysToWalk)156 TEST(MetadataTest, WalkKeysToWalk) {
157   TestWithScope setup;
158   Metadata metadata;
159   metadata.set_source_dir(SourceDir("/usr/home/files/"));
160 
161   Value a_expected(nullptr, Value::LIST);
162   a_expected.list_value().push_back(Value(nullptr, "//target"));
163 
164   metadata.contents().insert(
165       std::pair<std::string_view, Value>("a", a_expected));
166 
167   std::vector<std::string> data_keys;
168   std::vector<std::string> walk_keys;
169   walk_keys.emplace_back("a");
170   std::vector<Value> next_walk_keys;
171   std::vector<Value> results;
172 
173   std::vector<Value> expected_walk_keys;
174   expected_walk_keys.emplace_back(nullptr, "//target");
175 
176   Err err;
177   EXPECT_TRUE(metadata.WalkStep(setup.settings()->build_settings(), data_keys,
178                                 walk_keys, SourceDir(), &next_walk_keys,
179                                 &results, &err));
180   EXPECT_FALSE(err.has_error());
181   EXPECT_EQ(next_walk_keys, expected_walk_keys);
182   EXPECT_TRUE(results.empty());
183 }
184 
TEST(MetadataTest,WalkNoContents)185 TEST(MetadataTest, WalkNoContents) {
186   TestWithScope setup;
187   Metadata metadata;
188   metadata.set_source_dir(SourceDir("/usr/home/files/"));
189 
190   std::vector<std::string> data_keys;
191   std::vector<std::string> walk_keys;
192   std::vector<Value> next_walk_keys;
193   std::vector<Value> results;
194 
195   std::vector<Value> expected_walk_keys;
196   expected_walk_keys.emplace_back(nullptr, "");
197 
198   Err err;
199   EXPECT_TRUE(metadata.WalkStep(setup.settings()->build_settings(), data_keys,
200                                 walk_keys, SourceDir(), &next_walk_keys,
201                                 &results, &err));
202   EXPECT_FALSE(err.has_error());
203   EXPECT_EQ(next_walk_keys, expected_walk_keys);
204   EXPECT_TRUE(results.empty());
205 }
206 
TEST(MetadataTest,WalkNoKeysWithContents)207 TEST(MetadataTest, WalkNoKeysWithContents) {
208   TestWithScope setup;
209   Metadata metadata;
210   metadata.set_source_dir(SourceDir("/usr/home/files/"));
211 
212   Value a_expected(nullptr, Value::LIST);
213   a_expected.list_value().push_back(Value(nullptr, "//target"));
214 
215   metadata.contents().insert(
216       std::pair<std::string_view, Value>("a", a_expected));
217 
218   std::vector<std::string> data_keys;
219   std::vector<std::string> walk_keys;
220   std::vector<Value> next_walk_keys;
221   std::vector<Value> results;
222 
223   std::vector<Value> expected_walk_keys;
224   expected_walk_keys.emplace_back(nullptr, "");
225 
226   Err err;
227   EXPECT_TRUE(metadata.WalkStep(setup.settings()->build_settings(), data_keys,
228                                 walk_keys, SourceDir(), &next_walk_keys,
229                                 &results, &err));
230   EXPECT_FALSE(err.has_error());
231   EXPECT_EQ(next_walk_keys, expected_walk_keys);
232   EXPECT_TRUE(results.empty());
233 }
234