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/build_config.h"
8 #include "util/test/test.h"
9
TEST(LabelMatchesTest,MatchesSubTarget)10 TEST(LabelMatchesTest, MatchesSubTarget) {
11 TestWithScope setup;
12 FunctionCallNode function;
13
14 std::vector<Value> args;
15 args.push_back(Value(nullptr, "//foo:bar"));
16
17 Value patterns(nullptr, Value::LIST);
18 patterns.list_value().push_back(Value(nullptr, "//foo/*"));
19 patterns.list_value().push_back(Value(nullptr, "//bar:*"));
20 args.push_back(patterns);
21
22 Err err;
23 Value result =
24 functions::RunLabelMatches(setup.scope(), &function, args, &err);
25 ASSERT_FALSE(err.has_error());
26 ASSERT_EQ(result.type(), Value::BOOLEAN);
27 ASSERT_EQ(result.boolean_value(), true);
28 }
29
TEST(LabelMatchesTest,MatchesTargetInFile)30 TEST(LabelMatchesTest, MatchesTargetInFile) {
31 TestWithScope setup;
32 FunctionCallNode function;
33
34 std::vector<Value> args;
35 args.push_back(Value(nullptr, "//bar:foo"));
36
37 Value patterns(nullptr, Value::LIST);
38 patterns.list_value().push_back(Value(nullptr, "//foo/*"));
39 patterns.list_value().push_back(Value(nullptr, "//bar:*"));
40 args.push_back(patterns);
41
42 Err err;
43 Value result =
44 functions::RunLabelMatches(setup.scope(), &function, args, &err);
45 ASSERT_FALSE(err.has_error());
46 ASSERT_EQ(result.type(), Value::BOOLEAN);
47 ASSERT_EQ(result.boolean_value(), true);
48 }
49
TEST(LabelMatchesTest,NoMatch)50 TEST(LabelMatchesTest, NoMatch) {
51 TestWithScope setup;
52 FunctionCallNode function;
53
54 std::vector<Value> args;
55 args.push_back(Value(nullptr, "//baz/foo:bar"));
56
57 Value patterns(nullptr, Value::LIST);
58 patterns.list_value().push_back(Value(nullptr, "//foo/*"));
59 patterns.list_value().push_back(Value(nullptr, "//bar:*"));
60 args.push_back(patterns);
61
62 Err err;
63 Value result =
64 functions::RunLabelMatches(setup.scope(), &function, args, &err);
65 ASSERT_FALSE(err.has_error());
66 ASSERT_EQ(result.type(), Value::BOOLEAN);
67 ASSERT_EQ(result.boolean_value(), false);
68 }
69
TEST(LabelMatchesTest,LabelMustBeString)70 TEST(LabelMatchesTest, LabelMustBeString) {
71 TestWithScope setup;
72 FunctionCallNode function;
73
74 std::vector<Value> args;
75 args.push_back(Value(nullptr, true));
76
77 Value patterns(nullptr, Value::LIST);
78 patterns.list_value().push_back(Value(nullptr, "//foo/*"));
79 patterns.list_value().push_back(Value(nullptr, "//bar:*"));
80 args.push_back(patterns);
81
82 Err err;
83 Value result =
84 functions::RunLabelMatches(setup.scope(), &function, args, &err);
85 ASSERT_TRUE(err.has_error());
86 ASSERT_EQ(err.message(), "First argument must be a target label.");
87 }
88
TEST(LabelMatchesTest,PatternsMustBeList)89 TEST(LabelMatchesTest, PatternsMustBeList) {
90 TestWithScope setup;
91 FunctionCallNode function;
92
93 std::vector<Value> args;
94 args.push_back(Value(nullptr, "//baz/foo:bar"));
95
96 Value patterns(nullptr, true);
97 args.push_back(patterns);
98
99 Err err;
100 Value result =
101 functions::RunLabelMatches(setup.scope(), &function, args, &err);
102 ASSERT_TRUE(err.has_error());
103 ASSERT_EQ(err.message(), "Second argument must be a list of label patterns.");
104 }
105
TEST(LabelMatchesTest,PatternsMustBeListOfStrings)106 TEST(LabelMatchesTest, PatternsMustBeListOfStrings) {
107 TestWithScope setup;
108 FunctionCallNode function;
109
110 std::vector<Value> args;
111 args.push_back(Value(nullptr, "//baz/foo:bar"));
112
113 Value patterns(nullptr, Value::LIST);
114 patterns.list_value().push_back(Value(nullptr, "//foo/*"));
115 patterns.list_value().push_back(Value(nullptr, true));
116 args.push_back(patterns);
117
118 Err err;
119 Value result =
120 functions::RunLabelMatches(setup.scope(), &function, args, &err);
121 ASSERT_TRUE(err.has_error());
122 ASSERT_EQ(err.message(), "Second argument must be a list of label patterns.");
123 }
124