qua 0.71.0

Structure-aware query CLI for the Quarb engine
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! Under a collation, texts that differ only in case are no tie
//! (ruling 2026-11-12): the locale's own case order decides, lower
//! case first in most locales, upper case first where CLDR gives
//! the locale that order (Danish, Maltese, Church Slavic). Letters
//! decide first, then accents, then case: `a A b B`, never
//! `a b A B`. The order is colligo's (0.2.0, its default); the
//! answer no longer depends on the input order. Without a collation
//! text orders by code point, as ever.

use quarb::{QueryResult, Value};

fn doc() -> quarb_json::JsonAdapter {
    quarb_json::JsonAdapter::parse(r#"{"w": [1]}"#).unwrap()
}

fn values(q: &str) -> Vec<String> {
    match quarb::run(q, &doc()).unwrap_or_else(|e| panic!("{q}: {e}")) {
        QueryResult::Values(vs) => vs.iter().map(Value::to_quarb).collect(),
        other => panic!("{q}: expected values, got {other:?}"),
    }
}

fn one(q: &str) -> String {
    let v = values(q);
    assert_eq!(v.len(), 1, "{q}: {v:?}");
    v.into_iter().next().unwrap()
}

/// The answers of `q` under the output locale `locale`.
fn under(locale: &str, q: &str) -> Vec<String> {
    let mut run = quarb::Run::ambient();
    run.collation = Some(locale.into());
    let out = quarb::Query::parse(q, &doc())
        .unwrap_or_else(|e| panic!("{q}: {e}"))
        .run(&doc(), &run)
        .unwrap_or_else(|e| panic!("{q}: {e}"));
    match out.result {
        QueryResult::Values(vs) => vs.iter().map(Value::to_quarb).collect(),
        other => panic!("{q}: expected values, got {other:?}"),
    }
}

/// Every order the words can come in.
fn orders(words: &[&str]) -> Vec<String> {
    fn permute(rest: &mut Vec<String>, taken: &mut Vec<String>, out: &mut Vec<String>) {
        if rest.is_empty() {
            out.push(taken.join("; "));
            return;
        }
        for i in 0..rest.len() {
            let w = rest.remove(i);
            taken.push(w.clone());
            permute(rest, taken, out);
            taken.pop();
            rest.insert(i, w);
        }
    }
    let mut out = Vec::new();
    let mut rest: Vec<String> = words.iter().map(|w| format!("\"{w}\"")).collect();
    permute(&mut rest, &mut Vec::new(), &mut out);
    out
}

/// Lower case first, whatever order the texts came in: the cases of
/// lang's measurement on 0.61.0, where the input order decided.
#[test]
fn case_is_no_tie_under_a_collation() {
    for list in orders(&["B", "b", "a"]) {
        assert_eq!(
            one(&format!(r#"= @({list}) | sort("de")"#)),
            r#"@("a"; "b"; "B")"#,
            "{list}"
        );
    }
    for list in orders(&["aB", "Ab", "ab", "AB"]) {
        assert_eq!(
            one(&format!(r#"= @({list}) | sort("en")"#)),
            r#"@("ab"; "aB"; "Ab"; "AB")"#,
            "{list}"
        );
    }
    // accents before case: the accented pair after the plain one
    for list in orders(&["Resume", "résumé", "resume", "Résumé"]) {
        assert_eq!(
            one(&format!(r#"= @({list}) | sort("en")"#)),
            r#"@("resume"; "Resume"; "résumé"; "Résumé")"#,
            "{list}"
        );
    }
    // letters before any case: a A b B, and `ac` after every `ab`
    assert_eq!(
        one(r#"= @("b"; "A"; "a"; "B") | sort("de")"#),
        r#"@("a"; "A"; "b"; "B")"#
    );
    assert_eq!(
        one(r#"= @("ac"; "AB"; "ab") | sort("de")"#),
        r#"@("ab"; "AB"; "ac")"#
    );
    // the whole order flipped
    assert_eq!(
        one(r#"= @("b"; "A"; "a"; "B") | sort("de"; --desc)"#),
        r#"@("B"; "b"; "A"; "a")"#
    );
}

/// The locale's own case order: upper case first in Danish, Maltese
/// and Church Slavic, as CLDR orders them.
#[test]
fn the_locale_decides_which_case_leads() {
    for locale in ["da", "mt"] {
        for list in orders(&["b", "A", "a", "B"]) {
            assert_eq!(
                one(&format!(r#"= @({list}) | sort("{locale}")"#)),
                r#"@("A"; "a"; "B"; "b")"#,
                "{locale}: {list}"
            );
        }
    }
    assert_eq!(
        one(r#"= @("слово"; "Слово") | sort("cu")"#),
        r#"@("Слово"; "слово")"#
    );
    for locale in ["de", "en", "fr", "sv", "ru"] {
        assert_eq!(
            one(&format!(r#"= @("B"; "A"; "b"; "a") | sort("{locale}")"#)),
            r#"@("a"; "A"; "b"; "B")"#,
            "{locale}"
        );
    }
}

/// Every command that orders text under a collation follows: the
/// keyed ones with `--locale=`, all of them under the output locale,
/// and the a tergo order under a collation.
#[test]
fn every_collated_order_follows() {
    let words = r#"= @("B"; "b"; "A"; "a") | ..."#;
    assert_eq!(
        values(&format!("{words} @| sort_by($_; --locale=de)")),
        [r#""a""#, r#""A""#, r#""b""#, r#""B""#]
    );
    assert_eq!(
        values(&format!("{words} @| sort_by($_; --locale=da)")),
        [r#""A""#, r#""a""#, r#""B""#, r#""b""#]
    );
    // an extreme is one text now, where the case pair tied
    assert_eq!(
        values(&format!("{words} @| min_by($_; --locale=de)")),
        [r#""a""#]
    );
    assert_eq!(
        values(&format!("{words} @| max_by($_; --locale=de)")),
        [r#""B""#]
    );
    assert_eq!(
        values(&format!("{words} @| top(2; $_; --locale=de)")),
        [r#""B""#, r#""b""#]
    );
    assert_eq!(
        values(&format!("{words} @| bottom(2; $_; --locale=da)")),
        [r#""A""#, r#""a""#]
    );
    // the output locale, for the bare sort and the keyed commands
    assert_eq!(
        under("de", r#"= @("B"; "b"; "A"; "a") | sort"#),
        [r#"@("a"; "A"; "b"; "B")"#]
    );
    assert_eq!(
        under("da", r#"= @("B"; "b"; "A"; "a") | sort"#),
        [r#"@("A"; "a"; "B"; "b")"#]
    );
    assert_eq!(
        under("de", &format!("{words} @| sort_by($_)")),
        [r#""a""#, r#""A""#, r#""b""#, r#""B""#]
    );
    assert_eq!(under("de", &format!("{words} @| min_by($_)")), [r#""a""#]);
    // a tergo under a collation: the endings decide, then the case
    assert_eq!(
        one(r#"= @("Ab"; "ab"; "ba"; "Ba") | sort("de"; --atergo)"#),
        r#"@("ba"; "Ba"; "ab"; "Ab")"#
    );
}

/// What is no tie stays where it was, and without a collation text
/// orders by code point.
#[test]
fn what_was_no_tie_is_unchanged() {
    assert_eq!(
        one(r#"= @("é"; "e"; "f") | sort("fr")"#),
        r#"@("e"; "é"; "f")"#
    );
    assert_eq!(
        one(r#"= @("f"; "é"; "e") | sort("fr")"#),
        r#"@("e"; "é"; "f")"#
    );
    // numbers keep their own order under a locale, case or no case
    assert_eq!(
        one(r#"= @("b"; 10; "B"; 9) | sort("de")"#),
        r#"@(9; 10; "b"; "B")"#
    );
    // no collation: code points, upper case letters first
    assert_eq!(
        one(r#"= @("b"; "A"; "a"; "B") | sort"#),
        r#"@("A"; "B"; "a"; "b")"#
    );
}

fn refused(q: &str) -> String {
    match quarb::run(q, &doc()) {
        Ok(v) => panic!("{q}: expected a refusal, got {v:?}"),
        Err(e) => e.to_string(),
    }
}

const MIXED: &str = r#"= @("b"; "A"; "a"; "B")"#;

/// The case options (ruling 2026-11-12: "leverage colligo's sort
/// options fully"): `-u` (`--upper-first`) and `-l`
/// (`--lower-first`) name one case order on any locale, whatever
/// CLDR says of it; `-i` (`--ignore-case`) makes texts that differ
/// only in case a tie again, the input order kept.
#[test]
fn a_case_option_overrides_the_locale() {
    let lower = r#"@("a"; "A"; "b"; "B")"#;
    let upper = r#"@("A"; "a"; "B"; "b")"#;
    // lower first on a locale that sorts upper first, and the other
    // way; short and long spellings
    for flag in ["-l", "--lower-first"] {
        assert_eq!(
            one(&format!(r#"{MIXED} | sort("da"; {flag})"#)),
            lower,
            "{flag}"
        );
        assert_eq!(
            one(&format!(r#"{MIXED} | sort("de"; {flag})"#)),
            lower,
            "{flag}"
        );
    }
    for flag in ["-u", "--upper-first"] {
        assert_eq!(
            one(&format!(r#"{MIXED} | sort("de"; {flag})"#)),
            upper,
            "{flag}"
        );
        assert_eq!(
            one(&format!(r#"{MIXED} | sort("da"; {flag})"#)),
            upper,
            "{flag}"
        );
    }
    // case ignored: the pairs tie and keep the order they came in
    for flag in ["-i", "--ignore-case"] {
        assert_eq!(
            one(&format!(r#"{MIXED} | sort("de"; {flag})"#)),
            r#"@("A"; "a"; "b"; "B")"#,
            "{flag}"
        );
        assert_eq!(
            one(&format!(r#"= @("B"; "a"; "A"; "b") | sort("da"; {flag})"#)),
            r#"@("a"; "A"; "B"; "b")"#,
            "{flag}"
        );
    }
    // with the other flags of `sort`
    assert_eq!(
        one(&format!(r#"{MIXED} | sort("de"; -u -d)"#)),
        r#"@("b"; "B"; "a"; "A")"#
    );
    assert_eq!(
        one(r#"= @("Ab"; "ab"; "ba"; "Ba") | sort("de"; -a -u)"#),
        r#"@("Ba"; "ba"; "Ab"; "ab")"#
    );
    // a record's keys
    assert_eq!(
        one(r#"= %(b = 1; A = 2; a = 3; B = 4) | sort("de"; -u)"#),
        "%(A = 2; a = 3; B = 4; b = 1)"
    );
}

/// The keyed commands that order take the three, beside `--locale=`
/// and under the output locale.
#[test]
fn the_keyed_commands_take_the_case_options() {
    let words = format!("{MIXED} | ...");
    assert_eq!(
        values(&format!("{words} @| sort_by($_; --locale=da -l)")),
        [r#""a""#, r#""A""#, r#""b""#, r#""B""#]
    );
    assert_eq!(
        values(&format!(
            "{words} @| sort_by($_; --locale=de --upper-first)"
        )),
        [r#""A""#, r#""a""#, r#""B""#, r#""b""#]
    );
    // an extreme under `-i` is every text of the tie again
    assert_eq!(
        values(&format!("{words} @| min_by($_; --locale=de -i)")),
        [r#""A""#, r#""a""#]
    );
    assert_eq!(
        values(&format!("{words} @| max_by($_; --locale=de -i)")),
        [r#""b""#, r#""B""#]
    );
    assert_eq!(
        values(&format!("{words} @| max_by($_; --locale=de -u)")),
        [r#""b""#]
    );
    assert_eq!(
        values(&format!("{words} @| top(2; $_; --locale=de -u)")),
        [r#""b""#, r#""B""#]
    );
    assert_eq!(
        values(&format!("{words} @| bottom(2; $_; --locale=da -l)")),
        [r#""a""#, r#""A""#]
    );
    // a table's rows the same
    assert_eq!(
        one(
            r#"= @(%(k = "b"); %(k = "A"); %(k = "a"); %(k = "B")) | # | sort_by(:k; --locale=de -u) | :k"#
        ),
        r#"@("A"; "a"; "B"; "b")"#
    );
    // the output locale's collation, in the order the call names
    assert_eq!(
        under("da", &format!("{MIXED} | sort(-l)")),
        [r#"@("a"; "A"; "b"; "B")"#]
    );
    assert_eq!(
        under("de", &format!("{words} @| sort_by($_; -u)")),
        [r#""A""#, r#""a""#, r#""B""#, r#""b""#]
    );
    assert_eq!(
        under("de", &format!("{words} @| min_by($_; -i)")),
        [r#""A""#, r#""a""#]
    );
}

/// A case order is a collation's to give: with no locale named and
/// no output locale, a case option sorts text under the root
/// collation, so the case-blind sort needs no locale. The whole text
/// order is then the root collation's, accents beside their letters.
#[test]
fn a_case_option_alone_sorts_under_the_root_collation() {
    assert_eq!(
        one(&format!("{MIXED} | sort(-l)")),
        r#"@("a"; "A"; "b"; "B")"#
    );
    assert_eq!(
        one(&format!("{MIXED} | sort(-u)")),
        r#"@("A"; "a"; "B"; "b")"#
    );
    assert_eq!(
        one(&format!("{MIXED} | sort(-i)")),
        r#"@("A"; "a"; "b"; "B")"#
    );
    assert_eq!(
        one(r#"= @("Banana"; "apple"; "cherry"; "Apple") | sort(-i)"#),
        r#"@("apple"; "Apple"; "Banana"; "cherry")"#
    );
    // without the option: code points
    assert_eq!(one(r#"= @("z"; "é"; "e") | sort"#), r#"@("e"; "z"; "é")"#);
    assert_eq!(
        one(r#"= @("z"; "é"; "e") | sort(-i)"#),
        r#"@("e"; "é"; "z")"#
    );
    // numbers keep their own order
    assert_eq!(
        one(r#"= @("b"; 10; "B"; 9) | sort(-i)"#),
        r#"@(9; 10; "b"; "B")"#
    );
    // the keyed commands and a record the same
    assert_eq!(
        values(&format!("{MIXED} | ... @| sort_by($_; -l)")),
        [r#""a""#, r#""A""#, r#""b""#, r#""B""#]
    );
    assert_eq!(
        values(&format!("{MIXED} | ... @| max_by($_; -i)")),
        [r#""b""#, r#""B""#]
    );
    assert_eq!(
        one("= %(b = 1; A = 2; a = 3; B = 4) | sort(-l)"),
        "%(a = 3; A = 2; b = 1; B = 4)"
    );
}

/// The three are one choice, and only the commands that order text
/// under a collation take them.
#[test]
fn the_case_options_are_one_choice() {
    for q in [
        r#"= @("a"; "B") | sort(-u -l)"#,
        r#"= @("a"; "B") | sort("de"; -i --upper-first)"#,
        r#"= @("a"; "B") | ... @| sort_by($_; -l -i)"#,
        r#"= @("a"; "B") | ... @| top(1; $_; -u -i)"#,
    ] {
        let e = refused(q);
        assert!(
            e.contains("--upper-first, --lower-first and --ignore-case are one choice: name one"),
            "{q}: {e}"
        );
    }
    for q in [
        r#"= @("a"; "B") | unique(-i)"#,
        r#"= @("a"; "B") | ... @| unique_by($_; -i)"#,
        r#"= @("a"; "B") | reverse(-u)"#,
        r#"= @("a"; "B") | first(-l)"#,
    ] {
        let e = refused(q);
        assert!(e.contains("no option"), "{q}: {e}");
    }
}

/// `"C"` and `"POSIX"` name the code-point order (ruling
/// 2026-11-12), the words the tools take for no locale: text by
/// code point, ASCII order for ASCII text, whatever the output
/// locale. It is no collation, so it has no case order to give.
#[test]
fn c_and_posix_name_the_code_point_order() {
    let list = r#"= @("b"; "A"; "a"; "B"; "é"; "Z")"#;
    let points = r#"@("A"; "B"; "Z"; "a"; "b"; "é")"#;
    let threads = [r#""A""#, r#""B""#, r#""Z""#, r#""a""#, r#""b""#, r#""é""#];
    for name in ["C", "POSIX"] {
        assert_eq!(
            one(&format!(r#"{list} | sort("{name}")"#)),
            points,
            "{name}"
        );
        // under an output locale the bare sort collates, and the
        // name asks for the code points all the same
        assert_eq!(
            under("de", &format!(r#"{list} | sort("{name}")"#)),
            [points],
            "{name}"
        );
        assert_eq!(
            values(&format!("{list} | ... @| sort_by($_; --locale={name})")),
            threads,
            "{name}"
        );
        assert_eq!(
            under(
                "de",
                &format!("{list} | ... @| sort_by($_; --locale={name})")
            ),
            threads,
            "{name}"
        );
        assert_eq!(
            under(
                "de",
                &format!("{list} | ... @| min_by($_; --locale={name})")
            ),
            [r#""A""#],
            "{name}"
        );
        assert_eq!(
            under(
                "de",
                &format!("{list} | ... @| max_by($_; --locale={name})")
            ),
            [r#""é""#],
            "{name}"
        );
        assert_eq!(
            under(
                "de",
                &format!("{list} | ... @| top(2; $_; --locale={name})")
            ),
            [r#""é""#, r#""b""#],
            "{name}"
        );
        assert_eq!(
            under(
                "de",
                &format!("{list} | ... @| bottom(2; $_; --locale={name})")
            ),
            [r#""A""#, r#""B""#],
            "{name}"
        );
        // no case order
        let e = refused(&format!(r#"{list} | sort("{name}"; -i)"#));
        assert!(
            e.contains(&format!(
                r#"sort: a case order is a collation's, and "{name}" names the code-point order"#
            )),
            "{name}: {e}"
        );
        let e = refused(&format!("{list} | ... @| sort_by($_; --locale={name} -u)"));
        assert!(e.contains("names the code-point order"), "{name}: {e}");
    }
    assert_eq!(
        under("de", &format!("{list} | sort")),
        [r#"@("a"; "A"; "b"; "B"; "é"; "Z")"#]
    );
    // the endings by code point, and the whole order flipped
    assert_eq!(
        one(r#"= @("ab"; "Ba"; "aB") | sort("C"; --atergo)"#),
        r#"@("aB"; "Ba"; "ab")"#
    );
    assert_eq!(
        one(&format!(r#"{list} | sort("C"; --desc)"#)),
        r#"@("é"; "b"; "a"; "Z"; "B"; "A")"#
    );
    // numbers keep their own order
    assert_eq!(
        one(r#"= @("b"; 10; "B"; 9) | sort("C")"#),
        r#"@(9; 10; "B"; "b")"#
    );
    // the two words as written, and no other
    for other in ["c", "C.UTF-8", "ascii"] {
        let e = refused(&format!(r#"{list} | sort("{other}")"#));
        assert!(
            e.contains("is not a Unicode locale identifier")
                || e.contains("no collation for locale"),
            "{other}: {e}"
        );
    }
    let e = refused(&format!(r#"{list} | sort("c")"#));
    assert!(e.contains(r#""C" for the code-point order"#), "{e}");
}