[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / tools / gn / string_utils_unittest.cc
blob951a84912750dfa7c554072ac4ee119af882859d
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.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/err.h"
7 #include "tools/gn/scope.h"
8 #include "tools/gn/settings.h"
9 #include "tools/gn/string_utils.h"
10 #include "tools/gn/token.h"
11 #include "tools/gn/value.h"
13 namespace {
15 bool CheckExpansionCase(const char* input, const char* expected, bool success) {
16 Scope scope(static_cast<const Settings*>(nullptr));
17 int64 one = 1;
18 scope.SetValue("one", Value(nullptr, one), nullptr);
19 scope.SetValue("onestring", Value(nullptr, "one"), nullptr);
21 // Nested scope called "onescope" with a value "one" inside it.
22 scoped_ptr<Scope> onescope(new Scope(static_cast<const Settings*>(nullptr)));
23 onescope->SetValue("one", Value(nullptr, one), nullptr);
24 scope.SetValue("onescope", Value(nullptr, onescope.Pass()), nullptr);
26 // List called "onelist" with one value that maps to 1.
27 Value onelist(nullptr, Value::LIST);
28 onelist.list_value().push_back(Value(nullptr, one));
29 scope.SetValue("onelist", onelist, nullptr);
31 // Construct the string token, which includes the quotes.
32 std::string literal_string;
33 literal_string.push_back('"');
34 literal_string.append(input);
35 literal_string.push_back('"');
36 Token literal(Location(), Token::STRING, literal_string);
38 Value result(nullptr, Value::STRING);
39 Err err;
40 bool ret = ExpandStringLiteral(&scope, literal, &result, &err);
42 // Err and return value should agree.
43 EXPECT_NE(ret, err.has_error());
45 if (ret != success)
46 return false;
48 if (!success)
49 return true; // Don't check result on failure.
50 printf("%s\n", result.string_value().c_str());
51 return result.string_value() == expected;
54 } // namespace
56 TEST(StringUtils, ExpandStringLiteralIdentifier) {
57 EXPECT_TRUE(CheckExpansionCase("", "", true));
58 EXPECT_TRUE(CheckExpansionCase("hello", "hello", true));
59 EXPECT_TRUE(CheckExpansionCase("hello #$one", "hello #1", true));
60 EXPECT_TRUE(CheckExpansionCase("hello #$one/two", "hello #1/two", true));
61 EXPECT_TRUE(CheckExpansionCase("hello #${one}", "hello #1", true));
62 EXPECT_TRUE(CheckExpansionCase("hello #${one}one", "hello #1one", true));
63 EXPECT_TRUE(CheckExpansionCase("hello #${one}$one", "hello #11", true));
64 EXPECT_TRUE(CheckExpansionCase("$onestring${one}$one", "one11", true));
65 EXPECT_TRUE(CheckExpansionCase("$onescope", "{\n one = 1\n}", true));
66 EXPECT_TRUE(CheckExpansionCase("$onelist", "[1]", true));
68 // Errors
69 EXPECT_TRUE(CheckExpansionCase("hello #$", nullptr, false));
70 EXPECT_TRUE(CheckExpansionCase("hello #$%", nullptr, false));
71 EXPECT_TRUE(CheckExpansionCase("hello #${", nullptr, false));
72 EXPECT_TRUE(CheckExpansionCase("hello #${}", nullptr, false));
73 EXPECT_TRUE(CheckExpansionCase("hello #$nonexistant", nullptr, false));
74 EXPECT_TRUE(CheckExpansionCase("hello #${unterminated", nullptr, false));
76 // Unknown backslash values aren't special.
77 EXPECT_TRUE(CheckExpansionCase("\\", "\\", true));
78 EXPECT_TRUE(CheckExpansionCase("\\b", "\\b", true));
80 // Backslashes escape some special things. \"\$\\ -> "$\ Note that gtest
81 // doesn't like this escape sequence so we have to put it out-of-line.
82 const char* in = "\\\"\\$\\\\";
83 const char* out = "\"$\\";
84 EXPECT_TRUE(CheckExpansionCase(in, out, true));
87 TEST(StringUtils, ExpandStringLiteralExpression) {
88 // Accessing the scope.
89 EXPECT_TRUE(CheckExpansionCase("hello #${onescope.one}", "hello #1", true));
90 EXPECT_TRUE(CheckExpansionCase("hello #${onescope.two}", nullptr, false));
92 // Accessing the list.
93 EXPECT_TRUE(CheckExpansionCase("hello #${onelist[0]}", "hello #1", true));
94 EXPECT_TRUE(CheckExpansionCase("hello #${onelist[1]}", nullptr, false));
96 // Trying some other (otherwise valid) expressions should fail.
97 EXPECT_TRUE(CheckExpansionCase("${1 + 2}", nullptr, false));
98 EXPECT_TRUE(CheckExpansionCase("${print(1)}", nullptr, false));