mod support;
use std::collections::BTreeSet;
use datalog_structural_model::{
EmptyHeadCollection, ErrorCode, HeadConjunction, HeadForm, ProfileCheckError,
ProgramValidationError, RuleIndex, StratificationError, TermShapeError, TerminationViolation,
};
use support::errors::{cycle, every_program_error, termination_violation};
fn pinned_program_code(error: &ProgramValidationError) -> &'static str {
match error {
ProgramValidationError::UnsafeRule { .. } => "unsafe-rule",
ProgramValidationError::UnusedExistentialVariable { .. } => "unused-existential-variable",
ProgramValidationError::ExistentialVariableBoundInBody { .. } => {
"existential-variable-bound-in-body"
}
ProgramValidationError::UnsafeNegation { .. } => "unsafe-negation",
ProgramValidationError::UnsafeFilter { .. } => "unsafe-filter",
ProgramValidationError::UnsafeBind { .. } => "unsafe-bind",
ProgramValidationError::UnsafeAggregate { .. } => "unsafe-aggregate",
ProgramValidationError::NestedAggregateInInnerBody { .. } => "nested-aggregate",
ProgramValidationError::UnstratifiableCycle(_) => "unstratifiable-cycle",
ProgramValidationError::NegationUnderEqualityMode { .. } => "negation-under-equality-mode",
ProgramValidationError::AggregationUnderEqualityMode { .. } => {
"aggregation-under-equality-mode"
}
ProgramValidationError::SkolemInBodyAtom { .. } => "skolem-in-body",
ProgramValidationError::TripleTermNestingExceeded { .. } => "triple-term-nesting-exceeded",
ProgramValidationError::InvalidSkolemArguments { .. } => "invalid-skolem-arguments",
ProgramValidationError::RuleCountExceeded { .. } => "rule-count-exceeded",
ProgramValidationError::BodyLengthExceeded { .. } => "body-length-exceeded",
ProgramValidationError::SkolemDepthExceeded { .. } => "skolem-depth-exceeded",
}
}
fn assert_kebab(code: ErrorCode) {
let text = code.as_str();
assert!(!text.is_empty() && !text.starts_with('-') && !text.ends_with('-'));
assert!(
text.chars().all(|c| c.is_ascii_lowercase() || c == '-'),
"{text} is not kebab-case"
);
assert!(!text.contains("--"), "{text}");
}
#[test]
fn program_validation_codes_are_pinned_and_unique() {
let errors = every_program_error();
let mut seen = BTreeSet::new();
for error in &errors {
assert_eq!(
error.code().as_str(),
pinned_program_code(error),
"{error:?}"
);
assert_kebab(error.code());
assert!(seen.insert(error.code()), "duplicate code {}", error.code());
assert!(
error
.to_string()
.starts_with(&format!("[{}] ", error.code()))
);
}
assert_eq!(seen.len(), 17);
}
#[test]
fn associated_constants_match_codes() {
assert_eq!(
ProgramValidationError::CODE_UNSAFE_RULE.as_str(),
"unsafe-rule"
);
assert_eq!(
StratificationError::CODE_UNSTRATIFIABLE_CYCLE,
ProgramValidationError::CODE_UNSTRATIFIABLE_CYCLE
);
assert_eq!(
TermShapeError::CODE_TRIPLE_TERM_NESTING_EXCEEDED,
ProgramValidationError::CODE_TRIPLE_TERM_NESTING_EXCEEDED
);
}
#[test]
fn other_error_codes_are_pinned() {
let stratification = StratificationError::UnstratifiableCycle(cycle());
assert_eq!(stratification.code().as_str(), "unstratifiable-cycle");
for (error, code) in [
(
TermShapeError::TripleTermNestingExceeded { depth: 5, limit: 4 },
"triple-term-nesting-exceeded",
),
(
TermShapeError::InvalidSkolemArguments,
"invalid-skolem-arguments",
),
] {
assert_eq!(error.code().as_str(), code);
assert_kebab(error.code());
}
let invalid = ProfileCheckError::InvalidProgram(every_program_error().remove(0));
assert_eq!(invalid.code().as_str(), "invalid-program");
assert_eq!(
ProfileCheckError::CODE_UNSUPPORTED_CAPABILITIES.as_str(),
"unsupported-capabilities"
);
let head_form = ProfileCheckError::UnsupportedHeadForm {
rule: RuleIndex::new(0),
form: HeadForm::Disjunctive,
};
assert_eq!(head_form.code().as_str(), "unsupported-head-form");
assert_ne!(
head_form.code(),
ProfileCheckError::CODE_UNSUPPORTED_CAPABILITIES
);
assert_kebab(head_form.code());
let empty = HeadConjunction::try_new(vec![]).unwrap_err();
assert_eq!(empty.code().as_str(), "empty-head-collection");
assert_eq!(
empty.code(),
EmptyHeadCollection::CODE_EMPTY_HEAD_COLLECTION
);
assert!(empty.to_string().starts_with("[empty-head-collection] "));
assert_kebab(empty.code());
let uncertified = ProfileCheckError::TerminationUncertified(termination_violation());
assert_eq!(uncertified.code().as_str(), "termination-uncertified");
assert_kebab(uncertified.code());
}
#[test]
fn termination_violation_code_is_pinned() {
let violation = termination_violation();
assert_eq!(violation.code().as_str(), "not-weakly-acyclic");
assert_eq!(
violation.code(),
TerminationViolation::CODE_NOT_WEAKLY_ACYCLIC
);
assert_kebab(violation.code());
assert!(violation.to_string().starts_with("[not-weakly-acyclic] "));
}