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 14 matching lines...) Expand all Loading... |
25 ASSERT_FALSE(input.has_error()); | 25 ASSERT_FALSE(input.has_error()); |
26 | 26 |
27 Err err; | 27 Err err; |
28 input.parsed()->Execute(setup.scope(), &err); | 28 input.parsed()->Execute(setup.scope(), &err); |
29 ASSERT_FALSE(err.has_error()) << err.message(); | 29 ASSERT_FALSE(err.has_error()) << err.message(); |
30 | 30 |
31 EXPECT_EQ("target, 1, 2\n", setup.print_output()); | 31 EXPECT_EQ("target, 1, 2\n", setup.print_output()); |
32 setup.print_output().clear(); | 32 setup.print_output().clear(); |
33 } | 33 } |
34 | 34 |
| 35 TEST(FunctionForwardVariablesFrom, ListWithExclusion) { |
| 36 Scheduler scheduler; |
| 37 TestWithScope setup; |
| 38 |
| 39 // Defines a template and copy the two x and y, and z values out. |
| 40 TestParseInput input( |
| 41 "template(\"a\") {\n" |
| 42 " forward_variables_from(invoker, [\"x\", \"y\", \"z\"], [\"z\"])\n" |
| 43 " assert(!defined(z))\n" // "z" should still be undefined. |
| 44 " print(\"$target_name, $x, $y\")\n" |
| 45 "}\n" |
| 46 "a(\"target\") {\n" |
| 47 " x = 1\n" |
| 48 " y = 2\n" |
| 49 " z = 3\n" |
| 50 " print(\"$z\")\n" |
| 51 "}\n"); |
| 52 |
| 53 ASSERT_FALSE(input.has_error()); |
| 54 |
| 55 Err err; |
| 56 input.parsed()->Execute(setup.scope(), &err); |
| 57 ASSERT_FALSE(err.has_error()) << err.message(); |
| 58 |
| 59 EXPECT_EQ("3\ntarget, 1, 2\n", setup.print_output()); |
| 60 setup.print_output().clear(); |
| 61 } |
| 62 |
35 TEST(FunctionForwardVariablesFrom, ErrorCases) { | 63 TEST(FunctionForwardVariablesFrom, ErrorCases) { |
36 Scheduler scheduler; | 64 Scheduler scheduler; |
37 TestWithScope setup; | 65 TestWithScope setup; |
38 | 66 |
39 // Type check the source scope. | 67 // Type check the source scope. |
40 TestParseInput invalid_source( | 68 TestParseInput invalid_source( |
41 "template(\"a\") {\n" | 69 "template(\"a\") {\n" |
42 " forward_variables_from(42, [\"x\"])\n" | 70 " forward_variables_from(42, [\"x\"])\n" |
43 " print(\"$target_name\")\n" // Prevent unused var error. | 71 " print(\"$target_name\")\n" // Prevent unused var error. |
44 "}\n" | 72 "}\n" |
(...skipping 13 matching lines...) Expand all Loading... |
58 " print(\"$target_name\")\n" | 86 " print(\"$target_name\")\n" |
59 "}\n" | 87 "}\n" |
60 "b(\"target\") {\n" | 88 "b(\"target\") {\n" |
61 "}\n"); | 89 "}\n"); |
62 ASSERT_FALSE(invalid_list.has_error()); | 90 ASSERT_FALSE(invalid_list.has_error()); |
63 err = Err(); | 91 err = Err(); |
64 invalid_list.parsed()->Execute(setup.scope(), &err); | 92 invalid_list.parsed()->Execute(setup.scope(), &err); |
65 EXPECT_TRUE(err.has_error()); | 93 EXPECT_TRUE(err.has_error()); |
66 EXPECT_EQ("Not a valid list of variables to copy.", err.message()); | 94 EXPECT_EQ("Not a valid list of variables to copy.", err.message()); |
67 | 95 |
| 96 // Type check the exclusion list. |
| 97 TestParseInput invalid_exclusion_list( |
| 98 "template(\"c\") {\n" |
| 99 " forward_variables_from(invoker, \"*\", 42)\n" |
| 100 " print(\"$target_name\")\n" |
| 101 "}\n" |
| 102 "c(\"target\") {\n" |
| 103 "}\n"); |
| 104 ASSERT_FALSE(invalid_exclusion_list.has_error()); |
| 105 err = Err(); |
| 106 invalid_exclusion_list.parsed()->Execute(setup.scope(), &err); |
| 107 EXPECT_TRUE(err.has_error()); |
| 108 EXPECT_EQ("Not a valid list of variables to exclude.", err.message()); |
| 109 |
68 // Programmatic values should error. | 110 // Programmatic values should error. |
69 TestParseInput prog( | 111 TestParseInput prog( |
70 "template(\"c\") {\n" | 112 "template(\"d\") {\n" |
71 " forward_variables_from(invoker, [\"root_out_dir\"])\n" | 113 " forward_variables_from(invoker, [\"root_out_dir\"])\n" |
72 " print(\"$target_name\")\n" | 114 " print(\"$target_name\")\n" |
73 "}\n" | 115 "}\n" |
74 "c(\"target\") {\n" | 116 "d(\"target\") {\n" |
75 "}\n"); | 117 "}\n"); |
76 ASSERT_FALSE(prog.has_error()); | 118 ASSERT_FALSE(prog.has_error()); |
77 err = Err(); | 119 err = Err(); |
78 prog.parsed()->Execute(setup.scope(), &err); | 120 prog.parsed()->Execute(setup.scope(), &err); |
79 EXPECT_TRUE(err.has_error()); | 121 EXPECT_TRUE(err.has_error()); |
80 EXPECT_EQ("This value can't be forwarded.", err.message()); | 122 EXPECT_EQ("This value can't be forwarded.", err.message()); |
| 123 |
| 124 // Not enough arguments. |
| 125 TestParseInput not_enough_arguments( |
| 126 "template(\"e\") {\n" |
| 127 " forward_variables_from(invoker)\n" |
| 128 " print(\"$target_name\")\n" |
| 129 "}\n" |
| 130 "e(\"target\") {\n" |
| 131 "}\n"); |
| 132 ASSERT_FALSE(not_enough_arguments.has_error()); |
| 133 err = Err(); |
| 134 not_enough_arguments.parsed()->Execute(setup.scope(), &err); |
| 135 EXPECT_TRUE(err.has_error()); |
| 136 EXPECT_EQ("Wrong number of arguments.", err.message()); |
| 137 |
| 138 // Too many arguments. |
| 139 TestParseInput too_many_arguments( |
| 140 "template(\"f\") {\n" |
| 141 " forward_variables_from(invoker, \"*\", [], [])\n" |
| 142 " print(\"$target_name\")\n" |
| 143 "}\n" |
| 144 "f(\"target\") {\n" |
| 145 "}\n"); |
| 146 ASSERT_FALSE(too_many_arguments.has_error()); |
| 147 err = Err(); |
| 148 too_many_arguments.parsed()->Execute(setup.scope(), &err); |
| 149 EXPECT_TRUE(err.has_error()); |
| 150 EXPECT_EQ("Wrong number of arguments.", err.message()); |
81 } | 151 } |
82 | 152 |
83 TEST(FunctionForwardVariablesFrom, Star) { | 153 TEST(FunctionForwardVariablesFrom, Star) { |
84 Scheduler scheduler; | 154 Scheduler scheduler; |
85 TestWithScope setup; | 155 TestWithScope setup; |
86 | 156 |
87 // Defines a template and copy the two x and y values out. The "*" behavior | 157 // Defines a template and copy the two x and y values out. The "*" behavior |
88 // should clobber existing variables with the same name. | 158 // should clobber existing variables with the same name. |
89 TestParseInput input( | 159 TestParseInput input( |
90 "template(\"a\") {\n" | 160 "template(\"a\") {\n" |
91 " x = 1000000\n" // Should be clobbered. | 161 " x = 1000000\n" // Should be clobbered. |
92 " forward_variables_from(invoker, \"*\")\n" | 162 " forward_variables_from(invoker, \"*\")\n" |
93 " print(\"$target_name, $x, $y\")\n" | 163 " print(\"$target_name, $x, $y\")\n" |
94 "}\n" | 164 "}\n" |
95 "a(\"target\") {\n" | 165 "a(\"target\") {\n" |
96 " x = 1\n" | 166 " x = 1\n" |
97 " y = 2\n" | 167 " y = 2\n" |
98 "}\n"); | 168 "}\n"); |
99 | 169 |
100 ASSERT_FALSE(input.has_error()); | 170 ASSERT_FALSE(input.has_error()); |
101 | 171 |
102 Err err; | 172 Err err; |
103 input.parsed()->Execute(setup.scope(), &err); | 173 input.parsed()->Execute(setup.scope(), &err); |
104 ASSERT_FALSE(err.has_error()) << err.message(); | 174 ASSERT_FALSE(err.has_error()) << err.message(); |
105 | 175 |
106 EXPECT_EQ("target, 1, 2\n", setup.print_output()); | 176 EXPECT_EQ("target, 1, 2\n", setup.print_output()); |
107 setup.print_output().clear(); | 177 setup.print_output().clear(); |
108 } | 178 } |
| 179 |
| 180 |
| 181 TEST(FunctionForwardVariablesFrom, StarWithExclusion) { |
| 182 Scheduler scheduler; |
| 183 TestWithScope setup; |
| 184 |
| 185 // Defines a template and copy all values except z value. The "*" behavior |
| 186 // should clobber existing variables with the same name. |
| 187 TestParseInput input( |
| 188 "template(\"a\") {\n" |
| 189 " x = 1000000\n" // Should be clobbered. |
| 190 " forward_variables_from(invoker, \"*\", [\"z\"])\n" |
| 191 " print(\"$target_name, $x, $y\")\n" |
| 192 "}\n" |
| 193 "a(\"target\") {\n" |
| 194 " x = 1\n" |
| 195 " y = 2\n" |
| 196 " z = 3\n" |
| 197 " print(\"$z\")\n" |
| 198 "}\n"); |
| 199 |
| 200 ASSERT_FALSE(input.has_error()); |
| 201 |
| 202 Err err; |
| 203 input.parsed()->Execute(setup.scope(), &err); |
| 204 ASSERT_FALSE(err.has_error()) << err.message(); |
| 205 |
| 206 EXPECT_EQ("3\ntarget, 1, 2\n", setup.print_output()); |
| 207 setup.print_output().clear(); |
| 208 } |
OLD | NEW |