statifier 0.1.0

An SCXML statechart interpreter core, a conformant sibling of the Elixir statifier package.
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
use super::finding::{DefaultTransitionOwner, Reason, WarningReason};
use super::{Options, Refused, Warning, validate};
use crate::lowering::lower;
use crate::testing::corpus_documents;
use crate::xml::parse;

const SCXML: &str = "http://www.w3.org/2005/07/scxml";

// `source` read, lowered and validated with `options`.
fn run(source: &str, options: Options) -> Result<Vec<Warning>, Refused> {
    let root = match parse(source.as_bytes()) {
        Ok(root) => root,
        Err(error) => panic!("expected {source:?} to parse, got {}", error.message),
    };
    match lower(&root, source) {
        Ok(document) => validate(&document, source, options),
        Err(errors) => panic!("expected {source:?} to lower, got {errors:#?}"),
    }
}

// A conformant root holding `body`, with `attributes` on it.
fn chart_with(attributes: &str, body: &str) -> String {
    format!("<scxml xmlns=\"{SCXML}\" version=\"1.0\"{attributes}>{body}</scxml>")
}

fn chart(body: &str) -> String {
    chart_with("", body)
}

// The errors `source` is refused with, each as its reason, the text its span
// covers and where that span starts.
fn findings(source: &str) -> Vec<(Reason, String, usize)> {
    match run(source, Options::default()) {
        Ok(_) => panic!("expected {source:?} to be refused"),
        Err(refused) => refused
            .errors
            .into_iter()
            .map(|error| {
                let text = error.location.slice(source).expect("a span in the source");
                (error.reason, text.to_owned(), error.location.start_offset)
            })
            .collect(),
    }
}

// Each error's reason and the text its span covers.
fn refusals(source: &str) -> Vec<(Reason, String)> {
    findings(source)
        .into_iter()
        .map(|(reason, text, _)| (reason, text))
        .collect()
}

fn one(reason: Reason, text: &str) -> Vec<(Reason, String)> {
    vec![(reason, text.to_owned())]
}

fn some(id: &str) -> Option<String> {
    Some(id.to_owned())
}

// ---- ids, targets and initials

// Sabotage: with every id read as a repeat, and this test fails.
#[test]
fn a_repeated_id_is_reported_at_each_later_occurrence_a_data_id_included() {
    let source = chart(r#"<datamodel><data id="x" expr="1"/></datamodel><state id="x"/>"#);
    let found = findings(&source);
    assert_eq!(found.len(), 1, "{found:#?}");
    assert_eq!(found[0].0, Reason::DuplicateId { id: "x".into() });
    assert_eq!(
        found[0].2,
        source.rfind("\"x\"").expect("the state's id") + 1
    );
}

// Sabotage: with an empty id sent to the duplicate set, and this test fails.
#[test]
fn an_empty_id_is_reported_and_two_empty_ids_are_no_duplicate() {
    assert_eq!(
        refusals(&chart(r#"<state id=""/><state id=""/>"#)),
        vec![
            (Reason::EmptyId, String::new()),
            (Reason::EmptyId, String::new())
        ]
    );
}

// Sabotage: with an unresolved target skipped, and this test fails.
#[test]
fn a_transition_target_that_names_no_state_is_reported_at_the_target() {
    assert_eq!(
        refusals(&chart(
            r#"<state id="a"><transition target="a b"/></state>"#
        )),
        one(Reason::UnresolvedTarget { id: "b".into() }, "a b")
    );
}

// Sabotage: with the root's initial left unchecked, and this test fails.
#[test]
fn an_initial_that_names_no_state_is_reported_on_a_state_and_on_the_root() {
    let source = chart_with(
        r#" initial="q""#,
        r#"<state id="a" initial="z"><state id="b"/></state>"#,
    );
    assert_eq!(
        refusals(&source),
        vec![
            (Reason::UnresolvedInitial { id: "q".into() }, "q".into()),
            (Reason::UnresolvedInitial { id: "z".into() }, "z".into()),
        ]
    );
}

// Sabotage: with an initial attribute's target read as always inside its state, and this test fails.
#[test]
fn an_initial_outside_its_state_is_not_a_descendant() {
    let source = chart(
        r#"<state id="a" initial="c"><state id="b"/></state><state><initial><transition target="b"/></initial><state id="c"/></state>"#,
    );
    assert_eq!(
        refusals(&source),
        vec![
            (
                Reason::InitialNotDescendant {
                    id: "c".into(),
                    parent_id: some("a"),
                },
                "c".into()
            ),
            (
                Reason::InitialNotDescendant {
                    id: "b".into(),
                    parent_id: None,
                },
                "b".into()
            ),
        ]
    );
}

// Sabotage: with a parallel counted as able to take an initial, and this test fails.
#[test]
fn an_initial_on_a_state_with_nothing_to_enter_is_reported_once() {
    assert_eq!(
        refusals(&chart(
            r#"<state id="a" initial="nowhere"/><parallel id="p" initial="x"><state id="x"/></parallel>"#
        )),
        vec![
            (
                Reason::InitialOnAtomicState { id: some("a") },
                "nowhere".into()
            ),
            (Reason::InitialOnAtomicState { id: some("p") }, "x".into()),
        ]
    );
}

// Sabotage: with the attribute-and-element check never firing, and this test fails.
#[test]
fn an_initial_element_beside_an_initial_attribute_is_reported() {
    let initial = r#"<initial><transition target="b"/></initial>"#;
    let source = chart(&format!(
        r#"<state id="a" initial="b">{initial}<state id="b"/></state>"#
    ));
    assert_eq!(
        refusals(&source),
        one(
            Reason::InitialAttributeAndElement { id: some("a") },
            initial
        )
    );
}

// Sabotage: with the count check firing only on two or more, and this test fails.
#[test]
fn an_initial_element_with_no_transition_has_the_wrong_count() {
    assert_eq!(
        refusals(&chart(r#"<state id="a"><initial/><state id="b"/></state>"#)),
        one(
            Reason::TransitionCount {
                owner: DefaultTransitionOwner::Initial(some("a")),
                count: 0,
            },
            "<initial/>"
        )
    );
}

// Sabotage: with a missing target accepted, and this test fails.
#[test]
fn a_default_transition_without_a_target_is_reported() {
    assert_eq!(
        refusals(&chart(
            r#"<state id="a"><initial><transition target=""/></initial><state id="b"/></state>"#
        )),
        one(
            Reason::TransitionMissingTarget {
                owner: DefaultTransitionOwner::Initial(some("a")),
            },
            r#"<transition target=""/>"#
        )
    );
}

// Sabotage: with a written event read from the event list, which an empty one leaves empty, and this test fails.
#[test]
fn a_default_transition_with_event_or_cond_is_reported_at_each() {
    let owner = DefaultTransitionOwner::Initial(some("a"));
    assert_eq!(
        refusals(&chart(
            r#"<state id="a"><initial><transition event="" cond="c1" target="b"/></initial><state id="b"/></state>"#
        )),
        vec![
            (
                Reason::TransitionForbiddenAttribute {
                    owner: owner.clone(),
                    attribute: "event",
                },
                String::new()
            ),
            (
                Reason::TransitionForbiddenAttribute {
                    owner,
                    attribute: "cond",
                },
                "c1".into()
            ),
        ]
    );
}

// ---- histories, finals and default entry

// Sabotage: with the root counted as a compound parent, and this test fails.
#[test]
fn a_history_outside_a_compound_state_is_reported() {
    let history = r#"<history id="h"><transition target="a"/></history>"#;
    assert_eq!(
        refusals(&chart(&format!(r#"{history}<state id="a"/>"#))),
        one(
            Reason::HistoryBadParent {
                id: some("h"),
                parent_kind: "scxml",
            },
            history
        )
    );
}

// Sabotage: with the descendancy test against the history's parent inverted, and this test fails.
#[test]
fn a_history_default_target_outside_its_parent_is_not_a_descendant() {
    assert_eq!(
        refusals(&chart(
            r#"<state id="p"><state id="a"/><history id="h"><transition target="o"/></history></state><state id="o"/>"#
        )),
        one(
            Reason::InitialNotDescendant {
                id: "o".into(),
                parent_id: some("p"),
            },
            "o"
        )
    );
}

// Sabotage: with `sideways` added to the history types read, and this test fails.
#[test]
fn a_history_type_out_of_range_is_reported_as_written() {
    assert_eq!(
        refusals(&chart(
            r#"<state id="p"><state id="a"/><history id="h" type="sideways"><transition target="a"/></history></state>"#
        )),
        one(
            Reason::HistoryBadType {
                raw: "sideways".into()
            },
            "sideways"
        )
    );
}

// Sabotage: with a final's first state child skipped, and this test fails.
#[test]
fn a_state_inside_a_final_is_reported_at_the_child() {
    assert_eq!(
        refusals(&chart(r#"<final id="f"><state id="x"/></final>"#)),
        one(
            Reason::FinalHasStates { id: some("x") },
            r#"<state id="x"/>"#
        )
    );
}

// Sabotage: with a final's first transition skipped, and this test fails.
#[test]
fn a_transition_inside_a_final_is_reported_at_the_transition() {
    assert_eq!(
        refusals(&chart(r#"<final id="f"><transition target="f"/></final>"#)),
        one(
            Reason::FinalHasTransitions { id: some("f") },
            r#"<transition target="f"/>"#
        )
    );
}

// Sabotage: with the first final child's id replaced by none, and this test fails.
#[test]
fn a_state_holding_a_final_without_an_id_is_reported_once() {
    let state = r#"<state><final id="f"/><final id="g"/></state>"#;
    assert_eq!(
        refusals(&chart(state)),
        one(
            Reason::FinalParentMissingId {
                final_id: some("f"),
            },
            state
        )
    );
}

// Sabotage: with a parallel held to default entry too, and this test fails.
#[test]
fn a_state_entered_at_a_leading_history_is_reported_and_a_parallel_is_not() {
    let history = r#"<history id="h"><transition target="a"/></history>"#;
    let source = chart(&format!(
        r#"<state id="p">{history}<state id="a"/></state><parallel id="q"><history id="g"><transition target="b"/></history><state id="b"/></parallel>"#
    ));
    assert_eq!(
        refusals(&source),
        one(
            Reason::DefaultEntryNotEnterable {
                id: some("p"),
                child_kind: "history",
            },
            history
        )
    );
}

// ---- data and the root

// Sabotage: with a final counted as a state that may not carry donedata, and this test fails.
#[test]
fn donedata_on_a_state_that_is_not_final_is_reported() {
    let donedata = r#"<donedata><param name="n" expr="1"/></donedata>"#;
    assert_eq!(
        refusals(&chart(&format!(
            r#"<state id="a">{donedata}</state><final id="f">{donedata}</final>"#
        ))),
        one(Reason::DonedataNotOnFinal { id: some("a") }, donedata)
    );
}

// Sabotage: with the content half of the pair inverted, and this test fails.
#[test]
fn donedata_with_content_and_params_is_reported() {
    let donedata = r#"<donedata><content expr="1"/><param name="n" expr="1"/></donedata>"#;
    assert_eq!(
        refusals(&chart(&format!(r#"<final id="f">{donedata}</final>"#))),
        one(Reason::DonedataContentAndParams { id: some("f") }, donedata)
    );
}

// Sabotage: with blank text read as content, and this test fails.
#[test]
fn content_with_expr_and_inline_content_is_reported_with_its_form() {
    let source = chart(
        r#"<final id="f"><donedata><content expr="1">
  </content></donedata></final><state id="a"><invoke><content expr="2">x</content></invoke><invoke><content expr="3"><b/></content></invoke></state>"#,
    );
    let errors = run(&source, Options::default())
        .expect_err("refused")
        .errors;
    let found: Vec<(Reason, &str)> = errors
        .iter()
        .map(|error| (error.reason.clone(), error.message.as_str()))
        .collect();
    assert_eq!(
        found,
        vec![
            (
                Reason::ContentExprAndText { expr: "2".into() },
                "<content> must not specify both an expr attribute (\"2\") and inline text"
            ),
            (
                Reason::ContentExprAndText { expr: "3".into() },
                "<content> must not specify both an expr attribute (\"3\") and inline markup"
            ),
        ]
    );
}

// Sabotage: with a param holding both accepted, and this test fails.
#[test]
fn a_param_with_expr_and_location_is_reported() {
    let param = r#"<param name="n" expr="1" location="v"/>"#;
    assert_eq!(
        refusals(&chart(&format!(
            r#"<final id="f"><donedata>{param}</donedata></final>"#
        ))),
        one(Reason::ParamExprAndLocation { name: "n".into() }, param)
    );
}

// Sabotage: with the send params left out of the walk, and this test fails.
#[test]
fn a_param_with_no_value_is_reported_under_a_send_too() {
    let param = r#"<param name="n"/>"#;
    assert_eq!(
        refusals(&chart(&format!(
            r#"<state id="a"><onentry><foreach array="v" item="i"><send event="e">{param}</send></foreach></onentry></state>"#
        ))),
        one(Reason::ParamNoValue { name: "n".into() }, param)
    );
}

// Sabotage: with the relaxed mode ignored, and this test fails.
#[test]
fn a_root_with_no_namespace_is_reported_unless_invoke_content_markup_is_on() {
    let source = r#"<scxml version="1.0"><state id="a"/></scxml>"#;
    assert_eq!(
        refusals(source),
        one(Reason::BadNamespace { uri: None }, source)
    );
    let relaxed = Options {
        invoke_content_markup: true,
    };
    assert_eq!(run(source, relaxed), Ok(Vec::new()));
}

// Sabotage: with an absent version accepted, and this test fails.
#[test]
fn a_root_version_other_than_one_point_oh_is_reported() {
    let absent = format!(r#"<scxml xmlns="{SCXML}"><state id="a"/></scxml>"#);
    assert_eq!(
        refusals(&absent),
        one(Reason::BadVersion { version: None }, &absent)
    );
    assert_eq!(
        refusals(&format!(r#"<scxml xmlns="{SCXML}" version="2.0"/>"#)),
        one(
            Reason::BadVersion {
                version: some("2.0")
            },
            "2.0"
        )
    );
}

// Sabotage: with `sideways` added to the transition types read, and this test fails.
#[test]
fn a_transition_type_out_of_range_is_reported_as_written() {
    assert_eq!(
        refusals(&chart(
            r#"<state id="a"><transition type="sideways" target="a"/></state>"#
        )),
        one(
            Reason::TransitionBadType {
                raw: "sideways".into()
            },
            "sideways"
        )
    );
}

// Sabotage: with `whenever` added to the bindings read, and this test fails.
#[test]
fn a_binding_out_of_range_is_reported_as_written() {
    assert_eq!(
        refusals(&chart_with(r#" binding="whenever""#, "")),
        one(
            Reason::ScxmlBadBinding {
                raw: "whenever".into()
            },
            "whenever"
        )
    );
}

// Sabotage: with `javascript` added to the datamodels read, and this test fails.
#[test]
fn a_datamodel_out_of_range_is_reported_as_written() {
    assert_eq!(
        refusals(&chart_with(r#" datamodel="javascript""#, "")),
        one(
            Reason::ScxmlBadDatamodel {
                raw: "javascript".into()
            },
            "javascript"
        )
    );
}

// Sabotage: with `yes` added to the autoforward values read, and this test fails.
#[test]
fn an_autoforward_out_of_range_is_reported_as_written() {
    assert_eq!(
        refusals(&chart(
            r#"<state id="a"><invoke autoforward="yes"/></state>"#
        )),
        one(Reason::InvokeBadAutoforward { raw: "yes".into() }, "yes")
    );
}

// Sabotage: with the src half of the pair inverted, and this test fails.
#[test]
fn data_with_expr_and_src_is_reported() {
    assert_eq!(
        refusals(&chart(
            r#"<datamodel><data id="d" expr="1" src="s"/></datamodel>"#
        )),
        one(Reason::DataExprAndSrc { id: "d".into() }, "d")
    );
}

// Sabotage: with a data's child text left unread, and this test fails.
#[test]
fn data_with_a_value_and_child_text_is_reported() {
    assert_eq!(
        refusals(&chart(
            r#"<state id="a"><datamodel><data id="d" expr="1">2</data></datamodel></state>"#
        )),
        one(Reason::DataValueAndChildren { id: "d".into() }, "d")
    );
}

// Sabotage: with the reserved prefix read as `$`, and this test fails.
#[test]
fn a_data_id_starting_with_an_underscore_is_reported() {
    assert_eq!(
        refusals(&chart(r#"<datamodel><data id="_d"/></datamodel>"#)),
        one(Reason::DataReservedId { id: "_d".into() }, "_d")
    );
}

// Sabotage: with a final allowed a datamodel, and this test fails.
#[test]
fn a_datamodel_on_a_final_is_reported() {
    let datamodel = r#"<datamodel><data id="d"/></datamodel>"#;
    assert_eq!(
        refusals(&chart(&format!(r#"<final id="f">{datamodel}</final>"#))),
        one(Reason::DatamodelBadParent { kind: "final" }, datamodel)
    );
}

// ---- executable content

// Sabotage: with the walk not descending into an `<if>`, and this test fails.
#[test]
fn an_assign_with_expr_and_children_is_reported_inside_an_if() {
    let assign = r#"<assign location="v" expr="1">2</assign>"#;
    assert_eq!(
        refusals(&chart(&format!(
            r#"<state id="a"><onentry><if cond="c">{assign}</if></onentry></state>"#
        ))),
        one(Reason::AssignExprAndText { expr: "1".into() }, assign)
    );
}

// Sabotage: with an elseif after an else read as a duplicate else, and this test fails.
#[test]
fn an_elseif_after_an_else_is_reported() {
    assert_eq!(
        refusals(&chart(
            r#"<state id="a"><transition target="a"><if cond="c"><else/><elseif cond="d"/></if></transition></state>"#
        )),
        one(Reason::IfElseifAfterElse, r#"<elseif cond="d"/>"#)
    );
}

// Sabotage: with the else already seen left unset, and this test fails.
#[test]
fn a_second_else_is_reported_at_the_second() {
    let source = chart(
        r#"<state id="a"><onexit><if cond="c"><if cond="d"><else/><else/></if></if></onexit></state>"#,
    );
    let found = findings(&source);
    assert_eq!(found.len(), 1, "{found:#?}");
    assert_eq!(found[0].0, Reason::IfDuplicateElse);
    assert_eq!(
        found[0].2,
        source.rfind("<else/>").expect("the second else")
    );
}

// Sabotage: with the root's scripts left out, and this test fails.
#[test]
fn a_script_with_no_text_is_reported_in_a_state_and_under_the_root() {
    assert_eq!(
        refusals(&chart(
            r#"<state id="a"><onentry><script>  </script></onentry></state><script/>"#
        )),
        vec![
            (Reason::ScriptNoSrcOrText, "<script>  </script>".into()),
            (Reason::ScriptNoSrcOrText, "<script/>".into()),
        ]
    );
}

// Sabotage: with the invoke type pair read from `type` alone, and this test fails.
#[test]
fn an_invoke_with_type_and_typeexpr_is_reported() {
    let invoke = r#"<invoke type="t" typeexpr="x"/>"#;
    assert_eq!(
        refusals(&chart(&format!(
            r#"<state id="a">{invoke}<invoke type="t"/></state>"#
        ))),
        one(Reason::InvokeTypeAndTypeexpr, invoke)
    );
}

// Sabotage: with the invoke src pair removed, and this test fails.
#[test]
fn an_invoke_with_src_and_srcexpr_is_reported() {
    let invoke = r#"<invoke src="s" srcexpr="x"/>"#;
    assert_eq!(
        refusals(&chart(&format!(r#"<state id="a">{invoke}</state>"#))),
        one(Reason::InvokeSrcAndSrcexpr, invoke)
    );
}

// Sabotage: with a srcexpr beside content accepted, and this test fails.
#[test]
fn an_invoke_with_a_source_and_content_is_reported() {
    let invoke = r#"<invoke srcexpr="x"><content>c</content></invoke>"#;
    assert_eq!(
        refusals(&chart(&format!(r#"<state id="a">{invoke}</state>"#))),
        one(Reason::InvokeSrcAndContent, invoke)
    );
}

// Sabotage: with the invoke id pair removed, and this test fails.
#[test]
fn an_invoke_with_id_and_idlocation_is_reported() {
    let invoke = r#"<invoke id="i" idlocation="v"/>"#;
    assert_eq!(
        refusals(&chart(&format!(r#"<state id="a">{invoke}</state>"#))),
        one(Reason::InvokeIdAndIdlocation, invoke)
    );
}

// Sabotage: with the invoke namelist pair removed, and this test fails.
#[test]
fn an_invoke_with_namelist_and_param_is_reported() {
    let invoke = r#"<invoke namelist="v"><param name="n" expr="1"/></invoke>"#;
    assert_eq!(
        refusals(&chart(&format!(r#"<state id="a">{invoke}</state>"#))),
        one(Reason::InvokeNamelistAndParam, invoke)
    );
}

// A `<send>` in an `<onentry>` of one state, answering the errors.
fn send_refusals(send: &str) -> Vec<(Reason, String)> {
    refusals(&chart(&format!(
        r#"<state id="a"><onentry>{send}</onentry></state>"#
    )))
}

// Sabotage: with the send event pair removed, and this test fails.
#[test]
fn a_send_with_event_and_eventexpr_is_reported() {
    let send = r#"<send event="e" eventexpr="x"/>"#;
    assert_eq!(
        send_refusals(send),
        one(Reason::SendEventAndEventexpr, send)
    );
}

// Sabotage: with the send target pair removed, and this test fails.
#[test]
fn a_send_with_target_and_targetexpr_is_reported() {
    let send = r#"<send event="e" target="t" targetexpr="x"/>"#;
    assert_eq!(
        send_refusals(send),
        one(Reason::SendTargetAndTargetexpr, send)
    );
}

// Sabotage: with the send type pair removed, and this test fails.
#[test]
fn a_send_with_type_and_typeexpr_is_reported() {
    let send = r#"<send event="e" type="t" typeexpr="x"/>"#;
    assert_eq!(send_refusals(send), one(Reason::SendTypeAndTypeexpr, send));
}

// Sabotage: with the send id pair removed, and this test fails.
#[test]
fn a_send_with_id_and_idlocation_is_reported() {
    let send = r#"<send event="e" id="i" idlocation="v"/>"#;
    assert_eq!(send_refusals(send), one(Reason::SendIdAndIdlocation, send));
}

// Sabotage: with the send delay pair removed, and this test fails.
#[test]
fn a_send_with_delay_and_delayexpr_is_reported() {
    let send = r#"<send event="e" delay="1s" delayexpr="x"/>"#;
    assert_eq!(
        send_refusals(send),
        one(Reason::SendDelayAndDelayexpr, send)
    );
}

// Sabotage: with `#_internal` left out of the internal target, and this test fails.
#[test]
fn a_delayed_send_to_the_internal_target_is_reported_in_both_spellings() {
    let plain = r#"<send event="e" target="_internal" delay="1s"/>"#;
    let hashed = r##"<send event="e" target="#_internal" delayexpr="x"/>"##;
    assert_eq!(
        send_refusals(&format!("{plain}{hashed}")),
        vec![
            (Reason::SendDelayAndInternalTarget, plain.into()),
            (Reason::SendDelayAndInternalTarget, hashed.into()),
        ]
    );
}

// Sabotage: with the send namelist pair removed, and this test fails.
#[test]
fn a_send_with_namelist_and_content_is_reported() {
    let send = r#"<send event="e" namelist="v"><content>c</content></send>"#;
    assert_eq!(
        send_refusals(send),
        one(Reason::SendNamelistAndContent, send)
    );
}

// Sabotage: with the send param pair removed, and this test fails.
#[test]
fn a_send_with_param_and_content_is_reported() {
    let send = r#"<send event="e"><param name="n" expr="1"/><content>c</content></send>"#;
    assert_eq!(send_refusals(send), one(Reason::SendParamAndContent, send));
}

// Sabotage: with a cancel holding both accepted, and this test fails.
#[test]
fn a_cancel_with_sendid_and_sendidexpr_is_reported() {
    let cancel = r#"<cancel sendid="s" sendidexpr="x"/>"#;
    assert_eq!(
        send_refusals(cancel),
        one(Reason::CancelSendidAndSendidexpr, cancel)
    );
}

// Sabotage: with the finalize blocks left out of the cancel walk, and this test fails.
#[test]
fn a_cancel_with_no_sendid_is_reported_inside_finalize_too() {
    assert_eq!(
        refusals(&chart(
            r#"<state id="a"><invoke><finalize><cancel/></finalize></invoke></state>"#
        )),
        one(Reason::CancelNoSendid, "<cancel/>")
    );
}

// ---- the markup

// Sabotage: with the work list not descending into children, and this test fails.
#[test]
fn a_repeated_attribute_is_reported_at_each_repeat_in_content_markup_too() {
    let source = chart(
        r#"<state id="a" id="b"><invoke><content><x k="1" k="2"/></content></invoke></state>"#,
    );
    assert_eq!(
        refusals(&source),
        vec![
            (
                Reason::DuplicateAttribute {
                    element: "state".into(),
                    attribute: "id".into(),
                },
                r#"id="b""#.into()
            ),
            (
                Reason::DuplicateAttribute {
                    element: "x".into(),
                    attribute: "k".into(),
                },
                r#"k="2""#.into()
            ),
        ]
    );
}

// ---- the warning and the contracts

// Sabotage: with the finalize content read without descending into an `<if>`, and this test fails.
#[test]
fn a_raise_or_send_inside_finalize_is_a_warning_and_the_document_validates() {
    let raise = r#"<raise event="e"/>"#;
    let send = r#"<send event="e"/>"#;
    let source = chart(&format!(
        r#"<state id="a"><invoke><finalize>{raise}<if cond="c">{send}</if></finalize></invoke></state>"#
    ));
    let warnings = run(&source, Options::default()).expect("the document validates");
    let found: Vec<(WarningReason, &str, &str)> = warnings
        .iter()
        .map(|warning| {
            let text = warning.location.slice(&source).expect("a span");
            (warning.reason.clone(), text, warning.message.as_str())
        })
        .collect();
    assert_eq!(
        found,
        vec![
            (
                WarningReason::FinalizeForbiddenContent { element: "raise" },
                raise,
                "<raise> must not occur inside <finalize>"
            ),
            (
                WarningReason::FinalizeForbiddenContent { element: "send" },
                send,
                "<send> must not occur inside <finalize>"
            ),
        ]
    );
}

// Sabotage: with the warnings dropped when an error fires, and this test fails.
#[test]
fn a_refused_document_still_answers_its_warnings() {
    let source = chart(
        r#"<state id="a"><transition target="z"/><invoke><finalize><raise event="e"/></finalize></invoke></state>"#,
    );
    let refused = run(&source, Options::default()).expect_err("refused");
    assert_eq!(refused.errors.len(), 1, "{refused:#?}");
    assert_eq!(refused.warnings.len(), 1, "{refused:#?}");
}

// Sabotage: with the errors left in the order the checks ran, and this test fails.
#[test]
fn every_check_runs_and_the_errors_come_in_document_order_check_order_at_one_offset() {
    let invoke = r#"<invoke type="t" typeexpr="x" src="s" srcexpr="y"/>"#;
    let source = chart(&format!(
        r#"<state id="a">{invoke}<transition target="z"/></state><state id="a"/>"#
    ));
    let reasons: Vec<Reason> = findings(&source)
        .into_iter()
        .map(|(reason, ..)| reason)
        .collect();
    assert_eq!(
        reasons,
        vec![
            Reason::InvokeTypeAndTypeexpr,
            Reason::InvokeSrcAndSrcexpr,
            Reason::UnresolvedTarget { id: "z".into() },
            Reason::DuplicateId { id: "a".into() },
        ]
    );
}

// Sabotage: with an absent id printed as `null`, and this test fails.
#[test]
fn a_message_reads_as_the_reference_writes_it() {
    let source = chart(r#"<history><transition target="a"/></history><state id="a"/>"#);
    let errors = run(&source, Options::default())
        .expect_err("refused")
        .errors;
    assert_eq!(
        errors[0].message,
        "history nil must be a child of a compound <state> or <parallel>, not scxml"
    );
}

// Sabotage: with every transition target reported as unresolved, and this test fails.
#[test]
fn every_corpus_document_validates() {
    let documents = corpus_documents();
    let refused: Vec<String> = documents
        .iter()
        .filter_map(|(name, bytes)| {
            let source = std::str::from_utf8(bytes).expect("UTF-8");
            let root = parse(bytes).expect("a corpus document parses");
            let document = lower(&root, source).expect("a corpus document lowers");
            validate(&document, source, Options::default())
                .err()
                .map(|refused| format!("{name}: {:#?}", refused.errors))
        })
        .collect();
    println!("corpus documents validated: {}", documents.len());
    assert!(documents.len() > 300, "{}", documents.len());
    assert!(refused.is_empty(), "{refused:#?}");
}