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
, NULL
);
12 EXPECT_EQ("asdf$:$ \"$$\\bar", result
);
17 opts
.mode
= ESCAPE_SHELL
;
18 std::string result
= EscapeString("asdf: \"$\\bar", opts
, NULL
);
20 // Windows shell doesn't escape backslashes, but it does backslash-escape
22 EXPECT_EQ("\"asdf: \\\"$\\bar\"", result
);
24 EXPECT_EQ("\"asdf: \\\"$\\\\bar\"", result
);
28 TEST(Escape
, UsedQuotes
) {
29 EscapeOptions shell_options
;
30 shell_options
.mode
= ESCAPE_SHELL
;
32 EscapeOptions ninja_options
;
33 ninja_options
.mode
= ESCAPE_NINJA
;
35 EscapeOptions ninja_shell_options
;
36 ninja_shell_options
.mode
= ESCAPE_NINJA_SHELL
;
38 // Shell escaping with quoting inhibited.
39 bool used_quotes
= false;
40 shell_options
.inhibit_quoting
= true;
41 EXPECT_EQ("foo bar", EscapeString("foo bar", shell_options
, &used_quotes
));
42 EXPECT_TRUE(used_quotes
);
44 // Shell escaping with regular quoting.
46 shell_options
.inhibit_quoting
= false;
47 EXPECT_EQ("\"foo bar\"",
48 EscapeString("foo bar", shell_options
, &used_quotes
));
49 EXPECT_TRUE(used_quotes
);
51 // Ninja shell escaping should be the same.
53 EXPECT_EQ("\"foo$ bar\"",
54 EscapeString("foo bar", ninja_shell_options
, &used_quotes
));
55 EXPECT_TRUE(used_quotes
);
57 // Ninja escaping shouldn't use quoting.
59 EXPECT_EQ("foo$ bar", EscapeString("foo bar", ninja_options
, &used_quotes
));
60 EXPECT_FALSE(used_quotes
);
62 // Used quotes not reset if it's already true.
64 EscapeString("foo", ninja_options
, &used_quotes
);
65 EXPECT_TRUE(used_quotes
);