diff --git a/corpus/invalid.json b/corpus/invalid.json new file mode 100644 index 0000000..0cd610c --- /dev/null +++ b/corpus/invalid.json @@ -0,0 +1,20 @@ +{ + "name": "pair", + "abstract": "A key/value pair data type", + "maintainers": [ + { + "name": "David E. Wheeler", + "email": "david@justatheory.com" + } + ], + "license": "PostgreSQL", + "contents": { + "extensions": { + "pair": { + "sql": "sql/pair.sql", + "control": "pair.control" + } + } + }, + "meta-spec": { "version": "2.0.0" } +} diff --git a/src/main.rs b/src/main.rs index ca1b4b5..4ac9273 100644 --- a/src/main.rs +++ b/src/main.rs @@ -253,4 +253,36 @@ mod tests { Ok(()) } + + #[test] + fn test_validate() -> Result<(), Box> { + // Success first. + let meta = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("corpus") + .join("v2") + .join("minimal.json"); + + match validate(meta.as_os_str().to_str().unwrap()) { + Ok(_) => (), + Err(e) => panic!("Validation failed: {e}"), + } + + // Invalid next. + let meta = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("corpus") + .join("invalid.json"); + + match std::panic::catch_unwind(|| validate(meta.as_os_str().to_str().unwrap())) { + Ok(_) => panic!("Should have failed on invalid.json but did not"), + Err(e) => { + if let Ok(msg) = e.downcast::() { + assert!(msg.contains(" missing properties 'version")); + } else { + panic!("Unexpected panic error"); + } + } + } + + Ok(()) + } }