bc-envelope-pattern 0.14.0

Pattern matcher for Gordian Envelope
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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
mod common;

use bc_envelope::prelude::*;
use bc_envelope_pattern::{Matcher, Path, Pattern, Reluctance, format_paths};
use indoc::indoc;

fn fold(string: &str) -> Envelope {
    let chars: Vec<String> = string.chars().map(|c| c.to_string()).collect();
    let mut it = chars.into_iter().enumerate().rev();
    let (index, c) = it.next().unwrap();
    let mut env = Envelope::new_assertion(index, c);
    for (index, c) in it {
        let obj = Envelope::new(c.clone())
            .add_assertion_envelope(env)
            .unwrap();
        env = Envelope::new_assertion(index, obj);
    }
    Envelope::unit().add_assertion_envelope(env).unwrap()
}

fn unfold(env: impl AsRef<Envelope>) -> String {
    let mut result = String::new();
    let mut env = Some(env.as_ref().clone());
    while let Some(e) = env {
        if e.is_assertion() {
            let object = e.as_object().unwrap();
            let c: String = object.extract_subject().unwrap();
            result.push_str(&c);
            env = object.assertions().first().cloned();
        } else {
            env = e.assertions().first().cloned();
        }
    }
    result
}

#[test]
fn test_fold() {
    bc_envelope::register_tags();

    let s = "hello";
    let folded = fold(s);

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        '' [
            0: "h" [
                1: "e" [
                    2: "l" [
                        3: "l" [
                            4: "o"
                        ]
                    ]
                ]
            ]
        ]
    "#}.trim();
    assert_actual_expected!(folded.format(), expected);

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected =  indoc! {r#"
        b229d3cb NODE
            934312d6 subj ''
            1b47f7a1 ASSERTION
                6e340b9c pred 0
                dc1d9ddc obj NODE
                    70a0d519 subj "h"
                    354b5ed3 ASSERTION
                        4bf5122f pred 1
                        a899ff63 obj NODE
                            f9a00f43 subj "e"
                            7e272ce6 ASSERTION
                                dbc1b4c9 pred 2
                                bff05dca obj NODE
                                    63518250 subj "l"
                                    d71e5aaf ASSERTION
                                        084fed08 pred 3
                                        73381991 obj NODE
                                            63518250 subj "l"
                                            7c92231b ASSERTION
                                                e52d9c50 pred 4
                                                2dd41130 obj "o"
    "#}.trim();
    assert_actual_expected!(folded.tree_format(), expected);

    let unfolded = unfold(folded);
    assert_eq!(unfolded, s);
}

#[test]
fn repeat_test() {
    bc_envelope::register_tags();

    let s = "hello";
    let env = fold(s);

    let pattern = Pattern::traverse(vec![Pattern::any_assertion()]);
    assert_eq!(format!("{}", pattern), "assert");
    let paths = pattern.paths(&env);
    assert_eq!(unfold(paths[0].last().unwrap()), s);

    let assertion_object_pattern = Pattern::traverse(vec![
        Pattern::any_assertion(),
        Pattern::any_object(),
    ]);
    assert_eq!(format!("{}", assertion_object_pattern), "assert -> obj");

    let pattern =
        Pattern::repeat(assertion_object_pattern, 3..=3, Reluctance::Greedy);
    assert_eq!(format!("{}", pattern), "(assert -> obj){3}");
    let paths = pattern.paths(&env);
    assert_eq!(paths.len(), 1);

    let path = &paths[0];
    assert_eq!(transpose(path), "hel");
    assert_eq!(unfold(path.last().unwrap()), "lo");
}

#[test]
fn test_repeat_2() {
    let str = "AabBbabB";
    let env = fold(str);

    let seq_a = Pattern::traverse(vec![
        Pattern::assertion_with_object(Pattern::text("A")),
        Pattern::any_object(),
    ]);
    assert_eq!(format!("{}", seq_a), r#"assertobj("A") -> obj"#);

    let seq_any = Pattern::traverse(vec![
        Pattern::any_assertion(),
        Pattern::any_object(),
    ]);
    assert_eq!(format!("{}", seq_any), r#"assert -> obj"#);

    let seq_b = Pattern::traverse(vec![
        Pattern::assertion_with_object(Pattern::text("B")),
        Pattern::any_object(),
    ]);
    assert_eq!(format!("{}", seq_b), r#"assertobj("B") -> obj"#);

    let pat = |mode| {
        Pattern::traverse(vec![
            seq_a.clone(),
            Pattern::repeat(seq_any.clone(), .., mode),
            seq_b.clone(),
        ])
    };

    let pattern = pat(Reluctance::Greedy);
    assert_eq!(
        format!("{}", pattern),
        r#"assertobj("A") -> obj -> (assert -> obj)* -> assertobj("B") -> obj"#
    );
    let paths = pattern.paths(&env);
    assert_eq!(paths.len(), 1);
    assert_eq!(transpose(&paths[0]), "AabBbabB");

    let pattern = pat(Reluctance::Lazy);
    assert_eq!(
        format!("{}", pattern),
        r#"assertobj("A") -> obj -> (assert -> obj)*? -> assertobj("B") -> obj"#
    );
    let paths = pattern.paths(&env);
    assert_eq!(paths.len(), 1);
    assert_eq!(transpose(&paths[0]), "AabB");

    let pattern = pat(Reluctance::Possessive);
    assert_eq!(
        format!("{}", pattern),
        r#"assertobj("A") -> obj -> (assert -> obj)*+ -> assertobj("B") -> obj"#
    );
    let paths = pattern.paths(&env);
    assert_eq!(paths.len(), 0);
}

fn transpose(path: impl AsRef<Path>) -> String {
    path.as_ref()
        .iter()
        .filter_map(|e| e.subject().as_text())
        .collect::<Vec<_>>()
        .join("")
}

fn wrap_n(mut env: Envelope, n: usize) -> Envelope {
    for _ in 0..n {
        env = env.wrap();
    }
    env
}

#[test]
fn repeat_any_greedy() {
    let pat = Pattern::traverse(vec![
        Pattern::repeat(Pattern::unwrap(), .., Reluctance::Greedy),
        Pattern::any_cbor(),
    ]);

    let env = wrap_n(Envelope::new(42), 4);
    let paths = pat.paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        3a0b1e87 WRAPPED { { { { 42 } } } }
            75659622 WRAPPED { { { 42 } } }
                81bb1f5e WRAPPED { { 42 } }
                    58b1ac6a WRAPPED { 42 }
                        7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn repeat_any_lazy() {
    let env = wrap_n(Envelope::new(42), 4);
    let pat = Pattern::traverse(vec![
        Pattern::repeat(Pattern::unwrap(), .., Reluctance::Lazy),
        Pattern::any_cbor(),
    ]);
    let paths = pat.paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        3a0b1e87 WRAPPED { { { { 42 } } } }
            75659622 WRAPPED { { { 42 } } }
                81bb1f5e WRAPPED { { 42 } }
                    58b1ac6a WRAPPED { 42 }
                        7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn repeat_any_possessive() {
    let env = wrap_n(Envelope::new(42), 4);
    let pat = Pattern::traverse(vec![
        Pattern::repeat(Pattern::unwrap(), .., Reluctance::Possessive),
        Pattern::any_cbor(),
    ]);
    let paths = pat.paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        3a0b1e87 WRAPPED { { { { 42 } } } }
            75659622 WRAPPED { { { 42 } } }
                81bb1f5e WRAPPED { { 42 } }
                    58b1ac6a WRAPPED { 42 }
                        7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn repeat_some_greedy() {
    let env = wrap_n(Envelope::new(42), 3);
    let pat = Pattern::traverse(vec![
        Pattern::repeat(Pattern::unwrap(), 1.., Reluctance::Greedy),
        Pattern::any_cbor(),
    ]);
    let paths = pat.paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        75659622 WRAPPED { { { 42 } } }
            81bb1f5e WRAPPED { { 42 } }
                58b1ac6a WRAPPED { 42 }
                    7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn repeat_some_lazy() {
    let env = wrap_n(Envelope::new(42), 3);
    let pat = Pattern::traverse(vec![
        Pattern::repeat(Pattern::unwrap(), 1.., Reluctance::Lazy),
        Pattern::any_cbor(),
    ]);
    let paths = pat.paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        75659622 WRAPPED { { { 42 } } }
            81bb1f5e WRAPPED { { 42 } }
                58b1ac6a WRAPPED { 42 }
                    7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn repeat_some_possessive() {
    let env = wrap_n(Envelope::new(42), 3);
    let pat = Pattern::traverse(vec![
        Pattern::repeat(Pattern::unwrap(), 1.., Reluctance::Possessive),
        Pattern::any_cbor(),
    ]);
    let paths = pat.paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        75659622 WRAPPED { { { 42 } } }
            81bb1f5e WRAPPED { { 42 } }
                58b1ac6a WRAPPED { 42 }
                    7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn repeat_optional_greedy() {
    let pat = Pattern::traverse(vec![
        Pattern::repeat(Pattern::unwrap(), 0..=1, Reluctance::Greedy),
        Pattern::any_cbor(),
    ]);
    let paths = pat.paths(&wrap_n(Envelope::new(42), 0));
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    let paths = pat.paths(&wrap_n(Envelope::new(42), 1));
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        58b1ac6a WRAPPED { 42 }
            7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn repeat_optional_lazy() {
    let pat = Pattern::traverse(vec![
        Pattern::repeat(Pattern::unwrap(), 0..=1, Reluctance::Lazy),
        Pattern::any_cbor(),
    ]);
    let paths = pat.paths(&wrap_n(Envelope::new(42), 0));
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
    let paths = pat.paths(&wrap_n(Envelope::new(42), 1));
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        58b1ac6a WRAPPED { 42 }
            7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn repeat_optional_possessive() {
    let pat = Pattern::traverse(vec![
        Pattern::repeat(Pattern::unwrap(), 0..=1, Reluctance::Possessive),
        Pattern::any_cbor(),
    ]);
    let paths = pat.paths(&wrap_n(Envelope::new(42), 0));
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
    let paths = pat.paths(&wrap_n(Envelope::new(42), 1));
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        58b1ac6a WRAPPED { 42 }
            7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn repeat_range_greedy() {
    let pat = Pattern::traverse(vec![
        Pattern::repeat(Pattern::unwrap(), 2..=3, Reluctance::Greedy),
        Pattern::any_cbor(),
    ]);
    let env = wrap_n(Envelope::new(42), 3);
    assert!(pat.matches(&env));
    let paths = pat.paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        75659622 WRAPPED { { { 42 } } }
            81bb1f5e WRAPPED { { 42 } }
                58b1ac6a WRAPPED { 42 }
                    7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn repeat_range_lazy() {
    let pat = Pattern::traverse(vec![
        Pattern::repeat(Pattern::unwrap(), 2..=3, Reluctance::Lazy),
        Pattern::any_cbor(),
    ]);
    let env = wrap_n(Envelope::new(42), 3);
    let paths = pat.paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        75659622 WRAPPED { { { 42 } } }
            81bb1f5e WRAPPED { { 42 } }
                58b1ac6a WRAPPED { 42 }
                    7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn repeat_range_possessive() {
    let pat = Pattern::traverse(vec![
        Pattern::repeat(Pattern::unwrap(), 2..=3, Reluctance::Possessive),
        Pattern::any_cbor(),
    ]);
    let env = wrap_n(Envelope::new(42), 3);
    let paths = pat.paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        75659622 WRAPPED { { { 42 } } }
            81bb1f5e WRAPPED { { 42 } }
                58b1ac6a WRAPPED { 42 }
                    7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn repeat_any_modes() {
    let env = wrap_n(Envelope::new("data"), 2);

    let pat = |mode| {
        Pattern::traverse(vec![
            Pattern::repeat(Pattern::unwrap(), 0.., mode),
            Pattern::wrapped(),
            Pattern::unwrap(),
            Pattern::text("data"),
        ])
    };

    let greedy_paths = pat(Reluctance::Greedy).paths(&env);
    let lazy_paths = pat(Reluctance::Lazy).paths(&env);
    let possessive_paths = pat(Reluctance::Possessive).paths(&env);

    assert_eq!(greedy_paths, lazy_paths);
    assert!(possessive_paths.is_empty());

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        ee8cade0 WRAPPED { { "data" } }
            febc1555 WRAPPED { "data" }
                e909da9a LEAF "data"
    "#}.trim();
    assert_actual_expected!(format_paths(&greedy_paths), expected);
}

#[test]
fn repeat_optional_modes() {
    let env = wrap_n(Envelope::new(42), 1);

    let pat = |mode| {
        Pattern::traverse(vec![
            Pattern::repeat(Pattern::unwrap(), 0..=1, mode),
            Pattern::number(42),
        ])
    };

    let greedy_paths = pat(Reluctance::Greedy).paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        58b1ac6a WRAPPED { 42 }
            7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&greedy_paths), expected);

    let lazy_paths = pat(Reluctance::Lazy).paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        58b1ac6a WRAPPED { 42 }
            7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&lazy_paths), expected);

    let possessive_paths = pat(Reluctance::Possessive).paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        58b1ac6a WRAPPED { 42 }
            7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&possessive_paths), expected);
}

#[test]
fn repeat_some_order() {
    let env = wrap_n(Envelope::new("x"), 2);

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        06bb2465 WRAPPED
            70b5f17d cont WRAPPED
                5e85370e cont "x"
    "#}.trim();
    assert_actual_expected!(env.tree_format(), expected);

    let pat = |mode| {
        Pattern::traverse(vec![
            Pattern::repeat(Pattern::unwrap(), 1.., mode),
            Pattern::any_subject(),
        ])
    };

    let greedy_paths = pat(Reluctance::Greedy).paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        06bb2465 WRAPPED { { "x" } }
            70b5f17d WRAPPED { "x" }
                5e85370e LEAF "x"
    "#}.trim();
    assert_actual_expected!(format_paths(&greedy_paths), expected);

    let lazy_paths = pat(Reluctance::Lazy).paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        06bb2465 WRAPPED { { "x" } }
            70b5f17d WRAPPED { "x" }
    "#}.trim();
    assert_actual_expected!(format_paths(&lazy_paths), expected);

    let possessive_paths = pat(Reluctance::Possessive).paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        06bb2465 WRAPPED { { "x" } }
            70b5f17d WRAPPED { "x" }
                5e85370e LEAF "x"
    "#}.trim();
    assert_actual_expected!(format_paths(&possessive_paths), expected);
}

#[test]
fn repeat_range_order() {
    let env = wrap_n(Envelope::new("x"), 4);

    let pat = |mode| {
        Pattern::traverse(vec![
            Pattern::repeat(Pattern::unwrap(), 2..=3, mode),
            Pattern::any_subject(),
        ])
    };

    let greedy_paths = pat(Reluctance::Greedy).paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        88e28c8b WRAPPED { { { { "x" } } } }
            79962374 WRAPPED { { { "x" } } }
                06bb2465 WRAPPED { { "x" } }
                    70b5f17d WRAPPED { "x" }
    "#}.trim();
    assert_actual_expected!(format_paths(&greedy_paths), expected);

    let lazy_paths = pat(Reluctance::Lazy).paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        88e28c8b WRAPPED { { { { "x" } } } }
            79962374 WRAPPED { { { "x" } } }
                06bb2465 WRAPPED { { "x" } }
    "#}.trim();
    assert_actual_expected!(format_paths(&lazy_paths), expected);

    let possessive_paths = pat(Reluctance::Possessive).paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        88e28c8b WRAPPED { { { { "x" } } } }
            79962374 WRAPPED { { { "x" } } }
                06bb2465 WRAPPED { { "x" } }
                    70b5f17d WRAPPED { "x" }
    "#}.trim();
    assert_actual_expected!(format_paths(&possessive_paths), expected);
}

#[test]
#[ignore]
fn test_repeat() {
    // A pattern that matches zero or more `unwrap` elements leading to a
    // `node`.
    let pat = Pattern::parse("(unwrap)*>node").unwrap();

    let env = Envelope::new("Alice");
    // There is no `node` in the envelope, so the pattern should not match.
    assert!(!pat.matches(&env));

    let env = env.add_assertion("knows", "Bob");
    let paths = pat.paths(&env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        8955db5e NODE "Alice" [ "knows": "Bob" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    let wrapped_env = env.wrap();
    let paths = pat.paths(&wrapped_env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        fd881a24 WRAPPED { "Alice" [ "knows": "Bob" ] }
            8955db5e NODE "Alice" [ "knows": "Bob" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // A pattern that matches zero or more `wrapped` elements leading to a
    // `node`.
    let pat = Pattern::parse("(wrapped)*>node").unwrap();
    // Does not match, because even though the `wrapped` part matches, it
    // does not make progress into the wrapped node to get to the `node`.
    assert!(!pat.matches(&wrapped_env));

    let pat = Pattern::parse("@cap((wrapped)*)>unwrap>node").unwrap();
    let (paths, captures) = pat.paths_with_captures(&wrapped_env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        fd881a24 WRAPPED { "Alice" [ "knows": "Bob" ] }
            8955db5e NODE "Alice" [ "knows": "Bob" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
    let caps = captures.get("cap").unwrap();
    assert_eq!(caps.len(), 1);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected_cap = indoc! {r#"
        fd881a24 WRAPPED { "Alice" [ "knows": "Bob" ] }
    "#}.trim();
    assert_actual_expected!(format_paths(caps), expected_cap);

    let wrapped_env = wrapped_env.wrap();
    let pat = Pattern::parse("@cap((wrapped>unwrap)*)>node").unwrap();
    let (paths, captures) = pat.paths_with_captures(&wrapped_env);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        3defda74 WRAPPED { { "Alice" [ "knows": "Bob" ] } }
            fd881a24 WRAPPED { "Alice" [ "knows": "Bob" ] }
                8955db5e NODE "Alice" [ "knows": "Bob" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
    let caps = captures.get("cap").unwrap();
    assert_eq!(caps.len(), 1);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected_cap = indoc! {r#"
        3defda74 WRAPPED { { "Alice" [ "knows": "Bob" ] } }
            fd881a24 WRAPPED { "Alice" [ "knows": "Bob" ] }
    "#}.trim();
    assert_actual_expected!(format_paths(caps), expected_cap);
}

#[test]
fn test_capture() {
    let env = Envelope::new("Alice")
        .add_assertion("knows", "Bob")
        .wrap()
        .wrap();

    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        3defda74 WRAPPED
            fd881a24 cont WRAPPED
                8955db5e cont NODE
                    13941b48 subj "Alice"
                    78d666eb ASSERTION
                        db7dd21c pred "knows"
                        13b74194 obj "Bob"
    "#}.trim();
    assert_actual_expected!(env.tree_format(), expected);

    // Pattern only captures `wrapped` elements leading to a `node`,
    // but not the `node` itself.
    let pat = Pattern::parse("@cap( (wrapped -> unwrap)* ) -> node").unwrap();
    let (paths, captures) = pat.paths_with_captures(&env);
    // Pattern matches the `wrapped` elements leading to the `node`,
    // and the `node` itself.
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        3defda74 WRAPPED { { "Alice" [ "knows": "Bob" ] } }
            fd881a24 WRAPPED { "Alice" [ "knows": "Bob" ] }
                8955db5e NODE "Alice" [ "knows": "Bob" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
    let caps = captures.get("cap").unwrap();
    assert_eq!(caps.len(), 1);
    // The capture contains the `wrapped` elements leading to the `node`, but
    // not the `NODE` itself.
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected_cap = indoc! {r#"
        3defda74 WRAPPED { { "Alice" [ "knows": "Bob" ] } }
            fd881a24 WRAPPED { "Alice" [ "knows": "Bob" ] }
    "#}.trim();
    assert_actual_expected!(format_paths(caps), expected_cap);
}