dol 0.8.1

DOL (Design Ontology Language) - A declarative specification language for ontology-first development
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
//! DOL 2.0 Integration Tests
//!
//! End-to-end tests for new DOL 2.0 syntax including functional programming
//! features, control flow, pattern matching, and meta-programming constructs.

use metadol::ast::{BinaryOp, Block, Declaration, Expr, Pattern, Stmt, TypeExpr};
use metadol::parser::Parser;

// ============================================
// Complete DOL 2.0 Gene Tests
// ============================================

#[test]
fn test_full_dol2_gene_with_expressions() {
    let input = r#"
gene functional.transform {
  container has identity
  transform is pipeline
}

exegesis {
  A gene demonstrating DOL 2.0 functional features.
}
"#;
    let mut parser = Parser::new(input);
    let result = parser.parse();
    assert!(result.is_ok(), "Parse error: {:?}", result.err());

    match result.unwrap() {
        Declaration::Gene(gene) => {
            assert_eq!(gene.name, "functional.transform");
            assert_eq!(gene.statements.len(), 2);
        }
        _ => panic!("Expected Gene declaration"),
    }
}

// ============================================
// Backward Compatibility Tests
// ============================================

#[test]
fn test_backward_compatibility_basic_gene() {
    let input = r#"
gene container.exists {
  container has identity
  container has status
}

exegesis {
  Basic DOL 1.x gene should still work.
}
"#;
    let mut parser = Parser::new(input);
    let result = parser.parse();
    assert!(result.is_ok(), "DOL 1.x gene failed to parse");
}

#[test]
fn test_backward_compatibility_trait() {
    let input = r#"
trait container.lifecycle {
  uses container.exists
  container is created
  container is destroyed
}

exegesis {
  Basic DOL 1.x trait should still work.
}
"#;
    let mut parser = Parser::new(input);
    let result = parser.parse();
    assert!(result.is_ok(), "DOL 1.x trait failed to parse");
}

#[test]
fn test_backward_compatibility_constraint() {
    let input = r#"
constraint container.uniqueness {
  each container has identity
  container is unique
}

exegesis {
  Basic DOL 1.x constraint should still work.
}
"#;
    let mut parser = Parser::new(input);
    let result = parser.parse();
    assert!(
        result.is_ok(),
        "DOL 1.x constraint failed to parse: {:?}",
        result.err()
    );
}

#[test]
fn test_backward_compatibility_system() {
    let input = r#"
system container.orchestration @1.0.0 {
  requires container.exists >= 1.0.0
  uses container.lifecycle
  orchestrator has status
}

exegesis {
  Basic DOL 1.x system should still work.
}
"#;
    let mut parser = Parser::new(input);
    let result = parser.parse();
    assert!(result.is_ok(), "DOL 1.x system failed to parse");
}

#[test]
fn test_backward_compatibility_evolution() {
    let input = r#"
evolves container.exists @2.0.0 > 1.0.0 {
  adds container has metadata
  deprecates container has legacy
  removes oldfield
  because "Modernization"
}

exegesis {
  Basic DOL 1.x evolution should still work.
}
"#;
    let mut parser = Parser::new(input);
    let result = parser.parse();
    assert!(result.is_ok(), "DOL 1.x evolution failed to parse");
}

// ============================================
// Functional Pipeline Tests
// ============================================

#[test]
fn test_functional_pipeline_complex() {
    let input = "data |> validate |> transform |> store";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    // Verify it parses as a pipe expression
    match expr {
        Expr::Binary {
            op: BinaryOp::Pipe, ..
        } => {}
        _ => panic!("Expected pipe expression"),
    }
}

#[test]
fn test_functional_compose_complex() {
    let input = "trim >> lowercase >> validate >> normalize";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    // Verify it parses as a compose expression
    match expr {
        Expr::Binary {
            op: BinaryOp::Compose,
            ..
        } => {}
        _ => panic!("Expected compose expression"),
    }
}

#[test]
fn test_mixed_pipe_and_compose() {
    let input = "data |> (trim >> validate) |> process";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    // Verify it parses correctly
    match expr {
        Expr::Binary {
            op: BinaryOp::Pipe, ..
        } => {}
        _ => panic!("Expected top-level pipe"),
    }
}

// Note: BackPipe operator (<|) is in the lexer but not yet fully implemented in parser
// Uncomment this test when BackPipe is added to BinaryOp enum
// #[test]
// fn test_back_pipe_operator() {
//     let input = "result <| transform <| data";
//     let mut parser = Parser::new(input);
//     let expr = parser.parse_expr(0).unwrap();
//
//     match expr {
//         Expr::Binary {
//             op: BinaryOp::BackPipe,
//             ..
//         } => {}
//         _ => panic!("Expected back pipe"),
//     }
// }

// ============================================
// Pattern Matching Tests
// ============================================

#[test]
fn test_complex_match_expression() {
    let input = r#"match value {
        Some(x) => x,
        None => default,
        _ => fallback
    }"#;
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    match expr {
        Expr::Match { arms, .. } => {
            assert_eq!(arms.len(), 3);

            // First arm: constructor pattern
            match &arms[0].pattern {
                Pattern::Constructor { name, .. } => assert_eq!(name, "Some"),
                _ => panic!("Expected constructor pattern"),
            }

            // Second arm: identifier pattern
            match &arms[1].pattern {
                Pattern::Identifier(name) => assert_eq!(name, "None"),
                _ => panic!("Expected identifier pattern"),
            }

            // Third arm: wildcard pattern
            match &arms[2].pattern {
                Pattern::Wildcard => {}
                _ => panic!("Expected wildcard pattern"),
            }
        }
        _ => panic!("Expected match expression"),
    }
}

#[test]
fn test_match_with_guards() {
    // Match with guard: pattern if guard_expr => body
    let input = r#"match x {
        value if condition => positive,
        _ => zero
    }"#;
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    match expr {
        Expr::Match { arms, .. } => {
            assert!(arms[0].guard.is_some(), "Expected guard on first arm");
            assert!(arms[1].guard.is_none(), "Expected no guard on wildcard arm");
        }
        _ => panic!("Expected match expression"),
    }
}

#[test]
fn test_nested_pattern_matching() {
    let input = r#"match pair {
        (Some(x), Some(y)) => result,
        _ => default
    }"#;
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    match expr {
        Expr::Match { arms, .. } => {
            // First arm should have tuple pattern with nested constructors
            match &arms[0].pattern {
                Pattern::Tuple(patterns) => {
                    assert_eq!(patterns.len(), 2);
                    match &patterns[0] {
                        Pattern::Constructor { name, .. } => assert_eq!(name, "Some"),
                        _ => panic!("Expected constructor in tuple"),
                    }
                }
                _ => panic!("Expected tuple pattern"),
            }
        }
        _ => panic!("Expected match expression"),
    }
}

// ============================================
// Control Flow Tests
// ============================================

#[test]
fn test_complex_if_else_chain() {
    let input = r#"if x { a } else if y { b } else if z { c } else { d }"#;
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    // Count the nesting depth
    let mut depth = 0;
    let mut current = &expr;
    while let Expr::If { else_branch, .. } = current {
        depth += 1;
        match else_branch {
            Some(else_expr) => current = else_expr,
            None => break,
        }
    }
    assert_eq!(depth, 3, "Expected 3 levels of if nesting");
}

#[test]
fn test_nested_loops() {
    let input = r#"for outer in outers {
        for inner in inners {
            break;
        }
    }"#;
    let mut parser = Parser::new(input);
    let stmt = parser.parse_stmt().unwrap();

    match stmt {
        Stmt::For { body, .. } => {
            assert_eq!(body.len(), 1);
            match &body[0] {
                Stmt::For { .. } => {}
                _ => panic!("Expected nested for loop"),
            }
        }
        _ => panic!("Expected for loop"),
    }
}

#[test]
fn test_while_with_break_continue() {
    // Simplified - just test while with break and continue
    let input = r#"while condition {
        continue;
        break;
    }"#;
    let mut parser = Parser::new(input);
    let stmt = parser.parse_stmt().unwrap();

    match stmt {
        Stmt::While { body, .. } => {
            assert!(body.len() >= 2);
        }
        _ => panic!("Expected while loop"),
    }
}

#[test]
fn test_infinite_loop_with_break() {
    // Loop needs statements with semicolons, or use expr version
    let input = r#"loop {
        break;
    }"#;
    let mut parser = Parser::new(input);
    let stmt = parser.parse_stmt().unwrap();

    match stmt {
        Stmt::Loop { body } => {
            assert_eq!(body.len(), 1);
        }
        _ => panic!("Expected loop statement"),
    }
}

// ============================================
// Lambda and Higher-Order Function Tests
// ============================================

#[test]
fn test_lambda_as_argument() {
    let input = "map(|x| x, list)";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    match expr {
        Expr::Call { args, .. } => {
            assert_eq!(args.len(), 2);
            match &args[0] {
                Expr::Lambda { .. } => {}
                _ => panic!("Expected lambda as first argument"),
            }
        }
        _ => panic!("Expected function call"),
    }
}

#[test]
fn test_lambda_with_multiple_typed_params() {
    let input = "|x: Int32, y: Int32, z: Int32| -> Int32 { x }";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    match expr {
        Expr::Lambda {
            params,
            return_type,
            ..
        } => {
            assert_eq!(params.len(), 3);
            for param in &params {
                assert!(param.1.is_some(), "Expected type annotation");
            }
            assert!(return_type.is_some(), "Expected return type");
        }
        _ => panic!("Expected lambda"),
    }
}

#[test]
fn test_nested_lambdas() {
    let input = "|x| |y| x";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    match expr {
        Expr::Lambda { body, .. } => match *body {
            Expr::Lambda { .. } => {}
            _ => panic!("Expected nested lambda"),
        },
        _ => panic!("Expected lambda"),
    }
}

#[test]
fn test_lambda_in_pipeline() {
    let input = "data |> (|x| x) |> result";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    // Just verify it parses
    match expr {
        Expr::Binary {
            op: BinaryOp::Pipe, ..
        } => {}
        _ => panic!("Expected pipe expression"),
    }
}

// ============================================
// Type System Tests
// ============================================

#[test]
fn test_all_builtin_types() {
    let types = vec![
        "Int8", "Int16", "Int32", "Int64", "UInt8", "UInt16", "UInt32", "UInt64", "Float32",
        "Float64", "Bool", "String", "Void",
    ];

    for type_name in types {
        let mut parser = Parser::new(type_name);
        let result = parser.parse_type();
        assert!(result.is_ok(), "Failed to parse type: {}", type_name);
    }
}

#[test]
fn test_generic_type_nested() {
    let input = "Result<Option<Int32>, String>";
    let mut parser = Parser::new(input);
    let type_expr = parser.parse_type().unwrap();

    match type_expr {
        TypeExpr::Generic { args, .. } => {
            assert_eq!(args.len(), 2);
            match &args[0] {
                TypeExpr::Generic { .. } => {}
                _ => panic!("Expected nested generic"),
            }
        }
        _ => panic!("Expected generic type"),
    }
}

#[test]
fn test_complex_function_type() {
    let input = "(Int32, String, Bool) -> Result<Int32, String>";
    let mut parser = Parser::new(input);
    let type_expr = parser.parse_type().unwrap();

    match type_expr {
        TypeExpr::Function {
            params,
            return_type,
        } => {
            assert_eq!(params.len(), 3);
            match *return_type {
                TypeExpr::Generic { .. } => {}
                _ => panic!("Expected generic return type"),
            }
        }
        _ => panic!("Expected function type"),
    }
}

// ============================================
// Meta-programming Tests
// ============================================

#[test]
fn test_quote_expression() {
    let input = "'{ value }";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    // Quote followed by block
    match expr {
        Expr::Quote(_) => {}
        _ => panic!("Expected quote expression"),
    }
}

#[test]
fn test_eval_expression() {
    // Eval syntax: !{ expr }
    let input = "!{ code }";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    // The parser creates Eval which contains the expression inside braces
    match expr {
        Expr::Eval(_) => {}
        _ => panic!("Expected eval expression"),
    }
}

#[test]
fn test_reflect_on_type() {
    // Reflect is currently parsed as a unary operator
    let input = "?TypeName";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    // Reflect is a unary operator applied to an expression
    match expr {
        Expr::Unary {
            op: metadol::ast::UnaryOp::Reflect,
            ..
        } => {}
        _ => panic!("Expected reflect unary expression, got: {:?}", expr),
    }
}

// ============================================
// Block Expression Tests
// ============================================

#[test]
fn test_block_with_statements_and_final_expr() {
    let input = r#"{
        let x = 1;
        let y = 2;
        x
    }"#;
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    match expr {
        Expr::Block(Block {
            statements,
            final_expr,
            ..
        }) => {
            assert_eq!(statements.len(), 2);
            assert!(final_expr.is_some());
        }
        _ => panic!("Expected block expression"),
    }
}

#[test]
fn test_nested_blocks() {
    let input = r#"{
        let outer = 1;
        {
            let inner = 2;
            inner
        }
    }"#;
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    match expr {
        Expr::Block(Block { statements, .. }) => {
            // First statement is let
            // Second statement should be expression containing nested block
            assert!(!statements.is_empty());
        }
        _ => panic!("Expected block expression"),
    }
}

// ============================================
// Operator Precedence Tests
// ============================================

#[test]
fn test_arithmetic_precedence() {
    let input = "a + b * c";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    // Should parse as a + (b * c), so top level is Add
    match expr {
        Expr::Binary {
            op: BinaryOp::Add,
            right,
            ..
        } => {
            // Right side should be multiplication
            match *right {
                Expr::Binary {
                    op: BinaryOp::Mul, ..
                } => {}
                _ => panic!("Expected multiplication on right side"),
            }
        }
        _ => panic!("Expected addition at top level"),
    }
}

#[test]
fn test_comparison_precedence() {
    let input = "a == b && c != d";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    // Should parse as (a == b) && (c != d), so top level is And
    match expr {
        Expr::Binary {
            op: BinaryOp::And, ..
        } => {}
        _ => panic!("Expected logical and at top level"),
    }
}

#[test]
fn test_pipe_vs_compose_precedence() {
    let input = "a |> f >> g >> h";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();

    // Should parse as a |> (f >> g >> h), so top level is Pipe
    match expr {
        Expr::Binary {
            op: BinaryOp::Pipe,
            right,
            ..
        } => {
            // Right side should start with compose
            match *right {
                Expr::Binary {
                    op: BinaryOp::Compose,
                    ..
                } => {}
                _ => panic!("Expected compose on right side"),
            }
        }
        _ => panic!("Expected pipe at top level"),
    }
}

// ============================================
// Mixed DOL 1.x and DOL 2.0 Tests
// ============================================

#[test]
fn test_gene_with_dol1_and_dol2_features() {
    let input = r#"
gene mixed.features {
  container has identity
  transform is functional
}

exegesis {
  A gene mixing DOL 1.x statements with DOL 2.0 potential.
}
"#;
    let mut parser = Parser::new(input);
    let result = parser.parse();
    assert!(result.is_ok(), "Mixed gene failed to parse");
}

// ============================================
// Error Recovery Tests
// ============================================

#[test]
fn test_incomplete_if_fails() {
    let input = "if condition { }";
    let mut parser = Parser::new(input);
    let result = parser.parse_expr(0);
    // Should still parse even with empty block
    assert!(result.is_ok());
}

#[test]
fn test_unmatched_pattern_parens() {
    let input = "(x, y";
    let mut parser = Parser::new(input);
    let result = parser.parse_pattern();
    assert!(result.is_err(), "Should fail on unmatched parens");
}

// ============================================
// Let Statement Tests
// ============================================

#[test]
fn test_let_with_complex_expression() {
    let input = "let result = data |> transform |> validate;";
    let mut parser = Parser::new(input);
    let stmt = parser.parse_stmt().unwrap();

    match stmt {
        Stmt::Let { name, value, .. } => {
            assert_eq!(name, "result");
            match value {
                Expr::Binary {
                    op: BinaryOp::Pipe, ..
                } => {}
                _ => panic!("Expected pipe in let value"),
            }
        }
        _ => panic!("Expected let statement"),
    }
}

#[test]
fn test_let_with_lambda() {
    let input = "let transform = |x: Int32| -> Int32 { x };";
    let mut parser = Parser::new(input);
    let stmt = parser.parse_stmt().unwrap();

    match stmt {
        Stmt::Let { value, .. } => match value {
            Expr::Lambda { .. } => {}
            _ => panic!("Expected lambda in let value"),
        },
        _ => panic!("Expected let statement"),
    }
}

// ============================================
// Return Statement Tests
// ============================================

#[test]
fn test_return_with_expression() {
    let input = "return a |> b;";
    let mut parser = Parser::new(input);
    let stmt = parser.parse_stmt().unwrap();

    match stmt {
        Stmt::Return(Some(expr)) => match expr {
            Expr::Binary {
                op: BinaryOp::Pipe, ..
            } => {}
            _ => panic!("Expected pipe in return"),
        },
        _ => panic!("Expected return with value"),
    }
}

// ============================================
// Member Access Tests
// ============================================

#[test]
fn test_chained_member_access() {
    let input = "obj.field.subfield";
    let mut parser = Parser::new(input);
    let result = parser.parse_expr(0);

    // Member access chaining - just verify it parses
    assert!(result.is_ok(), "Failed to parse chained member access");
}

#[test]
fn test_member_access_with_call() {
    let input = "obj.method(arg)";
    let mut parser = Parser::new(input);
    let result = parser.parse_expr(0);

    // Member access with method call - just verify it parses
    assert!(result.is_ok(), "Failed to parse member access with call");
}

// ============================================
// Boolean Literal Tests
// ============================================

#[test]
fn test_boolean_literals() {
    let input = "true";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();
    match expr {
        Expr::Literal(_) => {}
        _ => panic!("Expected boolean literal"),
    }

    let input = "false";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();
    match expr {
        Expr::Literal(_) => {}
        _ => panic!("Expected boolean literal"),
    }
}

#[test]
fn test_boolean_in_if() {
    let input = "if true { a } else { b }";
    let mut parser = Parser::new(input);
    let expr = parser.parse_expr(0).unwrap();
    match expr {
        Expr::If { .. } => {}
        _ => panic!("Expected if expression"),
    }
}