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
3 // found in the LICENSE file.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/scheduler.h"
7 #include "tools/gn/test_with_scope.h"
9 TEST(FunctionForwardVariablesFrom
, List
) {
13 // Defines a template and copy the two x and y, and z values out.
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"
25 ASSERT_FALSE(input
.has_error());
28 input
.parsed()->Execute(setup
.scope(), &err
);
29 ASSERT_FALSE(err
.has_error()) << err
.message();
31 EXPECT_EQ("target, 1, 2\n", setup
.print_output());
32 setup
.print_output().clear();
35 TEST(FunctionForwardVariablesFrom
, ErrorCases
) {
39 // Type check the source scope.
40 TestParseInput
invalid_source(
42 " forward_variables_from(42, [\"x\"])\n"
43 " print(\"$target_name\")\n" // Prevent unused var error.
47 ASSERT_FALSE(invalid_source
.has_error());
49 invalid_source
.parsed()->Execute(setup
.scope(), &err
);
50 EXPECT_TRUE(err
.has_error());
51 EXPECT_EQ("Expected an identifier for the scope.", err
.message());
53 // Type check the list. We need to use a new template name each time since
54 // all of these invocations are executing in sequence in the same scope.
55 TestParseInput
invalid_list(
57 " forward_variables_from(invoker, 42)\n"
58 " print(\"$target_name\")\n"
62 ASSERT_FALSE(invalid_list
.has_error());
64 invalid_list
.parsed()->Execute(setup
.scope(), &err
);
65 EXPECT_TRUE(err
.has_error());
66 EXPECT_EQ("Not a valid list of variables to copy.", err
.message());
68 // Programmatic values should error.
71 " forward_variables_from(invoker, [\"root_out_dir\"])\n"
72 " print(\"$target_name\")\n"
76 ASSERT_FALSE(prog
.has_error());
78 prog
.parsed()->Execute(setup
.scope(), &err
);
79 EXPECT_TRUE(err
.has_error());
80 EXPECT_EQ("This value can't be forwarded.", err
.message());
83 TEST(FunctionForwardVariablesFrom
, Star
) {
87 // Defines a template and copy the two x and y values out. The "*" behavior
88 // should clobber existing variables with the same name.
91 " x = 1000000\n" // Should be clobbered.
92 " forward_variables_from(invoker, \"*\")\n"
93 " print(\"$target_name, $x, $y\")\n"
100 ASSERT_FALSE(input
.has_error());
103 input
.parsed()->Execute(setup
.scope(), &err
);
104 ASSERT_FALSE(err
.has_error()) << err
.message();
106 EXPECT_EQ("target, 1, 2\n", setup
.print_output());
107 setup
.print_output().clear();