Simplifies test manifest parser

pull/334/head
Tpt 2 years ago committed by Thomas Tanon
parent 7fdd045516
commit de4c5abd9c
  1. 78
      testsuite/src/manifest.rs

@ -1,6 +1,6 @@
use crate::files::load_to_graph; use crate::files::load_to_graph;
use crate::vocab::*; use crate::vocab::*;
use anyhow::{anyhow, Result}; use anyhow::{bail, Result};
use oxigraph::model::vocab::*; use oxigraph::model::vocab::*;
use oxigraph::model::*; use oxigraph::model::*;
use std::collections::VecDeque; use std::collections::VecDeque;
@ -73,10 +73,10 @@ impl Iterator for TestManifest {
fn next(&mut self) -> Option<Result<Test>> { fn next(&mut self) -> Option<Result<Test>> {
loop { loop {
if let Some(next) = self.next_test() { if let Some(next) = self.next_test().transpose() {
return Some(next); return Some(next);
} }
if let Err(e) = self.load_next_manifest()? { if let Err(e) = self.load_next_manifest().transpose()? {
return Some(Err(e)); return Some(Err(e));
} }
} }
@ -84,40 +84,44 @@ impl Iterator for TestManifest {
} }
impl TestManifest { impl TestManifest {
fn next_test(&mut self) -> Option<Result<Test>> { fn next_test(&mut self) -> Result<Option<Test>> {
let test_node = self.tests_to_do.pop_front()?; let test_node = if let Some(test_node) = self.tests_to_do.pop_front() {
let test_node = match test_node { test_node
Term::NamedNode(n) => n, } else {
_ => { return Ok(None);
return Some(Err(anyhow!("Invalid test identifier. Got {test_node}"))); };
} let test_node = if let Term::NamedNode(test_node) = test_node {
test_node
} else {
bail!("Invalid test identifier. Got {test_node}");
}; };
let name = match self let name = if let Some(TermRef::Literal(c)) = self
.graph .graph
.object_for_subject_predicate(&test_node, mf::NAME) .object_for_subject_predicate(&test_node, mf::NAME)
{ {
Some(TermRef::Literal(c)) => Some(c.value().to_string()), Some(c.value().to_string())
_ => None, } else {
None
}; };
let kind = match self let kind = if let Some(TermRef::NamedNode(c)) = self
.graph .graph
.object_for_subject_predicate(&test_node, rdf::TYPE) .object_for_subject_predicate(&test_node, rdf::TYPE)
{ {
Some(TermRef::NamedNode(c)) => c.into_owned(), c.into_owned()
_ => { } else {
return Some(Err(anyhow!( bail!(
"The test {test_node} named {} has no rdf:type", "The test {test_node} named {} has no rdf:type",
name.as_deref().unwrap_or("") name.as_deref().unwrap_or("")
))); );
}
}; };
let comment = match self let comment = if let Some(TermRef::Literal(c)) = self
.graph .graph
.object_for_subject_predicate(&test_node, rdfs::COMMENT) .object_for_subject_predicate(&test_node, rdfs::COMMENT)
{ {
Some(TermRef::Literal(c)) => Some(c.value().to_string()), Some(c.value().to_string())
_ => None, } else {
None
}; };
let (action, query, update, data, graph_data, service_data) = match self let (action, query, update, data, graph_data, service_data) = match self
.graph .graph
@ -199,9 +203,9 @@ impl TestManifest {
.collect(); .collect();
(None, query, update, data, graph_data, service_data) (None, query, update, data, graph_data, service_data)
} }
Some(_) => return Some(Err(anyhow!("invalid action"))), Some(_) => bail!("invalid action"),
None => { None => {
return Some(Err(anyhow!("action not found for test {test_node}"))); bail!("action not found for test {test_node}");
} }
}; };
let (result, result_graph_data) = match self let (result, result_graph_data) = match self
@ -243,10 +247,10 @@ impl TestManifest {
}) })
.collect(), .collect(),
), ),
Some(_) => return Some(Err(anyhow!("invalid result"))), Some(_) => bail!("invalid result"),
None => (None, Vec::new()), None => (None, Vec::new()),
}; };
Some(Ok(Test { Ok(Some(Test {
id: test_node, id: test_node,
kind, kind,
name, name,
@ -262,21 +266,21 @@ impl TestManifest {
})) }))
} }
fn load_next_manifest(&mut self) -> Option<Result<()>> { fn load_next_manifest(&mut self) -> Result<Option<()>> {
let url = self.manifests_to_do.pop_front()?; let url = if let Some(url) = self.manifests_to_do.pop_front() {
url
} else {
return Ok(None);
};
self.graph.clear(); self.graph.clear();
if let Err(error) = load_to_graph(&url, &mut self.graph) { load_to_graph(&url, &mut self.graph)?;
return Some(Err(error));
}
let manifests = self let manifests = self
.graph .graph
.subjects_for_predicate_object(rdf::TYPE, mf::MANIFEST) .subjects_for_predicate_object(rdf::TYPE, mf::MANIFEST)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
if manifests.len() != 1 { if manifests.len() != 1 {
return Some(Err(anyhow!( bail!("The file {url} should contain a single manifest");
"The file {url} should contain a single manifest"
)));
} }
for manifest in manifests { for manifest in manifests {
match self match self
@ -291,7 +295,7 @@ impl TestManifest {
}), }),
); );
} }
Some(_) => return Some(Err(anyhow!("invalid tests list"))), Some(_) => bail!("invalid tests list"),
None => (), None => (),
} }
@ -305,12 +309,12 @@ impl TestManifest {
.extend(RdfListIterator::iter(&self.graph, list.into())); .extend(RdfListIterator::iter(&self.graph, list.into()));
} }
Some(term) => { Some(term) => {
return Some(Err(anyhow!("Invalid tests list. Got term {term}"))); bail!("Invalid tests list. Got term {term}");
} }
None => (), None => (),
} }
} }
Some(Ok(())) Ok(Some(()))
} }
} }

Loading…
Cancel
Save