syster-base 0.1.11-alpha

Core library for SysML v2 and KerML parsing, AST, and semantic analysis
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
//! Tests for the Rowan-based formatter

use super::super::{FormatOptions, format_async};
use tokio_util::sync::CancellationToken;

/// Synchronous format helper for tests
fn format(source: &str, options: &FormatOptions) -> String {
    format_async(source, options, &CancellationToken::new()).unwrap_or_default()
}

/// Assert that formatting produces the expected output (single line)
fn assert_format(input: &str, expected: &str) {
    let result = format(input, &FormatOptions::default());
    assert_eq!(
        result.trim(),
        expected,
        "\nInput:    |{}|\nExpected: |{}|\nGot:      |{}|",
        input,
        expected,
        result.trim()
    );
}

/// Assert multiline formatting produces expected output
fn assert_format_multiline(input: &str, expected: &str) {
    let result = format(input, &FormatOptions::default());
    assert_eq!(
        result.trim(),
        expected.trim(),
        "\n=== Input ===\n{}\n=== Expected ===\n{}\n=== Got ===\n{}",
        input,
        expected.trim(),
        result.trim()
    );
}

// ============================================================================
// Basic formatting tests
// ============================================================================

#[test]
fn test_format_simple_package() {
    assert_format("package Test { }", "package Test { }");
}

#[test]
fn test_format_normalizes_spaces() {
    assert_format("package   Test   {   }", "package Test { }");
}

#[test]
fn test_format_part_definition() {
    assert_format("part def Vehicle { }", "part def Vehicle { }");
    assert_format("part def   Vehicle   {  }", "part def Vehicle { }");
}

#[test]
fn test_format_part_usage() {
    assert_format("part myPart ;", "part myPart ;");
    assert_format("part   myPart  ;", "part myPart ;");
}

#[test]
fn test_format_empty_input() {
    assert_format("", "");
}

#[test]
fn test_format_whitespace_only() {
    let result = format("   \n\n   ", &FormatOptions::default());
    assert!(result.trim().is_empty());
}

// ============================================================================
// Definition tests - verify exact formatting
// ============================================================================

#[test]
fn test_format_action_definition() {
    assert_format("action def   Drive  { }", "action def Drive { }");
}

#[test]
fn test_format_action_usage() {
    assert_format("action   drive  ;", "action drive ;");
}

#[test]
fn test_format_attribute_with_type() {
    assert_format(
        "attribute   speed :   Integer  ;",
        "attribute speed : Integer ;",
    );
}

#[test]
fn test_format_attribute_with_default_value() {
    assert_format(
        "attribute count : Integer = 5 ;",
        "attribute count : Integer = 5 ;",
    );
}

#[test]
fn test_format_const_attribute() {
    assert_format(
        "const  attribute   id : String ;",
        "const attribute id : String ;",
    );
}

#[test]
fn test_format_port_definition() {
    assert_format("port def   FuelPort  { }", "port def FuelPort { }");
}

#[test]
fn test_format_port_usage_in() {
    assert_format("in port   fuel :  FuelPort  ;", "in port fuel : FuelPort ;");
}

#[test]
fn test_format_port_usage_out() {
    assert_format(
        "out   port   exhaust : ExhaustPort ;",
        "out port exhaust : ExhaustPort ;",
    );
}

#[test]
fn test_format_port_usage_inout() {
    assert_format(
        "inout  port  data  : DataPort ;",
        "inout port data : DataPort ;",
    );
}

#[test]
fn test_format_state_definition() {
    assert_format(
        "state def   VehicleState  { }",
        "state def VehicleState { }",
    );
}

#[test]
fn test_format_connection_definition() {
    assert_format(
        "connection def   FuelConnection  { }",
        "connection def FuelConnection { }",
    );
}

#[test]
fn test_format_flow_connection_definition() {
    assert_format(
        "flow connection def   FuelFlow  { }",
        "flow connection def FuelFlow { }",
    );
}

#[test]
fn test_format_requirement_definition() {
    assert_format(
        "requirement def   SafetyReq  { }",
        "requirement def SafetyReq { }",
    );
}

#[test]
fn test_format_constraint_definition() {
    assert_format(
        "constraint def   SpeedLimit  { }",
        "constraint def SpeedLimit { }",
    );
}

#[test]
fn test_format_interface_definition() {
    assert_format(
        "interface def   FuelInterface  { }",
        "interface def FuelInterface { }",
    );
}

#[test]
fn test_format_allocation_definition() {
    assert_format(
        "allocation def   SoftwareAllocation  { }",
        "allocation def SoftwareAllocation { }",
    );
}

#[test]
fn test_format_item_definition() {
    assert_format("item def   Fuel  { }", "item def Fuel { }");
}

#[test]
fn test_format_item_usage() {
    assert_format("item   fuel  :  Fuel  ;", "item fuel : Fuel ;");
}

#[test]
fn test_format_enum_definition() {
    assert_format("enum def   Color  { }", "enum def Color { }");
}

#[test]
fn test_format_use_case_definition() {
    assert_format(
        "use case def   DriveVehicle  { }",
        "use case def DriveVehicle { }",
    );
}

#[test]
fn test_format_view_definition() {
    assert_format("view def   SystemView  { }", "view def SystemView { }");
}

#[test]
fn test_format_viewpoint_definition() {
    assert_format(
        "viewpoint def   SafetyViewpoint  { }",
        "viewpoint def SafetyViewpoint { }",
    );
}

#[test]
fn test_format_rendering_definition() {
    assert_format(
        "rendering def   DiagramRendering  { }",
        "rendering def DiagramRendering { }",
    );
}

#[test]
fn test_format_metadata_definition() {
    assert_format(
        "metadata def   ToolVariable  { }",
        "metadata def ToolVariable { }",
    );
}

#[test]
fn test_format_analysis_definition() {
    assert_format(
        "analysis def   TradeStudy  { }",
        "analysis def TradeStudy { }",
    );
}

#[test]
fn test_format_verification_definition() {
    assert_format(
        "verification def   SafetyTest  { }",
        "verification def SafetyTest { }",
    );
}

#[test]
fn test_format_concern_definition() {
    assert_format("concern def   Safety  { }", "concern def Safety { }");
}

#[test]
fn test_format_calc_definition() {
    assert_format("calc def   Speed  { }", "calc def Speed { }");
}

#[test]
fn test_format_individual_definition() {
    assert_format("individual def   MyCar  { }", "individual def MyCar { }");
}

#[test]
fn test_format_occurrence_definition() {
    assert_format("occurrence def   Event  { }", "occurrence def Event { }");
}

// ============================================================================
// Modifier tests
// ============================================================================

#[test]
fn test_format_abstract_part_def() {
    assert_format(
        "abstract   part def   Vehicle  { }",
        "abstract part def Vehicle { }",
    );
}

#[test]
fn test_format_ref_part() {
    assert_format(
        "ref   part   engine  :  Engine  ;",
        "ref part engine : Engine ;",
    );
}

// ============================================================================
// Relationship tests
// ============================================================================

#[test]
fn test_format_specializes() {
    assert_format(
        "part def Car   specializes   Vehicle  { }",
        "part def Car specializes Vehicle { }",
    );
}

#[test]
fn test_format_subsets() {
    assert_format(
        "part frontWheel   subsets   wheels  ;",
        "part frontWheel subsets wheels ;",
    );
}

#[test]
fn test_format_redefines() {
    assert_format(
        "attribute speed   redefines   baseSpeed  ;",
        "attribute speed redefines baseSpeed ;",
    );
}

// ============================================================================
// Import tests
// ============================================================================

#[test]
fn test_format_import_wildcard() {
    assert_format("import   Pkg :: *  ;", "import Pkg :: * ;");
}

#[test]
fn test_format_import_specific() {
    assert_format("import   Pkg :: Element  ;", "import Pkg :: Element ;");
}

#[test]
fn test_format_import_nested_path() {
    assert_format("import   A :: B :: C :: *  ;", "import A :: B :: C :: * ;");
}

#[test]
fn test_format_import_already_formatted() {
    assert_format("import Pkg :: * ;", "import Pkg :: * ;");
}

// ============================================================================
// Alias tests
// ============================================================================

#[test]
fn test_format_alias() {
    assert_format("alias   V   for   Vehicle  ;", "alias V for Vehicle ;");
}

// ============================================================================
// Comment preservation tests
// ============================================================================

#[test]
fn test_format_preserves_line_comments() {
    assert_format_multiline(
        "// This is a comment\npackage Test { }",
        "// This is a comment\npackage Test { }",
    );
}

#[test]
fn test_format_preserves_block_comments() {
    assert_format_multiline(
        "/* Block comment */\npackage Test { }",
        "/* Block comment */\npackage Test { }",
    );
}

#[test]
fn test_format_preserves_inline_comments() {
    assert_format_multiline(
        "package Test { // inline comment\n}",
        "package Test { // inline comment\n}",
    );
}

#[test]
fn test_format_comment_only() {
    assert_format("// Just a comment", "// Just a comment");
}

#[test]
fn test_format_comment_between_tokens() {
    assert_format(
        "part /* inline */ def Vehicle { }",
        "part /* inline */ def Vehicle { }",
    );
}

#[test]
fn test_format_multiple_line_comments() {
    assert_format_multiline(
        "// Comment 1\n// Comment 2\n// Comment 3\npackage Test { }",
        "// Comment 1\n// Comment 2\n// Comment 3\npackage Test { }",
    );
}

#[test]
fn test_format_trailing_comment_on_brace() {
    assert_format_multiline(
        "package Test { // opening brace comment\n}",
        "package Test { // opening brace comment\n}",
    );
}

#[test]
fn test_format_comment_after_semicolon() {
    assert_format("part x ; // end of part", "part x ; // end of part");
}

#[test]
fn test_format_doc_comment() {
    assert_format_multiline(
        "doc /* Documentation */\npackage Test { }",
        "doc /* Documentation */\npackage Test { }",
    );
}

#[test]
fn test_format_multiline_block_comment() {
    assert_format_multiline(
        "/*\n * Multiline\n * comment\n */\npackage Test { }",
        "/*\n * Multiline\n * comment\n */\npackage Test { }",
    );
}

// ============================================================================
// Brace formatting tests
// ============================================================================

#[test]
fn test_format_brace_on_next_line() {
    assert_format(
        "metadata def ToolVariable\n{",
        "metadata def ToolVariable {",
    );
}

#[test]
fn test_format_brace_with_body() {
    assert_format_multiline(
        "metadata def ToolVariable\n\t{\n\t\tattribute name : String ;\n\t}",
        "metadata def ToolVariable {\n    attribute name : String ;\n}",
    );
}

// ============================================================================
// Multiline formatting tests
// ============================================================================

#[test]
fn test_format_multiline_package_with_parts() {
    assert_format_multiline(
        "package A {\n    part x ;\n    part y ;\n}",
        "package A {\n    part x ;\n    part y ;\n}",
    );
}

#[test]
fn test_format_multiline_enum_with_values() {
    assert_format_multiline(
        "enum def Color {\n    enum Red ;\n    enum Green ;\n    enum Blue ;\n}",
        "enum def Color {\n    enum Red ;\n    enum Green ;\n    enum Blue ;\n}",
    );
}

#[test]
fn test_format_multiline_nested_package() {
    assert_format_multiline(
        "package Outer {\n    package Inner {\n        part x ;\n    }\n}",
        "package Outer {\n    package Inner {\n        part x ;\n    }\n}",
    );
}

#[test]
fn test_format_multiline_part_def_with_attributes() {
    assert_format_multiline(
        "part def Car {\n    attribute wheels : Integer ;\n    attribute color : String ;\n}",
        "part def Car {\n    attribute wheels : Integer ;\n    attribute color : String ;\n}",
    );
}

#[test]
fn test_format_multiline_normalizes_inner_spaces() {
    assert_format_multiline(
        "package A {\n    part   x  :  Type  ;\n}",
        "package A {\n    part x : Type ;\n}",
    );
}

#[test]
fn test_format_multiline_requirement_with_doc() {
    assert_format_multiline(
        "requirement def SafetyReq {\n    doc /* The system shall be safe. */\n}",
        "requirement def SafetyReq {\n    doc /* The system shall be safe. */\n}",
    );
}

// ============================================================================
// Options tests
// ============================================================================

#[test]
fn test_format_options_default() {
    let options = FormatOptions::default();
    assert_eq!(options.tab_size, 4);
    assert!(options.insert_spaces);
    assert_eq!(options.print_width, 80);
}

#[test]
fn test_format_with_tabs_option() {
    let source = "package Test { part x ; }";
    let options = FormatOptions {
        tab_size: 4,
        insert_spaces: false,
        print_width: 80,
    };
    let result = format(source, &options);
    assert_eq!(result.trim(), "package Test { part x ; }");
}

#[test]
fn test_format_multiline_uses_spaces() {
    let source = "package Test {\n    part x ;\n}";
    let result = format(source, &FormatOptions::default());
    // Find indented line
    let indented: Option<&str> = result.lines().find(|l| l.starts_with(' '));
    if let Some(line) = indented {
        assert!(!line.starts_with('\t'), "Should use spaces, not tabs");
    }
}

// ============================================================================
// Edge cases
// ============================================================================

#[test]
fn test_format_package_semicolon() {
    assert_format("package Test ;", "package Test ;");
}

#[test]
fn test_format_preserves_identifier_case() {
    assert_format(
        "part def MyPart_v2_FINAL { }",
        "part def MyPart_v2_FINAL { }",
    );
}

#[test]
fn test_format_unicode_identifiers() {
    assert_format("part def Véhicule { }", "part def Véhicule { }");
}

#[test]
fn test_format_multiplicity() {
    assert_format(
        "attribute count [ 0 .. 10 ] ;",
        "attribute count [ 0 .. 10 ] ;",
    );
}

#[test]
fn test_format_idempotent_single_line() {
    let sources = [
        "package Test { part x ; }",
        "part def Vehicle { }",
        "import Pkg :: * ;",
        "attribute speed : Integer ;",
    ];

    for source in sources {
        let first = format(source, &FormatOptions::default());
        let second = format(&first, &FormatOptions::default());
        assert_eq!(
            first, second,
            "Formatting should be idempotent for: {source}\nFirst:  |{first}|\nSecond: |{second}|"
        );
    }
}

#[test]
fn test_format_idempotent_multiline() {
    let sources = [
        "package A {\n    part x ;\n}",
        "enum def Color {\n    enum Red ;\n}",
        "part def Car {\n    attribute wheels : Integer ;\n}",
    ];

    for source in sources {
        let first = format(source, &FormatOptions::default());
        let second = format(&first, &FormatOptions::default());
        assert_eq!(
            first, second,
            "Formatting should be idempotent for multiline:\n{source}\nFirst:\n{first}\nSecond:\n{second}"
        );
    }
}

// ============================================================================
// Complex/real-world tests
// ============================================================================

#[test]
fn test_format_complex_file() {
    let input = "// File header comment\npackage Vehicle {\n    // Part comment\n    part def Car {\n        attribute wheels : Integer ;\n    }\n    \n    part myCar : Car ;\n}";
    let expected = "// File header comment\npackage Vehicle {\n    // Part comment\n    part def Car {\n        attribute wheels : Integer ;\n    }\n\n    part myCar : Car ;\n}";
    assert_format_multiline(input, expected);
}

#[test]
fn test_format_real_world_example() {
    // Note: formatter normalizes whitespace-only lines to empty lines
    let input = r#"package VehicleSystem {
    import SI :: * ;
    
    // Base vehicle definition
    abstract part def Vehicle {
        attribute mass : MassValue ;
        attribute maxSpeed : SpeedValue ;
        
        port fuelIn : FuelPort ;
    }
    
    part def Car specializes Vehicle {
        attribute wheels : Integer = 4 ;
        part engine : Engine ;
    }
}"#;
    let expected = "package VehicleSystem {\n    import SI :: * ;\n\n    // Base vehicle definition\n    abstract part def Vehicle {\n        attribute mass : MassValue ;\n        attribute maxSpeed : SpeedValue ;\n\n        port fuelIn : FuelPort ;\n    }\n\n    part def Car specializes Vehicle {\n        attribute wheels : Integer = 4 ;\n        part engine : Engine ;\n    }\n}";
    assert_format_multiline(input, expected);
}