forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_get_target_outputs_unittest.cc
130 lines (107 loc) · 4.57 KB
/
function_get_target_outputs_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2014 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/functions.h"
#include "tools/gn/target.h"
#include "tools/gn/test_with_scope.h"
namespace {
class GetTargetOutputsTest : public testing::Test {
public:
GetTargetOutputsTest() {
// Want consistent target names so explicitly set platform.
setup_.settings()->set_target_os(Settings::LINUX);
setup_.scope()->set_item_collector(&items_);
}
Value GetTargetOutputs(const std::string& name, Err* err) {
FunctionCallNode function;
std::vector<Value> args;
args.push_back(Value(NULL, name));
return functions::RunGetTargetOutputs(setup_.scope(), &function, args, err);
}
// Shortcut to get a label with the current toolchain.
Label GetLabel(const std::string& dir, const std::string& name) {
return Label(SourceDir(dir), name, setup_.toolchain()->label().dir(),
setup_.toolchain()->label().name());
}
// Asserts that the given list contains a single string with the given value.
void AssertSingleStringEquals(const Value& list,
const std::string& expected) {
ASSERT_TRUE(list.type() == Value::LIST);
ASSERT_EQ(1u, list.list_value().size());
ASSERT_TRUE(list.list_value()[0].type() == Value::STRING);
ASSERT_EQ(expected, list.list_value()[0].string_value());
}
void AssertTwoStringsEqual(const Value& list,
const std::string& expected1,
const std::string& expected2) {
ASSERT_TRUE(list.type() == Value::LIST);
ASSERT_EQ(2u, list.list_value().size());
ASSERT_TRUE(list.list_value()[0].type() == Value::STRING);
ASSERT_EQ(expected1, list.list_value()[0].string_value());
ASSERT_TRUE(list.list_value()[1].type() == Value::STRING);
ASSERT_EQ(expected2, list.list_value()[1].string_value());
}
protected:
TestWithScope setup_;
Scope::ItemVector items_;
};
} // namespace
TEST_F(GetTargetOutputsTest, Executable) {
Target* exe = new Target(setup_.settings(), GetLabel("//foo/", "bar"));
exe->set_output_type(Target::EXECUTABLE);
items_.push_back(new scoped_ptr<Item>(exe));
Err err;
Value result = GetTargetOutputs("//foo:bar", &err);
ASSERT_FALSE(err.has_error());
// The TestWithScope declares that the build is Linux, so we'll have no
// extension for the binaries.
AssertSingleStringEquals(result, "//out/Debug/bar");
}
TEST_F(GetTargetOutputsTest, SourceSet) {
Target* source_set = new Target(setup_.settings(), GetLabel("//foo/", "bar"));
source_set->set_output_type(Target::SOURCE_SET);
items_.push_back(new scoped_ptr<Item>(source_set));
Err err;
Value result = GetTargetOutputs("//foo:bar", &err);
ASSERT_FALSE(err.has_error());
AssertSingleStringEquals(result, "//out/Debug/obj/foo/bar.stamp");
}
TEST_F(GetTargetOutputsTest, Copy) {
Target* action = new Target(setup_.settings(), GetLabel("//foo/", "bar"));
action->set_output_type(Target::COPY_FILES);
action->sources().push_back(SourceFile("//file.txt"));
action->action_values().outputs() =
SubstitutionList::MakeForTest("//out/Debug/{{source_file_part}}.one");
items_.push_back(new scoped_ptr<Item>(action));
Err err;
Value result = GetTargetOutputs("//foo:bar", &err);
ASSERT_FALSE(err.has_error());
AssertSingleStringEquals(result, "//out/Debug/file.txt.one");
}
TEST_F(GetTargetOutputsTest, Action) {
Target* action = new Target(setup_.settings(), GetLabel("//foo/", "bar"));
action->set_output_type(Target::ACTION);
action->action_values().outputs() = SubstitutionList::MakeForTest(
"//output1.txt",
"//output2.txt");
items_.push_back(new scoped_ptr<Item>(action));
Err err;
Value result = GetTargetOutputs("//foo:bar", &err);
ASSERT_FALSE(err.has_error());
AssertTwoStringsEqual(result, "//output1.txt", "//output2.txt");
}
TEST_F(GetTargetOutputsTest, ActionForeach) {
Target* action = new Target(setup_.settings(), GetLabel("//foo/", "bar"));
action->set_output_type(Target::ACTION_FOREACH);
action->sources().push_back(SourceFile("//file.txt"));
action->action_values().outputs() = SubstitutionList::MakeForTest(
"//out/Debug/{{source_file_part}}.one",
"//out/Debug/{{source_file_part}}.two");
items_.push_back(new scoped_ptr<Item>(action));
Err err;
Value result = GetTargetOutputs("//foo:bar", &err);
ASSERT_FALSE(err.has_error());
AssertTwoStringsEqual(result, "//out/Debug/file.txt.one",
"//out/Debug/file.txt.two");
}