Skip to content

Commit ccbf61a

Browse files
committed
Test corpuses in unit tests
1 parent 22a6b1c commit ccbf61a

File tree

4 files changed

+38
-94
lines changed

4 files changed

+38
-94
lines changed

src/valid/mod.rs

+38-43
Original file line numberDiff line numberDiff line change
@@ -124,46 +124,41 @@ fn is_license(v: &Value) -> Result<(), Box<dyn Error>> {
124124
Ok(())
125125
}
126126

127-
// #[cfg(test)]
128-
// mod tests {
129-
// use super::*;
130-
// use serde::{Deserialize, Serialize};
131-
// use serde_json::Value;
132-
// use std::error::Error;
133-
// use std::io::{prelude::*, BufReader};
134-
// use std::path::PathBuf;
135-
136-
// #[derive(Deserialize, Serialize)]
137-
// struct CorpusCase {
138-
// test: String,
139-
// error: Option<String>,
140-
// meta: Value,
141-
// }
142-
143-
// #[test]
144-
// fn test_validator() -> Result<(), Box<dyn Error>> {
145-
// let schemas_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "schema"].iter().collect();
146-
147-
// let mut validator = Validator::new(schemas_dir)?;
148-
// let valid_file: PathBuf = [
149-
// env!("CARGO_MANIFEST_DIR"),
150-
// "tests",
151-
// "corpus",
152-
// "v1",
153-
// "valid.txt",
154-
// ]
155-
// .iter()
156-
// .collect();
157-
// let file = File::open(&valid_file)?;
158-
// let reader = BufReader::new(file);
159-
// for line in reader.lines() {
160-
// let tc: CorpusCase = serde_json::from_str(&line?)?;
161-
// if let Err(e) = validator.validate(&tc.meta) {
162-
// panic!("{} failed: {e}", &tc.test);
163-
// }
164-
// println!("Example {} ok", &tc.test);
165-
// }
166-
167-
// Ok(())
168-
// }
169-
// }
127+
#[cfg(test)]
128+
mod tests {
129+
use super::*;
130+
use serde::{Deserialize, Serialize};
131+
use serde_json::Value;
132+
use std::error::Error;
133+
use std::path::PathBuf;
134+
135+
#[derive(Deserialize, Serialize)]
136+
struct CorpusCase {
137+
test: String,
138+
error: Option<String>,
139+
meta: Value,
140+
}
141+
142+
#[test]
143+
fn test_corpus() -> Result<(), Box<dyn Error>> {
144+
let schemas_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "schema"].iter().collect();
145+
let mut validator = Validator::new(schemas_dir)?;
146+
147+
for v_dir in ["v1", "v2"] {
148+
let dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "tests", "corpus", v_dir]
149+
.iter()
150+
.collect();
151+
let glob = Glob::new("*.json")?;
152+
153+
for path in glob.walk(dir) {
154+
let path = path?.into_path();
155+
let meta: Value = serde_json::from_reader(File::open(&path)?)?;
156+
if let Err(e) = validator.validate(&meta) {
157+
panic!("{v_dir}/{:?} failed: {e}", path.file_name().unwrap());
158+
}
159+
println!("Example {v_dir}/{:?} ok", path.file_name().unwrap());
160+
}
161+
}
162+
Ok(())
163+
}
164+
}

tests/common/mod.rs

-25
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
use std::fs::{self, File};
2-
use std::path::PathBuf;
32
use std::{collections::HashMap, error::Error};
43

54
use boon::{Compiler, Schemas};
6-
use pgxn_meta::*;
75
use serde_json::{json, Value};
8-
use wax::Glob;
96

107
const SCHEMA_BASE: &str = "https://pgxn.org/meta/v";
118

@@ -240,25 +237,3 @@ pub fn test_schema_version(version: u8) -> Result<(), Box<dyn Error>> {
240237

241238
Ok(())
242239
}
243-
244-
pub fn test_corpus(version: u8) -> Result<(), Box<dyn Error>> {
245-
let schemas_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "schema"].iter().collect();
246-
let mut validator = Validator::new(schemas_dir)?;
247-
let v_dir = format!("v{version}");
248-
249-
let dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "tests", "corpus", &v_dir]
250-
.iter()
251-
.collect();
252-
let glob = Glob::new("*.json")?;
253-
254-
for path in glob.walk(dir) {
255-
let path = path?.into_path();
256-
let meta: Value = serde_json::from_reader(File::open(&path)?)?;
257-
if let Err(e) = validator.validate(&meta) {
258-
panic!("{v_dir}/{:?} failed: {e}", path.file_name().unwrap());
259-
}
260-
println!("Example {v_dir}/{:?} ok", path.file_name().unwrap());
261-
}
262-
263-
Ok(())
264-
}

tests/v1_schema_test.rs

-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::error::Error;
22

33
use boon::Schemas;
4-
use serde::{Deserialize, Serialize};
54
use serde_json::{json, Map, Value};
65

76
// importing common module.
@@ -17,18 +16,6 @@ fn test_schema_v1() -> Result<(), Box<dyn Error>> {
1716
test_schema_version(SCHEMA_VERSION)
1817
}
1918

20-
#[derive(Deserialize, Serialize)]
21-
struct CorpusCase {
22-
test: String,
23-
error: Option<String>,
24-
meta: Value,
25-
}
26-
27-
#[test]
28-
fn test_corpus_v1_valid() -> Result<(), Box<dyn Error>> {
29-
test_corpus(1)
30-
}
31-
3219
#[test]
3320
fn test_v1_term() -> Result<(), Box<dyn Error>> {
3421
// Load the schemas and compile the term schema.

tests/v2_schema_test.rs

-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::error::Error;
22

33
use boon::Schemas;
4-
use serde::{Deserialize, Serialize};
54
use serde_json::{json, Map, Value};
65

76
// importing common module.
@@ -3334,15 +3333,3 @@ fn test_v2_distribution() -> Result<(), Box<dyn Error>> {
33343333

33353334
Ok(())
33363335
}
3337-
3338-
#[derive(Deserialize, Serialize)]
3339-
struct CorpusCase {
3340-
test: String,
3341-
error: Option<String>,
3342-
meta: Value,
3343-
}
3344-
3345-
#[test]
3346-
fn test_corpus_v2_valid() -> Result<(), Box<dyn Error>> {
3347-
test_corpus(2)
3348-
}

0 commit comments

Comments
 (0)