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/err.h"
7#include "tools/gn/substitution_pattern.h"
8
9TEST(SubstitutionPattern, ParseLiteral) {
10  SubstitutionPattern pattern;
11  Err err;
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);
17}
18
19TEST(SubstitutionPattern, ParseComplex) {
20  SubstitutionPattern pattern;
21  Err err;
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());
26
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);
34}
35
36TEST(SubstitutionPattern, ParseErrors) {
37  SubstitutionPattern pattern;
38  Err err;
39  EXPECT_FALSE(pattern.Parse("AA{{source", NULL, &err));
40  EXPECT_TRUE(err.has_error());
41
42  err = Err();
43  EXPECT_FALSE(pattern.Parse("{{source_of_evil}}", NULL, &err));
44  EXPECT_TRUE(err.has_error());
45
46  err = Err();
47  EXPECT_FALSE(pattern.Parse("{{source{{source}}", NULL, &err));
48  EXPECT_TRUE(err.has_error());
49}
50