jspg progress
This commit is contained in:
63
src/util.rs
63
src/util.rs
@ -39,10 +39,10 @@ where
|
||||
|
||||
pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
|
||||
// Clear registry to ensure isolation
|
||||
{
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
registry.clear();
|
||||
}
|
||||
// {
|
||||
// let mut registry = REGISTRY.write().unwrap();
|
||||
// registry.clear();
|
||||
// }
|
||||
|
||||
let content =
|
||||
fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read file: {}", path));
|
||||
@ -56,8 +56,10 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
|
||||
let group = &suite[index];
|
||||
let mut failures = Vec::<String>::new();
|
||||
|
||||
let mut registry = crate::registry::Registry::new();
|
||||
|
||||
// Helper to register items with 'schemas'
|
||||
let register_schemas = |items_val: Option<&Value>| {
|
||||
let register_schemas = |registry: &mut crate::registry::Registry, items_val: Option<&Value>| {
|
||||
if let Some(val) = items_val {
|
||||
if let Value::Array(arr) = val {
|
||||
for item in arr {
|
||||
@ -69,8 +71,6 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
|
||||
{
|
||||
// Clone ID upfront to avoid borrow issues
|
||||
if let Some(id_clone) = schema.obj.id.clone() {
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
// Utilize the new compile method which handles strictness
|
||||
let compiled =
|
||||
crate::compiler::Compiler::compile(schema, Some(id_clone.clone()));
|
||||
registry.insert(id_clone, compiled);
|
||||
@ -118,7 +118,6 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
|
||||
});
|
||||
|
||||
if let Ok(schema) = serde_json::from_value::<crate::schema::Schema>(schema_json) {
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
|
||||
registry.insert(id, compiled);
|
||||
}
|
||||
@ -127,32 +126,40 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
|
||||
}
|
||||
|
||||
// 2. Register items directly
|
||||
register_schemas(group.enums.as_ref());
|
||||
register_schemas(group.types.as_ref());
|
||||
register_schemas(group.puncs.as_ref());
|
||||
register_schemas(&mut registry, group.enums.as_ref());
|
||||
register_schemas(&mut registry, group.types.as_ref());
|
||||
register_schemas(&mut registry, group.puncs.as_ref());
|
||||
|
||||
// 3. Register root 'schemas' if present (generic test support)
|
||||
// Some tests use a raw 'schema' or 'schemas' field at the group level
|
||||
if let Some(schema_val) = &group.schema {
|
||||
if let Ok(schema) = serde_json::from_value::<crate::schema::Schema>(schema_val.clone()) {
|
||||
let id = schema
|
||||
.obj
|
||||
.id
|
||||
.clone()
|
||||
.or_else(|| {
|
||||
// Fallback ID if none provided in schema
|
||||
Some("root".to_string())
|
||||
})
|
||||
.unwrap();
|
||||
match serde_json::from_value::<crate::schema::Schema>(schema_val.clone()) {
|
||||
Ok(schema) => {
|
||||
let id = schema
|
||||
.obj
|
||||
.id
|
||||
.clone()
|
||||
.or_else(|| {
|
||||
// Fallback ID if none provided in schema
|
||||
Some(format!("test:{}:{}", path, index))
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let mut registry = REGISTRY.write().unwrap();
|
||||
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
|
||||
registry.insert(id, compiled);
|
||||
let mut registry_ref = &mut registry;
|
||||
let compiled = crate::compiler::Compiler::compile(schema, Some(id.clone()));
|
||||
registry_ref.insert(id, compiled);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!(
|
||||
"DEBUG: FAILED to deserialize group schema for index {}: {}",
|
||||
index, e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Run Tests
|
||||
for (test_index, test) in group.tests.iter().enumerate() {
|
||||
for (_test_index, test) in group.tests.iter().enumerate() {
|
||||
let mut schema_id = test.schema_id.clone();
|
||||
|
||||
// If no explicit schema_id, try to infer from the single schema in the group
|
||||
@ -165,7 +172,7 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
|
||||
}
|
||||
}
|
||||
if schema_id.is_none() {
|
||||
schema_id = Some("root".to_string());
|
||||
schema_id = Some(format!("test:{}:{}", path, index));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -186,7 +193,7 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
|
||||
}
|
||||
|
||||
if let Some(sid) = schema_id {
|
||||
let result = Validator::validate(&sid, &test.data);
|
||||
let result = Validator::validate_with_registry(&sid, &test.data, ®istry);
|
||||
|
||||
if !result.errors.is_empty() != !test.valid {
|
||||
failures.push(format!(
|
||||
@ -194,7 +201,7 @@ pub fn run_test_file_at_index(path: &str, index: usize) -> Result<(), String> {
|
||||
group.description,
|
||||
test.description,
|
||||
test.valid,
|
||||
!result.errors.is_empty(),
|
||||
!result.errors.is_empty(), // "Got Invalid?"
|
||||
result.errors
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user