einvoice-xpath-eval 0.2.0

Node model and evaluator of the XPath 2.0 subset used by the official e-invoicing validation stylesheets, with the behaviour of Saxon-HE 12.10
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Property tests of the node model and of path evaluation, on random trees, against a
//! naive reference: every axis is defined from parent links and document order only, and
//! paths are evaluated step by step with explicit sorting.

// Integration tests are only ever built with `cfg(test)`; saying so lets clippy apply its
// test exemptions (unwrap, expect, panic) to the helpers outside `#[test]` functions too.
#![cfg(test)]
// Proptest is not a dependency on wasm targets.
#![cfg(not(target_family = "wasm"))]

use std::fmt::Write as _;
use std::rc::Rc;

use einvoice_xpath::{NamespaceMap, QName, parse_expr};
use einvoice_xpath_eval::{
    Clock, Context, Document, DynamicContext, Item, NoHost, Node, NodeAxis, NodeKind,
    SimpleStaticContext, TreeBuilder, compile,
};
use proptest::prelude::*;

/// A random tree: elements named `a`, `b` or `c` with attributes `x`/`y`, text and
/// comments.
#[derive(Debug, Clone)]
enum Tree {
    Element(&'static str, Vec<&'static str>, Vec<Tree>),
    Text(String),
    Comment,
}

fn tree() -> impl Strategy<Value = Tree> {
    let leaf = prop_oneof![
        "[a-z ]{1,3}".prop_map(Tree::Text),
        Just(Tree::Comment),
        (prop::sample::select(vec!["a", "b", "c"]), attributes())
            .prop_map(|(n, attrs)| Tree::Element(n, attrs, Vec::new())),
    ];
    leaf.prop_recursive(4, 40, 4, |inner| {
        (
            prop::sample::select(vec!["a", "b", "c"]),
            attributes(),
            prop::collection::vec(inner, 0..4),
        )
            .prop_map(|(n, attrs, children)| Tree::Element(n, attrs, children))
    })
}

fn attributes() -> impl Strategy<Value = Vec<&'static str>> {
    prop::sample::select(vec![vec![], vec!["x"], vec!["y"], vec!["x", "y"]])
}

fn xml(tree: &Tree, out: &mut String) {
    match tree {
        Tree::Element(name, attrs, children) => {
            let _ = write!(out, "<{name}");
            for (i, a) in attrs.iter().enumerate() {
                let _ = write!(out, " {a}='{i}'");
            }
            out.push('>');
            for child in children {
                xml(child, out);
            }
            let _ = write!(out, "</{name}>");
        }
        Tree::Text(text) => out.push_str(text),
        Tree::Comment => out.push_str("<!--c-->"),
    }
}

fn document(tree: &Tree) -> Rc<Document> {
    let mut text = String::new();
    // The document element is always an element.
    let root = match tree {
        Tree::Element(..) => tree.clone(),
        other => Tree::Element("a", Vec::new(), vec![other.clone()]),
    };
    xml(&root, &mut text);
    Document::parse(&text, None).unwrap()
}

/// Every node in document order (attributes after their element), built without the
/// axes under test: recursion over children and attributes only.
fn all_nodes(doc: &Rc<Document>) -> Vec<Node> {
    fn walk(node: &Node, out: &mut Vec<Node>) {
        out.push(node.clone());
        out.extend(node.attributes());
        for child in node.children() {
            walk(&child, out);
        }
    }
    let mut out = Vec::new();
    walk(&doc.root(), &mut out);
    out
}

fn ancestors(node: &Node) -> Vec<Node> {
    let mut out = Vec::new();
    let mut current = node.parent();
    while let Some(n) = current {
        current = n.parent();
        out.push(n);
    }
    out
}

/// The reference definition of each axis, in axis order.
fn reference_axis(all: &[Node], node: &Node, axis: NodeAxis) -> Vec<Node> {
    let position = |n: &Node| all.iter().position(|m| m == n).unwrap();
    let is_attribute = |n: &Node| n.kind() == NodeKind::Attribute;
    let here = position(node);
    let ancestors_of_node = ancestors(node);
    let mut out: Vec<Node> = match axis {
        NodeAxis::SelfAxis => vec![node.clone()],
        NodeAxis::Parent => node.parent().into_iter().collect(),
        NodeAxis::Child => all
            .iter()
            .filter(|n| !is_attribute(n) && n.parent().as_ref() == Some(node))
            .cloned()
            .collect(),
        NodeAxis::Attribute => all
            .iter()
            .filter(|n| is_attribute(n) && n.parent().as_ref() == Some(node))
            .cloned()
            .collect(),
        NodeAxis::Descendant | NodeAxis::DescendantOrSelf => all
            .iter()
            .filter(|n| {
                (!is_attribute(n) && ancestors(n).contains(node))
                    || (axis == NodeAxis::DescendantOrSelf && *n == node)
            })
            .cloned()
            .collect(),
        NodeAxis::FollowingSibling | NodeAxis::PrecedingSibling => {
            if is_attribute(node) {
                Vec::new()
            } else {
                all.iter()
                    .filter(|n| {
                        !is_attribute(n)
                            && n.parent().is_some()
                            && n.parent() == node.parent()
                            && if axis == NodeAxis::FollowingSibling {
                                position(n) > here
                            } else {
                                position(n) < here
                            }
                    })
                    .cloned()
                    .collect()
            }
        }
        NodeAxis::Ancestor => ancestors_of_node.iter().rev().cloned().collect(),
        NodeAxis::AncestorOrSelf => ancestors_of_node
            .iter()
            .rev()
            .cloned()
            .chain(std::iter::once(node.clone()))
            .collect(),
        NodeAxis::Preceding => all
            .iter()
            .filter(|n| !is_attribute(n) && position(n) < here && !ancestors_of_node.contains(n))
            .cloned()
            .collect(),
    };
    if axis.is_reverse() {
        out.reverse();
    }
    out
}

const AXES: [NodeAxis; 11] = [
    NodeAxis::Child,
    NodeAxis::Attribute,
    NodeAxis::SelfAxis,
    NodeAxis::Descendant,
    NodeAxis::DescendantOrSelf,
    NodeAxis::FollowingSibling,
    NodeAxis::Parent,
    NodeAxis::Ancestor,
    NodeAxis::AncestorOrSelf,
    NodeAxis::PrecedingSibling,
    NodeAxis::Preceding,
];

/// The XPath axes of the supported subset (no `descendant`).
const XPATH_AXES: [(&str, NodeAxis); 10] = [
    ("child", NodeAxis::Child),
    ("attribute", NodeAxis::Attribute),
    ("self", NodeAxis::SelfAxis),
    ("descendant-or-self", NodeAxis::DescendantOrSelf),
    ("following-sibling", NodeAxis::FollowingSibling),
    ("parent", NodeAxis::Parent),
    ("ancestor", NodeAxis::Ancestor),
    ("ancestor-or-self", NodeAxis::AncestorOrSelf),
    ("preceding-sibling", NodeAxis::PrecedingSibling),
    ("preceding", NodeAxis::Preceding),
];

const TESTS: [&str; 5] = ["node()", "*", "a", "text()", "x"];

fn test_matches(test: &str, axis: NodeAxis, node: &Node) -> bool {
    let principal = if axis == NodeAxis::Attribute {
        NodeKind::Attribute
    } else {
        NodeKind::Element
    };
    match test {
        "node()" => true,
        "text()" => node.kind() == NodeKind::Text,
        "*" => node.kind() == principal,
        name => node.kind() == principal && node.name().is_some_and(|q| q.local == name),
    }
}

/// One step: axis, node test, predicate (`""`, `[1]` or `[last()]`).
type PathStep = (usize, usize, &'static str);

/// The path: from every node (`/descendant-or-self::node()`), then the steps.
fn path_text(steps: &[PathStep]) -> String {
    let mut text = String::from("/descendant-or-self::node()");
    for &(axis, test, predicate) in steps {
        let _ = write!(text, "/{}::{}{predicate}", XPATH_AXES[axis].0, TESTS[test]);
    }
    text
}

fn reference_path(all: &[Node], steps: &[PathStep]) -> Vec<Node> {
    let mut current = reference_axis(all, &all[0], NodeAxis::DescendantOrSelf);
    for &(axis, test, predicate) in steps {
        let (_, node_axis) = XPATH_AXES[axis];
        let mut next = Vec::new();
        for node in &current {
            let matching: Vec<Node> = reference_axis(all, node, node_axis)
                .into_iter()
                .filter(|n| test_matches(TESTS[test], node_axis, n))
                .collect();
            match predicate {
                "[1]" => next.extend(matching.first().cloned()),
                "[last()]" => next.extend(matching.last().cloned()),
                _ => next.extend(matching),
            }
        }
        next.sort_by(Node::cmp_document_order);
        next.dedup();
        current = next;
    }
    current
}

fn evaluate(doc: &Rc<Document>, text: &str) -> Vec<Node> {
    let expr = compile(
        &parse_expr(text, &NamespaceMap::new()).unwrap(),
        &SimpleStaticContext::new(),
    )
    .unwrap();
    let dc = DynamicContext::new(&NoHost, Clock::from_unix_millis(0, 0).unwrap());
    expr.evaluate(&dc, &Context::for_item(Item::Node(doc.root())))
        .unwrap()
        .into_iter()
        .map(|item| item.as_node().unwrap().clone())
        .collect()
}

fn single(doc: &Rc<Document>, text: &str) -> String {
    let expr = compile(
        &parse_expr(text, &NamespaceMap::new()).unwrap(),
        &SimpleStaticContext::new(),
    )
    .unwrap();
    let dc = DynamicContext::new(&NoHost, Clock::from_unix_millis(0, 0).unwrap());
    let items = expr
        .evaluate(&dc, &Context::for_item(Item::Node(doc.root())))
        .unwrap();
    items[0].string_value().into_owned()
}

fn path_steps() -> impl Strategy<Value = Vec<PathStep>> {
    prop::collection::vec(
        (
            0..XPATH_AXES.len(),
            0..TESTS.len(),
            prop::sample::select(vec!["", "", "[1]", "[last()]"]),
        ),
        1..4,
    )
}

/// XML text with everything that makes a parsed text differ from its source: character
/// and entity references, line ends, CDATA sections, whitespace in attribute values,
/// comments and processing instructions, prefixed and default-namespace names.
fn source_text() -> impl Strategy<Value = String> {
    let piece = prop::sample::select(vec![
        "a",
        "é",
        " ",
        "\t",
        "\n",
        "\r\n",
        "\r",
        "&amp;",
        "&lt;",
        "&#65;",
        "&#x1F600;",
        "<![CDATA[<&]]>",
        "<!--c-->",
        "<!--\r\n-->",
        "<?t d\r\ne?>",
        "<e/>",
        "<p:e p:x='1'/>",
    ]);
    let content = prop::collection::vec(piece, 0..8).prop_map(|p| p.concat());
    let value = prop::collection::vec(
        prop::sample::select(vec![
            "v", " ", "\t", "\n", "\r\n", "&#9;", "&#10;", "&amp;", "&quot;", "é",
        ]),
        0..5,
    )
    .prop_map(|p| p.concat());
    (content.clone(), value, content).prop_map(|(before, value, inner)| {
        format!(
            "<?xml version='1.0'?><!--top-->\r\n<p:r xmlns:p='urn:p' xmlns='urn:d' a='{value}' \
             p:b='{value}'>{before}<e c='{value}'>{inner}</e>{before}</p:r>\r\n<?t top?>"
        )
    })
}

/// The tree of a parsed document, built through the public [`TreeBuilder`] from the
/// parser's values: the reference for [`Document::parse`].
fn reference_tree(text: &str) -> Rc<Document> {
    fn name(prefix: Option<&str>, namespace: Option<&str>, local: &str) -> QName {
        QName {
            prefix: prefix.map(str::to_owned),
            namespace: namespace.filter(|n| !n.is_empty()).map(str::to_owned),
            local: local.to_owned(),
        }
    }
    fn walk(text: &str, node: roxmltree::Node<'_, '_>, builder: &mut TreeBuilder) {
        match node.node_type() {
            roxmltree::NodeType::Root => {}
            roxmltree::NodeType::Element => {
                let start = node.range().start + 1;
                let written = &text[start..start + text[start..].find([' ', '/', '>']).unwrap()];
                let prefix = written.split_once(':').map(|(p, _)| p);
                builder
                    .start_element(name(
                        prefix,
                        node.tag_name().namespace(),
                        node.tag_name().name(),
                    ))
                    .unwrap();
                for a in node.attributes() {
                    let prefix = text[a.range_qname()].split_once(':').map(|(p, _)| p);
                    builder
                        .attribute(name(prefix, a.namespace(), a.name()), a.value())
                        .unwrap();
                }
                for child in node.children() {
                    walk(text, child, builder);
                }
                builder.end_element().unwrap();
            }
            roxmltree::NodeType::Text => {
                if node.parent().is_some_and(|p| !p.is_root()) {
                    builder.text(node.text().unwrap()).unwrap();
                }
            }
            roxmltree::NodeType::Comment => builder.comment(node.text().unwrap()).unwrap(),
            roxmltree::NodeType::PI => {
                let pi = node.pi().unwrap();
                builder
                    .processing_instruction(pi.target, pi.value.unwrap_or(""))
                    .unwrap();
            }
        }
    }
    let parsed = roxmltree::Document::parse(text).unwrap();
    let mut builder = TreeBuilder::new();
    for child in parsed.root().children() {
        walk(text, child, &mut builder);
    }
    builder.finish(None, None).unwrap()
}

/// Kind, name (with its prefix and namespace), string value and parent position of every
/// node, in document order.
fn dump(doc: &Rc<Document>) -> Vec<String> {
    let all = all_nodes(doc);
    all.iter()
        .map(|n| {
            let name = n
                .name()
                .map(|q| format!("{q} {{{}}}", q.namespace.as_deref().unwrap_or("")));
            let parent = n.parent().map(|p| all.iter().position(|m| *m == p));
            format!("{:?} {name:?} {:?} {parent:?}", n.kind(), n.string_value())
        })
        .collect()
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(500))]

    #[test]
    fn parsed_trees_match_the_built_reference(text in source_text()) {
        let expected = dump(&reference_tree(&text));
        prop_assert_eq!(dump(&Document::parse(text.as_str(), None).unwrap()), expected.clone());
        prop_assert_eq!(dump(&Document::parse(text, None).unwrap()), expected);
    }

    #[test]
    fn any_clock_reading_is_a_value_or_an_error(millis in any::<i64>(), offset in -900i16..=900) {
        // Never a panic (overflow checks are on in every profile).
        let clock = Clock::from_unix_millis(offset, millis);
        prop_assert_eq!(clock.is_ok(), (-840..=840).contains(&offset)
            && millis.checked_add(i64::from(offset) * 60_000).is_some());
        if let Ok(clock) = clock {
            prop_assert_eq!(clock.current_date_time().timezone(), Some(offset));
        }
    }

    #[test]
    fn axes_match_their_definition(tree in tree()) {
        let doc = document(&tree);
        let all = all_nodes(&doc);
        for node in &all {
            for axis in AXES {
                let actual: Vec<Node> = node.axis(axis).collect();
                prop_assert_eq!(actual, reference_axis(&all, node, axis), "{:?} {:?}", node, axis);
            }
        }
        // The arena order is document order.
        prop_assert!(all.windows(2).all(|w| w[0].cmp_document_order(&w[1]).is_lt()));
    }

    #[test]
    fn paths_match_the_reference(tree in tree(), steps in path_steps()) {
        let doc = document(&tree);
        let all = all_nodes(&doc);
        let text = path_text(&steps);
        let expected = reference_path(&all, &steps);
        prop_assert_eq!(&evaluate(&doc, &text), &expected, "{}", text);
        // Lazy and counting consumers agree with the full sequence.
        prop_assert_eq!(single(&doc, &format!("exists({text})")), (!expected.is_empty()).to_string());
        prop_assert_eq!(single(&doc, &format!("count({text})")), expected.len().to_string());
        prop_assert_eq!(evaluate(&doc, &format!("({text})[1]")), expected.first().cloned().into_iter().collect::<Vec<_>>());
        prop_assert_eq!(evaluate(&doc, &format!("({text})[last()]")), expected.last().cloned().into_iter().collect::<Vec<_>>());
        // A union with itself and with a reversed copy is the same sequence.
        prop_assert_eq!(&evaluate(&doc, &format!("{text} | {text}")), &expected);
        prop_assert_eq!(&evaluate(&doc, &format!("reverse({text})/.")), &expected);
    }
}