Home | History | Annotate | Download | only in gn
      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 
     10 namespace {
     11 
     12 class 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 
     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));
     65 
     66   Err err;
     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");
     72 }
     73 
     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));
     78 
     79   Err err;
     80   Value result = GetTargetOutputs("//foo:bar", &err);
     81   ASSERT_FALSE(err.has_error());
     82   AssertSingleStringEquals(result, "//out/Debug/obj/foo/bar.stamp");
     83 }
     84 
     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"));
     90 
     91   items_.push_back(new scoped_ptr<Item>(action));
     92 
     93   Err err;
     94   Value result = GetTargetOutputs("//foo:bar", &err);
     95   ASSERT_FALSE(err.has_error());
     96   AssertTwoStringsEqual(result, "//output1.txt", "//output2.txt");
     97 }
     98 
     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"));
    107 
    108   items_.push_back(new scoped_ptr<Item>(action));
    109 
    110   Err err;
    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");
    115 }
    116