llvm.org GIT mirror llvm / 79085a1
[YAML] Refactor escaping unittests git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@321284 91177308-0d34-0410-b5e6-96231b3b80d8 Francis Visoiu Mistrih 2 years ago
2 changed file(s) with 26 addition(s) and 113 deletion(s). Raw diff Collapse all Expand all
17241724 template <> struct ScalarTraits { \
17251725 static void output(const Type &Value, void *ctx, raw_ostream &Out); \
17261726 static StringRef input(StringRef Scalar, void *ctxt, Type &Value); \
1727 static QuotingType mustQuote(StringRef) { return MustQuote; } \
1727 static QuotingType mustQuote(StringRef) { return MustQuote; } \
17281728 }; \
17291729 } \
17301730 }
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "llvm/ADT/StringRef.h"
910 #include "llvm/ADT/Twine.h"
1011 #include "llvm/Support/Casting.h"
1112 #include "llvm/Support/Endian.h"
24492450 EXPECT_EQ(4, y["bar"].bar);
24502451 }
24512452
2452 TEST(YAMLIO, InvalidInput) {
2453 // polluting 1 value in the sequence
2454 Input yin("---\n- foo: 3\n bar: 5\n1\n- foo: 3\n bar: 5\n...\n");
2455 std::vector Data;
2456 yin >> Data;
2457 EXPECT_TRUE((bool)yin.error());
2458 }
2459
2460 TEST(YAMLIO, TestEscapedSingleQuote) {
2461 std::string Id = "@abc@";
2462
2453 static void TestEscaped(llvm::StringRef Input, llvm::StringRef Expected) {
24632454 std::string out;
24642455 llvm::raw_string_ostream ostr(out);
24652456 Output xout(ostr, nullptr, 0);
24662457
24672458 llvm::yaml::EmptyContext Ctx;
2468 yamlize(xout, Id, true, Ctx);
2459 yamlize(xout, Input, true, Ctx);
24692460
24702461 ostr.flush();
2471 EXPECT_EQ("'@abc@'", out);
2472 }
2473
2474 TEST(YAMLIO, TestEscapedNoQuote) {
2475 std::string Id = "abc/";
2476
2477 std::string out;
2478 llvm::raw_string_ostream ostr(out);
2479 Output xout(ostr, nullptr, 0);
2480
2481 llvm::yaml::EmptyContext Ctx;
2482 yamlize(xout, Id, true, Ctx);
2483
2484 ostr.flush();
2485 EXPECT_EQ("abc/", out);
2486 }
2487
2488 TEST(YAMLIO, TestEscapedDoubleQuoteNonPrintable) {
2489 std::string Id = "\01@abc@";
2490
2491 std::string out;
2492 llvm::raw_string_ostream ostr(out);
2493 Output xout(ostr, nullptr, 0);
2494
2495 llvm::yaml::EmptyContext Ctx;
2496 yamlize(xout, Id, true, Ctx);
2497
2498 ostr.flush();
2499 EXPECT_EQ("\"\\x01@abc@\"", out);
2500 }
2501
2502 TEST(YAMLIO, TestEscapedDoubleQuoteInsideSingleQuote) {
2503 std::string Id = "abc\"fdf";
2504
2505 std::string out;
2506 llvm::raw_string_ostream ostr(out);
2507 Output xout(ostr, nullptr, 0);
2508
2509 llvm::yaml::EmptyContext Ctx;
2510 yamlize(xout, Id, true, Ctx);
2511
2512 ostr.flush();
2513 EXPECT_EQ("'abc\"fdf'", out);
2514 }
2515
2516 TEST(YAMLIO, TestEscapedDoubleQuoteInsideDoubleQuote) {
2517 std::string Id = "\01bc\"fdf";
2518
2519 std::string out;
2520 llvm::raw_string_ostream ostr(out);
2521 Output xout(ostr, nullptr, 0);
2522
2523 llvm::yaml::EmptyContext Ctx;
2524 yamlize(xout, Id, true, Ctx);
2525
2526 ostr.flush();
2527 EXPECT_EQ("\"\\x01bc\\\"fdf\"", out);
2528 }
2529
2530 TEST(YAMLIO, TestEscapedSingleQuoteInsideSingleQuote) {
2531 std::string Id = "abc'fdf";
2532
2533 std::string out;
2534 llvm::raw_string_ostream ostr(out);
2535 Output xout(ostr, nullptr, 0);
2536
2537 llvm::yaml::EmptyContext Ctx;
2538 yamlize(xout, Id, true, Ctx);
2539
2540 ostr.flush();
2541 EXPECT_EQ("'abc''fdf'", out);
2542 }
2543
2544 TEST(YAMLIO, TestEscapedUTF8SingleQuoteInsideDoubleQuote) {
2545 std::string Id = "parameter 'параметр' is unused";
2546
2547 std::string out;
2548 llvm::raw_string_ostream ostr(out);
2549 Output xout(ostr, nullptr, 0);
2550
2551 llvm::yaml::EmptyContext Ctx;
2552 yamlize(xout, Id, true, Ctx);
2553
2554 ostr.flush();
2555 EXPECT_EQ("\"parameter 'параметр' is unused\"", out);
2556 }
2557
2558 TEST(YAMLIO, TestEscapedUTF8) {
2559 std::string Id = "/*параметр*/";
2560
2561 std::string out;
2562 llvm::raw_string_ostream ostr(out);
2563 Output xout(ostr, nullptr, 0);
2564
2565 llvm::yaml::EmptyContext Ctx;
2566 yamlize(xout, Id, true, Ctx);
2567
2568 ostr.flush();
2569 EXPECT_EQ("\"/*параметр*/\"", out);
2570 }
2462 EXPECT_EQ(Expected, out);
2463 }
2464
2465 TEST(YAMLIO, TestEscaped) {
2466 // Single quote
2467 TestEscaped("@abc@", "'@abc@'");
2468 // No quote
2469 TestEscaped("abc/", "abc/");
2470 // Double quote non-printable
2471 TestEscaped("\01@abc@", "\"\\x01@abc@\"");
2472 // Double quote inside single quote
2473 TestEscaped("abc\"fdf", "'abc\"fdf'");
2474 // Double quote inside double quote
2475 TestEscaped("\01bc\"fdf", "\"\\x01bc\\\"fdf\"");
2476 // Single quote inside single quote
2477 TestEscaped("abc'fdf", "'abc''fdf'");
2478 // UTF8
2479 TestEscaped("/*параметр*/", "\"/*параметр*/\"");
2480 // UTF8 with single quote inside double quote
2481 TestEscaped("parameter 'параметр' is unused",
2482 "\"parameter 'параметр' is unused\"");
2483 }