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 "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"
9
10namespace {
11
12class GetTargetOutputsTest : public testing::Test {
13 public:
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_);
18  }
19
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);
25  }
26
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());
31  }
32
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());
40  }
41
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());
51  }
52
53 protected:
54  TestWithScope setup_;
55
56  Scope::ItemVector items_;
57};
58
59}  // namespace
60
61TEST_F(GetTargetOutputsTest, Copy) {
62  Target* action = new 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(new scoped_ptr<Item>(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
76TEST_F(GetTargetOutputsTest, Action) {
77  Target* action = new Target(setup_.settings(), GetLabel("//foo/", "bar"));
78  action->set_output_type(Target::ACTION);
79  action->action_values().outputs() = SubstitutionList::MakeForTest(
80      "//output1.txt",
81      "//output2.txt");
82
83  items_.push_back(new scoped_ptr<Item>(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
91TEST_F(GetTargetOutputsTest, ActionForeach) {
92  Target* action = new Target(setup_.settings(), GetLabel("//foo/", "bar"));
93  action->set_output_type(Target::ACTION_FOREACH);
94  action->sources().push_back(SourceFile("//file.txt"));
95  action->action_values().outputs() = SubstitutionList::MakeForTest(
96      "//out/Debug/{{source_file_part}}.one",
97      "//out/Debug/{{source_file_part}}.two");
98
99  items_.push_back(new scoped_ptr<Item>(action));
100
101  Err err;
102  Value result = GetTargetOutputs("//foo:bar", &err);
103  ASSERT_FALSE(err.has_error());
104  AssertTwoStringsEqual(result, "//out/Debug/file.txt.one",
105                        "//out/Debug/file.txt.two");
106}
107