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/escape.h" | 6 #include "tools/gn/escape.h" |
7 | 7 |
8 TEST(Escape, Ninja) { | 8 TEST(Escape, Ninja) { |
9 EscapeOptions opts; | 9 EscapeOptions opts; |
10 opts.mode = ESCAPE_NINJA; | 10 opts.mode = ESCAPE_NINJA; |
11 std::string result = EscapeString("asdf: \"$\\bar", opts, NULL); | 11 std::string result = EscapeString("asdf: \"$\\bar", opts, NULL); |
12 EXPECT_EQ("asdf$:$ \"$$\\bar", result); | 12 EXPECT_EQ("asdf$:$ \"$$\\bar", result); |
13 } | 13 } |
14 | 14 |
15 TEST(Escape, Shell) { | 15 TEST(Escape, Shell) { |
16 EscapeOptions opts; | 16 EscapeOptions opts; |
17 opts.mode = ESCAPE_SHELL; | 17 opts.mode = ESCAPE_SHELL; |
18 std::string result = EscapeString("asdf: \"$\\bar", opts, NULL); | 18 std::string result = EscapeString("asdf: \"$\\bar", opts, NULL); |
19 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
20 // Windows shell doesn't escape backslashes, but it does backslash-escape | 20 // Windows shell doesn't escape backslashes, but it does backslash-escape |
21 // quotes. | 21 // quotes. |
22 EXPECT_EQ("\"asdf: \\\"$\\bar\"", result); | 22 EXPECT_EQ("\"asdf: \\\"\\$\\bar\"", result); |
23 #else | 23 #else |
24 EXPECT_EQ("\"asdf: \\\"$\\\\bar\"", result); | 24 EXPECT_EQ("\"asdf: \\\"\\$\\\\bar\"", result); |
25 #endif | 25 #endif |
| 26 |
| 27 // Some more generic shell chars. |
| 28 result = EscapeString("a_;<*b", opts, NULL); |
| 29 EXPECT_EQ("a_\\;\\<\\*b", result); |
| 30 } |
| 31 |
| 32 TEST(Escape, NinjaShell) { |
| 33 EscapeOptions opts; |
| 34 opts.mode = ESCAPE_NINJA_SHELL; |
| 35 |
| 36 // When escaping for Ninja and the shell, we would escape a $, then escape |
| 37 // the backslash again. |
| 38 std::string result = EscapeString("a$b", opts, NULL); |
| 39 EXPECT_EQ("a\\$$b", result); |
26 } | 40 } |
27 | 41 |
28 TEST(Escape, UsedQuotes) { | 42 TEST(Escape, UsedQuotes) { |
29 EscapeOptions shell_options; | 43 EscapeOptions shell_options; |
30 shell_options.mode = ESCAPE_SHELL; | 44 shell_options.mode = ESCAPE_SHELL; |
31 | 45 |
32 EscapeOptions ninja_options; | 46 EscapeOptions ninja_options; |
33 ninja_options.mode = ESCAPE_NINJA; | 47 ninja_options.mode = ESCAPE_NINJA; |
34 | 48 |
35 EscapeOptions ninja_shell_options; | 49 EscapeOptions ninja_shell_options; |
(...skipping 21 matching lines...) Expand all Loading... |
57 // Ninja escaping shouldn't use quoting. | 71 // Ninja escaping shouldn't use quoting. |
58 used_quotes = false; | 72 used_quotes = false; |
59 EXPECT_EQ("foo$ bar", EscapeString("foo bar", ninja_options, &used_quotes)); | 73 EXPECT_EQ("foo$ bar", EscapeString("foo bar", ninja_options, &used_quotes)); |
60 EXPECT_FALSE(used_quotes); | 74 EXPECT_FALSE(used_quotes); |
61 | 75 |
62 // Used quotes not reset if it's already true. | 76 // Used quotes not reset if it's already true. |
63 used_quotes = true; | 77 used_quotes = true; |
64 EscapeString("foo", ninja_options, &used_quotes); | 78 EscapeString("foo", ninja_options, &used_quotes); |
65 EXPECT_TRUE(used_quotes); | 79 EXPECT_TRUE(used_quotes); |
66 } | 80 } |
OLD | NEW |