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/test_with_scope.h"
8
9namespace {
10
11class GetLabelInfoTest : public testing::Test {
12 public:
13  GetLabelInfoTest() : testing::Test() {
14    setup_.scope()->set_source_dir(SourceDir("//src/foo/"));
15  }
16
17  // Convenience wrapper to call GetLabelInfo.
18  std::string Call(const std::string& label, const std::string& what) {
19    FunctionCallNode function;
20
21    std::vector<Value> args;
22    args.push_back(Value(NULL, label));
23    args.push_back(Value(NULL, what));
24
25    Err err;
26    Value result = functions::RunGetLabelInfo(setup_.scope(), &function,
27                                              args, &err);
28    if (err.has_error()) {
29      EXPECT_TRUE(result.type() == Value::NONE);
30      return std::string();
31    }
32    return result.string_value();
33  }
34
35 protected:
36  // Note: TestWithScope's default toolchain is "//toolchain:default" and
37  // output dir is "//out/Debug".
38  TestWithScope setup_;
39};
40
41}  // namespace
42
43TEST_F(GetLabelInfoTest, BadInput) {
44  EXPECT_EQ("", Call(":name", "incorrect_value"));
45  EXPECT_EQ("", Call("", "name"));
46}
47
48TEST_F(GetLabelInfoTest, Name) {
49  EXPECT_EQ("name", Call(":name", "name"));
50  EXPECT_EQ("name", Call("//foo/bar:name", "name"));
51}
52
53TEST_F(GetLabelInfoTest, Dir) {
54  EXPECT_EQ("//src/foo", Call(":name", "dir"));
55  EXPECT_EQ("//foo/bar", Call("//foo/bar:baz", "dir"));
56}
57
58TEST_F(GetLabelInfoTest, RootOutDir) {
59  EXPECT_EQ("//out/Debug", Call(":name", "root_out_dir"));
60  EXPECT_EQ("//out/Debug/random",
61            Call(":name(//toolchain:random)", "root_out_dir"));
62}
63
64TEST_F(GetLabelInfoTest, RootGetDir) {
65  EXPECT_EQ("//out/Debug/gen", Call(":name", "root_gen_dir"));
66  EXPECT_EQ("//out/Debug/random/gen",
67            Call(":name(//toolchain:random)", "root_gen_dir"));
68}
69
70TEST_F(GetLabelInfoTest, TargetOutDir) {
71  EXPECT_EQ("//out/Debug/obj/src/foo", Call(":name", "target_out_dir"));
72  EXPECT_EQ("//out/Debug", Call(":name", "root_out_dir"));
73}
74
75TEST_F(GetLabelInfoTest, LabelNoToolchain) {
76  EXPECT_EQ("//src/foo:name", Call(":name", "label_no_toolchain"));
77  EXPECT_EQ("//src/foo:name",
78            Call("//src/foo:name(//toolchain:random)", "label_no_toolchain"));
79}
80
81TEST_F(GetLabelInfoTest, LabelWithToolchain) {
82  EXPECT_EQ("//src/foo:name(//toolchain:default)",
83            Call(":name", "label_with_toolchain"));
84  EXPECT_EQ("//src/foo:name(//toolchain:random)",
85            Call(":name(//toolchain:random)", "label_with_toolchain"));
86}
87
88TEST_F(GetLabelInfoTest, Toolchain) {
89  EXPECT_EQ("//toolchain:default", Call(":name", "toolchain"));
90  EXPECT_EQ("//toolchain:random",
91            Call(":name(//toolchain:random)", "toolchain"));
92}
93