-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparser.rs
More file actions
3159 lines (2821 loc) · 92.3 KB
/
Copy pathparser.rs
File metadata and controls
3159 lines (2821 loc) · 92.3 KB
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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use super::*;
/// Test helper: a labelled value (`"label" = value`) with a default span.
fn pair<'i>(label: &'i str, value: Expression<'i>) -> Expression<'i> {
Expression::Pair(Box::new(Pair { label, value }), Span::default())
}
#[test]
fn magic_line() {
let mut input = Parser::new();
input.initialize("% technique v1");
assert!(is_magic_line(input.source));
let result = input.read_magic_line();
assert_eq!(result, Ok(1));
input.initialize("%technique v1");
assert!(is_magic_line(input.source));
let result = input.read_magic_line();
assert_eq!(result, Ok(1));
input.initialize("%techniquev1");
assert!(is_magic_line(input.source));
// this is rejected because the technique keyword isn't present.
let result = input.read_magic_line();
assert!(result.is_err());
}
#[test]
fn magic_line_wrong_keyword_error_position() {
// Test that error position points to the first character of the wrong keyword
assert_eq!(analyze_magic_line("% tecnique v1"), 2); // Points to "t" in "tecnique"
assert_eq!(analyze_magic_line("% tecnique v1"), 3); // Points to "t" in "tecnique" with extra space
assert_eq!(analyze_magic_line("% \ttechniqe v1"), 3); // Points to "t" in "techniqe" with tab
assert_eq!(analyze_magic_line("% wrong v1"), 5); // Points to "w" in "wrong" with multiple spaces
assert_eq!(analyze_magic_line("% foo v1"), 2); // Points to "f" in "foo"
assert_eq!(analyze_magic_line("% TECHNIQUE v1"), 2); // Points to "T" in uppercase "TECHNIQUE"
// Test missing keyword entirely - should point to position after %
assert_eq!(analyze_magic_line("% v1"), 2); // Points to "v" when keyword is missing
assert_eq!(analyze_magic_line("% v1"), 3); // Points to "v" when keyword is missing with space
}
#[test]
fn magic_line_wrong_version_error_position() {
// Test that error position points to the version number after "v" in wrong version strings
assert_eq!(analyze_magic_line("% technique v0"), 13); // Points to "0" in "v0"
assert_eq!(analyze_magic_line("% technique v2"), 14); // Points to "2" in "v2" with extra space
assert_eq!(analyze_magic_line("% technique\tv0"), 13); // Points to "0" in "v0" with tab
assert_eq!(analyze_magic_line("% technique vX"), 15); // Points to "X" in "vX" with multiple spaces
assert_eq!(analyze_magic_line("% technique v99"), 13); // Points to "9" in "v99"
assert_eq!(analyze_magic_line("% technique v0.5"), 15); // Points to "0" in "v0.5" with multiple spaces
// Test edge case where there's no "v" at all - should point to where version should start
assert_eq!(analyze_magic_line("% technique 1.0"), 12); // Points to "1" when there's no "v"
assert_eq!(analyze_magic_line("% technique v1.0"), 14); // Points to "." when there is a "v1" but it has minor version
assert_eq!(analyze_magic_line("% technique 2"), 13); // Points to "2" when there's no "v" with extra space
assert_eq!(analyze_magic_line("% technique beta"), 12); // Points to "b" in "beta" when there's no "v"
}
#[test]
fn header_spdx() {
let mut input = Parser::new();
input.initialize("! PD");
assert!(is_spdx_line(input.source));
let result = input.read_spdx_line();
assert_eq!(result, Ok((Some("PD"), None)));
input.initialize("! MIT; (c) ACME, Inc.");
assert!(is_spdx_line(input.source));
let result = input.read_spdx_line();
assert_eq!(result, Ok((Some("MIT"), Some("ACME, Inc."))));
input.initialize("! MIT; (C) 2024 ACME, Inc.");
assert!(is_spdx_line(input.source));
let result = input.read_spdx_line();
assert_eq!(result, Ok((Some("MIT"), Some("2024 ACME, Inc."))));
input.initialize("! CC BY-SA 3.0 [IGO]; (c) 2024 ACME, Inc.");
assert!(is_spdx_line(input.source));
let result = input.read_spdx_line();
assert_eq!(
result,
Ok((Some("CC BY-SA 3.0 [IGO]"), Some("2024 ACME, Inc.")))
);
}
#[test]
fn header_domain() {
let mut input = Parser::new();
input.initialize("& checklist");
assert!(is_domain_line(input.source));
let result = input.read_domain_line();
assert_eq!(result, Ok(Some("checklist")));
input.initialize("& nasa-esa-iss,v4.0");
assert!(is_domain_line(input.source));
let result = input.read_domain_line();
assert_eq!(result, Ok(Some("nasa-esa-iss,v4.0")));
}
// now we test incremental parsing
#[test]
fn check_not_eof() {
let mut input = Parser::new();
input.initialize("Hello World");
assert!(!input.is_finished());
input.initialize("");
assert!(input.is_finished());
}
#[test]
fn consume_whitespace() {
let mut input = Parser::new();
input.initialize(" hello");
input.trim_whitespace();
assert_eq!(input.source, "hello");
input.initialize("\n \nthere");
input.trim_whitespace();
assert_eq!(input.source, "there");
assert_eq!(input.offset, 3);
}
// It is not clear that we will ever actually need parse_identifier(),
// parse_forma(), parse_genus(), or parse_signature() as they are not
// called directly, but even though they are not used in composition of
// the parse_procedure_declaration() parser, it is highly likely that
// someday we will need to be able to parse them individually, perhaps for
// a future language server or code highlighter. So we test them properly
// here; in any event it exercises the underlying validate_*() codepaths.
#[test]
fn identifier_rules() {
let mut input = Parser::new();
input.initialize("p");
let result = input.read_identifier();
assert_eq!(result, Ok(Identifier::new("p")));
input.initialize("cook_pizza");
let result = input.read_identifier();
assert_eq!(result, Ok(Identifier::new("cook_pizza")));
input.initialize("cook-pizza");
let result = input.read_identifier();
assert!(result.is_err());
}
#[test]
fn signatures() {
let mut input = Parser::new();
input.initialize("A -> B");
let result = input.read_signature();
assert_eq!(
result,
Ok(Signature {
requires: Genus::Single(Forma::new("A")),
provides: Genus::Single(Forma::new("B"))
})
);
input.initialize("Beans -> Coffee");
let result = input.read_signature();
assert_eq!(
result,
Ok(Signature {
requires: Genus::Single(Forma::new("Beans")),
provides: Genus::Single(Forma::new("Coffee"))
})
);
input.initialize("[Bits] -> Bob");
let result = input.read_signature();
assert_eq!(
result,
Ok(Signature {
requires: Genus::List(Forma::new("Bits")),
provides: Genus::Single(Forma::new("Bob"))
})
);
input.initialize("Complex -> (Real, Imaginary)");
let result = input.read_signature();
assert_eq!(
result,
Ok(Signature {
requires: Genus::Single(Forma::new("Complex")),
provides: Genus::Tuple(vec![Forma::new("Real"), Forma::new("Imaginary")])
})
);
}
#[test]
fn signature_wildcards() {
let mut input = Parser::new();
input.initialize("* -> *");
assert_eq!(
input.read_signature(),
Ok(Signature {
requires: Genus::Single(Forma::new("*")),
provides: Genus::Single(Forma::new("*"))
})
);
input.initialize("(A, *) -> [*]");
assert_eq!(
input.read_signature(),
Ok(Signature {
requires: Genus::Tuple(vec![Forma::new("A"), Forma::new("*")]),
provides: Genus::List(Forma::new("*"))
})
);
}
#[test]
fn hole_as_expression() {
// A bare `?` parses as a Hole outside argument position too — in a code
// block or as a binding value, not only inside an invocation's parens.
let mut input = Parser::new();
input.initialize("?");
assert_eq!(
input.read_expression(),
Ok(Expression::Hole(Span::default()))
);
}
#[test]
fn unit_as_expression() {
// Exactly `()` is the unit literal. It must not be confused with a
// function call `name()`, an invocation `<name>()`, or parens that
// enclose whitespace or content.
let mut input = Parser::new();
input.initialize("()");
assert_eq!(
input.read_expression(),
Ok(Expression::Unit(Span::default()))
);
input.initialize("name()");
assert_eq!(
input.read_expression(),
Ok(Expression::Execution(
Function {
target: Identifier::new("name"),
parameters: vec![]
},
Span::default()
))
);
input.initialize("<name>()");
assert_eq!(
input.read_expression(),
Ok(Expression::Application(
Invocation {
target: Target::Local(Identifier::new("name")),
parameters: Some(vec![])
},
Span::default()
))
);
}
#[test]
fn response_as_expression() {
// A single-quoted value is a response literal usable anywhere an
// expression is expected: bare in a code block, or as an argument.
let mut input = Parser::new();
input.initialize("'Monarchy'");
assert_eq!(
input.read_expression(),
Ok(Expression::Response("Monarchy", Span::default()))
);
input.initialize("evaluate('Democracy')");
assert_eq!(
input.read_expression(),
Ok(Expression::Execution(
Function {
target: Identifier::new("evaluate"),
parameters: vec![Expression::Response("Democracy", Span::default())]
},
Span::default()
))
);
}
#[test]
fn cost_as_expression() {
// `$(<expression>)` is a cost literal usable wherever an expression is
// expected: bare in a code block, or as an invocation argument.
let mut input = Parser::new();
input.initialize("$(10 minutes)");
assert_eq!(
input.read_expression(),
Ok(Expression::Cost(
Box::new(Expression::Number(
Numeric::Scientific(Quantity {
mantissa: Decimal {
number: 10,
precision: 0
},
uncertainty: None,
magnitude: None,
symbol: "minutes"
}),
Span::default()
)),
Span::default()
))
);
// The argument is any expression, not just a literal quantity.
input.initialize("$(calculate())");
assert_eq!(
input.read_expression(),
Ok(Expression::Cost(
Box::new(Expression::Execution(
Function {
target: Identifier::new("calculate"),
parameters: vec![]
},
Span::default()
)),
Span::default()
))
);
}
#[test]
fn cost_in_code_block() {
// A cost is a single expression inside braces: `{ $(calculate()) }`.
let mut input = Parser::new();
input.initialize("{ $(calculate()) }");
let result = input.read_code_block();
assert_eq!(
result,
Ok(vec![Expression::Cost(
Box::new(Expression::Execution(
Function {
target: Identifier::new("calculate"),
parameters: vec![]
},
Span::default()
)),
Span::default()
)])
);
}
#[test]
fn declaration_simple() {
let mut input = Parser::new();
input.initialize("making_coffee :");
assert!(is_procedure_declaration(input.source));
let result = input.parse_procedure_declaration();
assert_eq!(result, Ok((Identifier::new("making_coffee"), None, None)));
}
#[test]
fn declaration_full() {
let mut input = Parser::new();
input.initialize("f : A -> B");
assert!(is_procedure_declaration(input.source));
let result = input.parse_procedure_declaration();
assert_eq!(
result,
Ok((
Identifier::new("f"),
None,
Some(Signature {
requires: Genus::Single(Forma::new("A")),
provides: Genus::Single(Forma::new("B"))
})
))
);
input.initialize("making_coffee : (Beans, Milk) -> [Coffee]");
assert!(is_procedure_declaration(input.source));
let result = input.parse_procedure_declaration();
assert_eq!(
result,
Ok((
Identifier::new("making_coffee"),
None,
Some(Signature {
requires: Genus::Tuple(vec![Forma::new("Beans"), Forma::new("Milk")]),
provides: Genus::List(Forma::new("Coffee"))
})
))
);
let content = "f : B";
// we still need to detect procedure declarations with malformed
// signatures; the user's intent will be to declare a procedure though
// it will fail validation in the parser shortly after.
assert!(is_procedure_declaration(content));
let content = r#"
connectivity_check(e,s) : LocalEnvironment, TargetService -> NetworkHealth
"#;
assert!(is_procedure_declaration(content));
}
// At one point we had a bug where parsing was racing ahead and taking too
// much content, which was only uncovered when we expanded to be agnostic
// about whitespace in procedure declarations.
#[test]
fn multiline_declaration() {
let content = r#"
making_coffee (b, m) :
(Beans, Milk)
-> Coffee
And now we will make coffee as follows...
1. Add the beans to the machine
2. Pour in the milk
"#;
assert!(is_procedure_declaration(content));
}
#[test]
fn multiline_signature_parsing() {
let mut input = Parser::new();
let content = r#"
making_coffee :
Ingredients
-> Coffee
"#
.trim_ascii();
input.initialize(content);
let result = input.parse_procedure_declaration();
assert_eq!(
result,
Ok((
Identifier::new("making_coffee"),
None,
Some(Signature {
requires: Genus::Single(Forma::new("Ingredients")),
provides: Genus::Single(Forma::new("Coffee"))
})
))
);
// Test complex multiline signature with parameters and tuple
let content = r#"
making_coffee(b, m) :
(Beans, Milk)
-> Coffee
"#
.trim_ascii();
input.initialize(content);
let result = input.parse_procedure_declaration();
assert_eq!(
result,
Ok((
Identifier::new("making_coffee"),
Some(vec![Identifier::new("b"), Identifier::new("m")]),
Some(Signature {
requires: Genus::Tuple(vec![Forma::new("Beans"), Forma::new("Milk")]),
provides: Genus::Single(Forma::new("Coffee"))
})
))
);
}
#[test]
fn character_delimited_blocks() {
let mut input = Parser::new();
input.initialize("{ todo() }");
let result = input.take_block_chars("inline code", '{', '}', true, |parser| {
let text = parser.source;
assert_eq!(text, " todo() ");
Ok(true)
});
assert_eq!(result, Ok(true));
// this is somewhat contrived as we would not be using this to parse
// strings (We will need to preserve whitespace inside strings when
// we find ourselves parsing them, so subparser() won't work.
input.initialize("XhelloX world");
let result = input.take_block_chars("", 'X', 'X', false, |parser| {
let text = parser.source;
assert_eq!(text, "hello");
Ok(true)
});
assert_eq!(result, Ok(true));
}
#[test]
fn skip_string_content_flag() {
let mut input = Parser::new();
// Test skip_string_content: true - should ignore braces inside strings
input.initialize(r#"{ "string with { brace" }"#);
let result = input.take_block_chars("code block", '{', '}', true, |parser| {
let text = parser.source;
assert_eq!(text, r#" "string with { brace" "#);
Ok(true)
});
assert_eq!(result, Ok(true));
// Test skip_string_content: false - should treat braces normally
input.initialize(r#""string with } brace""#);
let result = input.take_block_chars("string content", '"', '"', false, |parser| {
let text = parser.source;
assert_eq!(text, "string with } brace");
Ok(true)
});
assert_eq!(result, Ok(true));
}
#[test]
fn string_delimited_blocks() {
let mut input = Parser::new();
input.initialize("```bash\nls -l\necho hello```");
assert_eq!(input.offset, 0);
let result = input.take_block_delimited("```", |parser| {
let text = parser.source;
assert_eq!(text, "bash\nls -l\necho hello");
Ok(true)
});
assert_eq!(result, Ok(true));
assert_eq!(input.source, "");
assert_eq!(input.offset, 27);
// Test with different delimiter
input.initialize("---start\ncontent here\nmore content---end");
let result = input.take_block_delimited("---", |parser| {
let text = parser.source;
assert_eq!(text, "start\ncontent here\nmore content");
Ok(true)
});
assert_eq!(result, Ok(true));
// Test with whitespace around delimiters
input.initialize("``` hello world ``` and now goodbye");
let result = input.take_block_delimited("```", |parser| {
let text = parser.source;
assert_eq!(text, " hello world ");
Ok(true)
});
assert_eq!(result, Ok(true));
assert_eq!(input.source, " and now goodbye");
assert_eq!(input.offset, 21);
}
#[test]
fn taking_until() {
let mut input = Parser::new();
// Test take_until() with an identifier up to a limiting character
input.initialize("hello,world");
let result = input.take_until(&[','], |inner| inner.read_identifier());
assert_eq!(result, Ok(Identifier::new("hello")));
assert_eq!(input.source, ",world");
// Test take_until() with whitespace delimiters
input.initialize("test \t\nmore");
let result = input.take_until(&[' ', '\t', '\n'], |inner| inner.read_identifier());
assert_eq!(result, Ok(Identifier::new("test")));
assert_eq!(input.source, " \t\nmore");
// Test take_until() when no delimiter found (it should take everything)
input.initialize("onlytext");
let result = input.take_until(&[',', ';'], |inner| inner.read_identifier());
assert_eq!(result, Ok(Identifier::new("onlytext")));
assert_eq!(input.source, "");
}
#[test]
fn reading_invocations() {
let mut input = Parser::new();
// Test simple invocation without parameters
input.initialize("<hello>");
let result = input.read_invocation();
assert_eq!(
result,
Ok(Invocation {
target: Target::Local(Identifier::new("hello")),
parameters: None
})
);
// Test invocation with empty parameters
input.initialize("<hello_world>()");
let result = input.read_invocation();
assert_eq!(
result,
Ok(Invocation {
target: Target::Local(Identifier::new("hello_world")),
parameters: Some(vec![])
})
);
// Test invocation with multiple parameters
input.initialize("<greetings>(name, title, occupation)");
let result = input.read_invocation();
assert_eq!(
result,
Ok(Invocation {
target: Target::Local(Identifier::new("greetings")),
parameters: Some(vec![
Expression::Variable(Identifier::new("name"), Span::default()),
Expression::Variable(Identifier::new("title"), Span::default()),
Expression::Variable(Identifier::new("occupation"), Span::default())
])
})
);
// A `?` argument is a deferred value: it parses to a Hole in parameter
// position, satisfying the callee's arity without naming a value.
input.initialize("<resume>(?)");
let result = input.read_invocation();
assert_eq!(
result,
Ok(Invocation {
target: Target::Local(Identifier::new("resume")),
parameters: Some(vec![Expression::Hole(Span::default())])
})
);
// We don't have real support for this yet, but syntactically we will
// support the idea of invoking a procedure at an external URL, so we
// have this case as a placeholder.
input.initialize("<https://example.com/proc>");
let result = input.read_invocation();
assert_eq!(
result,
Ok(Invocation {
target: Target::Remote(External::new("https://example.com/proc")),
parameters: None
})
);
// Any scheme reads as external; a `:` cannot occur in a local identifier.
input.initialize("<file://./OtherDoor.tq>");
let result = input.read_invocation();
assert_eq!(
result,
Ok(Invocation {
target: Target::Remote(External::new("file://./OtherDoor.tq")),
parameters: None
})
);
}
#[test]
fn step_detection() {
// Test main dependent steps (whitespace agnostic)
assert!(is_step_dependent("1. First step"));
assert!(is_step_dependent(" 1. Indented step"));
assert!(is_step_dependent("10. Tenth step"));
assert!(!is_step_dependent("a. Letter step"));
assert!(!is_step_dependent("1.No space"));
// Test dependent substeps (whitespace agnostic)
assert!(is_substep_dependent("a. Substep"));
assert!(is_substep_dependent(" a. Indented substep"));
assert!(!is_substep_dependent("2. Substep can't have number"));
assert!(!is_substep_dependent(" 1. Even if it is indented"));
assert!(!is_substep_dependent("i. unus is a sub-substep"));
assert!(!is_substep_dependent("v. quinque is a sub-substep"));
assert!(!is_substep_dependent("x. decem is a sub-substep"));
// Test parallel substeps (whitespace agnostic)
assert!(is_substep_parallel("- Parallel substep"));
assert!(is_substep_parallel(" - Indented parallel"));
assert!(is_substep_parallel(" - Deeper indented"));
assert!(!is_substep_parallel("-No space")); // it's possible we may allow this in the future
assert!(!is_substep_parallel("* Different bullet"));
// Test top-level parallel steps
assert!(is_step_parallel("- Top level parallel"));
assert!(is_step_parallel(" - Indented parallel"));
assert!(is_step("- Top level parallel")); // general step detection
assert!(is_step("1. Numbered step"));
// Test recognition of sub-sub-steps
assert!(is_subsubstep_dependent("i. One"));
assert!(is_subsubstep_dependent(" ii. Two"));
assert!(is_subsubstep_dependent("v. Five"));
assert!(is_subsubstep_dependent("vi. Six"));
assert!(is_subsubstep_dependent("ix. Nine"));
assert!(is_subsubstep_dependent("x. Ten"));
assert!(is_subsubstep_dependent("xi. Eleven"));
assert!(is_subsubstep_dependent("xxxix. Thirty-nine"));
// Test attribute assignments
assert!(is_attribute_assignment("@surgeon"));
assert!(is_attribute_assignment(" @nursing_team"));
assert!(is_attribute_assignment("^kitchen"));
assert!(is_attribute_assignment(" ^garden "));
assert!(is_attribute_assignment("@chef + ^kitchen"));
assert!(is_attribute_assignment("^room1 + @barista"));
assert!(!is_attribute_assignment("surgeon"));
assert!(!is_attribute_assignment("@123invalid"));
assert!(!is_attribute_assignment("^InvalidPlace"));
// Test enum responses
assert!(is_enum_response("'Yes'"));
assert!(is_enum_response(" 'No'"));
assert!(is_enum_response("'Not Applicable'"));
assert!(!is_enum_response("Yes"));
assert!(!is_enum_response("'unclosed"));
}
#[test]
fn read_toplevel_steps() {
let mut input = Parser::new();
// Test simple dependent step
input.initialize("1. First step");
let result = input.read_step_dependent();
assert_eq!(
result,
Ok(Scope::DependentBlock {
ordinal: "1",
description: vec![Paragraph::new(vec![Descriptive::Text("First step")])],
subscopes: vec![],
span: Span::default(),
})
);
// Test simple parallel step
input.initialize(
r#"
- a top-level task to be one in parallel with
- another top-level task
"#,
);
let result = input.read_step_parallel();
assert_eq!(
result,
Ok(Scope::ParallelBlock {
bullet: '-',
description: vec![Paragraph::new(vec![Descriptive::Text(
"a top-level task to be one in parallel with"
)]),],
subscopes: vec![],
span: Span::default(),
})
);
let result = input.read_step_parallel();
assert_eq!(
result,
Ok(Scope::ParallelBlock {
bullet: '-',
description: vec![Paragraph::new(vec![Descriptive::Text(
"another top-level task"
)]),],
subscopes: vec![],
span: Span::default(),
})
);
// Test multi-line dependent step
input.initialize(
r#"
1. Have you done the first thing in the first one?
"#,
);
let result = input.read_step_dependent();
assert_eq!(
result,
Ok(Scope::DependentBlock {
ordinal: "1",
description: vec![Paragraph::new(vec![Descriptive::Text(
"Have you done the first thing in the first one?"
)])],
subscopes: vec![],
span: Span::default(),
})
);
// Test invalid step
input.initialize("Not a step");
let result = input.read_step_dependent();
assert_eq!(result, Err(ParsingError::InvalidStep(Span::new(0, 0))));
}
#[test]
fn reading_substeps_basic() {
let mut input = Parser::new();
// Test simple dependent sub-step
input.initialize("a. First subordinate task");
let result = input.read_substep_dependent();
assert_eq!(
result,
Ok(Scope::DependentBlock {
ordinal: "a",
description: vec![Paragraph::new(vec![Descriptive::Text(
"First subordinate task"
)])],
subscopes: vec![],
span: Span::default(),
})
);
// Test simple parallel sub-step
input.initialize("- Parallel task");
let result = input.read_substep_parallel();
assert_eq!(
result,
Ok(Scope::ParallelBlock {
bullet: '-',
description: vec![Paragraph::new(vec![Descriptive::Text("Parallel task")])],
subscopes: vec![],
span: Span::default(),
})
);
}
#[test]
fn single_step_with_dependent_substeps() {
let mut input = Parser::new();
input.initialize(
r#"
1. Main step
a. First substep
b. Second substep
"#,
);
let result = input.read_step_dependent();
assert_eq!(
result,
Ok(Scope::DependentBlock {
ordinal: "1",
description: vec![Paragraph::new(vec![Descriptive::Text("Main step")])],
subscopes: vec![
Scope::DependentBlock {
ordinal: "a",
description: vec![Paragraph::new(vec![Descriptive::Text("First substep")])],
subscopes: vec![],
span: Span::default(),
},
Scope::DependentBlock {
ordinal: "b",
description: vec![Paragraph::new(vec![Descriptive::Text("Second substep")])],
subscopes: vec![],
span: Span::default(),
},
],
span: Span::default(),
})
);
}
#[test]
fn single_step_with_parallel_substeps() {
let mut input = Parser::new();
input.initialize(
r#"
1. Main step
- First substep
- Second substep
"#,
);
let result = input.read_step_dependent();
assert_eq!(
result,
Ok(Scope::DependentBlock {
ordinal: "1",
description: vec![Paragraph::new(vec![Descriptive::Text("Main step")])],
subscopes: vec![
Scope::ParallelBlock {
bullet: '-',
description: vec![Paragraph::new(vec![Descriptive::Text("First substep")])],
subscopes: vec![],
span: Span::default(),
},
Scope::ParallelBlock {
bullet: '-',
description: vec![Paragraph::new(vec![Descriptive::Text("Second substep")])],
subscopes: vec![],
span: Span::default(),
},
],
span: Span::default(),
})
);
}
#[test]
fn multiple_steps_with_substeps() {
let mut input = Parser::new();
input.initialize(
r#"
1. First step
a. Substep
2. Second step
"#,
);
let first_result = input.read_step_dependent();
let second_result = input.read_step_dependent();
assert_eq!(
first_result,
Ok(Scope::DependentBlock {
ordinal: "1",
description: vec![Paragraph::new(vec![Descriptive::Text("First step")])],
subscopes: vec![Scope::DependentBlock {
ordinal: "a",
description: vec![Paragraph::new(vec![Descriptive::Text("Substep")])],
subscopes: vec![],
span: Span::default(),
}],
span: Span::default(),
})
);
assert_eq!(
second_result,
Ok(Scope::DependentBlock {
ordinal: "2",
description: vec![Paragraph::new(vec![Descriptive::Text("Second step")])],
subscopes: vec![],
span: Span::default(),
})
);
}
#[test]
fn is_step_with_failing_input() {
let test_input = "1. Have you done the first thing in the first one?\n a. Do the first thing. Then ask yourself if you are done:\n 'Yes' | 'No' but I have an excuse\n2. Do the second thing in the first one.";
// Test each line that should be a step
assert!(is_step_dependent(
"1. Have you done the first thing in the first one?"
));
assert!(is_step_dependent(
"2. Do the second thing in the first one."
));
// Test lines that should NOT be steps
assert!(!is_step_dependent(
" a. Do the first thing. Then ask yourself if you are done:"
));
assert!(!is_step_dependent(
" 'Yes' | 'No' but I have an excuse"
));
// Finally, test content over multiple lines
assert!(is_step_dependent(test_input));
}
#[test]
fn read_step_with_content() {
let mut input = Parser::new();
input.initialize(
r#"
1. Have you done the first thing in the first one?
a. Do the first thing. Then ask yourself if you are done:
'Yes' | 'No' but I have an excuse
2. Do the second thing in the first one.
"#,
);
let result = input.read_step_dependent();
// Should parse the complete first step with substeps
assert_eq!(
result,
Ok(Scope::DependentBlock {
ordinal: "1",
description: vec![Paragraph::new(vec![Descriptive::Text(