diff options
Diffstat (limited to 'unittests/Support/YAMLIOTest.cpp')
-rw-r--r-- | unittests/Support/YAMLIOTest.cpp | 119 |
1 files changed, 60 insertions, 59 deletions
diff --git a/unittests/Support/YAMLIOTest.cpp b/unittests/Support/YAMLIOTest.cpp index 6c0b9e6..52a8f6b 100644 --- a/unittests/Support/YAMLIOTest.cpp +++ b/unittests/Support/YAMLIOTest.cpp @@ -27,6 +27,13 @@ using llvm::yaml::Hex32; using llvm::yaml::Hex64; + + +static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) { +} + + + //===----------------------------------------------------------------------===// // Test MappingTraits //===----------------------------------------------------------------------===// @@ -140,6 +147,7 @@ TEST(YAMLIO, TestSequenceMapWriteAndRead) { struct BuiltInTypes { llvm::StringRef str; + std::string stdstr; uint64_t u64; uint32_t u32; uint16_t u16; @@ -163,6 +171,7 @@ namespace yaml { struct MappingTraits<BuiltInTypes> { static void mapping(IO &io, BuiltInTypes& bt) { io.mapRequired("str", bt.str); + io.mapRequired("stdstr", bt.stdstr); io.mapRequired("u64", bt.u64); io.mapRequired("u32", bt.u32); io.mapRequired("u16", bt.u16); @@ -191,6 +200,7 @@ TEST(YAMLIO, TestReadBuiltInTypes) { BuiltInTypes map; Input yin("---\n" "str: hello there\n" + "stdstr: hello where?\n" "u64: 5000000000\n" "u32: 4000000000\n" "u16: 65000\n" @@ -211,6 +221,7 @@ TEST(YAMLIO, TestReadBuiltInTypes) { EXPECT_FALSE(yin.error()); EXPECT_TRUE(map.str.equals("hello there")); + EXPECT_TRUE(map.stdstr == "hello where?"); EXPECT_EQ(map.u64, 5000000000ULL); EXPECT_EQ(map.u32, 4000000000U); EXPECT_EQ(map.u16, 65000); @@ -237,6 +248,7 @@ TEST(YAMLIO, TestReadWriteBuiltInTypes) { { BuiltInTypes map; map.str = "one two"; + map.stdstr = "three four"; map.u64 = 6000000000ULL; map.u32 = 3000000000U; map.u16 = 50000; @@ -265,6 +277,7 @@ TEST(YAMLIO, TestReadWriteBuiltInTypes) { EXPECT_FALSE(yin.error()); EXPECT_TRUE(map.str.equals("one two")); + EXPECT_TRUE(map.stdstr == "three four"); EXPECT_EQ(map.u64, 6000000000ULL); EXPECT_EQ(map.u32, 3000000000U); EXPECT_EQ(map.u16, 50000); @@ -289,6 +302,11 @@ struct StringTypes { llvm::StringRef str3; llvm::StringRef str4; llvm::StringRef str5; + std::string stdstr1; + std::string stdstr2; + std::string stdstr3; + std::string stdstr4; + std::string stdstr5; }; namespace llvm { @@ -301,6 +319,11 @@ namespace yaml { io.mapRequired("str3", st.str3); io.mapRequired("str4", st.str4); io.mapRequired("str5", st.str5); + io.mapRequired("stdstr1", st.stdstr1); + io.mapRequired("stdstr2", st.stdstr2); + io.mapRequired("stdstr3", st.stdstr3); + io.mapRequired("stdstr4", st.stdstr4); + io.mapRequired("stdstr5", st.stdstr5); } }; } @@ -315,6 +338,11 @@ TEST(YAMLIO, TestReadWriteStringTypes) { map.str3 = "`ccc"; map.str4 = "@ddd"; map.str5 = ""; + map.stdstr1 = "'eee"; + map.stdstr2 = "\"fff"; + map.stdstr3 = "`ggg"; + map.stdstr4 = "@hhh"; + map.stdstr5 = ""; llvm::raw_string_ostream ostr(intermediate); Output yout(ostr); @@ -327,6 +355,11 @@ TEST(YAMLIO, TestReadWriteStringTypes) { EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'")); EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'")); EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n")); + EXPECT_NE(std::string::npos, flowOut.find("'''eee")); + EXPECT_NE(std::string::npos, flowOut.find("'\"fff'")); + EXPECT_NE(std::string::npos, flowOut.find("'`ggg'")); + EXPECT_NE(std::string::npos, flowOut.find("'@hhh'")); + EXPECT_NE(std::string::npos, flowOut.find("''\n")); { Input yin(intermediate); @@ -339,6 +372,11 @@ TEST(YAMLIO, TestReadWriteStringTypes) { EXPECT_TRUE(map.str3.equals("`ccc")); EXPECT_TRUE(map.str4.equals("@ddd")); EXPECT_TRUE(map.str5.equals("")); + EXPECT_TRUE(map.stdstr1 == "'eee"); + EXPECT_TRUE(map.stdstr2 == "\"fff"); + EXPECT_TRUE(map.stdstr3 == "`ggg"); + EXPECT_TRUE(map.stdstr4 == "@hhh"); + EXPECT_TRUE(map.stdstr5 == ""); } } @@ -1085,86 +1123,49 @@ TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) { //===----------------------------------------------------------------------===// -// Test dyn_cast<> on IO object +// Test mapping validation //===----------------------------------------------------------------------===// -struct DynCast { - int value; +struct MyValidation { + double value; }; -typedef std::vector<DynCast> DynCastSequence; -LLVM_YAML_IS_SEQUENCE_VECTOR(DynCast) +LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation) namespace llvm { namespace yaml { template <> - struct MappingTraits<DynCast> { - static void mapping(IO &io, DynCast& info) { - // Change 10 to 13 when writing yaml. - if (Output *output = dyn_cast<Output>(&io)) { - (void)output; - if (info.value == 10) - info.value = 13; - } - io.mapRequired("value", info.value); - // Change 20 to 23 when parsing yaml. - if (Input *input = dyn_cast<Input>(&io)) { - (void)input; - if (info.value == 20) - info.value = 23; - } + struct MappingTraits<MyValidation> { + static void mapping(IO &io, MyValidation &d) { + io.mapRequired("value", d.value); + } + static StringRef validate(IO &io, MyValidation &d) { + if (d.value < 0) + return "negative value"; + return StringRef(); } }; -} + } } + // -// Test writing then reading back a sequence of mappings +// Test that validate() is called and complains about the negative value. // -TEST(YAMLIO, TestDynCast) { - std::string intermediate; - { - DynCast entry1; - entry1.value = 10; - DynCast entry2; - entry2.value = 20; - DynCast entry3; - entry3.value = 30; - DynCastSequence seq; - seq.push_back(entry1); - seq.push_back(entry2); - seq.push_back(entry3); - - llvm::raw_string_ostream ostr(intermediate); - Output yout(ostr); - yout << seq; - } - - { - Input yin(intermediate); - DynCastSequence seq2; - yin >> seq2; - - EXPECT_FALSE(yin.error()); - EXPECT_EQ(seq2.size(), 3UL); - EXPECT_EQ(seq2[0].value, 13); // Verify changed to 13. - EXPECT_EQ(seq2[1].value, 23); // Verify changed to 23. - EXPECT_EQ(seq2[2].value, 30); // Verify stays same. - } +TEST(YAMLIO, TestValidatingInput) { + std::vector<MyValidation> docList; + Input yin("--- \nvalue: 3.0\n" + "--- \nvalue: -1.0\n...\n", + NULL, suppressErrorMessages); + yin >> docList; + EXPECT_TRUE(yin.error()); } - //===----------------------------------------------------------------------===// // Test error handling //===----------------------------------------------------------------------===// - - -static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) { -} - - // // Test error handling of unknown enumerated scalar // |