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.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/escape.h"
10 opts
.mode
= ESCAPE_NINJA
;
11 std::string result
= EscapeString("asdf: \"$\\bar", opts
, nullptr);
12 EXPECT_EQ("asdf$:$ \"$$\\bar", result
);
15 TEST(Escape
, WindowsCommand
) {
17 opts
.mode
= ESCAPE_NINJA_COMMAND
;
18 opts
.platform
= ESCAPE_PLATFORM_WIN
;
20 // Regular string is passed, even if it has backslashes.
21 EXPECT_EQ("foo\\bar", EscapeString("foo\\bar", opts
, nullptr));
23 // Spaces means the string is quoted, normal backslahes untouched.
24 bool needs_quoting
= false;
25 EXPECT_EQ("\"foo\\$ bar\"", EscapeString("foo\\ bar", opts
, &needs_quoting
));
26 EXPECT_TRUE(needs_quoting
);
29 needs_quoting
= false;
30 opts
.inhibit_quoting
= true;
31 EXPECT_EQ("foo\\$ bar", EscapeString("foo\\ bar", opts
, &needs_quoting
));
32 EXPECT_TRUE(needs_quoting
);
33 opts
.inhibit_quoting
= false;
35 // Backslashes at the end of the string get escaped.
36 EXPECT_EQ("\"foo$ bar\\\\\\\\\"", EscapeString("foo bar\\\\", opts
, nullptr));
38 // Backslashes preceeding quotes are escaped, and the quote is escaped.
39 EXPECT_EQ("\"foo\\\\\\\"$ bar\"", EscapeString("foo\\\" bar", opts
, nullptr));
42 TEST(Escape
, PosixCommand
) {
44 opts
.mode
= ESCAPE_NINJA_COMMAND
;
45 opts
.platform
= ESCAPE_PLATFORM_POSIX
;
47 // : and $ ninja escaped with $. Then Shell-escape backslashes and quotes.
48 EXPECT_EQ("a$:\\$ \\\"\\$$\\\\b", EscapeString("a: \"$\\b", opts
, nullptr));
50 // Some more generic shell chars.
51 EXPECT_EQ("a_\\;\\<\\*b", EscapeString("a_;<*b", opts
, nullptr));
54 TEST(Escape
, NinjaPreformatted
) {
56 opts
.mode
= ESCAPE_NINJA_PREFORMATTED_COMMAND
;
59 EXPECT_EQ("a: \"$$\\b<;", EscapeString("a: \"$\\b<;", opts
, nullptr));