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/substitution_pattern.h"
9 TEST(SubstitutionPattern
, ParseLiteral
) {
10 SubstitutionPattern pattern
;
12 EXPECT_TRUE(pattern
.Parse("This is a literal", NULL
, &err
));
13 EXPECT_FALSE(err
.has_error());
14 ASSERT_EQ(1u, pattern
.ranges().size());
15 EXPECT_EQ(SUBSTITUTION_LITERAL
, pattern
.ranges()[0].type
);
16 EXPECT_EQ("This is a literal", pattern
.ranges()[0].literal
);
19 TEST(SubstitutionPattern
, ParseComplex
) {
20 SubstitutionPattern pattern
;
22 EXPECT_TRUE(pattern
.Parse(
23 "AA{{source}}{{source_name_part}}BB{{source_file_part}}", NULL
, &err
));
24 EXPECT_FALSE(err
.has_error());
25 ASSERT_EQ(5u, pattern
.ranges().size());
27 EXPECT_EQ(SUBSTITUTION_LITERAL
, pattern
.ranges()[0].type
);
28 EXPECT_EQ("AA", pattern
.ranges()[0].literal
);
29 EXPECT_EQ(SUBSTITUTION_SOURCE
, pattern
.ranges()[1].type
);
30 EXPECT_EQ(SUBSTITUTION_SOURCE_NAME_PART
, pattern
.ranges()[2].type
);
31 EXPECT_EQ(SUBSTITUTION_LITERAL
, pattern
.ranges()[3].type
);
32 EXPECT_EQ("BB", pattern
.ranges()[3].literal
);
33 EXPECT_EQ(SUBSTITUTION_SOURCE_FILE_PART
, pattern
.ranges()[4].type
);
36 TEST(SubstitutionPattern
, ParseErrors
) {
37 SubstitutionPattern pattern
;
39 EXPECT_FALSE(pattern
.Parse("AA{{source", NULL
, &err
));
40 EXPECT_TRUE(err
.has_error());
43 EXPECT_FALSE(pattern
.Parse("{{source_of_evil}}", NULL
, &err
));
44 EXPECT_TRUE(err
.has_error());
47 EXPECT_FALSE(pattern
.Parse("{{source{{source}}", NULL
, &err
));
48 EXPECT_TRUE(err
.has_error());