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.
4
5 #include <memory>
6 #include <utility>
7
8 #include "gn/functions.h"
9 #include "gn/target.h"
10 #include "gn/test_with_scope.h"
11 #include "util/test/test.h"
12
13 namespace {
14
15 class GetTargetOutputsTest : public testing::Test {
16 public:
GetTargetOutputsTest()17 GetTargetOutputsTest() { setup_.scope()->set_item_collector(&items_); }
18
GetTargetOutputs(const std::string & name,Err * err)19 Value GetTargetOutputs(const std::string& name, Err* err) {
20 FunctionCallNode function;
21 std::vector<Value> args;
22 args.push_back(Value(nullptr, name));
23 return functions::RunGetTargetOutputs(setup_.scope(), &function, args, err);
24 }
25
26 // Shortcut to get a label with the current toolchain.
GetLabel(const std::string & dir,const std::string & name)27 Label GetLabel(const std::string& dir, const std::string& name) {
28 return Label(SourceDir(dir), name, setup_.toolchain()->label().dir(),
29 setup_.toolchain()->label().name());
30 }
31
32 // Asserts that the given list contains a single string with the given value.
AssertSingleStringEquals(const Value & list,const std::string & expected)33 void AssertSingleStringEquals(const Value& list,
34 const std::string& expected) {
35 ASSERT_TRUE(list.type() == Value::LIST);
36 ASSERT_EQ(1u, list.list_value().size());
37 ASSERT_TRUE(list.list_value()[0].type() == Value::STRING);
38 ASSERT_EQ(expected, list.list_value()[0].string_value());
39 }
40
AssertTwoStringsEqual(const Value & list,const std::string & expected1,const std::string & expected2)41 void AssertTwoStringsEqual(const Value& list,
42 const std::string& expected1,
43 const std::string& expected2) {
44 ASSERT_TRUE(list.type() == Value::LIST);
45 ASSERT_EQ(2u, list.list_value().size());
46 ASSERT_TRUE(list.list_value()[0].type() == Value::STRING);
47 ASSERT_EQ(expected1, list.list_value()[0].string_value());
48 ASSERT_TRUE(list.list_value()[1].type() == Value::STRING);
49 ASSERT_EQ(expected2, list.list_value()[1].string_value());
50 }
51
52 protected:
53 TestWithScope setup_;
54
55 Scope::ItemVector items_;
56 };
57
58 } // namespace
59
TEST_F(GetTargetOutputsTest,Copy)60 TEST_F(GetTargetOutputsTest, Copy) {
61 auto action =
62 std::make_unique<Target>(setup_.settings(), GetLabel("//foo/", "bar"));
63 action->set_output_type(Target::COPY_FILES);
64 action->sources().push_back(SourceFile("//file.txt"));
65 action->action_values().outputs() =
66 SubstitutionList::MakeForTest("//out/Debug/{{source_file_part}}.one");
67
68 items_.push_back(std::move(action));
69
70 Err err;
71 Value result = GetTargetOutputs("//foo:bar", &err);
72 ASSERT_FALSE(err.has_error());
73 AssertSingleStringEquals(result, "//out/Debug/file.txt.one");
74 }
75
TEST_F(GetTargetOutputsTest,Action)76 TEST_F(GetTargetOutputsTest, Action) {
77 auto action =
78 std::make_unique<Target>(setup_.settings(), GetLabel("//foo/", "bar"));
79 action->set_output_type(Target::ACTION);
80 action->action_values().outputs() =
81 SubstitutionList::MakeForTest("//output1.txt", "//output2.txt");
82
83 items_.push_back(std::move(action));
84
85 Err err;
86 Value result = GetTargetOutputs("//foo:bar", &err);
87 ASSERT_FALSE(err.has_error());
88 AssertTwoStringsEqual(result, "//output1.txt", "//output2.txt");
89 }
90
TEST_F(GetTargetOutputsTest,ActionForeach)91 TEST_F(GetTargetOutputsTest, ActionForeach) {
92 auto action =
93 std::make_unique<Target>(setup_.settings(), GetLabel("//foo/", "bar"));
94 action->set_output_type(Target::ACTION_FOREACH);
95 action->sources().push_back(SourceFile("//file.txt"));
96 action->action_values().outputs() =
97 SubstitutionList::MakeForTest("//out/Debug/{{source_file_part}}.one",
98 "//out/Debug/{{source_file_part}}.two");
99
100 items_.push_back(std::move(action));
101
102 Err err;
103 Value result = GetTargetOutputs("//foo:bar", &err);
104 ASSERT_FALSE(err.has_error());
105 AssertTwoStringsEqual(result, "//out/Debug/file.txt.one",
106 "//out/Debug/file.txt.two");
107 }
108