summaryrefslogtreecommitdiffstats
path: root/tools/gn/escape_unittest.cc
blob: 7ba0af060d18099de347dee1297c86a5c35ba700 (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
61
62
63
64
65
66
// 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, NULL);
  EXPECT_EQ("asdf$:$ \"$$\\bar", result);
}

TEST(Escape, Shell) {
  EscapeOptions opts;
  opts.mode = ESCAPE_SHELL;
  std::string result = EscapeString("asdf: \"$\\bar", opts, NULL);
#if defined(OS_WIN)
  // Windows shell doesn't escape backslashes, but it does backslash-escape
  // quotes.
  EXPECT_EQ("\"asdf: \\\"$\\bar\"", result);
#else
  EXPECT_EQ("\"asdf: \\\"$\\\\bar\"", result);
#endif
}

TEST(Escape, UsedQuotes) {
  EscapeOptions shell_options;
  shell_options.mode = ESCAPE_SHELL;

  EscapeOptions ninja_options;
  ninja_options.mode = ESCAPE_NINJA;

  EscapeOptions ninja_shell_options;
  ninja_shell_options.mode = ESCAPE_NINJA_SHELL;

  // Shell escaping with quoting inhibited.
  bool used_quotes = false;
  shell_options.inhibit_quoting = true;
  EXPECT_EQ("foo bar", EscapeString("foo bar", shell_options, &used_quotes));
  EXPECT_TRUE(used_quotes);

  // Shell escaping with regular quoting.
  used_quotes = false;
  shell_options.inhibit_quoting = false;
  EXPECT_EQ("\"foo bar\"",
            EscapeString("foo bar", shell_options, &used_quotes));
  EXPECT_TRUE(used_quotes);

  // Ninja shell escaping should be the same.
  used_quotes = false;
  EXPECT_EQ("\"foo$ bar\"",
            EscapeString("foo bar", ninja_shell_options, &used_quotes));
  EXPECT_TRUE(used_quotes);

  // Ninja escaping shouldn't use quoting.
  used_quotes = false;
  EXPECT_EQ("foo$ bar", EscapeString("foo bar", ninja_options, &used_quotes));
  EXPECT_FALSE(used_quotes);

  // Used quotes not reset if it's already true.
  used_quotes = true;
  EscapeString("foo", ninja_options, &used_quotes);
  EXPECT_TRUE(used_quotes);
}