Skip to content

Commit

Permalink
Finish testing schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Jul 2, 2024
1 parent ec543f1 commit 46a0aad
Showing 1 changed file with 149 additions and 0 deletions.
149 changes: 149 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,3 +1342,152 @@ fn test_v1_prereqs() -> Result<(), Box<dyn Error>> {

Ok(())
}

#[test]
fn test_v1_repository() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the repository schema.
let mut compiler = new_compiler("schema/v1")?;
let mut schemas = Schemas::new();
let id = format!("{SCHEMA_BASE}/repository.schema.json");
let idx = compiler.compile(&id, &mut schemas)?;

for valid_repository in [
("web", json!({"web": "https://example.com"})),
(
"type and url",
json!({"type": "git", "url": "https://example.com"}),
),
(
"x_ property",
json!({"web": "https://example.com", "x_y": 0}),
),
(
"X_ property",
json!({"web": "https://example.com", "X_y": 0}),
),
] {
if let Err(e) = schemas.validate(&valid_repository.1, idx) {
panic!("extension {} failed: {e}", valid_repository.0);
}
}

for invalid_repository in [
("empty array", json!([])),
("empty string", json!("")),
("empty string in array", json!(["hi", ""])),
("true", json!(true)),
("false", json!(false)),
("null", json!(null)),
("object", json!({})),
(
"bare x_ property",
json!({"web": "https://example.com", "x_": 0}),
),
(
"unknown property",
json!({"web": "https://example.com", "foo": 0}),
),
("url without type", json!({"url": "x:y"})),
("type without url", json!({"type": "cvs"})),
// web
("bad web URL", json!({"web": ":hello"})),
("web array", json!({"web": ["x:y"]})),
("web object", json!({"web": {}})),
("web bool", json!({"web": true})),
("web number", json!({"web": 42})),
("web null", json!({"web": null})),
// url
("bad url", json!({"type": "git", "url": ":hello"})),
("url array", json!({"type": "git", "url": ["x:y"]})),
("url object", json!({"type": "git", "url": {}})),
("url bool", json!({"type": "git", "url": true})),
("url number", json!({"type": "git", "url": 42})),
("url null", json!({"type": "git", "url": null})),
] {
if schemas.validate(&invalid_repository.1, idx).is_ok() {
panic!("{} unexpectedly passed!", invalid_repository.0)
}
}

Ok(())
}

#[test]
fn test_v1_resources() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the resources schema.
let mut compiler = new_compiler("schema/v1")?;
let mut schemas = Schemas::new();
let id = format!("{SCHEMA_BASE}/resources.schema.json");
let idx = compiler.compile(&id, &mut schemas)?;

for valid_resources in [
("homepage", json!({"homepage": "https://example.com"})),
(
"bugtracker web",
json!({"bugtracker": {"web": "https://foo.com"}}),
),
(
"bugtracker mailto",
json!({"bugtracker": {"mailto": "[email protected]"}}),
),
(
"repository web",
json!({"repository": {"web": "https://example.com"}}),
),
(
"repository url and type",
json!({"repository": {"type": "git", "url": "https://example.com"}}),
),
("x_ property", json!({"homepage": "x:y", "x_y": 0})),
("X_ property", json!({"homepage": "x:y", "X_y": 0})),
] {
if let Err(e) = schemas.validate(&valid_resources.1, idx) {
panic!("extension {} failed: {e}", valid_resources.0);
}
}

for invalid_resources in [
("empty array", json!([])),
("empty string", json!("")),
("empty string in array", json!(["hi", ""])),
("true", json!(true)),
("false", json!(false)),
("null", json!(null)),
("empty object", json!({})),
("bare x_ property", json!({"homepage": "x:y", "x_": 0})),
("unknown property", json!({"homepage": "x:y", "foo": 0})),
// homepage
("bad homepage url", json!({"homepage": ":hi"})),
("homepage array", json!({"homepage": ["x:y"]})),
("homepage object", json!({"homepage": {}})),
("homepage bool", json!({"homepage": true})),
("homepage number", json!({"homepage": 42})),
("homepage null", json!({"homepage": null})),
// bugtracker
(
"bad bugtracker url",
json!({"bugtracker": {"web": "3ttp://a.com"}}),
),
("bugtracker array", json!({"bugtracker": ["x:y"]})),
("bugtracker empty object", json!({"bugtracker": {}})),
("bugtracker bool", json!({"bugtracker": true})),
("bugtracker number", json!({"bugtracker": 42})),
("bugtracker null", json!({"bugtracker": null})),
// repository
(
"bad repository url",
json!({"repository": {"web": "3ttp://a.com"}}),
),
("repository array", json!({"repository": ["x:y"]})),
("repository empty object", json!({"repository": {}})),
("repository bool", json!({"repository": true})),
("repository number", json!({"repository": 42})),
("repository null", json!({"repository": null})),
] {
if schemas.validate(&invalid_resources.1, idx).is_ok() {
panic!("{} unexpectedly passed!", invalid_resources.0)
}
}

Ok(())
}

0 comments on commit 46a0aad

Please sign in to comment.