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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
//===- unittests/Basic/ShellUtilityTest.cpp -------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "llbuild/Basic/ShellUtility.h"
#include "gtest/gtest.h"
using namespace llbuild;
using namespace llbuild::basic;
namespace {
std::string quoted(std::string s) {
#if defined(_WIN32)
std::string quote = "\"";
#else
std::string quote = "'";
#endif
return quote + s + quote;
}
TEST(UtilityTest, basic) {
// No escapable char.
std::string output = shellEscaped("input01");
EXPECT_EQ(output, "input01");
// Space.
output = shellEscaped("input A");
#if defined(_WIN32)
std::string quote = "\"";
#else
std::string quote = "'";
#endif
EXPECT_EQ(output, quoted("input A"));
// Two spaces.
output = shellEscaped("input A B");
EXPECT_EQ(output, quoted("input A B"));
// Double Quote.
output = shellEscaped("input\"A");
#if defined(_WIN32)
EXPECT_EQ(output, quoted("input") + "\\" + quote + quoted("A"));
#else
EXPECT_EQ(output, quoted("input\"A"));
#endif
// Single Quote.
output = shellEscaped("input'A");
#if defined(_WIN32)
EXPECT_EQ(output, quoted("input'A"));
#else
EXPECT_EQ(output, "'input'\\''A'");
#endif
// Question Mark.
output = shellEscaped("input?A");
EXPECT_EQ(output, quoted("input?A"));
// New line.
output = shellEscaped("input\nA");
EXPECT_EQ(output, quoted("input\nA"));
// Multiple special chars.
#if defined(_WIN32)
output = shellEscaped("input\nA'B C>D*[$;()^><");
EXPECT_EQ(output, quoted("input\nA'B C>D*[$;()^><"));
#else
output = shellEscaped("input\nA\"B C>D*[$;()^><");
EXPECT_EQ(output, quoted("input\nA\"B C>D*[$;()^><"));
#endif
}
}
|