rxeval 0.1.1

Evaluate ODK/OpenRosa XForms logic: XPath with the OpenRosa extensions, over a form instance
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
//! What the rule graph must get right.
//!
//! Each of these is a way a form engine can be wrong without failing: the
//! submission is accepted, the report runs, and the number at the bottom is
//! not the number that was collected.

use rxeval::rules::{referenced_paths, ViolationKind};
use rxeval::{parse, Binding, Fixed, Instance, Rules};

fn env() -> Fixed {
    Fixed {
        today: "2026-08-14".into(),
        now: "2026-08-14T09:30:00.000-03:00".into(),
    }
}

fn binding(path: &str) -> Binding {
    Binding::new(path)
}

fn rules(bindings: Vec<Binding>) -> Rules {
    Rules::new(bindings).unwrap_or_else(|e| panic!("building rules: {e}"))
}

#[test]
fn a_constraint_judges_the_answer_it_was_given() {
    let mut age = binding("/data/age");
    age.constraint = Some(parse(". >= 0 and . <= 120").unwrap());
    age.constraint_message = Some("age must be between 0 and 120".into());
    let rules = rules(vec![age]);

    let ok = Instance::from_xml("<data><age>42</age></data>").unwrap();
    assert!(rules.check(&ok, &env()).is_empty());

    let bad = Instance::from_xml("<data><age>140</age></data>").unwrap();
    let found = rules.check(&bad, &env());
    assert_eq!(found.len(), 1);
    assert_eq!(found[0].kind, ViolationKind::Constraint);
    assert!(found[0].describe().contains("between 0 and 120"));

    // no answer is not a constraint failure — that is what required is for
    let empty = Instance::from_xml("<data><age/></data>").unwrap();
    assert!(rules.check(&empty, &env()).is_empty());
}

#[test]
fn required_fires_only_when_the_question_is_asked() {
    let mut consent = binding("/data/consent");
    consent.required = Some(parse("true()").unwrap());
    let mut name = binding("/data/name");
    name.required = Some(parse("true()").unwrap());
    name.relevant = Some(parse("/data/consent = 'yes'").unwrap());
    let rules = rules(vec![consent, name]);

    // consent refused: the name was never asked, so its absence is correct
    let refused = Instance::from_xml("<data><consent>no</consent><name/></data>").unwrap();
    assert!(
        rules.check(&refused, &env()).is_empty(),
        "an unasked question was reported as unanswered"
    );

    // consent given: now the blank name is a real omission
    let given = Instance::from_xml("<data><consent>yes</consent><name/></data>").unwrap();
    let found = rules.check(&given, &env());
    assert_eq!(found.len(), 1);
    assert_eq!(found[0].kind, ViolationKind::Required);
    assert!(found[0].node_path.ends_with("/name"));
}

#[test]
fn relevance_cascades_from_the_group_down() {
    let mut group = binding("/data/details");
    group.relevant = Some(parse("/data/consent = 'yes'").unwrap());
    let mut phone = binding("/data/details/phone");
    // the child's own relevance says yes, but its group says no
    phone.relevant = Some(parse("true()").unwrap());
    phone.required = Some(parse("true()").unwrap());
    phone.constraint = Some(parse("string-length(.) = 11").unwrap());
    let rules = rules(vec![group, phone]);

    let hidden = Instance::from_xml(
        "<data><consent>no</consent><details><phone>123</phone></details></data>",
    )
    .unwrap();
    assert!(
        rules.check(&hidden, &env()).is_empty(),
        "a rule was enforced on an answer inside a hidden group"
    );

    let shown = Instance::from_xml(
        "<data><consent>yes</consent><details><phone>123</phone></details></data>",
    )
    .unwrap();
    let found = rules.check(&shown, &env());
    assert_eq!(found.len(), 1);
    assert_eq!(found[0].kind, ViolationKind::Constraint);
}

#[test]
fn calculations_run_in_dependency_order_not_declaration_order() {
    // total depends on subtotal, which depends on price — declared backwards
    let mut total = binding("/data/total");
    total.calculate = Some(parse("/data/subtotal * 2").unwrap());
    let mut subtotal = binding("/data/subtotal");
    subtotal.calculate = Some(parse("/data/price + 10").unwrap());
    let price = binding("/data/price");
    let rules = rules(vec![total, subtotal, price]);

    let instance = Instance::from_xml(
        "<data><total>40</total><subtotal>20</subtotal><price>10</price></data>",
    )
    .unwrap();
    assert!(
        rules.check(&instance, &env()).is_empty(),
        "{:?}",
        rules
            .check(&instance, &env())
            .iter()
            .map(|v| v.describe())
            .collect::<Vec<_>>()
    );
}

#[test]
fn a_stored_calculation_that_disagrees_with_the_form_is_reported() {
    let mut total = binding("/data/total");
    total.calculate = Some(parse("/data/a + /data/b").unwrap());
    let rules = rules(vec![total, binding("/data/a"), binding("/data/b")]);

    let tampered = Instance::from_xml("<data><a>2</a><b>3</b><total>99</total></data>").unwrap();
    let found = rules.check(&tampered, &env());
    assert_eq!(found.len(), 1);
    match &found[0].kind {
        ViolationKind::Calculation { stored, computed } => {
            assert_eq!(stored, "99");
            assert_eq!(computed, "5");
        }
        other => panic!("expected a calculation mismatch, got {other:?}"),
    }
}

#[test]
fn calculations_that_feed_each_other_are_refused_at_build_time() {
    let mut a = binding("/data/a");
    a.calculate = Some(parse("/data/b + 1").unwrap());
    let mut b = binding("/data/b");
    b.calculate = Some(parse("/data/a + 1").unwrap());

    let error = Rules::new(vec![a, b]).unwrap_err();
    assert!(error.contains("depend on each other"), "{error}");
    assert!(error.contains("/data/a"), "{error}");
    assert!(error.contains("/data/b"), "{error}");
}

#[test]
fn every_repeat_instance_is_checked_on_its_own() {
    let mut age = binding("/data/resident/age");
    age.constraint = Some(parse(". < 120").unwrap());
    let rules = rules(vec![age]);

    let instance = Instance::from_xml(
        "<data>
           <resident><age>34</age></resident>
           <resident><age>200</age></resident>
           <resident><age>19</age></resident>
         </data>",
    )
    .unwrap();
    let found = rules.check(&instance, &env());
    assert_eq!(found.len(), 1, "expected exactly the middle resident");
    assert_eq!(found[0].path, "/data/resident/age");
}

#[test]
fn a_rule_inside_a_repeat_reads_its_own_instance() {
    // the classic: `../` must mean this resident, not the first one
    let mut check = binding("/data/resident/adult");
    check.calculate = Some(parse("if(../age >= 18, 'yes', 'no')").unwrap());
    let rules = rules(vec![check, binding("/data/resident/age")]);

    let instance = Instance::from_xml(
        "<data>
           <resident><age>34</age><adult>yes</adult></resident>
           <resident><age>7</age><adult>no</adult></resident>
         </data>",
    )
    .unwrap();
    assert!(
        rules.check(&instance, &env()).is_empty(),
        "{:?}",
        rules
            .check(&instance, &env())
            .iter()
            .map(|v| v.describe())
            .collect::<Vec<_>>()
    );

    // and a wrong one is caught in the instance it belongs to
    let wrong = Instance::from_xml(
        "<data>
           <resident><age>34</age><adult>yes</adult></resident>
           <resident><age>7</age><adult>yes</adult></resident>
         </data>",
    )
    .unwrap();
    let found = rules.check(&wrong, &env());
    assert_eq!(found.len(), 1);
    assert!(matches!(found[0].kind, ViolationKind::Calculation { .. }));
}

#[test]
fn a_rule_that_cannot_run_is_reported_not_assumed() {
    let mut field = binding("/data/x");
    field.constraint = Some(parse("pulldata('t', 'c', 'k', .) = 'sim'").unwrap());
    let rules = rules(vec![field]);

    let instance = Instance::from_xml("<data><x>2026-01-01</x></data>").unwrap();
    let found = rules.check(&instance, &env());
    assert_eq!(found.len(), 1);
    match &found[0].kind {
        ViolationKind::Failed(why) => assert!(why.contains("pulldata"), "{why}"),
        other => panic!("expected a failure to evaluate, got {other:?}"),
    }
}

#[test]
fn relative_dependencies_resolve_against_the_binding() {
    // `../age` written on /data/resident/adult means /data/resident/age
    let expr = parse("if(../age >= 18, 'yes', 'no')").unwrap();
    let paths = referenced_paths(&expr, "/data/resident/adult");
    assert!(
        paths.contains(&"/data/resident/age".to_string()),
        "{paths:?}"
    );

    // an absolute path is itself
    let expr = parse("/data/total + 1").unwrap();
    assert_eq!(
        referenced_paths(&expr, "/data/x"),
        vec!["/data/total".to_string()]
    );

    // a wildcard names no single path, and must not invent one
    let expr = parse("count(/data/*)").unwrap();
    assert!(referenced_paths(&expr, "/data/x").is_empty());
}

// ---------------------------------------------------------------------------
// From a real XLSForm, through the XForm it generates
// ---------------------------------------------------------------------------

fn sheet(rows: &[&[&str]]) -> rxform::xls::Sheet {
    rxform::xls::sheet_from_rows(
        rows.iter()
            .map(|r| r.iter().map(|c| c.to_string()).collect()),
    )
}

/// Spreadsheet → XForm → rules, which is the path a real form takes.
fn rules_of(workbook: &rxform::xls::Workbook, name: &str) -> Result<Rules, String> {
    let survey = rxform::parser::parse(workbook, name).map_err(|e| e.to_string())?;
    let xform = rxform::xform::generate(&survey).map_err(|e| e.to_string())?;
    rxeval::rules::from_xform(&xform)
}

#[test]
fn rules_come_out_of_the_form_the_device_was_given() {
    let workbook = rxform::xls::Workbook {
        survey: sheet(&[
            &[
                "type",
                "name",
                "label",
                "relevant",
                "constraint",
                "required",
                "calculation",
                "constraint_message",
            ],
            &[
                "integer",
                "idade",
                "Idade",
                "",
                ". >= 0 and . <= 120",
                "yes",
                "",
                "idade entre 0 e 120",
            ],
            &[
                "begin group",
                "adulto",
                "Adulto",
                "${idade} >= 18",
                "",
                "",
                "",
                "",
            ],
            &[
                "text",
                "cpf",
                "CPF",
                "",
                "string-length(.) = 11",
                "yes",
                "",
                "CPF tem 11 digitos",
            ],
            &["end group", "", "", "", "", "", "", ""],
            &[
                "calculate",
                "faixa",
                "",
                "",
                "",
                "",
                "if(${idade} >= 60, 'idoso', 'nao')",
                "",
            ],
        ]),
        settings: sheet(&[&["form_id", "form_title"], &["cadastro", "Cadastro"]]),
        ..Default::default()
    };
    let rules = rules_of(&workbook, "cadastro").unwrap();

    // a minor: the group is hidden, so the CPF rule must not fire
    let minor = Instance::from_xml(
        "<data><idade>7</idade><adulto><cpf/></adulto><faixa>nao</faixa></data>",
    )
    .unwrap();
    let found = rules.check(&minor, &env());
    assert!(
        found.is_empty(),
        "rules fired on a hidden group: {:?}",
        found.iter().map(|v| v.describe()).collect::<Vec<_>>()
    );

    // an adult with a short CPF, reported in the form author's own words
    let adult = Instance::from_xml(
        "<data><idade>30</idade><adulto><cpf>123</cpf></adulto><faixa>nao</faixa></data>",
    )
    .unwrap();
    let found = rules.check(&adult, &env());
    assert_eq!(
        found.len(),
        1,
        "{:?}",
        found.iter().map(|v| v.describe()).collect::<Vec<_>>()
    );
    assert!(found[0].describe().contains("11 digitos"), "{:?}", found[0]);

    // an impossible age
    let impossible = Instance::from_xml(
        "<data><idade>200</idade><adulto><cpf>12345678901</cpf></adulto><faixa>nao</faixa></data>",
    )
    .unwrap();
    let found = rules.check(&impossible, &env());
    assert!(
        found.iter().any(|v| v.describe().contains("entre 0 e 120")),
        "{:?}",
        found.iter().map(|v| v.describe()).collect::<Vec<_>>()
    );

    // and the derived value is recomputed from the answers that feed it
    let wrong_band = Instance::from_xml(
        "<data><idade>70</idade><adulto><cpf>12345678901</cpf></adulto><faixa>nao</faixa></data>",
    )
    .unwrap();
    let found = rules.check(&wrong_band, &env());
    assert!(
        found
            .iter()
            .any(|v| matches!(v.kind, ViolationKind::Calculation { .. })),
        "{:?}",
        found.iter().map(|v| v.describe()).collect::<Vec<_>>()
    );
}

#[test]
fn an_expression_the_engine_cannot_evaluate_is_named_with_its_path() {
    let xform = r#"<h:html xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa">
      <h:head><model>
        <instance><data id="f"><x/></data></instance>
        <bind nodeset="/data/x" type="int" constraint=". &gt;"/>
      </model></h:head>
      <h:body/>
    </h:html>"#;
    let error = rxeval::rules::from_xform(xform).unwrap_err();
    assert!(error.contains("constraint"), "{error}");
    assert!(error.contains("/data/x"), "{error}");
}

#[test]
fn a_form_without_binds_says_so() {
    let xform = r#"<h:html xmlns:h="http://www.w3.org/1999/xhtml">
      <h:head><model><instance><data id="f"><x/></data></instance></model></h:head>
      <h:body/>
    </h:html>"#;
    assert!(rxeval::rules::from_xform(xform)
        .unwrap_err()
        .contains("no binds"));
}