use crate::{LinkNetwork, ParseConfiguration};
use super::{LinkRule, LinkRuleRegistry};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LinkRuleSnapshotExpectation {
Valid,
Invalid,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LinkRuleSnapshotCase {
name: String,
source: String,
language: String,
expectation: LinkRuleSnapshotExpectation,
}
impl LinkRuleSnapshotCase {
#[must_use]
pub fn new(
name: impl Into<String>,
source: impl Into<String>,
language: impl Into<String>,
expectation: LinkRuleSnapshotExpectation,
) -> Self {
Self {
name: name.into(),
source: source.into(),
language: language.into(),
expectation,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LinkRuleSnapshotSuite {
rule: LinkRule,
cases: Vec<LinkRuleSnapshotCase>,
}
impl LinkRuleSnapshotSuite {
#[must_use]
pub const fn new(rule: LinkRule) -> Self {
Self {
rule,
cases: Vec::new(),
}
}
#[must_use]
pub fn with_case(mut self, case: LinkRuleSnapshotCase) -> Self {
self.cases.push(case);
self
}
#[must_use]
pub fn run(
&self,
registry: &LinkRuleRegistry,
configuration: ParseConfiguration,
) -> LinkRuleSnapshotReport {
let cases = self
.cases
.iter()
.map(|case| {
let network = LinkNetwork::parse(&case.source, &case.language, configuration);
let matches = self.rule.matches(&network, registry);
let has_match = !matches.is_empty();
let passed = match case.expectation {
LinkRuleSnapshotExpectation::Valid => has_match,
LinkRuleSnapshotExpectation::Invalid => !has_match,
};
LinkRuleSnapshotResult {
name: case.name.clone(),
expectation: case.expectation,
matched: has_match,
match_count: matches.len(),
passed,
}
})
.collect();
LinkRuleSnapshotReport { cases }
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LinkRuleSnapshotReport {
cases: Vec<LinkRuleSnapshotResult>,
}
impl LinkRuleSnapshotReport {
#[must_use]
pub fn is_success(&self) -> bool {
self.cases.iter().all(|case| case.passed)
}
#[must_use]
pub fn cases(&self) -> &[LinkRuleSnapshotResult] {
&self.cases
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LinkRuleSnapshotResult {
name: String,
expectation: LinkRuleSnapshotExpectation,
matched: bool,
match_count: usize,
passed: bool,
}
impl LinkRuleSnapshotResult {
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub const fn expectation(&self) -> LinkRuleSnapshotExpectation {
self.expectation
}
#[must_use]
pub const fn matched(&self) -> bool {
self.matched
}
#[must_use]
pub const fn match_count(&self) -> usize {
self.match_count
}
#[must_use]
pub const fn passed(&self) -> bool {
self.passed
}
}