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 "gn/escape.h"
6 #include "util/test/test.h"
7
TEST(Escape,Ninja)8 TEST(Escape, Ninja) {
9 EscapeOptions opts;
10 opts.mode = ESCAPE_NINJA;
11 std::string result = EscapeString("asdf: \"$\\bar", opts, nullptr);
12 EXPECT_EQ("asdf$:$ \"$$\\bar", result);
13 }
14
TEST(Escape,Depfile)15 TEST(Escape, Depfile) {
16 EscapeOptions opts;
17 opts.mode = ESCAPE_DEPFILE;
18 std::string result = EscapeString("asdf:$ \\#*[|]bar", opts, nullptr);
19 EXPECT_EQ("asdf:$$\\ \\\\\\#\\*\\[\\|\\]bar", result);
20 }
21
TEST(Escape,WindowsCommand)22 TEST(Escape, WindowsCommand) {
23 EscapeOptions opts;
24 opts.mode = ESCAPE_NINJA_COMMAND;
25 opts.platform = ESCAPE_PLATFORM_WIN;
26
27 // Regular string is passed, even if it has backslashes.
28 EXPECT_EQ("foo\\bar", EscapeString("foo\\bar", opts, nullptr));
29
30 // Spaces means the string is quoted, normal backslahes untouched.
31 bool needs_quoting = false;
32 EXPECT_EQ("\"foo\\$ bar\"", EscapeString("foo\\ bar", opts, &needs_quoting));
33 EXPECT_TRUE(needs_quoting);
34
35 // Inhibit quoting.
36 needs_quoting = false;
37 opts.inhibit_quoting = true;
38 EXPECT_EQ("foo\\$ bar", EscapeString("foo\\ bar", opts, &needs_quoting));
39 EXPECT_TRUE(needs_quoting);
40 opts.inhibit_quoting = false;
41
42 // Backslashes at the end of the string get escaped.
43 EXPECT_EQ("\"foo$ bar\\\\\\\\\"", EscapeString("foo bar\\\\", opts, nullptr));
44
45 // Backslashes preceding quotes are escaped, and the quote is escaped.
46 EXPECT_EQ("\"foo\\\\\\\"$ bar\"", EscapeString("foo\\\" bar", opts, nullptr));
47 }
48
TEST(Escape,PosixCommand)49 TEST(Escape, PosixCommand) {
50 EscapeOptions opts;
51 opts.mode = ESCAPE_NINJA_COMMAND;
52 opts.platform = ESCAPE_PLATFORM_POSIX;
53
54 // : and $ ninja escaped with $. Then Shell-escape backslashes and quotes.
55 EXPECT_EQ("a$:\\$ \\\"\\$$\\\\b", EscapeString("a: \"$\\b", opts, nullptr));
56
57 // Some more generic shell chars.
58 EXPECT_EQ("a_\\;\\<\\*b", EscapeString("a_;<*b", opts, nullptr));
59
60 // Curly braces must be escaped to avoid brace expansion on systems using
61 // bash as default shell..
62 EXPECT_EQ("\\{a,b\\}\\{c,d\\}", EscapeString("{a,b}{c,d}", opts, nullptr));
63 }
64
TEST(Escape,NinjaPreformatted)65 TEST(Escape, NinjaPreformatted) {
66 EscapeOptions opts;
67 opts.mode = ESCAPE_NINJA_PREFORMATTED_COMMAND;
68
69 // Only $ is escaped.
70 EXPECT_EQ("a: \"$$\\b<;", EscapeString("a: \"$\\b<;", opts, nullptr));
71 }
72
TEST(Escape,Space)73 TEST(Escape, Space) {
74 EscapeOptions opts;
75 opts.mode = ESCAPE_SPACE;
76
77 // ' ' is escaped.
78 EXPECT_EQ("-VERSION=\"libsrtp2\\ 2.1.0-pre\"",
79 EscapeString("-VERSION=\"libsrtp2 2.1.0-pre\"", opts, nullptr));
80 }
81