forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_forward_variables_from_unittest.cc
108 lines (93 loc) · 3.16 KB
/
function_forward_variables_from_unittest.cc
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
// 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;
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\"])\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");
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, 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("Expected an identifier for the 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());
// Programmatic values should error.
TestParseInput prog(
"template(\"c\") {\n"
" forward_variables_from(invoker, [\"root_out_dir\"])\n"
" print(\"$target_name\")\n"
"}\n"
"c(\"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());
}
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();
}