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 "gn/functions.h"
6 #include "gn/test_with_scope.h"
7 #include "util/test/test.h"
8
9 namespace {
10
11 class GetLabelInfoTest : public testing::Test {
12 public:
GetLabelInfoTest()13 GetLabelInfoTest() : testing::Test() {
14 setup_.scope()->set_source_dir(SourceDir("//src/foo/"));
15 }
16
17 // Convenience wrapper to call GetLabelInfo.
Call(const std::string & label,const std::string & what)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(nullptr, label));
23 args.push_back(Value(nullptr, what));
24
25 Err err;
26 Value result =
27 functions::RunGetLabelInfo(setup_.scope(), &function, 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
TEST_F(GetLabelInfoTest,BadInput)43 TEST_F(GetLabelInfoTest, BadInput) {
44 EXPECT_EQ("", Call(":name", "incorrect_value"));
45 EXPECT_EQ("", Call("", "name"));
46 }
47
TEST_F(GetLabelInfoTest,Name)48 TEST_F(GetLabelInfoTest, Name) {
49 EXPECT_EQ("name", Call(":name", "name"));
50 EXPECT_EQ("name", Call("//foo/bar:name", "name"));
51 EXPECT_EQ("name", Call("//foo/bar:name(//other:tc)", "name"));
52 }
53
TEST_F(GetLabelInfoTest,Dir)54 TEST_F(GetLabelInfoTest, Dir) {
55 EXPECT_EQ("//src/foo", Call(":name", "dir"));
56 EXPECT_EQ("//foo/bar", Call("//foo/bar:baz", "dir"));
57 EXPECT_EQ("//foo/bar", Call("//foo/bar:baz(//other:tc)", "dir"));
58 }
59
TEST_F(GetLabelInfoTest,RootOutDir)60 TEST_F(GetLabelInfoTest, RootOutDir) {
61 EXPECT_EQ("//out/Debug", Call(":name", "root_out_dir"));
62 EXPECT_EQ("//out/Debug/random",
63 Call(":name(//toolchain:random)", "root_out_dir"));
64 }
65
TEST_F(GetLabelInfoTest,RootGenDir)66 TEST_F(GetLabelInfoTest, RootGenDir) {
67 EXPECT_EQ("//out/Debug/gen", Call(":name", "root_gen_dir"));
68 EXPECT_EQ("//out/Debug/gen",
69 Call(":name(//toolchain:default)", "root_gen_dir"));
70 EXPECT_EQ("//out/Debug/random/gen",
71 Call(":name(//toolchain:random)", "root_gen_dir"));
72 }
73
TEST_F(GetLabelInfoTest,TargetOutDir)74 TEST_F(GetLabelInfoTest, TargetOutDir) {
75 EXPECT_EQ("//out/Debug/obj/src/foo", Call(":name", "target_out_dir"));
76 EXPECT_EQ("//out/Debug/obj/foo",
77 Call("//foo:name(//toolchain:default)", "target_out_dir"));
78 EXPECT_EQ("//out/Debug/random/obj/foo",
79 Call("//foo:name(//toolchain:random)", "target_out_dir"));
80 }
81
TEST_F(GetLabelInfoTest,TargetGenDir)82 TEST_F(GetLabelInfoTest, TargetGenDir) {
83 EXPECT_EQ("//out/Debug/gen/src/foo", Call(":name", "target_gen_dir"));
84 EXPECT_EQ("//out/Debug/gen/foo",
85 Call("//foo:name(//toolchain:default)", "target_gen_dir"));
86 EXPECT_EQ("//out/Debug/random/gen/foo",
87 Call("//foo:name(//toolchain:random)", "target_gen_dir"));
88 }
89
TEST_F(GetLabelInfoTest,LabelNoToolchain)90 TEST_F(GetLabelInfoTest, LabelNoToolchain) {
91 EXPECT_EQ("//src/foo:name", Call(":name", "label_no_toolchain"));
92 EXPECT_EQ("//src/foo:name",
93 Call("//src/foo:name(//toolchain:random)", "label_no_toolchain"));
94 }
95
TEST_F(GetLabelInfoTest,LabelWithToolchain)96 TEST_F(GetLabelInfoTest, LabelWithToolchain) {
97 EXPECT_EQ("//src/foo:name(//toolchain:default)",
98 Call(":name", "label_with_toolchain"));
99 EXPECT_EQ("//src/foo:name(//toolchain:random)",
100 Call(":name(//toolchain:random)", "label_with_toolchain"));
101 }
102
TEST_F(GetLabelInfoTest,Toolchain)103 TEST_F(GetLabelInfoTest, Toolchain) {
104 EXPECT_EQ("//toolchain:default", Call(":name", "toolchain"));
105 EXPECT_EQ("//toolchain:random",
106 Call(":name(//toolchain:random)", "toolchain"));
107 }
108