/tools/gn/escape_unittest.cc

https://gitlab.com/jonnialva90/iridium-browser · C++ · 60 lines · 36 code · 13 blank · 11 comment · 0 complexity · 9a45de9753921b0e15e71ec0b4c0f564 MD5 · raw file

  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. #include "testing/gtest/include/gtest/gtest.h"
  5. #include "tools/gn/escape.h"
  6. TEST(Escape, Ninja) {
  7. EscapeOptions opts;
  8. opts.mode = ESCAPE_NINJA;
  9. std::string result = EscapeString("asdf: \"$\\bar", opts, nullptr);
  10. EXPECT_EQ("asdf$:$ \"$$\\bar", result);
  11. }
  12. TEST(Escape, WindowsCommand) {
  13. EscapeOptions opts;
  14. opts.mode = ESCAPE_NINJA_COMMAND;
  15. opts.platform = ESCAPE_PLATFORM_WIN;
  16. // Regular string is passed, even if it has backslashes.
  17. EXPECT_EQ("foo\\bar", EscapeString("foo\\bar", opts, nullptr));
  18. // Spaces means the string is quoted, normal backslahes untouched.
  19. bool needs_quoting = false;
  20. EXPECT_EQ("\"foo\\$ bar\"", EscapeString("foo\\ bar", opts, &needs_quoting));
  21. EXPECT_TRUE(needs_quoting);
  22. // Inhibit quoting.
  23. needs_quoting = false;
  24. opts.inhibit_quoting = true;
  25. EXPECT_EQ("foo\\$ bar", EscapeString("foo\\ bar", opts, &needs_quoting));
  26. EXPECT_TRUE(needs_quoting);
  27. opts.inhibit_quoting = false;
  28. // Backslashes at the end of the string get escaped.
  29. EXPECT_EQ("\"foo$ bar\\\\\\\\\"", EscapeString("foo bar\\\\", opts, nullptr));
  30. // Backslashes preceeding quotes are escaped, and the quote is escaped.
  31. EXPECT_EQ("\"foo\\\\\\\"$ bar\"", EscapeString("foo\\\" bar", opts, nullptr));
  32. }
  33. TEST(Escape, PosixCommand) {
  34. EscapeOptions opts;
  35. opts.mode = ESCAPE_NINJA_COMMAND;
  36. opts.platform = ESCAPE_PLATFORM_POSIX;
  37. // : and $ ninja escaped with $. Then Shell-escape backslashes and quotes.
  38. EXPECT_EQ("a$:\\$ \\\"\\$$\\\\b", EscapeString("a: \"$\\b", opts, nullptr));
  39. // Some more generic shell chars.
  40. EXPECT_EQ("a_\\;\\<\\*b", EscapeString("a_;<*b", opts, nullptr));
  41. }
  42. TEST(Escape, NinjaPreformatted) {
  43. EscapeOptions opts;
  44. opts.mode = ESCAPE_NINJA_PREFORMATTED_COMMAND;
  45. // Only $ is escaped.
  46. EXPECT_EQ("a: \"$$\\b<;", EscapeString("a: \"$\\b<;", opts, nullptr));
  47. }