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; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // variable in the scope before it. | 43 // variable in the scope before it. |
44 TestParseInput clobber("x = 1\n" + program); | 44 TestParseInput clobber("x = 1\n" + program); |
45 ASSERT_FALSE(clobber.has_error()); | 45 ASSERT_FALSE(clobber.has_error()); |
46 | 46 |
47 clobber.parsed()->Execute(setup.scope(), &err); | 47 clobber.parsed()->Execute(setup.scope(), &err); |
48 ASSERT_TRUE(err.has_error()); // Should thow a clobber error. | 48 ASSERT_TRUE(err.has_error()); // Should thow a clobber error. |
49 EXPECT_EQ("Clobbering existing value.", err.message()); | 49 EXPECT_EQ("Clobbering existing value.", err.message()); |
50 } | 50 } |
51 } | 51 } |
52 | 52 |
| 53 TEST(FunctionForwardVariablesFrom, LiteralList) { |
| 54 Scheduler scheduler; |
| 55 TestWithScope setup; |
| 56 |
| 57 // Forwards all variables from a literal scope into another scope definition. |
| 58 TestParseInput input( |
| 59 "a = {\n" |
| 60 " forward_variables_from({x = 1 y = 2}, \"*\")\n" |
| 61 " z = 3\n" |
| 62 "}\n" |
| 63 "print(\"${a.x} ${a.y} ${a.z}\")\n"); |
| 64 |
| 65 ASSERT_FALSE(input.has_error()); |
| 66 |
| 67 Err err; |
| 68 input.parsed()->Execute(setup.scope(), &err); |
| 69 ASSERT_FALSE(err.has_error()) << err.message(); |
| 70 |
| 71 EXPECT_EQ("1 2 3\n", setup.print_output()); |
| 72 setup.print_output().clear(); |
| 73 } |
| 74 |
53 TEST(FunctionForwardVariablesFrom, ListWithExclusion) { | 75 TEST(FunctionForwardVariablesFrom, ListWithExclusion) { |
54 Scheduler scheduler; | 76 Scheduler scheduler; |
55 TestWithScope setup; | 77 TestWithScope setup; |
56 | 78 |
57 // Defines a template and copy the two x and y, and z values out. | 79 // Defines a template and copy the two x and y, and z values out. |
58 TestParseInput input( | 80 TestParseInput input( |
59 "template(\"a\") {\n" | 81 "template(\"a\") {\n" |
60 " forward_variables_from(invoker, [\"x\", \"y\", \"z\"], [\"z\"])\n" | 82 " forward_variables_from(invoker, [\"x\", \"y\", \"z\"], [\"z\"])\n" |
61 " assert(!defined(z))\n" // "z" should still be undefined. | 83 " assert(!defined(z))\n" // "z" should still be undefined. |
62 " print(\"$target_name, $x, $y\")\n" | 84 " print(\"$target_name, $x, $y\")\n" |
(...skipping 24 matching lines...) Expand all Loading... |
87 "template(\"a\") {\n" | 109 "template(\"a\") {\n" |
88 " forward_variables_from(42, [\"x\"])\n" | 110 " forward_variables_from(42, [\"x\"])\n" |
89 " print(\"$target_name\")\n" // Prevent unused var error. | 111 " print(\"$target_name\")\n" // Prevent unused var error. |
90 "}\n" | 112 "}\n" |
91 "a(\"target\") {\n" | 113 "a(\"target\") {\n" |
92 "}\n"); | 114 "}\n"); |
93 ASSERT_FALSE(invalid_source.has_error()); | 115 ASSERT_FALSE(invalid_source.has_error()); |
94 Err err; | 116 Err err; |
95 invalid_source.parsed()->Execute(setup.scope(), &err); | 117 invalid_source.parsed()->Execute(setup.scope(), &err); |
96 EXPECT_TRUE(err.has_error()); | 118 EXPECT_TRUE(err.has_error()); |
97 EXPECT_EQ("Expected an identifier for the scope.", err.message()); | 119 EXPECT_EQ("This is not a scope.", err.message()); |
98 | 120 |
99 // Type check the list. We need to use a new template name each time since | 121 // Type check the list. We need to use a new template name each time since |
100 // all of these invocations are executing in sequence in the same scope. | 122 // all of these invocations are executing in sequence in the same scope. |
101 TestParseInput invalid_list( | 123 TestParseInput invalid_list( |
102 "template(\"b\") {\n" | 124 "template(\"b\") {\n" |
103 " forward_variables_from(invoker, 42)\n" | 125 " forward_variables_from(invoker, 42)\n" |
104 " print(\"$target_name\")\n" | 126 " print(\"$target_name\")\n" |
105 "}\n" | 127 "}\n" |
106 "b(\"target\") {\n" | 128 "b(\"target\") {\n" |
107 "}\n"); | 129 "}\n"); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 | 239 |
218 ASSERT_FALSE(input.has_error()); | 240 ASSERT_FALSE(input.has_error()); |
219 | 241 |
220 Err err; | 242 Err err; |
221 input.parsed()->Execute(setup.scope(), &err); | 243 input.parsed()->Execute(setup.scope(), &err); |
222 ASSERT_FALSE(err.has_error()) << err.message(); | 244 ASSERT_FALSE(err.has_error()) << err.message(); |
223 | 245 |
224 EXPECT_EQ("3\ntarget, 1, 2\n", setup.print_output()); | 246 EXPECT_EQ("3\ntarget, 1, 2\n", setup.print_output()); |
225 setup.print_output().clear(); | 247 setup.print_output().clear(); |
226 } | 248 } |
OLD | NEW |