#![allow(
dead_code,
reason = "shared by several test targets, each using a part"
)]
use std::path::{Path, PathBuf};
pub(crate) const CORPUS: &str = "corpus/cen-en16931-ubl-1.3.17";
pub(crate) const ARTEFACTS: &str = "artefacts/cen-en16931-ubl-1.3.17";
pub(crate) const XSLT: &str = "ubl/xslt/EN16931-UBL-validation.xslt";
pub(crate) fn workspace_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.ancestors()
.nth(2)
.map(Path::to_path_buf)
.unwrap_or_default()
}
pub(crate) fn join(base: &Path, relative: &str) -> PathBuf {
let mut path = base.to_path_buf();
path.extend(relative.split('/').filter(|p| !p.is_empty()));
path
}
#[derive(Debug, Clone)]
pub(crate) struct Case {
pub(crate) id: String,
pub(crate) input: String,
pub(crate) error: Vec<String>,
}
impl Case {
pub(crate) fn path(&self) -> PathBuf {
join(&workspace_root(), &self.input)
}
pub(crate) fn golden(&self) -> PathBuf {
join(
&workspace_root(),
&format!("{CORPUS}/golden/{}.svrl.txt", self.id),
)
}
}
pub(crate) fn cases() -> Vec<Case> {
let text =
std::fs::read_to_string(join(&workspace_root(), &format!("{CORPUS}/cases.tsv"))).unwrap();
text.lines()
.skip(1)
.map(|line| {
let columns: Vec<&str> = line.split('\t').collect();
let error = columns
.get(5)
.map(|e| {
e.split(' ')
.filter(|s| !s.is_empty())
.map(str::to_owned)
.collect()
})
.unwrap_or_default();
Case {
id: columns[0].to_owned(),
input: columns[1].to_owned(),
error,
}
})
.collect()
}
pub(crate) fn case(id: &str) -> Case {
cases().into_iter().find(|c| c.id == id).unwrap()
}
pub(crate) type Expected = (String, Option<String>, Option<String>, String);
pub(crate) fn expected_findings(canonical: &str) -> Vec<Expected> {
einvoice_svrl::parse(canonical)
.unwrap()
.into_iter()
.filter(|l| l.depth == 0 && (l.kind == "failed-assert" || l.kind == "successful-report"))
.map(|l| {
(
l.kind.clone(),
l.attribute("id").map(str::to_owned),
l.attribute("flag").map(str::to_owned),
l.attribute("location").unwrap_or_default().to_owned(),
)
})
.collect()
}
pub(crate) fn temp_dir(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!(
"einvoice-validator-test-{}-{name}",
std::process::id()
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
pub(crate) fn depth(bytes: &[u8]) -> u32 {
let text = std::str::from_utf8(bytes).unwrap();
let doc = roxmltree::Document::parse(text).unwrap();
doc.descendants()
.filter(roxmltree::Node::is_element)
.map(|n| u32::try_from(n.ancestors().filter(roxmltree::Node::is_element).count()).unwrap())
.max()
.unwrap_or(0)
}
pub(crate) const RAISED_MAX_DEPTH: u32 = 1024;
pub(crate) fn validate_case(
validator: &einvoice_validator::Validator,
bytes: Vec<u8>,
options: &einvoice_validator::ValidateOptions<'_>,
) -> Result<einvoice_validator::Report, einvoice_validator::Error> {
use einvoice_validator::{DEFAULT_MAX_DEPTH, Error, LimitExceeded};
let deeper = depth(&bytes) > DEFAULT_MAX_DEPTH;
if !deeper {
let result = validator.validate_owned_with(bytes, options);
assert!(
!matches!(result, Err(Error::Limit { .. })),
"{:?}",
result.err()
);
return result;
}
match validator.validate_with(&bytes, options) {
Err(Error::Limit {
limit:
LimitExceeded::Depth {
max: DEFAULT_MAX_DEPTH,
..
},
..
}) => {}
other => panic!("a document deeper than the default maximum depth: {other:?}"),
}
validator.validate_owned_with(bytes, &options.max_depth(RAISED_MAX_DEPTH))
}