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.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/err.h"
7 #include "tools/gn/label_pattern.h"
8 #include "tools/gn/value.h"
16 LabelPattern::Type type
;
19 const char* toolchain
;
24 TEST(LabelPattern
, PatternParse
) {
25 SourceDir
current_dir("//foo/");
26 PatternCase cases
[] = {
28 { "", false, LabelPattern::MATCH
, "", "", "" },
29 { ":", false, LabelPattern::MATCH
, "", "", "" },
31 { ":bar", true, LabelPattern::MATCH
, "//foo/", "bar", "" },
32 { "//la:bar", true, LabelPattern::MATCH
, "//la/", "bar", "" },
33 { "*", true, LabelPattern::RECURSIVE_DIRECTORY
, "", "", "" },
34 { ":*", true, LabelPattern::DIRECTORY
, "//foo/", "", "" },
35 { "la:*", true, LabelPattern::DIRECTORY
, "//foo/la/", "", "" },
36 { "la/*:*", true, LabelPattern::RECURSIVE_DIRECTORY
, "//foo/la/", "", "" },
37 { "//la:*", true, LabelPattern::DIRECTORY
, "//la/", "", "" },
38 { "./*", true, LabelPattern::RECURSIVE_DIRECTORY
, "//foo/", "", "" },
39 { "foo/*", true, LabelPattern::RECURSIVE_DIRECTORY
, "//foo/foo/", "", "" },
40 { "//l/*", true, LabelPattern::RECURSIVE_DIRECTORY
, "//l/", "", "" },
42 { "//foo()", true, LabelPattern::MATCH
, "//foo/", "foo", "" },
43 { "//foo(//bar)", true, LabelPattern::MATCH
, "//foo/", "foo", "//bar:bar" },
44 { "//foo:*(//bar)", true, LabelPattern::DIRECTORY
, "//foo/", "",
46 { "//foo/*(//bar)", true, LabelPattern::RECURSIVE_DIRECTORY
, "//foo/", "",
48 // Wildcards in invalid places.
49 { "*foo*:bar", false, LabelPattern::MATCH
, "", "", "" },
50 { "foo*:*bar", false, LabelPattern::MATCH
, "", "", "" },
51 { "*foo:bar", false, LabelPattern::MATCH
, "", "", "" },
52 { "foo:bar*", false, LabelPattern::MATCH
, "", "", "" },
53 { "*:*", true, LabelPattern::RECURSIVE_DIRECTORY
, "", "", "" },
54 // Invalid toolchain stuff.
55 { "//foo(//foo/bar:*)", false, LabelPattern::MATCH
, "", "", "" },
56 { "//foo/*(*)", false, LabelPattern::MATCH
, "", "", "" },
57 { "//foo(//bar", false, LabelPattern::MATCH
, "", "", "" },
60 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
61 const PatternCase
& cur
= cases
[i
];
64 LabelPattern::GetPattern(current_dir
, Value(nullptr, cur
.input
), &err
);
66 EXPECT_EQ(cur
.success
, !err
.has_error()) << i
<< " " << cur
.input
;
67 EXPECT_EQ(cur
.type
, result
.type()) << i
<< " " << cur
.input
;
68 EXPECT_EQ(cur
.dir
, result
.dir().value()) << i
<< " " << cur
.input
;
69 EXPECT_EQ(cur
.name
, result
.name()) << i
<< " " << cur
.input
;
70 EXPECT_EQ(cur
.toolchain
, result
.toolchain().GetUserVisibleName(false))
71 << i
<< " " << cur
.input
;