Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 59f955a

Browse files
committed
[YAML] Refactor escaping unittests
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@321284 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent acf228a commit 59f955a

File tree

2 files changed

+25
-112
lines changed

2 files changed

+25
-112
lines changed

include/llvm/Support/YAMLTraits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ template <typename T> struct StdMapStringCustomMappingTraitsImpl {
17251725
template <> struct ScalarTraits<Type> { \
17261726
static void output(const Type &Value, void *ctx, raw_ostream &Out); \
17271727
static StringRef input(StringRef Scalar, void *ctxt, Type &Value); \
1728-
static QuotingType mustQuote(StringRef) { return MustQuote; } \
1728+
static QuotingType mustQuote(StringRef) { return MustQuote; } \
17291729
}; \
17301730
} \
17311731
}

unittests/Support/YAMLIOTest.cpp

Lines changed: 24 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10+
#include "llvm/ADT/StringRef.h"
1011
#include "llvm/ADT/Twine.h"
1112
#include "llvm/Support/Casting.h"
1213
#include "llvm/Support/Endian.h"
@@ -2450,122 +2451,34 @@ TEST(YAMLIO, TestCustomMappingStruct) {
24502451
EXPECT_EQ(4, y["bar"].bar);
24512452
}
24522453

2453-
TEST(YAMLIO, InvalidInput) {
2454-
// polluting 1 value in the sequence
2455-
Input yin("---\n- foo: 3\n bar: 5\n1\n- foo: 3\n bar: 5\n...\n");
2456-
std::vector<FooBar> Data;
2457-
yin >> Data;
2458-
EXPECT_TRUE((bool)yin.error());
2459-
}
2460-
2461-
TEST(YAMLIO, TestEscapedSingleQuote) {
2462-
std::string Id = "@abc@";
2463-
2464-
std::string out;
2465-
llvm::raw_string_ostream ostr(out);
2466-
Output xout(ostr, nullptr, 0);
2467-
2468-
llvm::yaml::EmptyContext Ctx;
2469-
yamlize(xout, Id, true, Ctx);
2470-
2471-
ostr.flush();
2472-
EXPECT_EQ("'@abc@'", out);
2473-
}
2474-
2475-
TEST(YAMLIO, TestEscapedNoQuote) {
2476-
std::string Id = "abc/";
2477-
2478-
std::string out;
2479-
llvm::raw_string_ostream ostr(out);
2480-
Output xout(ostr, nullptr, 0);
2481-
2482-
llvm::yaml::EmptyContext Ctx;
2483-
yamlize(xout, Id, true, Ctx);
2484-
2485-
ostr.flush();
2486-
EXPECT_EQ("abc/", out);
2487-
}
2488-
2489-
TEST(YAMLIO, TestEscapedDoubleQuoteNonPrintable) {
2490-
std::string Id = "\01@abc@";
2491-
2492-
std::string out;
2493-
llvm::raw_string_ostream ostr(out);
2494-
Output xout(ostr, nullptr, 0);
2495-
2496-
llvm::yaml::EmptyContext Ctx;
2497-
yamlize(xout, Id, true, Ctx);
2498-
2499-
ostr.flush();
2500-
EXPECT_EQ("\"\\x01@abc@\"", out);
2501-
}
2502-
2503-
TEST(YAMLIO, TestEscapedDoubleQuoteInsideSingleQuote) {
2504-
std::string Id = "abc\"fdf";
2505-
2506-
std::string out;
2507-
llvm::raw_string_ostream ostr(out);
2508-
Output xout(ostr, nullptr, 0);
2509-
2510-
llvm::yaml::EmptyContext Ctx;
2511-
yamlize(xout, Id, true, Ctx);
2512-
2513-
ostr.flush();
2514-
EXPECT_EQ("'abc\"fdf'", out);
2515-
}
2516-
2517-
TEST(YAMLIO, TestEscapedDoubleQuoteInsideDoubleQuote) {
2518-
std::string Id = "\01bc\"fdf";
2519-
2520-
std::string out;
2521-
llvm::raw_string_ostream ostr(out);
2522-
Output xout(ostr, nullptr, 0);
2523-
2524-
llvm::yaml::EmptyContext Ctx;
2525-
yamlize(xout, Id, true, Ctx);
2526-
2527-
ostr.flush();
2528-
EXPECT_EQ("\"\\x01bc\\\"fdf\"", out);
2529-
}
2530-
2531-
TEST(YAMLIO, TestEscapedSingleQuoteInsideSingleQuote) {
2532-
std::string Id = "abc'fdf";
2533-
2534-
std::string out;
2535-
llvm::raw_string_ostream ostr(out);
2536-
Output xout(ostr, nullptr, 0);
2537-
2538-
llvm::yaml::EmptyContext Ctx;
2539-
yamlize(xout, Id, true, Ctx);
2540-
2541-
ostr.flush();
2542-
EXPECT_EQ("'abc''fdf'", out);
2543-
}
2544-
2545-
TEST(YAMLIO, TestEscapedUTF8SingleQuoteInsideDoubleQuote) {
2546-
std::string Id = "parameter 'параметр' is unused";
2547-
2548-
std::string out;
2549-
llvm::raw_string_ostream ostr(out);
2550-
Output xout(ostr, nullptr, 0);
2551-
2552-
llvm::yaml::EmptyContext Ctx;
2553-
yamlize(xout, Id, true, Ctx);
2554-
2555-
ostr.flush();
2556-
EXPECT_EQ("\"parameter 'параметр' is unused\"", out);
2557-
}
2558-
2559-
TEST(YAMLIO, TestEscapedUTF8) {
2560-
std::string Id = "/*параметр*/";
2561-
2454+
static void TestEscaped(llvm::StringRef Input, llvm::StringRef Expected) {
25622455
std::string out;
25632456
llvm::raw_string_ostream ostr(out);
25642457
Output xout(ostr, nullptr, 0);
25652458

25662459
llvm::yaml::EmptyContext Ctx;
2567-
yamlize(xout, Id, true, Ctx);
2460+
yamlize(xout, Input, true, Ctx);
25682461

25692462
ostr.flush();
2570-
EXPECT_EQ("\"/*параметр*/\"", out);
2463+
EXPECT_EQ(Expected, out);
2464+
}
2465+
2466+
TEST(YAMLIO, TestEscaped) {
2467+
// Single quote
2468+
TestEscaped("@abc@", "'@abc@'");
2469+
// No quote
2470+
TestEscaped("abc/", "abc/");
2471+
// Double quote non-printable
2472+
TestEscaped("\01@abc@", "\"\\x01@abc@\"");
2473+
// Double quote inside single quote
2474+
TestEscaped("abc\"fdf", "'abc\"fdf'");
2475+
// Double quote inside double quote
2476+
TestEscaped("\01bc\"fdf", "\"\\x01bc\\\"fdf\"");
2477+
// Single quote inside single quote
2478+
TestEscaped("abc'fdf", "'abc''fdf'");
2479+
// UTF8
2480+
TestEscaped("/*параметр*/", "\"/*параметр*/\"");
2481+
// UTF8 with single quote inside double quote
2482+
TestEscaped("parameter 'параметр' is unused",
2483+
"\"parameter 'параметр' is unused\"");
25712484
}

0 commit comments

Comments
 (0)