pascal 0.1.4

A modern Pascal compiler with build/intepreter/package manager built with 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
//! Object-Oriented Programming tests for pascal-rs compiler
//! Tests classes, inheritance, interfaces, and generics

use pascal::Parser;

// Helper to compile and validate OOP features
fn compile_oop_program(source: &str) -> Result<(), String> {
    let mut parser = Parser::new(source);
    parser.parse_program().map_err(|e| format!("Parse error: {:?}", e))?;
    Ok(())
}

// Basic Class Tests

#[test]
fn test_oop_simple_class() {
    let source = r#"
        program Test;
        type
            TMyClass = class
                FValue: integer;
                procedure SetValue(val: integer);
                function GetValue: integer;
            end;
        procedure TMyClass.SetValue(val: integer);
        begin
            FValue := val;
        end;
        function TMyClass.GetValue: integer;
        begin
            Result := FValue;
        end;
        var
            obj: TMyClass;
        begin
            obj := TMyClass.Create;
            obj.SetValue(42);
            writeln(obj.GetValue);
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_class_constructor() {
    let source = r#"
        program Test;
        type
            TPoint = class
                X, Y: integer;
                constructor Create(x, y: integer);
            end;
        constructor TPoint.Create(x, y: integer);
        begin
            inherited Create;
            X := x;
            Y := y;
        end;
        var
            p: TPoint;
        begin
            p := TPoint.Create(10, 20);
            writeln(p.X, ' ', p.Y);
            p.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_class_destructor() {
    let source = r#"
        program Test;
        type
            TMyClass = class
                Data: ^integer;
                constructor Create;
                destructor Destroy; override;
            end;
        constructor TMyClass.Create;
        begin
            inherited Create;
            new(Data);
            Data^ := 42;
        end;
        destructor TMyClass.Destroy;
        begin
            dispose(Data);
            inherited Destroy;
        end;
        var
            obj: TMyClass;
        begin
            obj := TMyClass.Create;
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_private_fields() {
    let source = r#"
        program Test;
        type
            TMyClass = class
            private
                FValue: integer;
            public
                procedure SetValue(val: integer);
                function GetValue: integer;
            end;
        procedure TMyClass.SetValue(val: integer);
        begin
            FValue := val;
        end;
        function TMyClass.GetValue: integer;
        begin
            Result := FValue;
        end;
        var
            obj: TMyClass;
        begin
            obj := TMyClass.Create;
            obj.SetValue(42);
            writeln(obj.GetValue);
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_protected_members() {
    let source = r#"
        program Test;
        type
            TBase = class
            protected
                FProtected: integer;
            public
                constructor Create;
            end;
            TDerived = class(TBase)
            public
                procedure SetProtected(val: integer);
                function GetProtected: integer;
            end;
        constructor TBase.Create;
        begin
            inherited Create;
            FProtected := 10;
        end;
        procedure TDerived.SetProtected(val: integer);
        begin
            FProtected := val;
        end;
        function TDerived.GetProtected: integer;
        begin
            Result := FProtected;
        end;
        var
            obj: TDerived;
        begin
            obj := TDerived.Create;
            obj.SetProtected(20);
            writeln(obj.GetProtected);
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_public_members() {
    let source = r#"
        program Test;
        type
            TMyClass = class
            public
                PublicField: integer;
                constructor Create;
            end;
        constructor TMyClass.Create;
        begin
            inherited Create;
            PublicField := 42;
        end;
        var
            obj: TMyClass;
        begin
            obj := TMyClass.Create;
            writeln(obj.PublicField);
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_published_members() {
    let source = r#"
        program Test;
        type
            TMyClass = class
            published
                PropertyField: integer;
                constructor Create;
            end;
        constructor TMyClass.Create;
        begin
            inherited Create;
            PropertyField := 100;
        end;
        var
            obj: TMyClass;
        begin
            obj := TMyClass.Create;
            writeln(obj.PropertyField);
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

// Inheritance Tests

#[test]
fn test_oop_simple_inheritance() {
    let source = r#"
        program Test;
        type
            TAnimal = class
                Name: string;
                procedure Speak; virtual;
            end;
            TDog = class(TAnimal)
                procedure Speak; override;
            end;
        procedure TAnimal.Speak;
        begin
            writeln('Animal sound');
        end;
        procedure TDog.Speak;
        begin
            writeln('Bark');
        end;
        var
            dog: TDog;
        begin
            dog := TDog.Create;
            dog.Name := 'Buddy';
            dog.Speak;
            dog.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_polymorphism() {
    let source = r#"
        program Test;
        type
            TShape = class
                function GetArea: real; virtual;
            end;
            TCircle = class(TShape)
                Radius: real;
                constructor Create(r: real);
                function GetArea: real; override;
            end;
            TRectangle = class(TShape)
                Width, Height: real;
                constructor Create(w, h: real);
                function GetArea: real; override;
            end;
        function TShape.GetArea: real;
        begin
            Result := 0.0;
        end;
        constructor TCircle.Create(r: real);
        begin
            inherited Create;
            Radius := r;
        end;
        function TCircle.GetArea: real;
        begin
            Result := Pi * Radius * Radius;
        end;
        constructor TRectangle.Create(w, h: real);
        begin
            inherited Create;
            Width := w;
            Height := h;
        end;
        function TRectangle.GetArea: real;
        begin
            Result := Width * Height;
        end;
        var
            shape: TShape;
        begin
            shape := TCircle.Create(5.0);
            writeln(shape.GetArea:0:2);
            shape.Free;
            shape := TRectangle.Create(3.0, 4.0);
            writeln(shape.GetArea:0:2);
            shape.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_inherited_keyword() {
    let source = r#"
        program Test;
        type
            TBase = class
                procedure DoSomething; virtual;
            end;
            TDerived = class(TBase)
                procedure DoSomething; override;
            end;
        procedure TBase.DoSomething;
        begin
            writeln('Base doing something');
        end;
        procedure TDerived.DoSomething;
        begin
            writeln('Derived starting');
            inherited DoSomething;
            writeln('Derived ending');
        end;
        var
            obj: TDerived;
        begin
            obj := TDerived.Create;
            obj.DoSomething;
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

// Method Tests

#[test]
fn test_oop_virtual_methods() {
    let source = r#"
        program Test;
        type
            TBase = class
                procedure VirtualMethod; virtual;
            end;
            TDerived = class(TBase)
                procedure VirtualMethod; override;
            end;
        procedure TBase.VirtualMethod;
        begin
            writeln('Base virtual');
        end;
        procedure TDerived.VirtualMethod;
        begin
            writeln('Derived override');
        end;
        var
            obj: TBase;
        begin
            obj := TDerived.Create;
            obj.VirtualMethod;
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_abstract_methods() {
    let source = r#"
        program Test;
        type
            TShape = class
                function GetArea: real; virtual; abstract;
            end;
            TCircle = class(TShape)
                Radius: real;
                constructor Create(r: real);
                function GetArea: real; override;
            end;
        constructor TCircle.Create(r: real);
        begin
            inherited Create;
            Radius := r;
        end;
        function TCircle.GetArea: real;
        begin
            Result := Pi * Radius * Radius;
        end;
        var
            circle: TCircle;
        begin
            circle := TCircle.Create(5.0);
            writeln(circle.GetArea:0:2);
            circle.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_static_methods() {
    let source = r#"
        program Test;
        type
            TMathUtils = class
                class function Add(a, b: integer): integer;
                class function Multiply(a, b: integer): integer;
            end;
        class function TMathUtils.Add(a, b: integer): integer;
        begin
            Result := a + b;
        end;
        class function TMathUtils.Multiply(a, b: integer): integer;
        begin
            Result := a * b;
        end;
        begin
            writeln(TMathUtils.Add(10, 20));
            writeln(TMathUtils.Multiply(10, 20));
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

// Property Tests

#[test]
fn test_oop_simple_property() {
    let source = r#"
        program Test;
        type
            TMyClass = class
            private
                FValue: integer;
            public
                property Value: integer read FValue write FValue;
            end;
        var
            obj: TMyClass;
        begin
            obj := TMyClass.Create;
            obj.Value := 42;
            writeln(obj.Value);
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_property_with_methods() {
    let source = r#"
        program Test;
        type
            TMyClass = class
            private
                FValue: integer;
                procedure SetValue(val: integer);
                function GetValue: integer;
            public
                property Value: integer read GetValue write SetValue;
            end;
        procedure TMyClass.SetValue(val: integer);
        begin
            FValue := val * 2;
        end;
        function TMyClass.GetValue: integer;
        begin
            Result := FValue;
        end;
        var
            obj: TMyClass;
        begin
            obj := TMyClass.Create;
            obj.Value := 21;
            writeln(obj.Value);
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_readonly_property() {
    let source = r#"
        program Test;
        type
            TMyClass = class
            private
                FReadOnly: integer;
            public
                constructor Create;
                property ReadOnly: integer read FReadOnly;
            end;
        constructor TMyClass.Create;
        begin
            inherited Create;
            FReadOnly := 42;
        end;
        var
            obj: TMyClass;
        begin
            obj := TMyClass.Create;
            writeln(obj.ReadOnly);
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_default_property() {
    let source = r#"
        program Test;
        type
            TIntArray = class
            private
                FData: array[1..10] of integer;
                function GetItem(index: integer): integer;
                procedure SetItem(index: integer; value: integer);
            public
                property Items[index: integer]: integer read GetItem write SetItem; default;
            end;
        function TIntArray.GetItem(index: integer): integer;
        begin
            Result := FData[index];
        end;
        procedure TIntArray.SetItem(index: integer; value: integer);
        begin
            FData[index] := value;
        end;
        var
            arr: TIntArray;
        begin
            arr := TIntArray.Create;
            arr.Items[1] := 42;
            writeln(arr.Items[1]);
            arr.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

// Interface Tests

#[test]
fn test_oop_simple_interface() {
    let source = r#"
        program Test;
        type
            IComparable = interface
                function CompareTo(obj: TObject): integer;
            end;
            TMyClass = class(TObject, IComparable)
                Value: integer;
                function CompareTo(obj: TObject): integer;
            end;
        function TMyClass.CompareTo(obj: TObject): integer;
        begin
            if Value = TMyClass(obj).Value then
                Result := 0
            else if Value < TMyClass(obj).Value then
                Result := -1
            else
                Result := 1;
        end;
        var
            obj1, obj2: TMyClass;
        begin
            obj1 := TMyClass.Create;
            obj2 := TMyClass.Create;
            obj1.Value := 10;
            obj2.Value := 20;
            writeln(obj1.CompareTo(obj2));
            obj1.Free;
            obj2.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_multiple_interfaces() {
    let source = r#"
        program Test;
        type
            IPrintable = interface
                procedure Print;
            end;
            IComparable = interface
                function CompareTo(obj: TObject): integer;
            end;
            TMyClass = class(TObject, IPrintable, IComparable)
                Value: integer;
                procedure Print;
                function CompareTo(obj: TObject): integer;
            end;
        procedure TMyClass.Print;
        begin
            writeln(Value);
        end;
        function TMyClass.CompareTo(obj: TObject): integer;
        begin
            Result := Value - TMyClass(obj).Value;
        end;
        var
            obj: TMyClass;
        begin
            obj := TMyClass.Create;
            obj.Value := 42;
            obj.Print;
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

// Generic Tests

#[test]
fn test_oop_generic_class() {
    let source = r#"
        program Test;
        type
            TPair<T, U> = class
                Key: T;
                Value: U;
                constructor Create(k: T; v: U);
            end;
        constructor TPair<T, U>.Create(k: T; v: U);
        begin
            inherited Create;
            Key := k;
            Value := v;
        end;
        var
            pair: TPair<integer, string>;
        begin
            pair := TPair<integer, string>.Create(1, 'One');
            writeln(pair.Key);
            writeln(pair.Value);
            pair.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_generic_with_constraint() {
    let source = r#"
        program Test;
        type
            TComparable = class
                function CompareTo(other: TComparable): integer; virtual;
            end;
            TList<T: TComparable> = class
                function FindMax: T;
            end;
        function TList<T>.FindMax: T;
        begin
            Result := nil;
        end;
        var
            list: TList<TComparable>;
        begin
            list := TList<TComparable>.Create;
            list.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_generic_method() {
    let source = r#"
        program Test;
        type
            TUtils = class
                class function Swap<T>(var a, b: T);
                class function Max<T>(a, b: T): T;
            end;
        class function TUtils.Swap<T>(var a, b: T);
        var
            temp: T;
        begin
            temp := a;
            a := b;
            b := temp;
        end;
        class function TUtils.Max<T>(a, b: T): T;
        begin
            if a > b then
                Result := a
            else
                Result := b;
        end;
        var
            x, y: integer;
        begin
            x := 10;
            y := 20;
            writeln(TUtils.Max<integer>(x, y));
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

// Operator Overloading Tests

#[test]
fn test_oop_operator_overloading() {
    let source = r#"
        program Test;
        type
            TVector = class
                X, Y: real;
                constructor Create(x, y: real);
                class operator Add(a, b: TVector): TVector;
                class operator Multiply(a: TVector; scalar: real): TVector;
            end;
        constructor TVector.Create(x, y: real);
        begin
            inherited Create;
            X := x;
            Y := y;
        end;
        class operator TVector.Add(a, b: TVector): TVector;
        begin
            Result := TVector.Create(a.X + b.X, a.Y + b.Y);
        end;
        class operator TVector.Multiply(a: TVector; scalar: real): TVector;
        begin
            Result := TVector.Create(a.X * scalar, a.Y * scalar);
        end;
        var
            v1, v2, v3: TVector;
        begin
            v1 := TVector.Create(1.0, 2.0);
            v2 := TVector.Create(3.0, 4.0);
            v3 := v1 + v2;
            writeln(v3.X:0:2, ' ', v3.Y:0:2);
            v1.Free;
            v2.Free;
            v3.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

// Class Reference Tests

#[test]
fn test_oop_class_reference() {
    let source = r#"
        program Test;
        type
            TMyClass = class
                class function GetClassName: string;
            end;
        TMyClassClass = class of TMyClass;
        class function TMyClass.GetClassName: string;
        begin
            Result := 'TMyClass';
        end;
        procedure CreateInstance(cls: TMyClassClass);
        var
            obj: TMyClass;
        begin
            obj := cls.Create;
            writeln(cls.GetClassName);
            obj.Free;
        end;
        begin
            CreateInstance(TMyClass);
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

// Nested Classes Tests

#[test]
fn test_oop_nested_class() {
    let source = r#"
        program Test;
        type
            TOuter = class
            private
                type
                    TInner = class
                        Value: integer;
                    end;
                FInner: TInner;
            public
                constructor Create;
                destructor Destroy; override;
            end;
        constructor TOuter.Create;
        begin
            inherited Create;
            FInner := TInner.Create;
            FInner.Value := 42;
        end;
        destructor TOuter.Destroy;
        begin
            FInner.Free;
            inherited Destroy;
        end;
        var
            outer: TOuter;
        begin
            outer := TOuter.Create;
            outer.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

// Exception Handling in Classes Tests

#[test]
fn test_oop_exception_in_class() {
    let source = r#"
        program Test;
        type
            EMyException = class(Exception)
            end;
            TMyClass = class
                procedure DangerousMethod;
            end;
        procedure TMyClass.DangerousMethod;
        begin
            raise EMyException.Create('Something went wrong');
        end;
        var
            obj: TMyClass;
        begin
            obj := TMyClass.Create;
            try
                obj.DangerousMethod;
            except
                on E: EMyException do
                    writeln(E.Message);
            end;
            obj.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

// Metaclass Tests

#[test]
fn test_oop_metaclass() {
    let source = r#"
        program Test;
        type
            TComponent = class
                constructor Create(AOwner: TComponent); virtual;
            end;
            TComponentClass = class of TComponent;
            TControl = class(TComponent)
            end;
        constructor TComponent.Create(AOwner: TComponent);
        begin
            inherited Create;
        end;
        function CreateComponent(cls: TComponentClass): TComponent;
        begin
            Result := cls.Create(nil);
        end;
        var
            comp: TComponent;
        begin
            comp := CreateComponent(TControl);
            comp.Free;
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

// Advanced OOP Features

#[test]
fn test_oop_class_helper() {
    let source = r#"
        program Test;
        type
            TIntegerHelper = record helper for Integer
                function IsEven: boolean;
                function Double: integer;
            end;
        function TIntegerHelper.IsEven: boolean;
        begin
            Result := (Self mod 2) = 0;
        end;
        function TIntegerHelper.Double: integer;
        begin
            Result := Self * 2;
        end;
        var
            i: integer;
        begin
            i := 21;
            writeln(i.IsEven);
            writeln(i.Double);
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_anonymous_methods() {
    let source = r#"
        program Test;
        type
            TProc = reference to function(x: integer): integer;
        function Apply(func: TProc; value: integer): integer;
        begin
            Result := func(value);
        end;
        begin
            writeln(Apply(function(x: integer): integer
            begin
                Result := x * 2;
            end, 21));
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_class_vars() {
    let source = r#"
        program Test;
        type
            TMyClass = class
            private
                class var
                    FInstanceCount: integer;
            public
                constructor Create;
                destructor Destroy; override;
                class function GetInstanceCount: integer;
            end;
        constructor TMyClass.Create;
        begin
            inherited Create;
            Inc(FInstanceCount);
        end;
        destructor TMyClass.Destroy;
        begin
            Dec(FInstanceCount);
            inherited Destroy;
        end;
        class function TMyClass.GetInstanceCount: integer;
        begin
            Result := FInstanceCount;
        end;
        var
            obj1, obj2: TMyClass;
        begin
            writeln(TMyClass.GetInstanceCount);
            obj1 := TMyClass.Create;
            writeln(TMyClass.GetInstanceCount);
            obj2 := TMyClass.Create;
            writeln(TMyClass.GetInstanceCount);
            obj1.Free;
            writeln(TMyClass.GetInstanceCount);
            obj2.Free;
            writeln(TMyClass.GetInstanceCount);
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}

#[test]
fn test_oop_class_properties() {
    let source = r#"
        program Test;
        type
            TMyClass = class
            private
                class var
                    FDefault: integer;
                class function GetDefault: integer; static;
                class procedure SetDefault(value: integer); static;
            public
                class property Default: integer read GetDefault write SetDefault;
            end;
        class function TMyClass.GetDefault: integer;
        begin
            Result := FDefault;
        end;
        class procedure TMyClass.SetDefault(value: integer);
        begin
            FDefault := value;
        end;
        begin
            TMyClass.Default := 42;
            writeln(TMyClass.Default);
        end.
    "#;

    assert!(compile_oop_program(source).is_ok());
}