Skip to content

Commit d43c4c7

Browse files
committed
Add common module for tests
1 parent b4d2f5d commit d43c4c7

File tree

3 files changed

+37
-48
lines changed

3 files changed

+37
-48
lines changed

tests/common/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::error::Error;
2+
use std::fs::{self, File};
3+
4+
use boon::Compiler;
5+
use serde_json::Value;
6+
7+
pub fn new_compiler(dir: &str) -> Result<Compiler, Box<dyn Error>> {
8+
let mut compiler = Compiler::new();
9+
compiler.enable_format_assertions();
10+
let paths = fs::read_dir(dir)?;
11+
for path in paths {
12+
let path = path?.path();
13+
let bn = path.file_name().unwrap().to_str().unwrap();
14+
if bn.ends_with(".schema.json") {
15+
let schema: Value = serde_json::from_reader(File::open(path.clone())?)?;
16+
if let Value::String(s) = &schema["$id"] {
17+
// Add the schema to the compiler.
18+
compiler.add_resource(s, schema.to_owned())?;
19+
} else {
20+
panic!("Unable to find ID in {}", path.display());
21+
}
22+
} else {
23+
println!("Skipping {}", path.display());
24+
}
25+
}
26+
27+
Ok(compiler)
28+
}

tests/v1_schema_test.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ use boon::{Compiler, Schemas};
66
use serde::{Deserialize, Serialize};
77
use serde_json::{json, Map, Value};
88

9+
// importing common module.
10+
mod common;
11+
use common::*;
12+
913
const SCHEMA_BASE: &str = "https://pgxn.org/meta/v1";
1014
const SCHEMA_ID: &str = "https://pgxn.org/meta/v1/distribution.schema.json";
1115

@@ -63,29 +67,6 @@ fn test_schema_v1() -> Result<(), Box<dyn Error>> {
6367
Ok(())
6468
}
6569

66-
fn new_compiler(dir: &str) -> Result<Compiler, Box<dyn Error>> {
67-
let mut compiler = Compiler::new();
68-
compiler.enable_format_assertions();
69-
let paths = fs::read_dir(dir)?;
70-
for path in paths {
71-
let path = path?.path();
72-
let bn = path.file_name().unwrap().to_str().unwrap();
73-
if bn.ends_with(".schema.json") {
74-
let schema: Value = serde_json::from_reader(File::open(path.clone())?)?;
75-
if let Value::String(s) = &schema["$id"] {
76-
// Add the schema to the compiler.
77-
compiler.add_resource(s, schema.to_owned())?;
78-
} else {
79-
panic!("Unable to find ID in {}", path.display());
80-
}
81-
} else {
82-
println!("Skipping {}", path.display());
83-
}
84-
}
85-
86-
Ok(compiler)
87-
}
88-
8970
#[derive(Deserialize, Serialize)]
9071
struct CorpusCase {
9172
test: String,

tests/v2_schema_test.rs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
11
use std::error::Error;
2-
use std::fs::{self, File};
32

4-
use boon::{Compiler, Schemas};
3+
use boon::Schemas;
54
use serde_json::{json, Value};
65

6+
// importing common module.
7+
mod common;
8+
use common::*;
9+
710
const SCHEMA_BASE: &str = "https://pgxn.org/meta/v2";
811
// const SCHEMA_ID: &str = "https://pgxn.org/meta/v2/distribution.schema.json";
912

10-
fn new_compiler(dir: &str) -> Result<Compiler, Box<dyn Error>> {
11-
let mut compiler = Compiler::new();
12-
compiler.enable_format_assertions();
13-
let paths = fs::read_dir(dir)?;
14-
for path in paths {
15-
let path = path?.path();
16-
let bn = path.file_name().unwrap().to_str().unwrap();
17-
if bn.ends_with(".schema.json") {
18-
let schema: Value = serde_json::from_reader(File::open(path.clone())?)?;
19-
if let Value::String(s) = &schema["$id"] {
20-
// Add the schema to the compiler.
21-
compiler.add_resource(s, schema.to_owned())?;
22-
} else {
23-
panic!("Unable to find ID in {}", path.display());
24-
}
25-
} else {
26-
println!("Skipping {}", path.display());
27-
}
28-
}
29-
30-
Ok(compiler)
31-
}
32-
3313
// https://regex101.com/r/Ly7O1x/3/
3414
const VALID_VERSIONS: &[&str] = &[
3515
"0.0.4",

0 commit comments

Comments
 (0)