1 // Copyright (c) 2013 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/input_conversion.h"
6
7 #include "gn/err.h"
8 #include "gn/input_file.h"
9 #include "gn/parse_tree.h"
10 #include "gn/scheduler.h"
11 #include "gn/test_with_scheduler.h"
12 #include "gn/test_with_scope.h"
13 #include "gn/value.h"
14 #include "util/test/test.h"
15
16 namespace {
17
18 // InputConversion needs a global scheduler object.
19 class InputConversionTest : public TestWithScheduler {
20 public:
21 InputConversionTest() = default;
22
settings()23 const Settings* settings() { return setup_.settings(); }
24
25 private:
26 TestWithScope setup_;
27 };
28
29 } // namespace
30
TEST_F(InputConversionTest,String)31 TEST_F(InputConversionTest, String) {
32 Err err;
33 std::string input("\nfoo bar \n");
34 Value result = ConvertInputToValue(settings(), input, nullptr,
35 Value(nullptr, "string"), &err);
36 EXPECT_FALSE(err.has_error());
37 EXPECT_EQ(Value::STRING, result.type());
38 EXPECT_EQ(input, result.string_value());
39
40 // Test with trimming.
41 result = ConvertInputToValue(settings(), input, nullptr,
42 Value(nullptr, "trim string"), &err);
43 EXPECT_FALSE(err.has_error());
44 EXPECT_EQ(Value::STRING, result.type());
45 EXPECT_EQ("foo bar", result.string_value());
46 }
47
TEST_F(InputConversionTest,ListLines)48 TEST_F(InputConversionTest, ListLines) {
49 Err err;
50 std::string input("\nfoo\nbar \n\n");
51 Value result = ConvertInputToValue(settings(), input, nullptr,
52 Value(nullptr, "list lines"), &err);
53 EXPECT_FALSE(err.has_error());
54 EXPECT_EQ(Value::LIST, result.type());
55 ASSERT_EQ(4u, result.list_value().size());
56 EXPECT_EQ("", result.list_value()[0].string_value());
57 EXPECT_EQ("foo", result.list_value()[1].string_value());
58 EXPECT_EQ("bar", result.list_value()[2].string_value());
59 EXPECT_EQ("", result.list_value()[3].string_value());
60
61 // Test with trimming.
62 result = ConvertInputToValue(settings(), input, nullptr,
63 Value(nullptr, "trim list lines"), &err);
64 EXPECT_FALSE(err.has_error());
65 EXPECT_EQ(Value::LIST, result.type());
66 ASSERT_EQ(2u, result.list_value().size());
67 EXPECT_EQ("foo", result.list_value()[0].string_value());
68 EXPECT_EQ("bar", result.list_value()[1].string_value());
69 }
70
TEST_F(InputConversionTest,ValueString)71 TEST_F(InputConversionTest, ValueString) {
72 Err err;
73 std::string input("\"str\"");
74 Value result = ConvertInputToValue(settings(), input, nullptr,
75 Value(nullptr, "value"), &err);
76 EXPECT_FALSE(err.has_error());
77 EXPECT_EQ(Value::STRING, result.type());
78 EXPECT_EQ("str", result.string_value());
79 }
80
TEST_F(InputConversionTest,ValueInt)81 TEST_F(InputConversionTest, ValueInt) {
82 Err err;
83 std::string input("\n\n 6 \n ");
84 Value result = ConvertInputToValue(settings(), input, nullptr,
85 Value(nullptr, "value"), &err);
86 EXPECT_FALSE(err.has_error());
87 EXPECT_EQ(Value::INTEGER, result.type());
88 EXPECT_EQ(6, result.int_value());
89 }
90
TEST_F(InputConversionTest,ValueList)91 TEST_F(InputConversionTest, ValueList) {
92 Err err;
93 std::string input("\n [ \"a\", 5]");
94 Value result = ConvertInputToValue(settings(), input, nullptr,
95 Value(nullptr, "value"), &err);
96 EXPECT_FALSE(err.has_error());
97 ASSERT_EQ(Value::LIST, result.type());
98 ASSERT_EQ(2u, result.list_value().size());
99 EXPECT_EQ("a", result.list_value()[0].string_value());
100 EXPECT_EQ(5, result.list_value()[1].int_value());
101 }
102
TEST_F(InputConversionTest,ValueDict)103 TEST_F(InputConversionTest, ValueDict) {
104 Err err;
105 std::string input("\n a = 5 b = \"foo\" c = a + 2");
106 Value result = ConvertInputToValue(settings(), input, nullptr,
107 Value(nullptr, "scope"), &err);
108 EXPECT_FALSE(err.has_error());
109 ASSERT_EQ(Value::SCOPE, result.type());
110
111 const Value* a_value = result.scope_value()->GetValue("a");
112 ASSERT_TRUE(a_value);
113 EXPECT_EQ(5, a_value->int_value());
114
115 const Value* b_value = result.scope_value()->GetValue("b");
116 ASSERT_TRUE(b_value);
117 EXPECT_EQ("foo", b_value->string_value());
118
119 const Value* c_value = result.scope_value()->GetValue("c");
120 ASSERT_TRUE(c_value);
121 EXPECT_EQ(7, c_value->int_value());
122
123 // Tests that when we get Values out of the input conversion, the resulting
124 // values have an origin set to something corresponding to the input.
125 const ParseNode* a_origin = a_value->origin();
126 ASSERT_TRUE(a_origin);
127 LocationRange a_range = a_origin->GetRange();
128 EXPECT_EQ(2, a_range.begin().line_number());
129 EXPECT_EQ(6, a_range.begin().column_number());
130
131 const InputFile* a_file = a_range.begin().file();
132 EXPECT_EQ(input, a_file->contents());
133 }
134
TEST_F(InputConversionTest,ValueJSON)135 TEST_F(InputConversionTest, ValueJSON) {
136 Err err;
137 std::string input(R"*({
138 "a": 5,
139 "b": "foo",
140 "c": {
141 "d": true,
142 "e": [
143 {
144 "f": "bar"
145 }
146 ]
147 }
148 })*");
149 Value result = ConvertInputToValue(settings(), input, nullptr,
150 Value(nullptr, "json"), &err);
151 EXPECT_FALSE(err.has_error());
152 ASSERT_EQ(Value::SCOPE, result.type());
153
154 const Value* a_value = result.scope_value()->GetValue("a");
155 ASSERT_TRUE(a_value);
156 EXPECT_EQ(5, a_value->int_value());
157
158 const Value* b_value = result.scope_value()->GetValue("b");
159 ASSERT_TRUE(b_value);
160 EXPECT_EQ("foo", b_value->string_value());
161
162 const Value* c_value = result.scope_value()->GetValue("c");
163 ASSERT_TRUE(c_value);
164 ASSERT_EQ(Value::SCOPE, c_value->type());
165
166 const Value* d_value = c_value->scope_value()->GetValue("d");
167 ASSERT_TRUE(d_value);
168 EXPECT_EQ(true, d_value->boolean_value());
169
170 const Value* e_value = c_value->scope_value()->GetValue("e");
171 ASSERT_TRUE(e_value);
172 ASSERT_EQ(Value::LIST, e_value->type());
173
174 EXPECT_EQ(1u, e_value->list_value().size());
175 ASSERT_EQ(Value::SCOPE, e_value->list_value()[0].type());
176 const Value* f_value = e_value->list_value()[0].scope_value()->GetValue("f");
177 ASSERT_TRUE(f_value);
178 EXPECT_EQ("bar", f_value->string_value());
179 }
180
TEST_F(InputConversionTest,ValueJSONInvalidInput)181 TEST_F(InputConversionTest, ValueJSONInvalidInput) {
182 Err err;
183 std::string input(R"*({
184 "a": 5,
185 "b":
186 })*");
187 Value result = ConvertInputToValue(settings(), input, nullptr,
188 Value(nullptr, "json"), &err);
189 EXPECT_TRUE(err.has_error());
190 EXPECT_EQ("Input is not a valid JSON: Line: 4, column: 2, Unexpected token.",
191 err.message());
192 }
193
TEST_F(InputConversionTest,ValueJSONUnsupportedValue)194 TEST_F(InputConversionTest, ValueJSONUnsupportedValue) {
195 Err err;
196 std::string input(R"*({
197 "a": null
198 })*");
199 Value result = ConvertInputToValue(settings(), input, nullptr,
200 Value(nullptr, "json"), &err);
201 EXPECT_TRUE(err.has_error());
202 EXPECT_EQ("Null values are not supported.", err.message());
203 }
204
TEST_F(InputConversionTest,ValueJSONInvalidVariable)205 TEST_F(InputConversionTest, ValueJSONInvalidVariable) {
206 Err err;
207 std::string input(R"*({
208 "a\\x0001b": 5
209 })*");
210 Value result = ConvertInputToValue(settings(), input, nullptr,
211 Value(nullptr, "json"), &err);
212 EXPECT_TRUE(err.has_error());
213 EXPECT_EQ("Invalid identifier \"a\\x0001b\".", err.message());
214 }
215
TEST_F(InputConversionTest,ValueJSONUnsupported)216 TEST_F(InputConversionTest, ValueJSONUnsupported) {
217 Err err;
218 std::string input(R"*({
219 "d": 0.0
220 })*");
221 Value result = ConvertInputToValue(settings(), input, nullptr,
222 Value(nullptr, "json"), &err);
223 EXPECT_TRUE(err.has_error());
224 // Doubles aren't supported.
225 EXPECT_EQ("Input is not a valid JSON: ", err.message());
226 }
227
TEST_F(InputConversionTest,ValueEmpty)228 TEST_F(InputConversionTest, ValueEmpty) {
229 Err err;
230 Value result = ConvertInputToValue(settings(), "", nullptr,
231 Value(nullptr, "value"), &err);
232 EXPECT_FALSE(err.has_error());
233 EXPECT_EQ(Value::NONE, result.type());
234 }
235
TEST_F(InputConversionTest,ValueError)236 TEST_F(InputConversionTest, ValueError) {
237 static const char* const kTests[] = {
238 "\n [ \"a\", 5\nfoo bar",
239
240 // Blocks not allowed.
241 "{ foo = 5 }",
242
243 // Function calls not allowed.
244 "print(5)",
245
246 // Trailing junk not allowed.
247 "233105-1",
248
249 // Non-literals hidden in arrays are not allowed.
250 "[233105 - 1]",
251 "[rebase_path(\"//\")]",
252 };
253
254 for (auto* test : kTests) {
255 Err err;
256 std::string input(test);
257 Value result = ConvertInputToValue(settings(), input, nullptr,
258 Value(nullptr, "value"), &err);
259 EXPECT_TRUE(err.has_error()) << test;
260 }
261 }
262
263 // Passing none or the empty string for input conversion should ignore the
264 // result.
TEST_F(InputConversionTest,Ignore)265 TEST_F(InputConversionTest, Ignore) {
266 Err err;
267 Value result = ConvertInputToValue(settings(), "foo", nullptr, Value(), &err);
268 EXPECT_FALSE(err.has_error());
269 EXPECT_EQ(Value::NONE, result.type());
270
271 result =
272 ConvertInputToValue(settings(), "foo", nullptr, Value(nullptr, ""), &err);
273 EXPECT_FALSE(err.has_error());
274 EXPECT_EQ(Value::NONE, result.type());
275 }
276