Debian Codegraph @

tests/license_expr_reparse.rs

raw
1 //! Exhaustively check that parsing the Display output of a parsed license
2 //! expression yields the same expression, over all short combinations of
3 //! names, operators, and stray commas.
4 //!
5 //! Expressions with empty names (from dangling operators, e.g. `or and`)
6 //! are excluded: an empty name renders as an empty string, so it cannot
7 //! survive a round trip.
8
9 use debian_copyright::LicenseExpr;
10
11 fn has_empty_name(expr: &LicenseExpr) -> bool {
12 match expr {
13 LicenseExpr::Name(n) => n.is_empty(),
14 LicenseExpr::WithException(n, e) => n.is_empty() || e.is_empty(),
15 LicenseExpr::And(exprs) | LicenseExpr::Or(exprs) => exprs.iter().any(has_empty_name),
16 }
17 }
18
19 #[test]
20 fn reparse_is_stable() {
21 let alphabet = ["A", "B2.0", "X Y", "and", "or", "with", ",", "Z,"];
22 let mut inputs: Vec<String> = alphabet.iter().map(|t| t.to_string()).collect();
23 let mut frontier = inputs.clone();
24 for _ in 0..3 {
25 let mut next = Vec::new();
26 for prefix in &frontier {
27 for tok in &alphabet {
28 next.push(format!("{} {}", prefix, tok));
29 }
30 }
31 inputs.extend(next.iter().cloned());
32 frontier = next;
33 }
34 let mut checked = 0;
35 for input in &inputs {
36 let parsed = LicenseExpr::parse(input);
37 if has_empty_name(&parsed) {
38 continue;
39 }
40 let rendered = parsed.to_string();
41 let reparsed = LicenseExpr::parse(&rendered);
42 assert_eq!(
43 reparsed, parsed,
44 "not stable for input {input:?}: rendered as {rendered:?}"
45 );
46 checked += 1;
47 }
48 assert!(checked > 1000, "only {checked} inputs exercised");
49 }