From 2ab025a4eb60d289ef23988afe26b546870a10fe Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 3 Dec 2015 12:19:08 +0100 Subject: [PATCH] Add test: match_header_spec() testing --- src/storage/file.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/storage/file.rs b/src/storage/file.rs index a45c2292..da7dd66e 100644 --- a/src/storage/file.rs +++ b/src/storage/file.rs @@ -329,3 +329,39 @@ impl<'a> Debug for File<'a> { } } + +#[cfg(test)] +mod test { + // we use the JSON parser here, so we can generate FileHeaderData + use storage::json::parser::JsonHeaderParser; + use super::match_header_spec; + use storage::parser::{FileHeaderParser, ParserError}; + use storage::file::FileHeaderData as FHD; + use storage::file::FileHeaderSpec as FHS; + + #[test] + fn test_spec_matching() { + let text = String::from("{\"a\": 1, \"b\": -2}"); + let spec = FHS::Map { + keys: vec![ + FHS::Key { + name: String::from("a"), + value_type: Box::new(FHS::UInteger) + }, + FHS::Key { + name: String::from("b"), + value_type: Box::new(FHS::Integer) + } + ] + }; + + let parser = JsonHeaderParser::new(Some(spec.clone())); + let datares = parser.read(Some(text.clone())); + assert!(datares.is_ok(), "Text could not be parsed: '{}'", text); + let data = datares.unwrap(); + + let matchres = match_header_spec(&spec, &data); + assert!(matchres.is_none(), "Matching returns error: {:?}", matchres); + } +} +