1 // Copyright 2014 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/functions.h"
7 #include "tools/gn/target.h"
8 #include "tools/gn/test_with_scope.h"
12 class GetTargetOutputsTest
: public testing::Test
{
14 GetTargetOutputsTest() {
15 // Want consistent target names so explicitly set platform.
16 setup_
.settings()->set_target_os(Settings::LINUX
);
17 setup_
.scope()->set_item_collector(&items_
);
20 Value
GetTargetOutputs(const std::string
& name
, Err
* err
) {
21 FunctionCallNode function
;
22 std::vector
<Value
> args
;
23 args
.push_back(Value(NULL
, name
));
24 return functions::RunGetTargetOutputs(setup_
.scope(), &function
, args
, err
);
27 // Shortcut to get a label with the current toolchain.
28 Label
GetLabel(const std::string
& dir
, const std::string
& name
) {
29 return Label(SourceDir(dir
), name
, setup_
.toolchain()->label().dir(),
30 setup_
.toolchain()->label().name());
33 // Asserts that the given list contains a single string with the given value.
34 void AssertSingleStringEquals(const Value
& list
,
35 const std::string
& expected
) {
36 ASSERT_TRUE(list
.type() == Value::LIST
);
37 ASSERT_EQ(1u, list
.list_value().size());
38 ASSERT_TRUE(list
.list_value()[0].type() == Value::STRING
);
39 ASSERT_EQ(expected
, list
.list_value()[0].string_value());
42 void AssertTwoStringsEqual(const Value
& list
,
43 const std::string
& expected1
,
44 const std::string
& expected2
) {
45 ASSERT_TRUE(list
.type() == Value::LIST
);
46 ASSERT_EQ(2u, list
.list_value().size());
47 ASSERT_TRUE(list
.list_value()[0].type() == Value::STRING
);
48 ASSERT_EQ(expected1
, list
.list_value()[0].string_value());
49 ASSERT_TRUE(list
.list_value()[1].type() == Value::STRING
);
50 ASSERT_EQ(expected2
, list
.list_value()[1].string_value());
56 Scope::ItemVector items_
;
61 TEST_F(GetTargetOutputsTest
, Executable
) {
62 Target
* exe
= new Target(setup_
.settings(), GetLabel("//foo/", "bar"));
63 exe
->set_output_type(Target::EXECUTABLE
);
64 items_
.push_back(new scoped_ptr
<Item
>(exe
));
67 Value result
= GetTargetOutputs("//foo:bar", &err
);
68 ASSERT_FALSE(err
.has_error());
69 // The TestWithScope declares that the build is Linux, so we'll have no
70 // extension for the binaries.
71 AssertSingleStringEquals(result
, "//out/Debug/bar");
74 TEST_F(GetTargetOutputsTest
, SourceSet
) {
75 Target
* source_set
= new Target(setup_
.settings(), GetLabel("//foo/", "bar"));
76 source_set
->set_output_type(Target::SOURCE_SET
);
77 items_
.push_back(new scoped_ptr
<Item
>(source_set
));
80 Value result
= GetTargetOutputs("//foo:bar", &err
);
81 ASSERT_FALSE(err
.has_error());
82 AssertSingleStringEquals(result
, "//out/Debug/obj/foo/bar.stamp");
85 TEST_F(GetTargetOutputsTest
, Action
) {
86 Target
* action
= new Target(setup_
.settings(), GetLabel("//foo/", "bar"));
87 action
->set_output_type(Target::ACTION
);
88 action
->action_values().outputs().push_back(SourceFile("//output1.txt"));
89 action
->action_values().outputs().push_back(SourceFile("//output2.txt"));
91 items_
.push_back(new scoped_ptr
<Item
>(action
));
94 Value result
= GetTargetOutputs("//foo:bar", &err
);
95 ASSERT_FALSE(err
.has_error());
96 AssertTwoStringsEqual(result
, "//output1.txt", "//output2.txt");
99 TEST_F(GetTargetOutputsTest
, ActionForeach
) {
100 Target
* action
= new Target(setup_
.settings(), GetLabel("//foo/", "bar"));
101 action
->set_output_type(Target::ACTION_FOREACH
);
102 action
->sources().push_back(SourceFile("//file.txt"));
103 action
->action_values().outputs().push_back(
104 SourceFile("//out/Debug/{{source_file_part}}.one"));
105 action
->action_values().outputs().push_back(
106 SourceFile("//out/Debug/{{source_file_part}}.two"));
108 items_
.push_back(new scoped_ptr
<Item
>(action
));
111 Value result
= GetTargetOutputs("//foo:bar", &err
);
112 ASSERT_FALSE(err
.has_error());
113 AssertTwoStringsEqual(result
, "//out/Debug/file.txt.one",
114 "//out/Debug/file.txt.two");