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 "testing/gtest/include/gtest/gtest.h" 6 #include "tools/gn/escape.h" 7 8 TEST(Escape, Ninja) { 9 EscapeOptions opts; 10 opts.mode = ESCAPE_NINJA; 11 std::string result = EscapeString("asdf: \"$\\bar", opts, NULL); 12 EXPECT_EQ("asdf$:$ \"$$\\bar", result); 13 } 14 15 TEST(Escape, WindowsCommand) { 16 EscapeOptions opts; 17 opts.mode = ESCAPE_NINJA_COMMAND; 18 opts.platform = ESCAPE_PLATFORM_WIN; 19 20 // Regular string is passed, even if it has backslashes. 21 EXPECT_EQ("foo\\bar", EscapeString("foo\\bar", opts, NULL)); 22 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); 27 28 // Inhibit 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; 34 35 // Backslashes at the end of the string get escaped. 36 EXPECT_EQ("\"foo$ bar\\\\\\\\\"", EscapeString("foo bar\\\\", opts, NULL)); 37 38 // Backslashes preceeding quotes are escaped, and the quote is escaped. 39 EXPECT_EQ("\"foo\\\\\\\"$ bar\"", EscapeString("foo\\\" bar", opts, NULL)); 40 } 41 42 TEST(Escape, PosixCommand) { 43 EscapeOptions opts; 44 opts.mode = ESCAPE_NINJA_COMMAND; 45 opts.platform = ESCAPE_PLATFORM_POSIX; 46 47 // : and $ ninja escaped with $. Then Shell-escape backslashes and quotes. 48 EXPECT_EQ("a$:\\$ \\\"\\$$\\\\b", EscapeString("a: \"$\\b", opts, NULL)); 49 50 // Some more generic shell chars. 51 EXPECT_EQ("a_\\;\\<\\*b", EscapeString("a_;<*b", opts, NULL)); 52 } 53 54 TEST(Escape, NinjaPreformatted) { 55 EscapeOptions opts; 56 opts.mode = ESCAPE_NINJA_PREFORMATTED_COMMAND; 57 58 // Only $ is escaped. 59 EXPECT_EQ("a: \"$$\\b<;", EscapeString("a: \"$\\b<;", opts, NULL)); 60 } 61