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 | 7cd45f29b26b6cb4e0019b7355ed69db9ab55992 (patch) | |
| tree | aa298a7035d484dcec1ef66cdef4aaec18afe467 /llvm/unittests/Support | |
| parent | 4761c60eef46a9916b964395c0c68d51da7d3ae0 (diff) | |
| download | bcm5719-llvm-7cd45f29b26b6cb4e0019b7355ed69db9ab55992.tar.gz bcm5719-llvm-7cd45f29b26b6cb4e0019b7355ed69db9ab55992.zip | |
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>.
llvm-svn: 195286
Diffstat (limited to 'llvm/unittests/Support')
| -rw-r--r-- | llvm/unittests/Support/YAMLIOTest.cpp | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp index 07d70459fb8..52a8f6b88c2 100644 --- a/llvm/unittests/Support/YAMLIOTest.cpp +++ b/llvm/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) { |

