summaryrefslogtreecommitdiffstats
path: root/unittests
diff options
context:
space:
mode:
authorNick Kledzik <kledzik@apple.com>2013-11-14 02:38:07 +0000
committerNick Kledzik <kledzik@apple.com>2013-11-14 02:38:07 +0000
commit04fca67d6f4b314ba618714698b58dbfba3af005 (patch)
tree96b4e3f0ffe4a83918bb125ad8297fdc48743eae /unittests
parente2058ff5bd4fa0397b57f6bdd84e5a5aa2343433 (diff)
downloadexternal_llvm-04fca67d6f4b314ba618714698b58dbfba3af005.zip
external_llvm-04fca67d6f4b314ba618714698b58dbfba3af005.tar.gz
external_llvm-04fca67d6f4b314ba618714698b58dbfba3af005.tar.bz2
Add dyn_cast<> support to YAML I/O's IO class
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194655 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/Support/YAMLIOTest.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/unittests/Support/YAMLIOTest.cpp b/unittests/Support/YAMLIOTest.cpp
index db70b91..f77df57 100644
--- a/unittests/Support/YAMLIOTest.cpp
+++ b/unittests/Support/YAMLIOTest.cpp
@@ -1074,6 +1074,76 @@ TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
}
+//===----------------------------------------------------------------------===//
+// Test dyn_cast<> on IO object
+//===----------------------------------------------------------------------===//
+
+struct DynCast {
+ int value;
+};
+typedef std::vector<DynCast> DynCastSequence;
+
+LLVM_YAML_IS_SEQUENCE_VECTOR(DynCast)
+
+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;
+ }
+ }
+ };
+}
+}
+
+//
+// Test writing then reading back a sequence of mappings
+//
+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 error handling