summaryrefslogtreecommitdiff
path: root/chromium/tools/gn/function_forward_variables_from_unittest.cc
blob: 84d8b6eaf7acb23635a57c5ffad19f3c93506740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "testing/gtest/include/gtest/gtest.h"
#include "tools/gn/scheduler.h"
#include "tools/gn/test_with_scope.h"

TEST(FunctionForwardVariablesFrom, List) {
  Scheduler scheduler;
  Err err;
  std::string program =
      "template(\"a\") {\n"
      "  forward_variables_from(invoker, [\"x\", \"y\", \"z\"])\n"
      "  assert(!defined(z))\n"  // "z" should still be undefined.
      "  print(\"$target_name, $x, $y\")\n"
      "}\n"
      "a(\"target\") {\n"
      "  x = 1\n"
      "  y = 2\n"
      "}\n";

  {
    TestWithScope setup;

    // Defines a template and copy the two x and y, and z values out.
    TestParseInput input(program);
    ASSERT_FALSE(input.has_error());

    input.parsed()->Execute(setup.scope(), &err);
    ASSERT_FALSE(err.has_error()) << err.message();

    EXPECT_EQ("target, 1, 2\n", setup.print_output());
    setup.print_output().clear();
  }

  {
    TestWithScope setup;

    // Test that the same input but forwarding a variable with the name of
    // something in the given scope throws an error rather than clobbering it.
    // This uses the same known-good program as before, but adds another
    // variable in the scope before it.
    TestParseInput clobber("x = 1\n" + program);
    ASSERT_FALSE(clobber.has_error());

    clobber.parsed()->Execute(setup.scope(), &err);
    ASSERT_TRUE(err.has_error());  // Should thow a clobber error.
    EXPECT_EQ("Clobbering existing value.", err.message());
  }
}

TEST(FunctionForwardVariablesFrom, LiteralList) {
  Scheduler scheduler;
  TestWithScope setup;

  // Forwards all variables from a literal scope into another scope definition.
  TestParseInput input(
    "a = {\n"
    "  forward_variables_from({x = 1 y = 2}, \"*\")\n"
    "  z = 3\n"
    "}\n"
    "print(\"${a.x} ${a.y} ${a.z}\")\n");

  ASSERT_FALSE(input.has_error());

  Err err;
  input.parsed()->Execute(setup.scope(), &err);
  ASSERT_FALSE(err.has_error()) << err.message();

  EXPECT_EQ("1 2 3\n", setup.print_output());
  setup.print_output().clear();
}

TEST(FunctionForwardVariablesFrom, ListWithExclusion) {
  Scheduler scheduler;
  TestWithScope setup;

  // Defines a template and copy the two x and y, and z values out.
  TestParseInput input(
    "template(\"a\") {\n"
    "  forward_variables_from(invoker, [\"x\", \"y\", \"z\"], [\"z\"])\n"
    "  assert(!defined(z))\n"  // "z" should still be undefined.
    "  print(\"$target_name, $x, $y\")\n"
    "}\n"
    "a(\"target\") {\n"
    "  x = 1\n"
    "  y = 2\n"
    "  z = 3\n"
    "  print(\"$z\")\n"
    "}\n");

  ASSERT_FALSE(input.has_error());

  Err err;
  input.parsed()->Execute(setup.scope(), &err);
  ASSERT_FALSE(err.has_error()) << err.message();

  EXPECT_EQ("3\ntarget, 1, 2\n", setup.print_output());
  setup.print_output().clear();
}

TEST(FunctionForwardVariablesFrom, ErrorCases) {
  Scheduler scheduler;
  TestWithScope setup;

  // Type check the source scope.
  TestParseInput invalid_source(
    "template(\"a\") {\n"
    "  forward_variables_from(42, [\"x\"])\n"
    "  print(\"$target_name\")\n"  // Prevent unused var error.
    "}\n"
    "a(\"target\") {\n"
    "}\n");
  ASSERT_FALSE(invalid_source.has_error());
  Err err;
  invalid_source.parsed()->Execute(setup.scope(), &err);
  EXPECT_TRUE(err.has_error());
  EXPECT_EQ("This is not a scope.", err.message());

  // Type check the list. We need to use a new template name each time since
  // all of these invocations are executing in sequence in the same scope.
  TestParseInput invalid_list(
    "template(\"b\") {\n"
    "  forward_variables_from(invoker, 42)\n"
    "  print(\"$target_name\")\n"
    "}\n"
    "b(\"target\") {\n"
    "}\n");
  ASSERT_FALSE(invalid_list.has_error());
  err = Err();
  invalid_list.parsed()->Execute(setup.scope(), &err);
  EXPECT_TRUE(err.has_error());
  EXPECT_EQ("Not a valid list of variables to copy.", err.message());

  // Type check the exclusion list.
  TestParseInput invalid_exclusion_list(
    "template(\"c\") {\n"
    "  forward_variables_from(invoker, \"*\", 42)\n"
    "  print(\"$target_name\")\n"
    "}\n"
    "c(\"target\") {\n"
    "}\n");
  ASSERT_FALSE(invalid_exclusion_list.has_error());
  err = Err();
  invalid_exclusion_list.parsed()->Execute(setup.scope(), &err);
  EXPECT_TRUE(err.has_error());
  EXPECT_EQ("Not a valid list of variables to exclude.", err.message());

  // Programmatic values should error.
  TestParseInput prog(
    "template(\"d\") {\n"
    "  forward_variables_from(invoker, [\"root_out_dir\"])\n"
    "  print(\"$target_name\")\n"
    "}\n"
    "d(\"target\") {\n"
    "}\n");
  ASSERT_FALSE(prog.has_error());
  err = Err();
  prog.parsed()->Execute(setup.scope(), &err);
  EXPECT_TRUE(err.has_error());
  EXPECT_EQ("This value can't be forwarded.", err.message());

  // Not enough arguments.
  TestParseInput not_enough_arguments(
    "template(\"e\") {\n"
    "  forward_variables_from(invoker)\n"
    "  print(\"$target_name\")\n"
    "}\n"
    "e(\"target\") {\n"
    "}\n");
  ASSERT_FALSE(not_enough_arguments.has_error());
  err = Err();
  not_enough_arguments.parsed()->Execute(setup.scope(), &err);
  EXPECT_TRUE(err.has_error());
  EXPECT_EQ("Wrong number of arguments.", err.message());

  // Too many arguments.
  TestParseInput too_many_arguments(
    "template(\"f\") {\n"
    "  forward_variables_from(invoker, \"*\", [], [])\n"
    "  print(\"$target_name\")\n"
    "}\n"
    "f(\"target\") {\n"
    "}\n");
  ASSERT_FALSE(too_many_arguments.has_error());
  err = Err();
  too_many_arguments.parsed()->Execute(setup.scope(), &err);
  EXPECT_TRUE(err.has_error());
  EXPECT_EQ("Wrong number of arguments.", err.message());
}

TEST(FunctionForwardVariablesFrom, Star) {
  Scheduler scheduler;
  TestWithScope setup;

  // Defines a template and copy the two x and y values out. The "*" behavior
  // should clobber existing variables with the same name.
  TestParseInput input(
    "template(\"a\") {\n"
    "  x = 1000000\n"  // Should be clobbered.
    "  forward_variables_from(invoker, \"*\")\n"
    "  print(\"$target_name, $x, $y\")\n"
    "}\n"
    "a(\"target\") {\n"
    "  x = 1\n"
    "  y = 2\n"
    "}\n");

  ASSERT_FALSE(input.has_error());

  Err err;
  input.parsed()->Execute(setup.scope(), &err);
  ASSERT_FALSE(err.has_error()) << err.message();

  EXPECT_EQ("target, 1, 2\n", setup.print_output());
  setup.print_output().clear();
}


TEST(FunctionForwardVariablesFrom, StarWithExclusion) {
  Scheduler scheduler;
  TestWithScope setup;

  // Defines a template and copy all values except z value. The "*" behavior
  // should clobber existing variables with the same name.
  TestParseInput input(
    "template(\"a\") {\n"
    "  x = 1000000\n"  // Should be clobbered.
    "  forward_variables_from(invoker, \"*\", [\"z\"])\n"
    "  print(\"$target_name, $x, $y\")\n"
    "}\n"
    "a(\"target\") {\n"
    "  x = 1\n"
    "  y = 2\n"
    "  z = 3\n"
    "  print(\"$z\")\n"
    "}\n");

  ASSERT_FALSE(input.has_error());

  Err err;
  input.parsed()->Execute(setup.scope(), &err);
  ASSERT_FALSE(err.has_error()) << err.message();

  EXPECT_EQ("3\ntarget, 1, 2\n", setup.print_output());
  setup.print_output().clear();
}