From 7a37d08f0b548e206935202bae44342155d56bb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Haudebourg?= Date: Tue, 2 Apr 2024 14:51:29 +0200 Subject: [PATCH] Test `json_syntax`-based layout parser. --- layouts/tests/distill.rs | 63 ++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/layouts/tests/distill.rs b/layouts/tests/distill.rs index 61cd176..c1cbdfc 100644 --- a/layouts/tests/distill.rs +++ b/layouts/tests/distill.rs @@ -1,16 +1,47 @@ -use nquads_syntax::Parse; +use json_syntax::{Parse as ParseJson, TryFromJson}; +use nquads_syntax::Parse as ParseNQuads; use paste::paste; use rdf_types::dataset::IndexedBTreeDataset; use rdf_types::{BlankIdBuf, Term}; use static_iref::iri; use std::fs; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; +use treeldr_layouts::layout::LayoutType; use treeldr_layouts::utils::strip_rdf_quad; +use treeldr_layouts::{Layouts, Ref}; fn file_path(id: &str, suffix: &str) -> PathBuf { format!("{}/tests/distill/{id}{suffix}", env!("CARGO_MANIFEST_DIR")).into() } +fn load_layout(layout_path: &Path) -> (Layouts, Ref) { + // Initialize the layout builder. + let mut builder = treeldr_layouts::abs::Builder::new(); + + // Parse the layout definition. + let raw_json = fs::read_to_string(layout_path).unwrap(); + let (layout_json, layout_code_map) = json_syntax::Value::parse_str(&raw_json).unwrap(); + match treeldr_layouts::abs::syntax::Layout::try_from_json(&layout_json, &layout_code_map) { + Ok(layout_abs) => { + // We also test parsing through `serde`. + if let Err(e) = serde_json::from_str::(&raw_json) + { + panic!("layout `serde` parse error: {e}") + } + + let layout_ref = layout_abs.build(&mut builder).unwrap(); + + // Compile the layouts. + let layouts = builder.build(); + + (layouts, layout_ref) + } + Err(e) => { + panic!("layout parse error: {e}") + } + } +} + fn hydrate(id: &str, inputs: [Term; N]) { // File paths. let input_path = file_path(id, "-in.nq"); @@ -26,25 +57,13 @@ fn hydrate(id: &str, inputs: [Term; N]) { .map(strip_rdf_quad) .collect(); - // Initialize the layout builder. - let mut builder = treeldr_layouts::abs::Builder::new(); - - // Parse the layout definition. - let layout_ref = serde_json::from_str::( - &fs::read_to_string(layout_path).unwrap(), - ) - .unwrap() - .build(&mut builder) - .unwrap(); + let (layouts, layout_ref) = load_layout(&layout_path); // Parse the expected output. let expected_json: serde_json::Value = fs::read_to_string(output_path).unwrap().parse().unwrap(); let expected: treeldr_layouts::Value = expected_json.into(); - // Compile the layouts. - let layouts = builder.build(); - // Hydrate. let output = treeldr_layouts::distill::hydrate(&layouts, &dataset, &layout_ref, &inputs) .unwrap() @@ -73,19 +92,7 @@ fn dehydrate(id: &str, expected_values: [Term; N]) { .map(strip_rdf_quad) .collect(); - // Initialize the layout builder. - let mut builder = treeldr_layouts::abs::Builder::new(); - - // Parse the layout definition. - let layout_ref = serde_json::from_str::( - &fs::read_to_string(layout_path).unwrap(), - ) - .unwrap() - .build(&mut builder) - .unwrap(); - - // Compile the layouts. - let layouts = builder.build(); + let (layouts, layout_ref) = load_layout(&layout_path); // Hydrate. let (output_dataset, output_values) = treeldr_layouts::distill::dehydrate(