• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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/string_utils.h"
6 
7 #include <stdint.h>
8 
9 #include <memory>
10 #include <utility>
11 
12 #include "gn/err.h"
13 #include "gn/scope.h"
14 #include "gn/settings.h"
15 #include "gn/token.h"
16 #include "gn/value.h"
17 #include "util/test/test.h"
18 
19 namespace {
20 
CheckExpansionCase(const char * input,const char * expected,bool success)21 bool CheckExpansionCase(const char* input, const char* expected, bool success) {
22   Scope scope(static_cast<const Settings*>(nullptr));
23   int64_t one = 1;
24   scope.SetValue("one", Value(nullptr, one), nullptr);
25   scope.SetValue("onestring", Value(nullptr, "one"), nullptr);
26 
27   // Nested scope called "onescope" with a value "one" inside it.
28   std::unique_ptr<Scope> onescope =
29       std::make_unique<Scope>(static_cast<const Settings*>(nullptr));
30   onescope->SetValue("one", Value(nullptr, one), nullptr);
31   scope.SetValue("onescope", Value(nullptr, std::move(onescope)), nullptr);
32 
33   // List called "onelist" with one value that maps to 1.
34   Value onelist(nullptr, Value::LIST);
35   onelist.list_value().push_back(Value(nullptr, one));
36   scope.SetValue("onelist", onelist, nullptr);
37 
38   // Construct the string token, which includes the quotes.
39   std::string literal_string;
40   literal_string.push_back('"');
41   literal_string.append(input);
42   literal_string.push_back('"');
43   Token literal(Location(), Token::STRING, literal_string);
44 
45   Value result(nullptr, Value::STRING);
46   Err err;
47   bool ret = ExpandStringLiteral(&scope, literal, &result, &err);
48 
49   // Err and return value should agree.
50   EXPECT_NE(ret, err.has_error());
51 
52   if (ret != success)
53     return false;
54 
55   if (!success)
56     return true;  // Don't check result on failure.
57   return result.string_value() == expected;
58 }
59 
60 }  // namespace
61 
TEST(StringUtils,ExpandStringLiteralIdentifier)62 TEST(StringUtils, ExpandStringLiteralIdentifier) {
63   EXPECT_TRUE(CheckExpansionCase("", "", true));
64   EXPECT_TRUE(CheckExpansionCase("hello", "hello", true));
65   EXPECT_TRUE(CheckExpansionCase("hello #$one", "hello #1", true));
66   EXPECT_TRUE(CheckExpansionCase("hello #$one/two", "hello #1/two", true));
67   EXPECT_TRUE(CheckExpansionCase("hello #${one}", "hello #1", true));
68   EXPECT_TRUE(CheckExpansionCase("hello #${one}one", "hello #1one", true));
69   EXPECT_TRUE(CheckExpansionCase("hello #${one}$one", "hello #11", true));
70   EXPECT_TRUE(CheckExpansionCase("$onestring${one}$one", "one11", true));
71   EXPECT_TRUE(CheckExpansionCase("$onescope", "{\n  one = 1\n}", true));
72   EXPECT_TRUE(CheckExpansionCase("$onelist", "[1]", true));
73 
74   // Hex values
75   EXPECT_TRUE(CheckExpansionCase("$0x0AA",
76                                  "\x0A"
77                                  "A",
78                                  true));
79   EXPECT_TRUE(CheckExpansionCase("$0x0a$0xfF", "\x0A\xFF", true));
80 
81   // Errors
82   EXPECT_TRUE(CheckExpansionCase("hello #$", nullptr, false));
83   EXPECT_TRUE(CheckExpansionCase("hello #$%", nullptr, false));
84   EXPECT_TRUE(CheckExpansionCase("hello #${", nullptr, false));
85   EXPECT_TRUE(CheckExpansionCase("hello #${}", nullptr, false));
86   EXPECT_TRUE(CheckExpansionCase("hello #$nonexistant", nullptr, false));
87   EXPECT_TRUE(CheckExpansionCase("hello #${unterminated", nullptr, false));
88   EXPECT_TRUE(CheckExpansionCase("hex truncated: $0", nullptr, false));
89   EXPECT_TRUE(CheckExpansionCase("hex truncated: $0x", nullptr, false));
90   EXPECT_TRUE(CheckExpansionCase("hex truncated: $0x0", nullptr, false));
91   EXPECT_TRUE(CheckExpansionCase("hex with bad char: $0a", nullptr, false));
92   EXPECT_TRUE(CheckExpansionCase("hex with bad char: $0x1z", nullptr, false));
93   EXPECT_TRUE(CheckExpansionCase("hex with bad char: $0xz1", nullptr, false));
94 
95   // Unknown backslash values aren't special.
96   EXPECT_TRUE(CheckExpansionCase("\\", "\\", true));
97   EXPECT_TRUE(CheckExpansionCase("\\b", "\\b", true));
98 
99   // Backslashes escape some special things. \"\$\\ -> "$\  Note that gtest
100   // doesn't like this escape sequence so we have to put it out-of-line.
101   const char* in = "\\\"\\$\\\\";
102   const char* out = "\"$\\";
103   EXPECT_TRUE(CheckExpansionCase(in, out, true));
104 }
105 
TEST(StringUtils,ExpandStringLiteralExpression)106 TEST(StringUtils, ExpandStringLiteralExpression) {
107   // Accessing the scope.
108   EXPECT_TRUE(CheckExpansionCase("hello #${onescope.one}", "hello #1", true));
109   EXPECT_TRUE(CheckExpansionCase("hello #${onescope.two}", nullptr, false));
110 
111   // Accessing the list.
112   EXPECT_TRUE(CheckExpansionCase("hello #${onelist[0]}", "hello #1", true));
113   EXPECT_TRUE(CheckExpansionCase("hello #${onelist[1]}", nullptr, false));
114 
115   // Trying some other (otherwise valid) expressions should fail.
116   EXPECT_TRUE(CheckExpansionCase("${1 + 2}", nullptr, false));
117   EXPECT_TRUE(CheckExpansionCase("${print(1)}", nullptr, false));
118 }
119 
TEST(StringUtils,EditDistance)120 TEST(StringUtils, EditDistance) {
121   EXPECT_EQ(3u, EditDistance("doom melon", "dune melon", 100));
122   EXPECT_EQ(2u, EditDistance("doom melon", "dune melon", 1));
123 
124   EXPECT_EQ(2u, EditDistance("ab", "ba", 100));
125   EXPECT_EQ(2u, EditDistance("ba", "ab", 100));
126 
127   EXPECT_EQ(2u, EditDistance("ananas", "banana", 100));
128   EXPECT_EQ(2u, EditDistance("banana", "ananas", 100));
129 
130   EXPECT_EQ(2u, EditDistance("unclear", "nuclear", 100));
131   EXPECT_EQ(2u, EditDistance("nuclear", "unclear", 100));
132 
133   EXPECT_EQ(3u, EditDistance("chrome", "chromium", 100));
134   EXPECT_EQ(3u, EditDistance("chromium", "chrome", 100));
135 
136   EXPECT_EQ(4u, EditDistance("", "abcd", 100));
137   EXPECT_EQ(4u, EditDistance("abcd", "", 100));
138 
139   EXPECT_EQ(4u, EditDistance("xxx", "xxxxxxx", 100));
140   EXPECT_EQ(4u, EditDistance("xxxxxxx", "xxx", 100));
141 
142   EXPECT_EQ(7u, EditDistance("yyy", "xxxxxxx", 100));
143   EXPECT_EQ(7u, EditDistance("xxxxxxx", "yyy", 100));
144 }
145 
TEST(StringUtils,SpellcheckString)146 TEST(StringUtils, SpellcheckString) {
147   std::vector<std::string_view> words;
148   words.push_back("your");
149   words.push_back("bravado");
150   words.push_back("won\'t");
151   words.push_back("help");
152   words.push_back("you");
153   words.push_back("now");
154 
155   EXPECT_EQ("help", SpellcheckString("halp", words));
156 
157   // barbados has an edit distance of 4 from bravado, so there's no suggestion.
158   EXPECT_TRUE(SpellcheckString("barbados", words).empty());
159 }
160