diff options
author | Nick Kledzik <kledzik@apple.com> | 2013-11-21 00:28:07 +0000 |
---|---|---|
committer | Nick Kledzik <kledzik@apple.com> | 2013-11-21 00:28:07 +0000 |
commit | 9fd7416b3b5c437e3390ea9679d77e0221bec0b5 (patch) | |
tree | 556eba1681d33365b00d13b9f331a76ccb6c39e8 /unittests | |
parent | 25f01786ea6258bfddf2a051f8343b7f2b45e338 (diff) | |
download | llvm-9fd7416b3b5c437e3390ea9679d77e0221bec0b5.tar.gz llvm-9fd7416b3b5c437e3390ea9679d77e0221bec0b5.tar.bz2 llvm-9fd7416b3b5c437e3390ea9679d77e0221bec0b5.tar.xz |
YAML I/O add support for validate()
MappingTrait template specializations can now have a validate() method which
performs semantic checking. For details, see <http://llvm.org/docs/YamlIO.html>.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@195286 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/Support/YAMLIOTest.cpp | 46 |
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) { |