• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gn/string_output_buffer.h"
7 #include "util/test/test.h"
8 
TEST(Escape,Ninja)9 TEST(Escape, Ninja) {
10   EscapeOptions opts;
11   opts.mode = ESCAPE_NINJA;
12   std::string result = EscapeString("asdf: \"$\\bar", opts, nullptr);
13   EXPECT_EQ("asdf$:$ \"$$\\bar", result);
14 }
15 
TEST(Escape,Depfile)16 TEST(Escape, Depfile) {
17   EscapeOptions opts;
18   opts.mode = ESCAPE_DEPFILE;
19   std::string result = EscapeString("asdf:$ \\#*[|]bar", opts, nullptr);
20   EXPECT_EQ("asdf:$$\\ \\\\\\#\\*\\[\\|\\]bar", result);
21 }
22 
TEST(Escape,WindowsCommand)23 TEST(Escape, WindowsCommand) {
24   EscapeOptions opts;
25   opts.mode = ESCAPE_NINJA_COMMAND;
26   opts.platform = ESCAPE_PLATFORM_WIN;
27 
28   // Regular string is passed, even if it has backslashes.
29   EXPECT_EQ("foo\\bar", EscapeString("foo\\bar", opts, nullptr));
30 
31   // Spaces means the string is quoted, normal backslahes untouched.
32   bool needs_quoting = false;
33   EXPECT_EQ("\"foo\\$ bar\"", EscapeString("foo\\ bar", opts, &needs_quoting));
34   EXPECT_TRUE(needs_quoting);
35 
36   // Inhibit quoting.
37   needs_quoting = false;
38   opts.inhibit_quoting = true;
39   EXPECT_EQ("foo\\$ bar", EscapeString("foo\\ bar", opts, &needs_quoting));
40   EXPECT_TRUE(needs_quoting);
41   opts.inhibit_quoting = false;
42 
43   // Backslashes at the end of the string get escaped.
44   EXPECT_EQ("\"foo$ bar\\\\\\\\\"", EscapeString("foo bar\\\\", opts, nullptr));
45 
46   // Backslashes preceding quotes are escaped, and the quote is escaped.
47   EXPECT_EQ("\"foo\\\\\\\"$ bar\"", EscapeString("foo\\\" bar", opts, nullptr));
48 }
49 
TEST(Escape,PosixCommand)50 TEST(Escape, PosixCommand) {
51   EscapeOptions opts;
52   opts.mode = ESCAPE_NINJA_COMMAND;
53   opts.platform = ESCAPE_PLATFORM_POSIX;
54 
55   // : and $ ninja escaped with $. Then Shell-escape backslashes and quotes.
56   EXPECT_EQ("a$:\\$ \\\"\\$$\\\\b", EscapeString("a: \"$\\b", opts, nullptr));
57 
58   // Some more generic shell chars.
59   EXPECT_EQ("a_\\;\\<\\*b", EscapeString("a_;<*b", opts, nullptr));
60 
61   // Curly braces must be escaped to avoid brace expansion on systems using
62   // bash as default shell..
63   EXPECT_EQ("\\{a,b\\}\\{c,d\\}", EscapeString("{a,b}{c,d}", opts, nullptr));
64 }
65 
TEST(Escape,NinjaPreformatted)66 TEST(Escape, NinjaPreformatted) {
67   EscapeOptions opts;
68   opts.mode = ESCAPE_NINJA_PREFORMATTED_COMMAND;
69 
70   // Only $ is escaped.
71   EXPECT_EQ("a: \"$$\\b<;", EscapeString("a: \"$\\b<;", opts, nullptr));
72 }
73 
TEST(Escape,Space)74 TEST(Escape, Space) {
75   EscapeOptions opts;
76   opts.mode = ESCAPE_SPACE;
77 
78   // ' ' is escaped.
79   EXPECT_EQ("-VERSION=\"libsrtp2\\ 2.1.0-pre\"",
80             EscapeString("-VERSION=\"libsrtp2 2.1.0-pre\"", opts, nullptr));
81 }
82 
TEST(EscapeJSONString,NinjaPreformatted)83 TEST(EscapeJSONString, NinjaPreformatted) {
84   EscapeOptions opts;
85   opts.mode = ESCAPE_NINJA_PREFORMATTED_COMMAND;
86   opts.inhibit_quoting = true;
87 
88   StringOutputBuffer buffer;
89   std::ostream out(&buffer);
90 
91   EscapeJSONStringToStream(out, "foo\\\" bar", opts);
92   EXPECT_EQ("foo\\\\\\\" bar", buffer.str());
93 
94   StringOutputBuffer buffer1;
95   std::ostream out1(&buffer1);
96   EscapeJSONStringToStream(out1, "foo bar\\\\", opts);
97   EXPECT_EQ("foo bar\\\\\\\\", buffer1.str());
98 
99   StringOutputBuffer buffer2;
100   std::ostream out2(&buffer2);
101   EscapeJSONStringToStream(out2, "a: \"$\\b", opts);
102   EXPECT_EQ("a: \\\"$$\\\\b", buffer2.str());
103 }
104 
TEST(Escape,CompilationDatabase)105 TEST(Escape, CompilationDatabase) {
106   EscapeOptions opts;
107   opts.mode = ESCAPE_COMPILATION_DATABASE;
108 
109   // The only special characters are '"' and '\'.
110   std::string result = EscapeString("asdf:$ \\#*[|]bar", opts, nullptr);
111   EXPECT_EQ("\"asdf:$ \\\\#*[|]bar\"", result);
112 }
113