OLD | NEW |
(Empty) | |
| 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 "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" |
| 12 |
| 13 namespace { |
| 14 |
| 15 bool CheckExpansionCase(const char* input, const char* expected, bool success) { |
| 16 Scope scope(static_cast<const Settings*>(NULL)); |
| 17 scope.SetValue("one", Value(NULL, 1), NULL); |
| 18 scope.SetValue("onestring", Value(NULL, "one"), NULL); |
| 19 |
| 20 // Construct the string token, which includes the quotes. |
| 21 std::string literal_string; |
| 22 literal_string.push_back('"'); |
| 23 literal_string.append(input); |
| 24 literal_string.push_back('"'); |
| 25 Token literal(Location(), Token::STRING, literal_string); |
| 26 |
| 27 Value result(NULL, Value::STRING); |
| 28 Err err; |
| 29 bool ret = ExpandStringLiteral(&scope, literal, &result, &err); |
| 30 |
| 31 // Err and return value should agree. |
| 32 EXPECT_NE(ret, err.has_error()); |
| 33 |
| 34 if (ret != success) |
| 35 return false; |
| 36 |
| 37 if (!success) |
| 38 return true; // Don't check result on failure. |
| 39 return result.string_value() == expected; |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
| 44 TEST(StringUtils, ExpandStringLiteral) { |
| 45 EXPECT_TRUE(CheckExpansionCase("", "", true)); |
| 46 EXPECT_TRUE(CheckExpansionCase("hello", "hello", true)); |
| 47 EXPECT_TRUE(CheckExpansionCase("hello #$one", "hello #1", true)); |
| 48 EXPECT_TRUE(CheckExpansionCase("hello #$one/two", "hello #1/two", true)); |
| 49 EXPECT_TRUE(CheckExpansionCase("hello #${one}", "hello #1", true)); |
| 50 EXPECT_TRUE(CheckExpansionCase("hello #${one}one", "hello #1one", true)); |
| 51 EXPECT_TRUE(CheckExpansionCase("hello #${one}$one", "hello #11", true)); |
| 52 EXPECT_TRUE(CheckExpansionCase("$onestring${one}$one", "one11", true)); |
| 53 |
| 54 // Errors |
| 55 EXPECT_TRUE(CheckExpansionCase("hello #$", NULL, false)); |
| 56 EXPECT_TRUE(CheckExpansionCase("hello #$%", NULL, false)); |
| 57 EXPECT_TRUE(CheckExpansionCase("hello #${", NULL, false)); |
| 58 EXPECT_TRUE(CheckExpansionCase("hello #${}", NULL, false)); |
| 59 EXPECT_TRUE(CheckExpansionCase("hello #$nonexistant", NULL, false)); |
| 60 EXPECT_TRUE(CheckExpansionCase("hello #${unterminated", NULL, false)); |
| 61 |
| 62 // Unknown backslash values aren't special. |
| 63 EXPECT_TRUE(CheckExpansionCase("\\", "\\", true)); |
| 64 EXPECT_TRUE(CheckExpansionCase("\\b", "\\b", true)); |
| 65 |
| 66 // Backslashes escape some special things. \"\$\\ -> "$\ Note that gtest |
| 67 // doesn't like this escape sequence so we have to put it out-of-line. |
| 68 const char* in = "\\\"\\$\\\\"; |
| 69 const char* out = "\"$\\"; |
| 70 EXPECT_TRUE(CheckExpansionCase(in, out, true)); |
| 71 } |
OLD | NEW |