OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 #include "tools/gn/scheduler.h" | 6 #include "tools/gn/scheduler.h" |
7 #include "tools/gn/test_with_scope.h" | 7 #include "tools/gn/test_with_scope.h" |
8 | 8 |
9 TEST(FunctionForwardVariablesFrom, List) { | 9 TEST(FunctionForwardVariablesFrom, List) { |
10 Scheduler scheduler; | 10 Scheduler scheduler; |
11 TestWithScope setup; | 11 Err err; |
| 12 std::string program = |
| 13 "template(\"a\") {\n" |
| 14 " forward_variables_from(invoker, [\"x\", \"y\", \"z\"])\n" |
| 15 " assert(!defined(z))\n" // "z" should still be undefined. |
| 16 " print(\"$target_name, $x, $y\")\n" |
| 17 "}\n" |
| 18 "a(\"target\") {\n" |
| 19 " x = 1\n" |
| 20 " y = 2\n" |
| 21 "}\n"; |
12 | 22 |
13 // Defines a template and copy the two x and y, and z values out. | 23 { |
14 TestParseInput input( | 24 TestWithScope setup; |
15 "template(\"a\") {\n" | |
16 " forward_variables_from(invoker, [\"x\", \"y\", \"z\"])\n" | |
17 " assert(!defined(z))\n" // "z" should still be undefined. | |
18 " print(\"$target_name, $x, $y\")\n" | |
19 "}\n" | |
20 "a(\"target\") {\n" | |
21 " x = 1\n" | |
22 " y = 2\n" | |
23 "}\n"); | |
24 | 25 |
25 ASSERT_FALSE(input.has_error()); | 26 // Defines a template and copy the two x and y, and z values out. |
| 27 TestParseInput input(program); |
| 28 ASSERT_FALSE(input.has_error()); |
26 | 29 |
27 Err err; | 30 input.parsed()->Execute(setup.scope(), &err); |
28 input.parsed()->Execute(setup.scope(), &err); | 31 ASSERT_FALSE(err.has_error()) << err.message(); |
29 ASSERT_FALSE(err.has_error()) << err.message(); | |
30 | 32 |
31 EXPECT_EQ("target, 1, 2\n", setup.print_output()); | 33 EXPECT_EQ("target, 1, 2\n", setup.print_output()); |
32 setup.print_output().clear(); | 34 setup.print_output().clear(); |
| 35 } |
| 36 |
| 37 { |
| 38 TestWithScope setup; |
| 39 |
| 40 // Test that the same input but forwarding a variable with the name of |
| 41 // something in the given scope throws an error rather than clobbering it. |
| 42 // This uses the same known-good program as before, but adds another |
| 43 // variable in the scope before it. |
| 44 TestParseInput clobber("x = 1\n" + program); |
| 45 ASSERT_FALSE(clobber.has_error()); |
| 46 |
| 47 clobber.parsed()->Execute(setup.scope(), &err); |
| 48 ASSERT_TRUE(err.has_error()); // Should thow a clobber error. |
| 49 EXPECT_EQ("Clobbering existing value.", err.message()); |
| 50 } |
33 } | 51 } |
34 | 52 |
35 TEST(FunctionForwardVariablesFrom, ListWithExclusion) { | 53 TEST(FunctionForwardVariablesFrom, ListWithExclusion) { |
36 Scheduler scheduler; | 54 Scheduler scheduler; |
37 TestWithScope setup; | 55 TestWithScope setup; |
38 | 56 |
39 // Defines a template and copy the two x and y, and z values out. | 57 // Defines a template and copy the two x and y, and z values out. |
40 TestParseInput input( | 58 TestParseInput input( |
41 "template(\"a\") {\n" | 59 "template(\"a\") {\n" |
42 " forward_variables_from(invoker, [\"x\", \"y\", \"z\"], [\"z\"])\n" | 60 " forward_variables_from(invoker, [\"x\", \"y\", \"z\"], [\"z\"])\n" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 217 |
200 ASSERT_FALSE(input.has_error()); | 218 ASSERT_FALSE(input.has_error()); |
201 | 219 |
202 Err err; | 220 Err err; |
203 input.parsed()->Execute(setup.scope(), &err); | 221 input.parsed()->Execute(setup.scope(), &err); |
204 ASSERT_FALSE(err.has_error()) << err.message(); | 222 ASSERT_FALSE(err.has_error()) << err.message(); |
205 | 223 |
206 EXPECT_EQ("3\ntarget, 1, 2\n", setup.print_output()); | 224 EXPECT_EQ("3\ntarget, 1, 2\n", setup.print_output()); |
207 setup.print_output().clear(); | 225 setup.print_output().clear(); |
208 } | 226 } |
OLD | NEW |