summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
Diffstat (limited to 'unittests')
-rw-r--r--unittests/Support/YAMLIOTest.cpp46
1 files changed, 43 insertions, 3 deletions
diff --git a/unittests/Support/YAMLIOTest.cpp b/unittests/Support/YAMLIOTest.cpp
index 07d70459fb..52a8f6b88c 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
//===----------------------------------------------------------------------===//
@@ -1115,18 +1122,51 @@ TEST(YAMLIO, TestTaggedDocumentsWriteAndRead) {
}
-
//===----------------------------------------------------------------------===//
-// Test error handling
+// Test mapping validation
//===----------------------------------------------------------------------===//
+struct MyValidation {
+ double value;
+};
+LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(MyValidation)
-static void suppressErrorMessages(const llvm::SMDiagnostic &, void *) {
+namespace llvm {
+namespace yaml {
+ template <>
+ 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 that validate() is called and complains about the negative value.
+//
+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
+//===----------------------------------------------------------------------===//
+
+//
// Test error handling of unknown enumerated scalar
//
TEST(YAMLIO, TestColorsReadError) {