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 "tools/gn/string_utils.h" | 5 #include "tools/gn/string_utils.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 EXPECT_TRUE(CheckExpansionCase("hello", "hello", true)); | 62 EXPECT_TRUE(CheckExpansionCase("hello", "hello", true)); |
63 EXPECT_TRUE(CheckExpansionCase("hello #$one", "hello #1", true)); | 63 EXPECT_TRUE(CheckExpansionCase("hello #$one", "hello #1", true)); |
64 EXPECT_TRUE(CheckExpansionCase("hello #$one/two", "hello #1/two", true)); | 64 EXPECT_TRUE(CheckExpansionCase("hello #$one/two", "hello #1/two", true)); |
65 EXPECT_TRUE(CheckExpansionCase("hello #${one}", "hello #1", true)); | 65 EXPECT_TRUE(CheckExpansionCase("hello #${one}", "hello #1", true)); |
66 EXPECT_TRUE(CheckExpansionCase("hello #${one}one", "hello #1one", true)); | 66 EXPECT_TRUE(CheckExpansionCase("hello #${one}one", "hello #1one", true)); |
67 EXPECT_TRUE(CheckExpansionCase("hello #${one}$one", "hello #11", true)); | 67 EXPECT_TRUE(CheckExpansionCase("hello #${one}$one", "hello #11", true)); |
68 EXPECT_TRUE(CheckExpansionCase("$onestring${one}$one", "one11", true)); | 68 EXPECT_TRUE(CheckExpansionCase("$onestring${one}$one", "one11", true)); |
69 EXPECT_TRUE(CheckExpansionCase("$onescope", "{\n one = 1\n}", true)); | 69 EXPECT_TRUE(CheckExpansionCase("$onescope", "{\n one = 1\n}", true)); |
70 EXPECT_TRUE(CheckExpansionCase("$onelist", "[1]", true)); | 70 EXPECT_TRUE(CheckExpansionCase("$onelist", "[1]", true)); |
71 | 71 |
| 72 // Hex values |
| 73 EXPECT_TRUE(CheckExpansionCase("$0x0AA", "\x0A""A", true)); |
| 74 EXPECT_TRUE(CheckExpansionCase("$0x0a$0xfF", "\x0A\xFF", true)); |
| 75 |
72 // Errors | 76 // Errors |
73 EXPECT_TRUE(CheckExpansionCase("hello #$", nullptr, false)); | 77 EXPECT_TRUE(CheckExpansionCase("hello #$", nullptr, false)); |
74 EXPECT_TRUE(CheckExpansionCase("hello #$%", nullptr, false)); | 78 EXPECT_TRUE(CheckExpansionCase("hello #$%", nullptr, false)); |
75 EXPECT_TRUE(CheckExpansionCase("hello #${", nullptr, false)); | 79 EXPECT_TRUE(CheckExpansionCase("hello #${", nullptr, false)); |
76 EXPECT_TRUE(CheckExpansionCase("hello #${}", nullptr, false)); | 80 EXPECT_TRUE(CheckExpansionCase("hello #${}", nullptr, false)); |
77 EXPECT_TRUE(CheckExpansionCase("hello #$nonexistant", nullptr, false)); | 81 EXPECT_TRUE(CheckExpansionCase("hello #$nonexistant", nullptr, false)); |
78 EXPECT_TRUE(CheckExpansionCase("hello #${unterminated", nullptr, false)); | 82 EXPECT_TRUE(CheckExpansionCase("hello #${unterminated", nullptr, false)); |
| 83 EXPECT_TRUE(CheckExpansionCase("hex truncated: $0", nullptr, false)); |
| 84 EXPECT_TRUE(CheckExpansionCase("hex truncated: $0x", nullptr, false)); |
| 85 EXPECT_TRUE(CheckExpansionCase("hex truncated: $0x0", nullptr, false)); |
| 86 EXPECT_TRUE(CheckExpansionCase("hex with bad char: $0a", nullptr, false)); |
| 87 EXPECT_TRUE(CheckExpansionCase("hex with bad char: $0x1z", nullptr, false)); |
| 88 EXPECT_TRUE(CheckExpansionCase("hex with bad char: $0xz1", nullptr, false)); |
79 | 89 |
80 // Unknown backslash values aren't special. | 90 // Unknown backslash values aren't special. |
81 EXPECT_TRUE(CheckExpansionCase("\\", "\\", true)); | 91 EXPECT_TRUE(CheckExpansionCase("\\", "\\", true)); |
82 EXPECT_TRUE(CheckExpansionCase("\\b", "\\b", true)); | 92 EXPECT_TRUE(CheckExpansionCase("\\b", "\\b", true)); |
83 | 93 |
84 // Backslashes escape some special things. \"\$\\ -> "$\ Note that gtest | 94 // Backslashes escape some special things. \"\$\\ -> "$\ Note that gtest |
85 // doesn't like this escape sequence so we have to put it out-of-line. | 95 // doesn't like this escape sequence so we have to put it out-of-line. |
86 const char* in = "\\\"\\$\\\\"; | 96 const char* in = "\\\"\\$\\\\"; |
87 const char* out = "\"$\\"; | 97 const char* out = "\"$\\"; |
88 EXPECT_TRUE(CheckExpansionCase(in, out, true)); | 98 EXPECT_TRUE(CheckExpansionCase(in, out, true)); |
89 } | 99 } |
90 | 100 |
91 TEST(StringUtils, ExpandStringLiteralExpression) { | 101 TEST(StringUtils, ExpandStringLiteralExpression) { |
92 // Accessing the scope. | 102 // Accessing the scope. |
93 EXPECT_TRUE(CheckExpansionCase("hello #${onescope.one}", "hello #1", true)); | 103 EXPECT_TRUE(CheckExpansionCase("hello #${onescope.one}", "hello #1", true)); |
94 EXPECT_TRUE(CheckExpansionCase("hello #${onescope.two}", nullptr, false)); | 104 EXPECT_TRUE(CheckExpansionCase("hello #${onescope.two}", nullptr, false)); |
95 | 105 |
96 // Accessing the list. | 106 // Accessing the list. |
97 EXPECT_TRUE(CheckExpansionCase("hello #${onelist[0]}", "hello #1", true)); | 107 EXPECT_TRUE(CheckExpansionCase("hello #${onelist[0]}", "hello #1", true)); |
98 EXPECT_TRUE(CheckExpansionCase("hello #${onelist[1]}", nullptr, false)); | 108 EXPECT_TRUE(CheckExpansionCase("hello #${onelist[1]}", nullptr, false)); |
99 | 109 |
100 // Trying some other (otherwise valid) expressions should fail. | 110 // Trying some other (otherwise valid) expressions should fail. |
101 EXPECT_TRUE(CheckExpansionCase("${1 + 2}", nullptr, false)); | 111 EXPECT_TRUE(CheckExpansionCase("${1 + 2}", nullptr, false)); |
102 EXPECT_TRUE(CheckExpansionCase("${print(1)}", nullptr, false)); | 112 EXPECT_TRUE(CheckExpansionCase("${print(1)}", nullptr, false)); |
103 } | 113 } |
OLD | NEW |