create more function in expect struct
All checks were successful
CI/CD Pipeline / test (push) Successful in 4m44s

This commit is contained in:
philipp 2024-02-27 12:19:36 +01:00
parent 256763425b
commit 4eac917f2a
2 changed files with 28 additions and 11 deletions

View File

@ -88,7 +88,7 @@ impl Abschnitt {
break; break;
} }
assert_eq!(c.next(), None); Expect::empty(c.next());
ret.cont = true; ret.cont = true;
ret ret

View File

@ -61,6 +61,25 @@ impl<'a> Expect<'a> {
); );
self self
} }
fn typ(self, value: &str) -> Self {
if let Some(typ) = self.node.attribute("typ") {
assert!(
typ == value,
"Expected 'typ' attribute to have value {value}, got {typ} on node ({self})"
);
} else {
panic!("Expected 'typ' attribute on {self}");
}
self
}
fn empty(next: Option<Node<'_, '_>>) {
if let Some(n) = next {
let expect = Expect::from(&n);
assert!(false, "Expected no more elements, got {expect}");
}
}
} }
impl Display for Expect<'_> { impl Display for Expect<'_> {
@ -91,7 +110,7 @@ impl Risdok {
} }
Layoutdaten::parse(c.next().unwrap()); Layoutdaten::parse(c.next().unwrap());
assert_eq!(c.next(), None); Expect::empty(c.next());
true true
} }
@ -111,7 +130,7 @@ impl Metadaten {
pub(crate) fn parse(n: Node) -> Self { pub(crate) fn parse(n: Node) -> Self {
Expect::from(&n).tag("metadaten"); Expect::from(&n).tag("metadaten");
assert_eq!(n.children().next(), None); Expect::empty(n.children().next());
Self {} Self {}
} }
@ -127,7 +146,7 @@ impl Nutzdaten {
let ret = Abschnitt::parse(c.next().unwrap(), builder); let ret = Abschnitt::parse(c.next().unwrap(), builder);
assert_eq!(c.next(), None); Expect::empty(c.next());
ret.cont ret.cont
} }
@ -165,7 +184,7 @@ impl Listelem {
let text = c.next().unwrap().text().unwrap().into(); let text = c.next().unwrap().text().unwrap().into();
trace!("Parsed Listelem with text='{text}'"); trace!("Parsed Listelem with text='{text}'");
assert_eq!(c.next(), None); Expect::empty(c.next());
Self { symbol, text } Self { symbol, text }
} }
@ -271,7 +290,7 @@ impl Td {
let mut c = n.children(); let mut c = n.children();
let absatz = Absatz::parse(c.next().unwrap()); let absatz = Absatz::parse(c.next().unwrap());
assert_eq!(c.next(), None); Expect::empty(c.next());
Self { absatz } Self { absatz }
} }
@ -350,7 +369,7 @@ impl AbsatzAbs {
content: c.next().unwrap().text().unwrap().trim().into(), content: c.next().unwrap().text().unwrap().trim().into(),
}; };
assert_eq!(c.next(), None); Expect::empty(c.next());
ret ret
} }
@ -381,9 +400,7 @@ impl Ueberschrift {
} }
pub(crate) fn parse(n: Node, typ: &str) -> Self { pub(crate) fn parse(n: Node, typ: &str) -> Self {
Expect::from(&n).tag("ueberschrift"); Expect::from(&n).tag("ueberschrift").typ(typ);
assert_eq!(n.attribute("typ").unwrap(), typ);
Self { Self {
content: n.text().unwrap().into(), content: n.text().unwrap().into(),
@ -418,7 +435,7 @@ impl Layoutdaten {
pub(crate) fn parse(n: Node) -> Self { pub(crate) fn parse(n: Node) -> Self {
Expect::from(&n).tag("layoutdaten"); Expect::from(&n).tag("layoutdaten");
assert_eq!(n.children().next(), None); Expect::empty(n.children().next());
Self {} Self {}
} }