Skip to content

Commit

Permalink
Test validate()
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Aug 1, 2024
1 parent 6288a67 commit ec120b8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions corpus/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "pair",
"abstract": "A key/value pair data type",
"maintainers": [
{
"name": "David E. Wheeler",
"email": "[email protected]"
}
],
"license": "PostgreSQL",
"contents": {
"extensions": {
"pair": {
"sql": "sql/pair.sql",
"control": "pair.control"
}
}
},
"meta-spec": { "version": "2.0.0" }
}
32 changes: 32 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,36 @@ mod tests {

Ok(())
}

#[test]
fn test_validate() -> Result<(), Box<dyn Error>> {
// 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::<String>() {
assert!(msg.contains(" missing properties 'version"));
} else {
panic!("Unexpected panic error");
}
}
}

Ok(())
}
}

0 comments on commit ec120b8

Please sign in to comment.