From ec120b86411b8cada594d30ae8aeb8a9ba0055b7 Mon Sep 17 00:00:00 2001 From: "David E. Wheeler" Date: Thu, 1 Aug 2024 19:07:54 -0400 Subject: [PATCH] Test validate() --- corpus/invalid.json | 20 ++++++++++++++++++++ src/main.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 corpus/invalid.json 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(()) + } }