summaryrefslogtreecommitdiffstats
path: root/tools/gn/escape_unittest.cc
blob: f2b2eae19742e1451b17a5126d8710cf85275f1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "testing/gtest/include/gtest/gtest.h"
#include "tools/gn/escape.h"

TEST(Escape, Ninja) {
  EscapeOptions opts;
  opts.mode = ESCAPE_NINJA;
  std::string result = EscapeString("asdf: \"$\\bar", opts, nullptr);
  EXPECT_EQ("asdf$:$ \"$$\\bar", result);
}

TEST(Escape, WindowsCommand) {
  EscapeOptions opts;
  opts.mode = ESCAPE_NINJA_COMMAND;
  opts.platform = ESCAPE_PLATFORM_WIN;

  // Regular string is passed, even if it has backslashes.
  EXPECT_EQ("foo\\bar", EscapeString("foo\\bar", opts, nullptr));

  // Spaces means the string is quoted, normal backslahes untouched.
  bool needs_quoting = false;
  EXPECT_EQ("\"foo\\$ bar\"", EscapeString("foo\\ bar", opts, &needs_quoting));
  EXPECT_TRUE(needs_quoting);

  // Inhibit quoting.
  needs_quoting = false;
  opts.inhibit_quoting = true;
  EXPECT_EQ("foo\\$ bar", EscapeString("foo\\ bar", opts, &needs_quoting));
  EXPECT_TRUE(needs_quoting);
  opts.inhibit_quoting = false;

  // Backslashes at the end of the string get escaped.
  EXPECT_EQ("\"foo$ bar\\\\\\\\\"", EscapeString("foo bar\\\\", opts, nullptr));

  // Backslashes preceeding quotes are escaped, and the quote is escaped.
  EXPECT_EQ("\"foo\\\\\\\"$ bar\"", EscapeString("foo\\\" bar", opts, nullptr));
}

TEST(Escape, PosixCommand) {
  EscapeOptions opts;
  opts.mode = ESCAPE_NINJA_COMMAND;
  opts.platform = ESCAPE_PLATFORM_POSIX;

  // : and $ ninja escaped with $. Then Shell-escape backslashes and quotes.
  EXPECT_EQ("a$:\\$ \\\"\\$$\\\\b", EscapeString("a: \"$\\b", opts, nullptr));

  // Some more generic shell chars.
  EXPECT_EQ("a_\\;\\<\\*b", EscapeString("a_;<*b", opts, nullptr));
}

TEST(Escape, NinjaPreformatted) {
  EscapeOptions opts;
  opts.mode = ESCAPE_NINJA_PREFORMATTED_COMMAND;

  // Only $ is escaped.
  EXPECT_EQ("a: \"$$\\b<;", EscapeString("a: \"$\\b<;", opts, nullptr));
}