| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 #include "tools/gn/err.h" | 6 #include "tools/gn/err.h" |
| 7 #include "tools/gn/scope.h" | 7 #include "tools/gn/scope.h" |
| 8 #include "tools/gn/settings.h" | 8 #include "tools/gn/settings.h" |
| 9 #include "tools/gn/string_utils.h" | 9 #include "tools/gn/string_utils.h" |
| 10 #include "tools/gn/token.h" | 10 #include "tools/gn/token.h" |
| 11 #include "tools/gn/value.h" | 11 #include "tools/gn/value.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 bool CheckExpansionCase(const char* input, const char* expected, bool success) { | 15 bool CheckExpansionCase(const char* input, const char* expected, bool success) { |
| 16 Scope scope(static_cast<const Settings*>(nullptr)); | 16 Scope scope(static_cast<const Settings*>(nullptr)); |
| 17 int64 one = 1; | 17 int64 one = 1; |
| 18 scope.SetValue("one", Value(nullptr, one), nullptr); | 18 scope.SetValue("one", Value(nullptr, one), nullptr); |
| 19 scope.SetValue("onestring", Value(nullptr, "one"), nullptr); | 19 scope.SetValue("onestring", Value(nullptr, "one"), nullptr); |
| 20 | 20 |
| 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); |
| 25 |
| 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); |
| 30 |
| 21 // Construct the string token, which includes the quotes. | 31 // Construct the string token, which includes the quotes. |
| 22 std::string literal_string; | 32 std::string literal_string; |
| 23 literal_string.push_back('"'); | 33 literal_string.push_back('"'); |
| 24 literal_string.append(input); | 34 literal_string.append(input); |
| 25 literal_string.push_back('"'); | 35 literal_string.push_back('"'); |
| 26 Token literal(Location(), Token::STRING, literal_string); | 36 Token literal(Location(), Token::STRING, literal_string); |
| 27 | 37 |
| 28 Value result(nullptr, Value::STRING); | 38 Value result(nullptr, Value::STRING); |
| 29 Err err; | 39 Err err; |
| 30 bool ret = ExpandStringLiteral(&scope, literal, &result, &err); | 40 bool ret = ExpandStringLiteral(&scope, literal, &result, &err); |
| 31 | 41 |
| 32 // Err and return value should agree. | 42 // Err and return value should agree. |
| 33 EXPECT_NE(ret, err.has_error()); | 43 EXPECT_NE(ret, err.has_error()); |
| 34 | 44 |
| 35 if (ret != success) | 45 if (ret != success) |
| 36 return false; | 46 return false; |
| 37 | 47 |
| 38 if (!success) | 48 if (!success) |
| 39 return true; // Don't check result on failure. | 49 return true; // Don't check result on failure. |
| 50 printf("%s\n", result.string_value().c_str()); |
| 40 return result.string_value() == expected; | 51 return result.string_value() == expected; |
| 41 } | 52 } |
| 42 | 53 |
| 43 } // namespace | 54 } // namespace |
| 44 | 55 |
| 45 TEST(StringUtils, ExpandStringLiteral) { | 56 TEST(StringUtils, ExpandStringLiteralIdentifier) { |
| 46 EXPECT_TRUE(CheckExpansionCase("", "", true)); | 57 EXPECT_TRUE(CheckExpansionCase("", "", true)); |
| 47 EXPECT_TRUE(CheckExpansionCase("hello", "hello", true)); | 58 EXPECT_TRUE(CheckExpansionCase("hello", "hello", true)); |
| 48 EXPECT_TRUE(CheckExpansionCase("hello #$one", "hello #1", true)); | 59 EXPECT_TRUE(CheckExpansionCase("hello #$one", "hello #1", true)); |
| 49 EXPECT_TRUE(CheckExpansionCase("hello #$one/two", "hello #1/two", true)); | 60 EXPECT_TRUE(CheckExpansionCase("hello #$one/two", "hello #1/two", true)); |
| 50 EXPECT_TRUE(CheckExpansionCase("hello #${one}", "hello #1", true)); | 61 EXPECT_TRUE(CheckExpansionCase("hello #${one}", "hello #1", true)); |
| 51 EXPECT_TRUE(CheckExpansionCase("hello #${one}one", "hello #1one", true)); | 62 EXPECT_TRUE(CheckExpansionCase("hello #${one}one", "hello #1one", true)); |
| 52 EXPECT_TRUE(CheckExpansionCase("hello #${one}$one", "hello #11", true)); | 63 EXPECT_TRUE(CheckExpansionCase("hello #${one}$one", "hello #11", true)); |
| 53 EXPECT_TRUE(CheckExpansionCase("$onestring${one}$one", "one11", 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)); |
| 54 | 67 |
| 55 // Errors | 68 // Errors |
| 56 EXPECT_TRUE(CheckExpansionCase("hello #$", nullptr, false)); | 69 EXPECT_TRUE(CheckExpansionCase("hello #$", nullptr, false)); |
| 57 EXPECT_TRUE(CheckExpansionCase("hello #$%", nullptr, false)); | 70 EXPECT_TRUE(CheckExpansionCase("hello #$%", nullptr, false)); |
| 58 EXPECT_TRUE(CheckExpansionCase("hello #${", nullptr, false)); | 71 EXPECT_TRUE(CheckExpansionCase("hello #${", nullptr, false)); |
| 59 EXPECT_TRUE(CheckExpansionCase("hello #${}", nullptr, false)); | 72 EXPECT_TRUE(CheckExpansionCase("hello #${}", nullptr, false)); |
| 60 EXPECT_TRUE(CheckExpansionCase("hello #$nonexistant", nullptr, false)); | 73 EXPECT_TRUE(CheckExpansionCase("hello #$nonexistant", nullptr, false)); |
| 61 EXPECT_TRUE(CheckExpansionCase("hello #${unterminated", nullptr, false)); | 74 EXPECT_TRUE(CheckExpansionCase("hello #${unterminated", nullptr, false)); |
| 62 | 75 |
| 63 // Unknown backslash values aren't special. | 76 // Unknown backslash values aren't special. |
| 64 EXPECT_TRUE(CheckExpansionCase("\\", "\\", true)); | 77 EXPECT_TRUE(CheckExpansionCase("\\", "\\", true)); |
| 65 EXPECT_TRUE(CheckExpansionCase("\\b", "\\b", true)); | 78 EXPECT_TRUE(CheckExpansionCase("\\b", "\\b", true)); |
| 66 | 79 |
| 67 // Backslashes escape some special things. \"\$\\ -> "$\ Note that gtest | 80 // Backslashes escape some special things. \"\$\\ -> "$\ Note that gtest |
| 68 // doesn't like this escape sequence so we have to put it out-of-line. | 81 // doesn't like this escape sequence so we have to put it out-of-line. |
| 69 const char* in = "\\\"\\$\\\\"; | 82 const char* in = "\\\"\\$\\\\"; |
| 70 const char* out = "\"$\\"; | 83 const char* out = "\"$\\"; |
| 71 EXPECT_TRUE(CheckExpansionCase(in, out, true)); | 84 EXPECT_TRUE(CheckExpansionCase(in, out, true)); |
| 72 } | 85 } |
| 86 |
| 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)); |
| 91 |
| 92 // Accessing the list. |
| 93 EXPECT_TRUE(CheckExpansionCase("hello #${onelist[0]}", "hello #1", true)); |
| 94 EXPECT_TRUE(CheckExpansionCase("hello #${onelist[1]}", nullptr, false)); |
| 95 |
| 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)); |
| 99 } |
| OLD | NEW |