use edifact::{
ProfileRulePack, ValidationContext, ValidationIssue, ValidationSeverity,
group::{GroupDef, group_segments_indexed},
};
static SG6: &[GroupDef] = &[GroupDef::new("SG6", "QTY")];
static SCHEMA: &[GroupDef] = &[
GroupDef::new("SG1", "RFF"),
GroupDef::with_children("SG5", "LOC", SG6),
];
fn owned_segs(input: &[u8]) -> Vec<edifact::OwnedSegment> {
edifact::from_reader(std::io::Cursor::new(input))
.collect::<Result<_, _>>()
.expect("fixture should parse")
}
fn parse_segs(input: &str) -> Vec<edifact::Segment<'static>> {
let s: &'static str = Box::leak(input.to_owned().into_boxed_str());
edifact::from_bytes(s.as_bytes())
.collect::<Result<_, _>>()
.expect("fixture should parse")
}
#[test]
fn group_rule_fires_when_required_segment_absent_from_scoped_group() {
let input = "UNH+1+ORDERS:D:04B:UN'RFF+Z13:REF1'DTM+137:20230101:102'LOC+172+LOC1'UNT+5+1'";
let segs = parse_segs(input);
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("TEST").require_segment_in_group("SG5", "DTM", "SG5-DTM-M");
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate_grouped(&tree, &segs);
assert!(
report.has_errors(),
"expected error: DTM missing from SG5 — got: {report}"
);
let issue = report.errors().first().expect("at least one error");
assert_eq!(issue.segment_group.as_deref(), Some("SG5"));
assert_eq!(issue.rule_id.as_deref(), Some("SG5-DTM-M"));
}
#[test]
fn group_rule_does_not_fire_when_segment_present_in_scoped_group() {
let input = "UNH+1+ORDERS:D:04B:UN'LOC+172+LOC1'DTM+137:20230101:102'UNT+3+1'";
let segs = parse_segs(input);
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("TEST").require_segment_in_group("SG5", "DTM", "SG5-DTM-M");
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate_grouped(&tree, &segs);
assert!(
report.is_valid(),
"expected no errors (DTM is present in SG5): {report}"
);
}
#[test]
fn forbid_segment_in_group_fires_when_segment_present() {
let input = "UNH+1+ORDERS:D:04B:UN'LOC+172+LOC1'UNS+D'UNT+3+1'";
let segs = parse_segs(input);
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("TEST").forbid_segment_in_group("SG5", "UNS", "SG5-UNS-F");
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate_grouped(&tree, &segs);
assert!(report.has_errors(), "expected error: UNS in SG5 — {report}");
let issue = report.errors().first().unwrap();
assert_eq!(issue.segment_group.as_deref(), Some("SG5"));
}
#[test]
fn forbid_segment_in_group_does_not_fire_when_segment_absent() {
let input = "UNH+1+ORDERS:D:04B:UN'LOC+172+LOC1'DTM+137:20230101:102'UNT+3+1'";
let segs = parse_segs(input);
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("TEST").forbid_segment_in_group("SG5", "UNS", "SG5-UNS-F");
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
assert!(ctx.validate_grouped(&tree, &segs).is_valid());
}
#[test]
fn group_rules_for_different_groups_do_not_cross_contaminate() {
let input =
"UNH+1+ORDERS:D:04B:UN'RFF+Z13:R1'DTM+137:20230101:102'LOC+172+L1'QTY+220:100'UNT+5+1'";
let segs = parse_segs(input);
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("TEST")
.require_segment_in_group("SG1", "DTM", "SG1-DTM-M")
.require_segment_in_group("SG5", "DTM", "SG5-DTM-M");
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate_grouped(&tree, &segs);
assert_eq!(report.errors().len(), 1, "expected 1 error; got {report}");
assert_eq!(
report.errors()[0].rule_id.as_deref(),
Some("SG5-DTM-M"),
"the error must be for SG5, not SG1"
);
}
#[test]
fn group_rule_issues_are_auto_stamped_with_group_name() {
let input = "UNH+1+ORDERS:D:04B:UN'LOC+172+L1'UNT+2+1'";
let segs = parse_segs(input);
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("TEST").require_segment_in_group("SG5", "QTY", "SG5-QTY-M");
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate_grouped(&tree, &segs);
assert!(report.has_errors());
for issue in report.errors() {
assert_eq!(
issue.segment_group.as_deref(),
Some("SG5"),
"issue must be auto-stamped with SG5"
);
}
}
#[test]
fn validate_lenient_grouped_owned_works_with_owned_segments() {
let input = b"UNH+1+ORDERS:D:04B:UN'LOC+172+L1'DTM+137:20230101:102'UNT+3+1'";
let owned = owned_segs(input);
let tree = group_segments_indexed(&owned, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("TEST").require_segment_in_group("SG5", "DTM", "SG5-DTM-M");
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate_grouped(&tree, &owned);
assert!(
report.is_valid(),
"DTM present in SG5 — no errors expected: {report}"
);
}
#[test]
fn group_rule_fires_per_occurrence_when_group_repeats() {
let input = "UNH+1+ORDERS:D:04B:UN'LOC+172+L1'DTM+137:20230101:102'LOC+172+L2'UNT+4+1'";
let segs = parse_segs(input);
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("TEST").require_segment_in_group("SG5", "DTM", "SG5-DTM-M");
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate_grouped(&tree, &segs);
assert_eq!(
report.errors().len(),
1,
"exactly one SG5 missing DTM: {report}"
);
}
#[test]
fn custom_scoped_group_rule_fn_fires_and_sets_segment_group() {
let input = "UNH+1+ORDERS:D:04B:UN'LOC+172+L1'QTY+220:0'UNT+3+1'";
let segs = parse_segs(input);
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("TEST").with_scoped_group_rule_fn(
"SG6",
"SG6-QTY-NONZERO",
|_group, group_segs, _ctx, issues| {
for s in group_segs.iter().filter(|s| s.tag == "QTY") {
let qty_val = s
.get_element(0)
.and_then(|e| e.get_component(1))
.unwrap_or("0");
if qty_val == "0" {
issues.push(
ValidationIssue::new(
ValidationSeverity::Warning,
"QTY value is zero in SG6",
)
.with_segment("QTY")
.with_rule_id("SG6-QTY-NONZERO"),
);
}
}
},
);
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate_grouped(&tree, &segs);
assert!(!report.warnings().is_empty(), "expected zero-qty warning");
assert_eq!(
report.warnings()[0].segment_group.as_deref(),
Some("SG6"),
"warning must be auto-stamped with SG6"
);
}
#[test]
fn group_rules_respect_message_type_scoping() {
let input = "UNH+1+INVOIC:D:96A:UN'LOC+172+L1'UNT+2+1'";
let segs = parse_segs(input);
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("ORDERS-ONLY")
.for_message_type("ORDERS")
.require_segment_in_group("SG5", "DTM", "SG5-DTM-M");
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate_grouped(&tree, &segs);
assert!(
report.is_valid(),
"INVOIC message: ORDERS-scoped group rules must not fire: {report}"
);
}
#[test]
fn flat_and_group_validation_both_run_in_grouped_mode() {
let input = "UNH+1+ORDERS:D:04B:UN'LOC+172+L1'UNT+2+1'";
let segs = parse_segs(input);
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("TEST")
.require_segment("BGM", "BGM-M")
.require_segment_in_group("SG5", "DTM", "SG5-DTM-M");
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate_grouped(&tree, &segs);
let rule_ids: Vec<Option<&str>> = report
.errors()
.iter()
.map(|i| i.rule_id.as_deref())
.collect();
assert!(
rule_ids.contains(&Some("BGM-M")),
"flat rule BGM-M must fire: {report}"
);
assert!(
rule_ids.contains(&Some("SG5-DTM-M")),
"group rule SG5-DTM-M must fire: {report}"
);
}
#[test]
fn forbid_segment_segment_occurrence_is_relative_not_absolute() {
let segs = parse_segs("UNH+1+ORDERS:D:96A:UN'QTY+21:10'QTY+21:20'QTY+21:30'UNT+4+1'");
let pack = ProfileRulePack::new("TEST").forbid_segment("QTY", "TEST-FORBID-QTY");
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate(&segs);
let mut occurrences: Vec<u32> = report
.errors()
.iter()
.filter_map(|i| i.segment_occurrence)
.collect();
occurrences.sort_unstable();
assert_eq!(
occurrences,
vec![0, 1, 2],
"segment_occurrence must be 0-based relative to matching segments, not absolute positions"
);
}
#[test]
fn forbid_segment_in_group_occurrence_is_relative_not_absolute() {
let segs = parse_segs("UNH+1+ORDERS:D:04B:UN'LOC+172+L1'QTY+21:10'QTY+21:20'UNT+4+1'");
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("TEST").forbid_segment_in_group("SG5", "QTY", "TEST-SG5-QTY");
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate_grouped(&tree, &segs);
let mut occurrences: Vec<u32> = report
.errors()
.iter()
.filter_map(|i| i.segment_occurrence)
.collect();
occurrences.sort_unstable();
assert_eq!(
occurrences,
vec![0, 1],
"segment_occurrence in group must count only matching segments, not absolute group slice position"
);
}
#[test]
fn bail_on_first_error_does_not_skip_sibling_groups_due_to_earlier_flat_errors() {
let segs = parse_segs(
"UNH+1+ORDERS:D:04B:UN'\
LOC+172+L1'LOC+172+L2'UNT+3+1'",
);
let tree = group_segments_indexed(&segs, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("TEST")
.require_segment("BGM", "BGM-M")
.require_segment_in_group("SG5", "DTM", "SG5-DTM-M")
.with_bail_on_first_error(true);
let ctx = ValidationContext::builder().with_profile_pack(pack).build();
let report = ctx.validate_grouped(&tree, &segs);
assert!(report.has_errors(), "expected errors in report: {report}");
let rule_ids: Vec<&str> = report
.errors()
.iter()
.filter_map(|i| i.rule_id.as_deref())
.collect();
assert!(
rule_ids.contains(&"BGM-M"),
"flat error BGM-M must be present: {report}"
);
assert!(
rule_ids.contains(&"SG5-DTM-M"),
"group error SG5-DTM-M must not be skipped by pre-existing flat errors: {report}"
);
}
#[test]
fn a_schema_built_at_runtime_groups_identically_to_a_static_one() {
let names: Vec<String> = vec![
"SG1".to_owned(),
"RFF".to_owned(),
"SG5".to_owned(),
"LOC".to_owned(),
"SG6".to_owned(),
"QTY".to_owned(),
];
let owned =
|name: &str, trigger: &str| GroupDef::builder(name, edifact::SegmentPlace::owned(trigger));
let runtime_schema = vec![
owned(&names[0], &names[1]).build().expect("valid group"),
owned(&names[2], &names[3])
.group(owned(&names[4], &names[5]).build().expect("valid group"))
.build()
.expect("valid group"),
];
drop(names);
let input =
b"UNH+1+ORDERS:D:96A:UN'RFF+ON:1'LOC+172+DE1'DTM+163:20260101:102'QTY+220:5'UNT+6+1'";
let segs: Vec<_> = edifact::from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.expect("parse");
let from_static = group_segments_indexed(&segs, SCHEMA, "ROOT");
let from_runtime = group_segments_indexed(&segs, &runtime_schema, "ROOT");
fn shape(g: &edifact::SegmentGroupIndexed<'_>) -> Vec<(String, std::ops::Range<usize>)> {
let mut out = vec![(g.definition.to_owned(), g.total_span.clone())];
for child in &g.children {
out.extend(shape(child));
}
out
}
assert_eq!(shape(&from_static), shape(&from_runtime));
assert!(shape(&from_runtime).iter().any(|(n, _)| n == "SG6"));
}
#[test]
fn max_issues_per_rule_caps_a_group_rule_across_the_whole_tree() {
let mut input = String::from("UNH+1+ORDERS:D:96A:UN'");
for i in 0..20 {
input.push_str(&format!("RFF+ON:{i}'"));
}
input.push_str("UNT+22+1'");
let segments = parse_segs(&input);
let tree = group_segments_indexed(&segments, SCHEMA, "ROOT");
let pack = ProfileRulePack::new("CAP")
.with_max_issues_per_rule(3)
.with_scoped_group_rule_fn("SG1", "CAP-SG1", |_group, _segs, _ctx, issues| {
issues.push(ValidationIssue::new(
ValidationSeverity::Error,
"one issue per SG1 occurrence",
));
});
let report = ValidationContext::builder()
.with_profile_pack(pack)
.build()
.validate_grouped(&tree, &segments);
assert_eq!(
report.total_issues(),
3,
"the cap is per rule per call, so twenty group occurrences must still \
yield at most three issues, not twenty",
);
}
static SG12: &[GroupDef] = &[GroupDef::new("SG12", "NAD")];
static UTILMD_LIKE: &[GroupDef] = &[
GroupDef::new("SG2", "NAD"),
GroupDef::with_children("SG4", "IDE", SG12),
];
#[test]
fn a_group_scoped_nad_rule_fires_on_the_group_the_structure_assigns() {
let input = "UNH+1+UTILMD:D:11A:UN'BGM+E01+1+9' NAD+MS+SENDER::293'NAD+MR+RECEIVER::293' IDE+24+VORGANG1'NAD+Z09+KUNDE::293'DTM+92:20260101:102'NAD+VY+PARTY::293'";
let segments = parse_segs(input);
let tree = group_segments_indexed(&segments, UTILMD_LIKE, "ROOT");
let pack = ProfileRulePack::new("UTILMD-LIKE")
.with_scoped_group_rule_fn("SG2", "SG2-NAD", |_group, segs, _ctx, issues| {
for qualifier in segs
.iter()
.filter(|s| s.tag == "NAD")
.filter_map(|s| s.element_str(0))
{
issues.push(
ValidationIssue::new(ValidationSeverity::Info, qualifier).with_rule_id("SG2"),
);
}
})
.with_scoped_group_rule_fn("SG12", "SG12-NAD", |_group, segs, _ctx, issues| {
for qualifier in segs
.iter()
.filter(|s| s.tag == "NAD")
.filter_map(|s| s.element_str(0))
{
issues.push(
ValidationIssue::new(ValidationSeverity::Info, qualifier).with_rule_id("SG12"),
);
}
});
let report = ValidationContext::builder()
.with_profile_pack(pack)
.build()
.validate_grouped(&tree, &segments);
let seen = |rule: &str| -> Vec<String> {
report
.infos()
.iter()
.filter(|i| i.rule_id.as_deref() == Some(rule))
.map(|i| i.message.clone())
.collect()
};
assert_eq!(
seen("SG2"),
["MS", "MR"],
"SG2 sees only the message-level parties",
);
assert_eq!(
seen("SG12"),
["Z09", "VY"],
"SG12 sees the Vorgang's parties — and is reachable at all",
);
}
#[test]
fn every_nested_party_group_is_enumerable_from_the_tree() {
let input = "NAD+MS+SENDER::293' IDE+24+V1'NAD+Z09+KUNDE::293' IDE+24+V2'NAD+VY+PARTY::293'NAD+DP+DELIVERY::293'";
let segments = parse_segs(input);
let tree = group_segments_indexed(&segments, UTILMD_LIKE, "ROOT");
let per_vorgang: Vec<Vec<&str>> = tree
.find("SG4")
.map(|sg4| {
sg4.find("SG12")
.filter_map(|sg12| sg12.segments(&segments).first())
.filter_map(|nad| nad.element_str(0))
.collect()
})
.collect();
assert_eq!(per_vorgang, [vec!["Z09"], vec!["VY", "DP"]]);
}
static SG8: &[GroupDef] = &[GroupDef::new("SG8", "SEQ")];
static PATH_SCHEMA: &[GroupDef] = &[GroupDef::with_children("SG4", "IDE", SG8)];
#[test]
fn group_path_is_stamped() {
use edifact::{FindingKind, GroupPath, GroupStep};
let segs = parse_segs(
"UNH+1+UTILMD:D:11A:UN'BGM+E01'IDE+24+A'SEQ+Z01'SEQ+Z02'IDE+24+B'SEQ+Z01'UNT+8+1'",
);
let tree = group_segments_indexed(&segs, PATH_SCHEMA, "ROOT");
let pack = ProfileRulePack::new("AHB-UTILMD")
.require_segment_in_group("SG8", "RFF", "SG8-RFF-M")
.with_scoped_group_rule_fn("SG4", "SG4-OWN-PATH", |_group, _segs, _ctx, issues| {
issues.push(
ValidationIssue::new(ValidationSeverity::Warning, "own path")
.with_group_path(GroupPath::new(GroupStep::new("CUSTOM", 7)).unwrap()),
);
})
.with_scoped_group_rule_fn("ROOT", "ROOT-RULE", |_group, _segs, _ctx, issues| {
issues.push(ValidationIssue::new(ValidationSeverity::Info, "at root"));
});
let report = ValidationContext::builder()
.with_profile_pack(pack)
.build()
.validate_grouped(&tree, &segs);
let paths: Vec<String> = report
.issues_for_rule_id("SG8-RFF-M")
.map(|issue| {
assert_eq!(issue.segment_group(), Some("SG8"));
assert_eq!(issue.kind(), Some(FindingKind::Missing));
assert_eq!(issue.pack(), Some("AHB-UTILMD"));
issue.group_path().expect("path").to_string()
})
.collect();
assert_eq!(paths, ["SG4[0]/SG8[0]", "SG4[0]/SG8[1]", "SG4[1]/SG8[0]"]);
let own: Vec<String> = report
.warnings()
.iter()
.map(|issue| issue.group_path().expect("path").to_string())
.collect();
assert_eq!(own, ["CUSTOM[7]", "CUSTOM[7]"]);
let root = &report.infos()[0];
assert_eq!(root.message, "at root");
assert_eq!(root.group_path(), None);
assert_eq!(root.segment_group(), Some("ROOT"));
assert!(
report
.render_deterministic()
.contains("[path=SG4[0]/SG8[1]/RFF]"),
"{}",
report.render_deterministic()
);
}
mod placement_findings {
use super::parse_segs;
use edifact::group::{Discriminator, Entry, GroupDef, GroupSchema, SegmentPlace};
use edifact::{
FindingKind, PlacementValidator, ValidationContext, ValidationIssue, ValidationLayer,
};
use std::sync::Arc;
fn schema() -> GroupSchema {
let sg5 = GroupDef::builder("SG5", SegmentPlace::new("LOC").with_id("00047"))
.id("SG5-MALO")
.ordered()
.min(1)
.max(1)
.build()
.unwrap();
let sg4 = GroupDef::builder(
"SG4",
SegmentPlace::new("IDE").when(Discriminator::one_of(0, 0, ["24"])),
)
.segment(SegmentPlace::new("DTM").with_id("00020").with_max(1))
.group(sg5)
.build()
.unwrap();
GroupSchema::ordered(
"UTILMD",
[
Entry::Segment(SegmentPlace::new("UNH").with_min(1).with_max(1)),
Entry::Segment(SegmentPlace::new("BGM").with_min(1).with_max(1)),
Entry::Group(sg4),
Entry::Segment(SegmentPlace::new("UNT").with_min(1).with_max(1)),
],
)
.unwrap()
}
fn only<'a>(issues: &'a [ValidationIssue], code: &str) -> Vec<&'a ValidationIssue> {
issues
.iter()
.filter(|i| i.error_code() == Some(code))
.collect()
}
#[test]
fn placement_findings_unplaced() {
let segs = parse_segs(
"UNH+1+UTILMD:D:11A:UN'BGM+E01'IDE+24+A'LOC+Z16'IDE+24+B'LOC+Z16'IDE+24+C'LOC+Z16'FTX+ACB'UNT+10+1'",
);
let schema = schema();
let resolution = schema.resolve(&segs);
let findings = resolution.findings(&segs);
let unplaced = only(&findings, "E068");
assert_eq!(unplaced.len(), 1, "{findings:#?}");
let issue = unplaced[0];
assert_eq!(issue.kind(), Some(FindingKind::Placement));
assert_eq!(issue.layer(), Some(ValidationLayer::Structure));
assert_eq!(issue.segment_tag(), Some("FTX"));
assert_eq!(issue.span(), Some(segs[8].span));
assert_eq!(issue.segment_position(), Some(9));
assert_eq!(
issue.group_path().map(ToString::to_string).as_deref(),
Some("SG4[2]/SG5[0]"),
"the instance it was skipped in",
);
assert_eq!(findings.len(), 1, "nothing else is wrong: {findings:#?}");
}
#[test]
fn placement_findings_count() {
let segs = parse_segs(
"UNH+1+UTILMD:D:11A:UN'BGM+E01'IDE+24+A'DTM+92:1'DTM+93:2'LOC+Z16'LOC+Z17'UNT+8+1'",
);
let schema = schema();
let findings = schema.resolve(&segs).findings(&segs);
let dtm = only(&findings, "E069");
assert_eq!(dtm.len(), 1, "{findings:#?}");
assert_eq!(dtm[0].kind(), Some(FindingKind::Count));
assert_eq!(dtm[0].span(), Some(segs[4].span));
assert_eq!(dtm[0].segment_position(), Some(5));
assert_eq!(dtm[0].context_get("place"), Some("00020"));
assert_eq!(
dtm[0].group_path().map(ToString::to_string).as_deref(),
Some("SG4[0]")
);
let sg5 = only(&findings, "E070");
assert_eq!(sg5.len(), 1, "{findings:#?}");
assert_eq!(sg5[0].kind(), Some(FindingKind::Count));
assert_eq!(sg5[0].span(), Some(segs[6].span));
assert_eq!(sg5[0].context_get("place"), Some("SG5-MALO"));
assert_eq!(
sg5[0].group_path().map(ToString::to_string).as_deref(),
Some("SG4[0]/SG5[1]")
);
assert_eq!(findings.len(), 2, "{findings:#?}");
}
#[test]
fn placement_findings_missing() {
let segs = parse_segs("UNH+1+UTILMD:D:11A:UN'BGM+E01'IDE+24+A'UNT+4+1'");
let schema = schema();
let findings = schema.resolve(&segs).findings(&segs);
assert_eq!(findings.len(), 1, "{findings:#?}");
let issue = &findings[0];
assert_eq!(issue.error_code(), Some("E015"));
assert_eq!(issue.kind(), Some(FindingKind::Missing));
assert_eq!(issue.layer(), Some(ValidationLayer::Structure));
assert_eq!(issue.segment_tag(), Some("LOC"), "the group's trigger tag");
assert_eq!(
issue.group_path().map(ToString::to_string).as_deref(),
Some("SG4[0]")
);
assert_eq!(issue.context_get("place"), Some("SG5-MALO"));
assert_eq!(issue.span(), None, "nothing to point at");
let segs = parse_segs("UNH+1+UTILMD:D:11A:UN'UNT+2+1'");
let findings = schema.resolve(&segs).findings(&segs);
assert_eq!(findings.len(), 1, "{findings:#?}");
assert_eq!(findings[0].segment_tag(), Some("BGM"));
assert!(findings[0].group_path().is_none());
}
#[test]
fn placement_findings_through_a_validation_context() {
let segs =
parse_segs("UNH+1+UTILMD:D:11A:UN'BGM+E01'IDE+24+A'DTM+92:1'DTM+93:2'FTX+ACB'UNT+7+1'");
let schema = Arc::new(schema());
let direct = schema.resolve(&segs).findings(&segs);
let report = ValidationContext::builder()
.with_validator(
ValidationLayer::Structure,
PlacementValidator::new(Arc::clone(&schema)),
)
.build()
.validate(&segs);
let codes = |issues: Vec<&ValidationIssue>| -> Vec<String> {
let mut codes: Vec<String> = issues
.iter()
.map(|i| format!("{:?}@{:?}", i.error_code(), i.segment_position()))
.collect();
codes.sort();
codes
};
assert_eq!(
codes(report.iter_issues().collect()),
codes(direct.iter().collect()),
);
assert!(
report
.iter_issues()
.all(|i| i.layer() == Some(ValidationLayer::Structure))
);
assert_eq!(report.total_issues(), 3, "{report}");
}
}