lemma-engine 0.8.12

A language that means business.
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
//! Strict runtime-input grammar for Ratio-typed data overrides.
//!
//! Covers `engine.run(spec, ..., data: HashMap<String, String>, ...)` where each
//! string flows through `parse_value_from_string` → `parse_number_unit::Ratio` →
//! `RatioLiteral::parse`. Pins exact `ValueKind::Ratio(decimal, optional_unit)`,
//! not substrings of `Display`, so a 100x off value cannot pass.
//!
//! Also includes a Scale-side regression: `"5%"` against a Scale type must error
//! with the friendly Scale message (no leftover `%`/`%%` handling in the Scale
//! literal parser).

use lemma::evaluation::OperationResult;
use lemma::parsing::ast::DateTimeValue;
use lemma::Engine;
use lemma::ValueKind;
use rust_decimal::Decimal;
use std::collections::HashMap;
use std::str::FromStr;

fn load(engine: &mut Engine, code: &str) {
    engine
        .load(code, lemma::SourceType::Labeled("ratio_in.lemma"))
        .unwrap_or_else(|errs| {
            let joined = errs
                .iter()
                .map(|e| e.to_string())
                .collect::<Vec<_>>()
                .join("\n");
            panic!("expected load to succeed, got: {joined}");
        });
}

fn run_ratio(engine: &Engine, spec: &str, raw: &str) -> (Decimal, Option<String>) {
    let mut data = HashMap::new();
    data.insert("r".to_string(), raw.to_string());
    let now = DateTimeValue::now();
    let resp = engine
        .run(spec, Some(&now), data, false)
        .unwrap_or_else(|e| panic!("run failed for input '{raw}': {e}"));
    let rr = resp
        .results
        .get("out")
        .unwrap_or_else(|| panic!("rule 'out' not found"));
    let lit = match &rr.result {
        OperationResult::Value(v) => v.as_ref(),
        OperationResult::Veto(v) => panic!("input '{raw}' produced veto: {v}"),
    };
    match &lit.value {
        ValueKind::Ratio(n, u) => (*n, u.clone()),
        other => panic!("input '{raw}' produced non-Ratio: {:?}", other),
    }
}

fn run_err(engine: &Engine, spec: &str, raw: &str) -> String {
    let mut data = HashMap::new();
    data.insert("r".to_string(), raw.to_string());
    let now = DateTimeValue::now();
    engine
        .run(spec, Some(&now), data, false)
        .err()
        .unwrap_or_else(|| panic!("expected '{raw}' to be rejected, but run succeeded"))
        .to_string()
}

fn percent_spec() -> &'static str {
    r#"
spec s
data r: percent
rule out: r
"#
}

// ─── Accepted: bare number, no unit ───────────────────────────────────

#[test]
fn accepts_bare_zero() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "0");
    assert_eq!(n, Decimal::from(0));
    assert_eq!(u, None);
}

#[test]
fn accepts_bare_decimal() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "0.5");
    assert_eq!(n, Decimal::from_str("0.5").unwrap());
    assert_eq!(u, None);
}

#[test]
fn accepts_bare_negative() {
    // Round-trip: the evaluator can produce negative ratios from valid inputs;
    // the parser must accept anything the evaluator can emit.
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "-0.25");
    assert_eq!(n, Decimal::from_str("-0.25").unwrap());
    assert_eq!(u, None);
}

// ─── Accepted: percent sigil ──────────────────────────────────────────

#[test]
fn accepts_percent_sigil_integer() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "50%");
    assert_eq!(n, Decimal::from_str("0.50").unwrap());
    assert_eq!(u.as_deref(), Some("percent"));
}

#[test]
fn accepts_percent_sigil_decimal() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "50.5%");
    assert_eq!(n, Decimal::from_str("0.505").unwrap());
    assert_eq!(u.as_deref(), Some("percent"));
}

#[test]
fn accepts_percent_sigil_negative() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "-50%");
    assert_eq!(n, Decimal::from_str("-0.5").unwrap());
    assert_eq!(u.as_deref(), Some("percent"));
}

#[test]
fn accepts_percent_sigil_with_thousands_separator() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "5,000%");
    assert_eq!(n, Decimal::from_str("50").unwrap());
    assert_eq!(u.as_deref(), Some("percent"));
}

// ─── Accepted: permille sigil ─────────────────────────────────────────

#[test]
fn accepts_permille_sigil() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "25%%");
    assert_eq!(n, Decimal::from_str("0.025").unwrap());
    assert_eq!(u.as_deref(), Some("permille"));
}

#[test]
fn accepts_permille_sigil_negative() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "-25%%");
    assert_eq!(n, Decimal::from_str("-0.025").unwrap());
    assert_eq!(u.as_deref(), Some("permille"));
}

// ─── Accepted: keyword forms (single and multi-space) ─────────────────

#[test]
fn accepts_percent_keyword_single_space() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "50 percent");
    assert_eq!(n, Decimal::from_str("0.50").unwrap());
    assert_eq!(u.as_deref(), Some("percent"));
}

#[test]
fn accepts_percent_keyword_multi_space() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "50    percent");
    assert_eq!(n, Decimal::from_str("0.50").unwrap());
    assert_eq!(u.as_deref(), Some("percent"));
}

#[test]
fn accepts_percent_keyword_tab() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "50\tpercent");
    assert_eq!(n, Decimal::from_str("0.50").unwrap());
    assert_eq!(u.as_deref(), Some("percent"));
}

#[test]
fn accepts_permille_keyword() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let (n, u) = run_ratio(&engine, "s", "25 permille");
    assert_eq!(n, Decimal::from_str("0.025").unwrap());
    assert_eq!(u.as_deref(), Some("permille"));
}

// ─── Accepted: user-defined ratio unit ────────────────────────────────

#[test]
fn accepts_user_defined_ratio_unit() {
    let code = r#"
spec s
data r: ratio -> unit basis_points 10000
rule out: r
"#;
    let mut engine = Engine::new();
    load(&mut engine, code);
    let (n, u) = run_ratio(&engine, "s", "500 basis_points");
    assert_eq!(n, Decimal::from_str("0.05").unwrap());
    assert_eq!(u.as_deref(), Some("basis_points"));
}

// ─── Cross-form equivalence ───────────────────────────────────────────

#[test]
fn sigil_and_keyword_produce_same_value() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let sigil = run_ratio(&engine, "s", "50%");
    let keyword = run_ratio(&engine, "s", "50 percent");
    assert_eq!(sigil, keyword);
}

#[test]
fn permille_sigil_and_keyword_produce_same_value() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let sigil = run_ratio(&engine, "s", "25%%");
    let keyword = run_ratio(&engine, "s", "25 permille");
    assert_eq!(sigil, keyword);
}

// ─── Rejected: empty / whitespace ─────────────────────────────────────

#[test]
fn rejects_empty() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let msg = run_err(&engine, "s", "");
    assert!(
        msg.to_lowercase().contains("empty") || msg.to_lowercase().contains("ratio"),
        "expected empty/ratio message, got: {msg}"
    );
}

#[test]
fn rejects_whitespace_only() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let _msg = run_err(&engine, "s", "   ");
}

// ─── Rejected: sigil without number ───────────────────────────────────

#[test]
fn rejects_bare_percent_sigil() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let _msg = run_err(&engine, "s", "%");
}

#[test]
fn rejects_bare_permille_sigil() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let _msg = run_err(&engine, "s", "%%");
}

#[test]
fn rejects_sigil_before_number() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let _msg = run_err(&engine, "s", "%5");
    let _msg = run_err(&engine, "s", "%%5");
}

// ─── Rejected: sigil with separator (strict glue rule) ────────────────

#[test]
fn rejects_percent_sigil_with_space() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let msg = run_err(&engine, "s", "5 %");
    assert!(
        msg.contains("glued") || msg.contains("'%'"),
        "expected explicit glue-rule error, got: {msg}"
    );
}

#[test]
fn rejects_permille_sigil_with_space() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let msg = run_err(&engine, "s", "5  %%");
    assert!(
        msg.contains("glued") || msg.contains("'%%'"),
        "expected explicit glue-rule error, got: {msg}"
    );
}

// ─── Rejected: digit after sigil / mixed sigil-keyword ────────────────

#[test]
fn rejects_digit_after_percent_sigil() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let _msg = run_err(&engine, "s", "5%5");
}

#[test]
fn rejects_digit_after_permille_sigil() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let _msg = run_err(&engine, "s", "5%%5");
}

#[test]
fn rejects_sigil_glued_to_keyword() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let _msg = run_err(&engine, "s", "5%percent");
}

// ─── Rejected: trailing junk ──────────────────────────────────────────

#[test]
fn rejects_trailing_token_after_keyword() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let msg = run_err(&engine, "s", "50 percent extra");
    assert!(
        msg.to_lowercase().contains("extra") || msg.to_lowercase().contains("expected"),
        "expected extra-token error, got: {msg}"
    );
}

// ─── Rejected: unknown unit name ──────────────────────────────────────

#[test]
fn rejects_unknown_unit_name() {
    let mut engine = Engine::new();
    load(&mut engine, percent_spec());
    let msg = run_err(&engine, "s", "50 fictional");
    assert!(
        msg.contains("Unknown unit") && msg.contains("Valid units"),
        "expected `Unknown unit … Valid units …` error, got: {msg}"
    );
    assert!(
        msg.contains("fictional"),
        "error must name the offending unit, got: {msg}"
    );
}

// ─── Scale-side regression: `%` is no longer accepted by NumberWithUnit ───

#[test]
fn scale_type_rejects_percent_sigil() {
    let code = r#"
spec s
data r: scale -> unit eur 1
rule out: r
"#;
    let mut engine = Engine::new();
    load(&mut engine, code);
    let mut data = HashMap::new();
    data.insert("r".to_string(), "5%".to_string());
    let now = DateTimeValue::now();
    let err = engine
        .run("s", Some(&now), data, false)
        .expect_err("'5%' must not parse as a Scale value");
    let msg = err.to_string();
    assert!(
        msg.contains("Scale value")
            || msg.contains("must include a unit")
            || msg.contains("Invalid scale"),
        "expected friendly Scale-unit error, got: {msg}"
    );
    assert!(
        !msg.contains("Unknown unit 'percent'"),
        "Scale path must not leak a 'percent' unit lookup, got: {msg}"
    );
}

#[test]
fn scale_type_rejects_permille_sigil() {
    let code = r#"
spec s
data r: scale -> unit eur 1
rule out: r
"#;
    let mut engine = Engine::new();
    load(&mut engine, code);
    let mut data = HashMap::new();
    data.insert("r".to_string(), "5%%".to_string());
    let now = DateTimeValue::now();
    let err = engine
        .run("s", Some(&now), data, false)
        .expect_err("'5%%' must not parse as a Scale value");
    let msg = err.to_string();
    assert!(
        !msg.contains("Unknown unit 'permille'"),
        "Scale path must not leak a 'permille' unit lookup, got: {msg}"
    );
}