rumoca 0.7.28

Modelica compiler written in RUST
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
//! MLS §13: High Priority Package and Import Tests
//!
//! This module tests high priority normative requirements:
//! - §13.1: Package as specialized class restrictions
//! - §13.2: Import statement rules
//! - §13.2.1: Qualified import rules
//! - §13.2.2: Single definition import
//! - §13.2.3: Unqualified import rules
//! - §13.3: Library structure requirements
//!
//! Reference: https://specification.modelica.org/master/packages.html

use crate::spec::{expect_failure, expect_parse_success, expect_success};

// ============================================================================
// §13.1 PACKAGE AS SPECIALIZED CLASS
// ============================================================================

/// MLS §13.1: Package restrictions
mod package_restrictions {
    use super::*;

    /// MLS: "Package cannot have equations"
    #[test]
    #[ignore = "Package equation restriction not yet implemented"]
    fn mls_13_1_package_no_equations() {
        expect_failure(
            r#"
            package Test
                Real x;
            equation
                x = 1;
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Package cannot have algorithm sections"
    #[test]
    #[ignore = "Package algorithm restriction not yet implemented"]
    fn mls_13_1_package_no_algorithm() {
        expect_failure(
            r#"
            package Test
                Real x;
            algorithm
                x := 1;
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Package can only contain class definitions and constants"
    #[test]
    fn mls_13_1_package_with_classes() {
        expect_success(
            r#"
            package Test
                constant Real pi = 3.14159;

                model Inner
                    Real x;
                equation
                    x = 1;
                end Inner;

                function Square
                    input Real x;
                    output Real y;
                algorithm
                    y := x * x;
                end Square;
            end Test;

            model Main
                Test.Inner i;
            equation
            end Main;
            "#,
            "Main",
        );
    }

    /// Valid: Nested packages
    #[test]
    fn mls_13_1_nested_packages() {
        expect_success(
            r#"
            package Outer
                package Inner
                    constant Real c = 1.0;
                end Inner;
            end Outer;

            model Test
                Real x = Outer.Inner.c;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: Package with imports
    #[test]
    fn mls_13_1_package_with_imports() {
        expect_success(
            r#"
            package Lib
                constant Real pi = 3.14159;
            end Lib;

            package Test
                import Lib.pi;
                constant Real twoPi = 2 * pi;
            end Test;

            model Main
                Real x = Test.twoPi;
            equation
            end Main;
            "#,
            "Main",
        );
    }
}

// ============================================================================
// §13.2.1 QUALIFIED IMPORT
// ============================================================================

/// MLS §13.2.1: Qualified import rules
mod qualified_import {
    use super::*;

    /// MLS: "Qualified import must reference existing element"
    #[test]
    fn mls_13_2_1_import_nonexistent() {
        expect_failure(
            r#"
            package Lib
                constant Real pi = 3.14159;
            end Lib;

            model Test
                import Lib.nonexistent;
                Real x = nonexistent;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Qualified import must reference a package"
    #[test]
    #[ignore = "Import package checking not yet implemented"]
    fn mls_13_2_1_import_non_package() {
        expect_failure(
            r#"
            model NotAPackage
                Real x;
            equation
            end NotAPackage;

            model Test
                import NotAPackage.x;
                Real y = x;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: Qualified import of constant
    #[test]
    fn mls_13_2_1_import_constant() {
        expect_success(
            r#"
            package Lib
                constant Real pi = 3.14159;
            end Lib;

            model Test
                import Lib.pi;
                Real x = pi;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: Qualified import of model
    #[test]
    fn mls_13_2_1_import_model() {
        expect_success(
            r#"
            package Lib
                model Component
                    Real x;
                equation
                end Component;
            end Lib;

            model Test
                import Lib.Component;
                Component c;
            equation
            end Test;
            "#,
            "Test",
        );
    }
}

// ============================================================================
// §13.2.2 SINGLE DEFINITION IMPORT (RENAMING)
// ============================================================================

/// MLS §13.2.2: Renaming import rules
mod renaming_import {
    use super::*;

    /// MLS: "Renaming import creates an alias"
    #[test]
    fn mls_13_2_2_renaming_import() {
        expect_success(
            r#"
            package Lib
                constant Real pi = 3.14159;
            end Lib;

            model Test
                import P = Lib.pi;
                Real x = P;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Renaming import for package"
    #[test]
    fn mls_13_2_2_renaming_package() {
        expect_success(
            r#"
            package LongPackageName
                constant Real c = 1.0;
            end LongPackageName;

            model Test
                import L = LongPackageName;
                Real x = L.c;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Alias shadows original name"
    #[test]
    fn mls_13_2_2_alias_shadows() {
        expect_success(
            r#"
            package Lib
                constant Real c = 1.0;
            end Lib;

            model Test
                import MyC = Lib.c;
                Real c = 2.0;
                Real x = MyC;
                Real y = c;
            equation
            end Test;
            "#,
            "Test",
        );
    }
}

// ============================================================================
// §13.2.3 UNQUALIFIED IMPORT
// ============================================================================

/// MLS §13.2.3: Unqualified import rules
mod unqualified_import {
    use super::*;

    /// MLS: "Unqualified import brings all public elements into scope"
    #[test]
    #[ignore = "Unqualified import not fully implemented"]
    fn mls_13_2_3_unqualified_import() {
        expect_success(
            r#"
            package Lib
                constant Real pi = 3.14159;
                constant Real e = 2.71828;
            end Lib;

            model Test
                import Lib.*;
                Real x = pi;
                Real y = e;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Name conflict with unqualified import"
    #[test]
    fn mls_13_2_3_import_conflict() {
        expect_failure(
            r#"
            package Lib1
                constant Real c = 1.0;
            end Lib1;

            package Lib2
                constant Real c = 2.0;
            end Lib2;

            model Test
                import Lib1.*;
                import Lib2.*;
                Real x = c;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Local declaration shadows unqualified import"
    #[test]
    fn mls_13_2_3_local_shadows_import() {
        expect_success(
            r#"
            package Lib
                constant Real c = 1.0;
            end Lib;

            model Test
                import Lib.*;
                Real c = 2.0;
                Real x = c;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: Unqualified import from nested package
    #[test]
    #[ignore = "Unqualified import not fully implemented"]
    fn mls_13_2_3_unqualified_nested() {
        expect_success(
            r#"
            package Outer
                package Inner
                    constant Real c = 1.0;
                    constant Real d = 2.0;
                end Inner;
            end Outer;

            model Test
                import Outer.Inner.*;
                Real x = c;
                Real y = d;
            equation
            end Test;
            "#,
            "Test",
        );
    }
}

// ============================================================================
// §13.2 IMPORT SCOPE AND VISIBILITY
// ============================================================================

/// MLS §13.2: Import scope and visibility rules
mod import_scope {
    use super::*;

    /// MLS: "Import is only visible in the importing class"
    #[test]
    fn mls_13_2_import_local_scope() {
        expect_success(
            r#"
            package Lib
                constant Real c = 1.0;
            end Lib;

            model Outer
                import Lib.c;
                Real x = c;

                model Inner
                    import Lib.c;
                    Real y = c;
                equation
                end Inner;

                Inner i;
            equation
            end Outer;
            "#,
            "Outer",
        );
    }

    /// MLS: "Import in encapsulated class must use global path"
    #[test]
    #[ignore = "encapsulated model syntax not yet supported"]
    fn mls_13_2_encapsulated_import() {
        expect_parse_success(
            r#"
            package Lib
                constant Real c = 1.0;
            end Lib;

            encapsulated model Test
                import .Lib.c;
                Real x = c;
            equation
            end Test;
            "#,
        );
    }

    /// Valid: Multiple imports in same class
    #[test]
    fn mls_13_2_multiple_imports() {
        expect_success(
            r#"
            package Lib1
                constant Real a = 1.0;
            end Lib1;

            package Lib2
                constant Real b = 2.0;
            end Lib2;

            model Test
                import Lib1.a;
                import Lib2.b;
                Real x = a + b;
            equation
            end Test;
            "#,
            "Test",
        );
    }
}

// ============================================================================
// §13.3 LIBRARY STRUCTURE
// ============================================================================

/// MLS §13.3: Library structure requirements
mod library_structure {
    use super::*;

    /// Valid: Package hierarchy
    #[test]
    fn mls_13_3_package_hierarchy() {
        expect_success(
            r#"
            package Root
                package Sub1
                    constant Real c1 = 1.0;
                end Sub1;

                package Sub2
                    constant Real c2 = 2.0;
                end Sub2;
            end Root;

            model Test
                Real x = Root.Sub1.c1 + Root.Sub2.c2;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: Access via fully qualified name
    #[test]
    fn mls_13_3_fully_qualified_access() {
        expect_success(
            r#"
            package Lib
                package Math
                    constant Real pi = 3.14159;
                end Math;
            end Lib;

            model Test
                Real x = .Lib.Math.pi;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: Partial package (for documentation)
    #[test]
    fn mls_13_3_partial_package() {
        expect_parse_success(
            r#"
            partial package Base
                constant Real c = 1.0;
            end Base;

            package Derived
                extends Base;
                constant Real d = 2.0;
            end Derived;
            "#,
        );
    }
}

// ============================================================================
// §13.2 PROTECTED IMPORTS
// ============================================================================

/// MLS §13.2: Protected import behavior
mod protected_imports {
    use super::*;

    /// MLS: "Protected imports are not visible outside the class"
    #[test]
    fn mls_13_2_protected_import() {
        expect_success(
            r#"
            package Lib
                constant Real c = 1.0;
            end Lib;

            model Test
            protected
                import Lib.c;
            public
                Real x = c;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: Public and protected imports
    #[test]
    fn mls_13_2_mixed_visibility() {
        expect_success(
            r#"
            package Lib
                constant Real a = 1.0;
                constant Real b = 2.0;
            end Lib;

            model Test
            public
                import Lib.a;
            protected
                import Lib.b;
            public
                Real x = a + b;
            equation
            end Test;
            "#,
            "Test",
        );
    }
}