dist_agent_lang 1.0.21

Agentic programming with library and CLI support for Off/On-chain network integration
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
// Lexer Mutation Tests
// These tests are designed to catch mutations identified by mutation testing
// Tests use only public APIs to verify lexer behavior

use dist_agent_lang::lexer::tokens::{Keyword, Literal, Operator, Punctuation, Token};
use dist_agent_lang::lexer::Lexer;

// ============================================================================
// OPERATOR TOKEN TESTS
// ============================================================================
// These tests catch "delete match arm" mutations by ensuring all operators
// are tested, and catch comparison operator mutations by testing boundaries

#[test]
fn test_operator_plus() {
    let code = "+";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2); // Plus + EOF
    assert!(matches!(tokens[0], Token::Operator(Operator::Plus)));
}

#[test]
fn test_operator_minus() {
    let code = "-";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::Minus)));
}

#[test]
fn test_operator_star() {
    let code = "*";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::Star)));
}

#[test]
fn test_operator_slash() {
    let code = "/";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::Slash)));
}

#[test]
fn test_operator_percent() {
    let code = "%";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::Percent)));
}

#[test]
fn test_operator_less() {
    // Test < operator - catches comparison operator mutations
    let code = "<";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::Less)));
}

#[test]
fn test_operator_less_equal() {
    // Test <= operator - catches comparison operator mutations and boundary checks
    let code = "<=";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::LessEqual)));
}

#[test]
fn test_operator_greater() {
    // Test > operator - catches comparison operator mutations
    let code = ">";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::Greater)));
}

#[test]
fn test_operator_greater_equal() {
    // Test >= operator - catches comparison operator mutations and boundary checks
    let code = ">=";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::GreaterEqual)));
}

#[test]
fn test_operator_equal() {
    // Test == operator - catches comparison operator mutations
    let code = "==";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::Equal)));
}

#[test]
fn test_operator_not_equal() {
    // Test != operator - catches comparison operator mutations
    let code = "!=";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::NotEqual)));
}

#[test]
fn test_operator_not() {
    // Test ! operator (not !=)
    let code = "!";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::Not)));
}

#[test]
fn test_operator_assign() {
    // Test = operator (not ==)
    let code = "=";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::Assign)));
}

#[test]
fn test_operator_and() {
    // Test && operator - catches delete match arm mutations
    let code = "&&";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::And)));
}

#[test]
fn test_operator_or() {
    // Test || operator - catches delete match arm mutations
    let code = "||";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Operator(Operator::Or)));
}

#[test]
fn test_operator_comparison_boundary_less_vs_less_equal() {
    // Test that < and <= are distinct - catches comparison operator mutations
    let code1 = "x < 10";
    let code2 = "x <= 10";

    let tokens1 = Lexer::new(code1).tokenize_immutable().unwrap();
    let tokens2 = Lexer::new(code2).tokenize_immutable().unwrap();

    // Find the comparison operator in each
    let op1 = tokens1.iter().find(|t| matches!(t, Token::Operator(_)));
    let op2 = tokens2.iter().find(|t| matches!(t, Token::Operator(_)));

    assert!(op1.is_some() && op2.is_some());
    assert!(matches!(op1.unwrap(), Token::Operator(Operator::Less)));
    assert!(matches!(op2.unwrap(), Token::Operator(Operator::LessEqual)));
}

#[test]
fn test_operator_comparison_boundary_greater_vs_greater_equal() {
    // Test that > and >= are distinct - catches comparison operator mutations
    let code1 = "x > 10";
    let code2 = "x >= 10";

    let tokens1 = Lexer::new(code1).tokenize_immutable().unwrap();
    let tokens2 = Lexer::new(code2).tokenize_immutable().unwrap();

    let op1 = tokens1.iter().find(|t| matches!(t, Token::Operator(_)));
    let op2 = tokens2.iter().find(|t| matches!(t, Token::Operator(_)));

    assert!(op1.is_some() && op2.is_some());
    assert!(matches!(op1.unwrap(), Token::Operator(Operator::Greater)));
    assert!(matches!(
        op2.unwrap(),
        Token::Operator(Operator::GreaterEqual)
    ));
}

// ============================================================================
// PUNCTUATION TOKEN TESTS
// ============================================================================
// These tests catch "delete match arm" mutations by ensuring all punctuation
// types are tested

#[test]
fn test_punctuation_left_paren() {
    let code = "(";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(
        tokens[0],
        Token::Punctuation(Punctuation::LeftParen)
    ));
}

#[test]
fn test_punctuation_right_paren() {
    let code = ")";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(
        tokens[0],
        Token::Punctuation(Punctuation::RightParen)
    ));
}

#[test]
fn test_punctuation_left_brace() {
    let code = "{";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(
        tokens[0],
        Token::Punctuation(Punctuation::LeftBrace)
    ));
}

#[test]
fn test_punctuation_right_brace() {
    let code = "}";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(
        tokens[0],
        Token::Punctuation(Punctuation::RightBrace)
    ));
}

#[test]
fn test_punctuation_left_bracket() {
    let code = "[";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(
        tokens[0],
        Token::Punctuation(Punctuation::LeftBracket)
    ));
}

#[test]
fn test_punctuation_right_bracket() {
    let code = "]";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(
        tokens[0],
        Token::Punctuation(Punctuation::RightBracket)
    ));
}

#[test]
fn test_punctuation_semicolon() {
    let code = ";";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(
        tokens[0],
        Token::Punctuation(Punctuation::Semicolon)
    ));
}

#[test]
fn test_punctuation_colon() {
    // Test single : (not ::)
    let code = ":";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Punctuation(Punctuation::Colon)));
}

#[test]
fn test_punctuation_double_colon() {
    // Test :: - catches boundary check mutations
    let code = "::";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(
        tokens[0],
        Token::Punctuation(Punctuation::DoubleColon)
    ));
}

#[test]
fn test_punctuation_comma() {
    let code = ",";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Punctuation(Punctuation::Comma)));
}

#[test]
fn test_punctuation_dot() {
    let code = ".";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Punctuation(Punctuation::Dot)));
}

#[test]
fn test_punctuation_question() {
    let code = "?";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(
        tokens[0],
        Token::Punctuation(Punctuation::Question)
    ));
}

#[test]
fn test_punctuation_at() {
    let code = "@";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Punctuation(Punctuation::At)));
}

#[test]
fn test_punctuation_arrow() {
    // Test -> (not just -)
    let code = "->";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Punctuation(Punctuation::Arrow)));
}

#[test]
fn test_punctuation_colon_vs_double_colon_boundary() {
    // Test boundary between : and :: - catches comparison operator mutations
    let code1 = ":";
    let code2 = "::";

    let tokens1 = Lexer::new(code1).tokenize_immutable().unwrap();
    let tokens2 = Lexer::new(code2).tokenize_immutable().unwrap();

    assert!(matches!(tokens1[0], Token::Punctuation(Punctuation::Colon)));
    assert!(matches!(
        tokens2[0],
        Token::Punctuation(Punctuation::DoubleColon)
    ));
}

// ============================================================================
// POSITION TRACKING TESTS
// ============================================================================
// These tests catch comparison operator mutations in position calculations
// and arithmetic operator mutations in position increments

#[test]
fn test_position_tracking_single_char_token() {
    // Test that single character tokens advance position by 1
    // Catches arithmetic operator mutations (position += 1)
    let code = "+";
    let tokens_with_pos = Lexer::new(code)
        .tokenize_with_positions_immutable()
        .unwrap();

    assert_eq!(tokens_with_pos.len(), 2); // Plus + EOF
    assert_eq!(tokens_with_pos[0].line, 1);
    assert_eq!(tokens_with_pos[0].column, 1); // Starts at column 1
}

#[test]
fn test_position_tracking_two_char_token() {
    // Test that two character tokens advance position by 2
    // Catches arithmetic operator mutations (position += 2)
    let code = "==";
    let tokens_with_pos = Lexer::new(code)
        .tokenize_with_positions_immutable()
        .unwrap();

    assert_eq!(tokens_with_pos.len(), 2);
    assert_eq!(tokens_with_pos[0].line, 1);
    assert_eq!(tokens_with_pos[0].column, 1); // Starts at column 1
                                              // The next token (EOF) should be at column 3
    assert_eq!(tokens_with_pos[1].column, 3);
}

#[test]
fn test_position_tracking_newline() {
    // Test line/column tracking across newlines
    // Catches comparison operator mutations in skip_whitespace
    let code = "+\n-";
    let tokens_with_pos = Lexer::new(code)
        .tokenize_with_positions_immutable()
        .unwrap();

    assert_eq!(tokens_with_pos.len(), 3); // Plus, Minus, EOF
    assert_eq!(tokens_with_pos[0].line, 1);
    assert_eq!(tokens_with_pos[0].column, 1);
    assert_eq!(tokens_with_pos[1].line, 2); // Second token on line 2
    assert_eq!(tokens_with_pos[1].column, 1); // Column resets to 1
}

#[test]
fn test_position_tracking_whitespace() {
    // Test that whitespace advances column correctly
    // Catches arithmetic operator mutations in skip_whitespace
    let code = "  +";
    let tokens_with_pos = Lexer::new(code)
        .tokenize_with_positions_immutable()
        .unwrap();

    assert_eq!(tokens_with_pos.len(), 2);
    assert_eq!(tokens_with_pos[0].line, 1);
    assert_eq!(tokens_with_pos[0].column, 3); // Plus is at column 3 (after 2 spaces)
}

#[test]
fn test_position_tracking_boundary_end_of_input() {
    // Test position tracking at end of input
    // Catches comparison operator mutations (position >= input.len())
    let code = "x";
    let tokens_with_pos = Lexer::new(code)
        .tokenize_with_positions_immutable()
        .unwrap();

    assert_eq!(tokens_with_pos.len(), 2); // Identifier + EOF
    assert_eq!(tokens_with_pos[0].line, 1);
    assert_eq!(tokens_with_pos[0].column, 1);
    // EOF position is tracked - verify it exists and has valid position
    assert!(matches!(tokens_with_pos[1].token, Token::EOF));
    assert!(tokens_with_pos[1].column >= 1); // EOF has valid column position
}

#[test]
fn test_position_tracking_multi_char_operator() {
    // Test that <= advances position correctly
    // Catches arithmetic operator mutations
    let code = "x <= 10";
    let tokens_with_pos = Lexer::new(code)
        .tokenize_with_positions_immutable()
        .unwrap();

    // Find the <= operator
    let less_equal_token = tokens_with_pos
        .iter()
        .find(|t| matches!(t.token, Token::Operator(Operator::LessEqual)));

    assert!(less_equal_token.is_some());
    let token = less_equal_token.unwrap();
    // <= starts after "x " - verify it's at a valid position (column 2 or 3 depending on spacing)
    assert!(
        token.column >= 2 && token.column <= 3,
        "<= should be at column 2 or 3, got {}",
        token.column
    );
    assert_eq!(token.line, 1);
}

// ============================================================================
// KEYWORD TESTS
// ============================================================================
// These tests catch "delete match arm" mutations by testing keyword recognition

#[test]
fn test_keyword_let() {
    let code = "let";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Keyword(Keyword::Let)));
}

#[test]
fn test_keyword_fn() {
    let code = "fn";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Keyword(Keyword::Fn)));
}

#[test]
fn test_keyword_if() {
    let code = "if";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Keyword(Keyword::If)));
}

#[test]
fn test_keyword_return() {
    let code = "return";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Keyword(Keyword::Return)));
}

#[test]
fn test_keyword_vs_identifier() {
    // Test that keywords are recognized vs identifiers
    // Catches delete match arm mutations in is_keyword
    let code1 = "let";
    let code2 = "let_var";

    let tokens1 = Lexer::new(code1).tokenize_immutable().unwrap();
    let tokens2 = Lexer::new(code2).tokenize_immutable().unwrap();

    assert!(matches!(tokens1[0], Token::Keyword(Keyword::Let)));
    assert!(matches!(tokens2[0], Token::Identifier(_)));
}

// ============================================================================
// LITERAL TESTS
// ============================================================================
// These tests catch "delete match arm" mutations and return value mutations

#[test]
fn test_literal_bool_true() {
    let code = "true";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Literal(Literal::Bool(true))));
}

#[test]
fn test_literal_bool_false() {
    let code = "false";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Literal(Literal::Bool(false))));
}

#[test]
fn test_literal_null() {
    let code = "null";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    assert!(matches!(tokens[0], Token::Literal(Literal::Null)));
}

#[test]
fn test_literal_int() {
    // Test integer literal - catches return value mutations
    let code = "42";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);

    if let Token::Literal(Literal::Int(value)) = &tokens[0] {
        assert_eq!(*value, 42);
    } else {
        panic!("Expected Int(42), got {:?}", tokens[0]);
    }
}

#[test]
fn test_literal_string() {
    // Test string literal - catches return value mutations
    let code = r#""hello""#;
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);

    if let Token::Literal(Literal::String(value)) = &tokens[0] {
        assert_eq!(value, "hello");
    } else {
        panic!("Expected String(\"hello\"), got {:?}", tokens[0]);
    }
}

// ============================================================================
// EDGE CASE TESTS
// ============================================================================
// These tests catch boundary condition mutations and error handling

#[test]
fn test_empty_input() {
    // Test empty input - catches boundary check mutations
    let code = "";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 1); // Just EOF
    assert!(matches!(tokens[0], Token::EOF));
}

#[test]
fn test_whitespace_only() {
    // Test whitespace-only input - catches boundary check mutations
    let code = "   \n\t  ";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 1); // Just EOF
    assert!(matches!(tokens[0], Token::EOF));
}

#[test]
fn test_invalid_single_ampersand() {
    // Test that single & is invalid - catches boundary check mutations
    let code = "&";
    let result = Lexer::new(code).tokenize_immutable();
    assert!(result.is_err(), "Single & should be an error");
}

#[test]
fn test_invalid_single_pipe() {
    // Test that single | is invalid - catches boundary check mutations
    let code = "|";
    let result = Lexer::new(code).tokenize_immutable();
    assert!(result.is_err(), "Single | should be an error");
}

#[test]
fn test_unterminated_string() {
    // Test unterminated string - catches boundary check mutations
    let code = r#""hello"#;
    let result = Lexer::new(code).tokenize_immutable();
    assert!(result.is_err(), "Unterminated string should be an error");
}

#[test]
fn test_identifier_with_underscore() {
    // Test identifier starting with underscore
    let code = "_test";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);
    if let Token::Identifier(name) = &tokens[0] {
        assert_eq!(name, "_test");
    } else {
        panic!("Expected Identifier(\"_test\"), got {:?}", tokens[0]);
    }
}

#[test]
fn test_number_parsing() {
    // Test number parsing - catches return value mutations
    let code = "123";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    assert_eq!(tokens.len(), 2);

    if let Token::Literal(Literal::Int(value)) = &tokens[0] {
        assert_eq!(*value, 123);
    } else {
        panic!("Expected Int(123), got {:?}", tokens[0]);
    }
}

#[test]
fn test_complex_expression_tokenization() {
    // Test complex expression with multiple operators
    // Catches multiple delete match arm mutations
    let code = "x + y < z && a == b";
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();

    // Should have: x, +, y, <, z, &&, a, ==, b, EOF
    assert!(tokens.len() >= 9);

    // Verify specific operators are present
    let has_plus = tokens
        .iter()
        .any(|t| matches!(t, Token::Operator(Operator::Plus)));
    let has_less = tokens
        .iter()
        .any(|t| matches!(t, Token::Operator(Operator::Less)));
    let has_and = tokens
        .iter()
        .any(|t| matches!(t, Token::Operator(Operator::And)));
    let has_equal = tokens
        .iter()
        .any(|t| matches!(t, Token::Operator(Operator::Equal)));

    assert!(has_plus, "Should have Plus operator");
    assert!(has_less, "Should have Less operator");
    assert!(has_and, "Should have And operator");
    assert!(has_equal, "Should have Equal operator");
}

// ============================================================================
// ESCAPE SEQUENCE TESTS
// ============================================================================
// These tests catch "delete match arm" mutations in decode_escape (lines 694-698)
// Each escape sequence must be tested individually so deleting one arm is caught.

#[test]
fn test_escape_sequence_double_quote() {
    // Catches: delete match arm '"' in decode_escape (line 694)
    let code = r#""hello\"world""#;
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    if let Token::Literal(Literal::String(value)) = &tokens[0] {
        assert_eq!(
            value, "hello\"world",
            "Escaped double quote should produce literal quote"
        );
    } else {
        panic!("Expected string literal, got {:?}", tokens[0]);
    }
}

#[test]
fn test_escape_sequence_backslash() {
    // Catches: delete match arm '\\' in decode_escape (line 695)
    let code = r#""path\\to\\file""#;
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    if let Token::Literal(Literal::String(value)) = &tokens[0] {
        assert_eq!(
            value, "path\\to\\file",
            "Escaped backslash should produce literal backslash"
        );
    } else {
        panic!("Expected string literal, got {:?}", tokens[0]);
    }
}

#[test]
fn test_escape_sequence_newline() {
    // Catches: delete match arm 'n' in decode_escape (line 696)
    let code = r#""line1\nline2""#;
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    if let Token::Literal(Literal::String(value)) = &tokens[0] {
        assert_eq!(
            value, "line1\nline2",
            "\\n should produce newline character"
        );
        assert!(
            value.contains('\n'),
            "String must contain actual newline char"
        );
    } else {
        panic!("Expected string literal, got {:?}", tokens[0]);
    }
}

#[test]
fn test_escape_sequence_carriage_return() {
    // Catches: delete match arm 'r' in decode_escape (line 697)
    let code = r#""before\rafter""#;
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    if let Token::Literal(Literal::String(value)) = &tokens[0] {
        assert_eq!(
            value, "before\rafter",
            "\\r should produce carriage return character"
        );
        assert!(
            value.contains('\r'),
            "String must contain actual carriage return char"
        );
    } else {
        panic!("Expected string literal, got {:?}", tokens[0]);
    }
}

#[test]
fn test_escape_sequence_tab() {
    // Catches: delete match arm 't' in decode_escape (line 698)
    let code = r#""col1\tcol2""#;
    let tokens = Lexer::new(code).tokenize_immutable().unwrap();
    if let Token::Literal(Literal::String(value)) = &tokens[0] {
        assert_eq!(value, "col1\tcol2", "\\t should produce tab character");
        assert!(value.contains('\t'), "String must contain actual tab char");
    } else {
        panic!("Expected string literal, got {:?}", tokens[0]);
    }
}

// ============================================================================
// MUTABLE TOKENIZE() TESTS
// ============================================================================
// These tests use tokenize() (mutable version) to cover read_string, read_number,
// read_identifier — the non-immutable code paths that have 20+ missed mutations.

#[test]
fn test_mutable_tokenize_string_exact_value() {
    // Catches: replace Lexer::read_string -> Result<String, LexerError> with Ok(String::new())
    // Catches: replace Lexer::read_string -> Result<String, LexerError> with Ok("xyzzy".into())
    let code = r#""hello world""#;
    let mut lexer = Lexer::new(code);
    let tokens = lexer.tokenize().unwrap();
    if let Token::Literal(Literal::String(value)) = &tokens[0] {
        assert_eq!(
            value, "hello world",
            "read_string must return exact string content"
        );
        assert_ne!(value, "", "read_string must not return empty string");
        assert_ne!(value, "xyzzy", "read_string must not return garbage");
    } else {
        panic!("Expected string literal, got {:?}", tokens[0]);
    }
}

#[test]
fn test_mutable_tokenize_string_with_escape() {
    // Catches: replace == with != in Lexer::read_string (lines 373, 376)
    let code = r#""tab\there""#;
    let mut lexer = Lexer::new(code);
    let tokens = lexer.tokenize().unwrap();
    if let Token::Literal(Literal::String(value)) = &tokens[0] {
        assert_eq!(value, "tab\there");
    } else {
        panic!("Expected string literal, got {:?}", tokens[0]);
    }
}

#[test]
fn test_mutable_tokenize_string_boundary() {
    // Catches: replace < with == in Lexer::read_string (line 371)
    // Catches: replace < with > in Lexer::read_string (line 371)
    // Catches: replace < with <= in Lexer::read_string (line 371)
    // Tests: string at end of input, string with closing quote
    let code = r#""end""#;
    let mut lexer = Lexer::new(code);
    let tokens = lexer.tokenize().unwrap();
    assert_eq!(tokens.len(), 2); // string + EOF
    if let Token::Literal(Literal::String(value)) = &tokens[0] {
        assert_eq!(value, "end");
    } else {
        panic!("Expected string literal, got {:?}", tokens[0]);
    }
}

#[test]
fn test_mutable_tokenize_number_exact_value() {
    // Catches: replace Lexer::read_number -> Result<i64, LexerError> with Ok(0)
    // Catches: replace Lexer::read_number -> Result<i64, LexerError> with Ok(1)
    // Catches: replace Lexer::read_number -> Result<i64, LexerError> with Ok(-1)
    let code = "42";
    let mut lexer = Lexer::new(code);
    let tokens = lexer.tokenize().unwrap();
    if let Token::Literal(Literal::Int(value)) = &tokens[0] {
        assert_eq!(*value, 42, "read_number must return exact number");
        assert_ne!(*value, 0, "read_number must not return 0");
        assert_ne!(*value, 1, "read_number must not return 1");
        assert_ne!(*value, -1, "read_number must not return -1");
    } else {
        panic!("Expected int literal, got {:?}", tokens[0]);
    }
}

#[test]
fn test_mutable_tokenize_number_boundary() {
    // Catches: replace < with == in Lexer::read_number (line 352)
    // Catches: replace < with <= in Lexer::read_number (line 352)
    // Number at end of input — boundary condition
    let code = "999";
    let mut lexer = Lexer::new(code);
    let tokens = lexer.tokenize().unwrap();
    assert_eq!(tokens.len(), 2); // number + EOF
    if let Token::Literal(Literal::Int(value)) = &tokens[0] {
        assert_eq!(*value, 999);
    } else {
        panic!("Expected int literal, got {:?}", tokens[0]);
    }
}

#[test]
fn test_mutable_tokenize_number_followed_by_identifier() {
    // Verifies number stops at non-digit boundary
    let code = "123abc";
    let mut lexer = Lexer::new(code);
    let tokens = lexer.tokenize().unwrap();
    if let Token::Literal(Literal::Int(value)) = &tokens[0] {
        assert_eq!(*value, 123, "Number should stop at 'a'");
    } else {
        panic!("Expected int literal, got {:?}", tokens[0]);
    }
    if let Token::Identifier(name) = &tokens[1] {
        assert_eq!(name, "abc", "Identifier should be 'abc' after number");
    } else {
        panic!("Expected identifier 'abc', got {:?}", tokens[1]);
    }
}

#[test]
fn test_mutable_tokenize_identifier_exact_value() {
    // Catches: replace Lexer::read_identifier -> String with String::new()
    // Catches: replace Lexer::read_identifier -> String with "xyzzy".into()
    let code = "myVariable";
    let mut lexer = Lexer::new(code);
    let tokens = lexer.tokenize().unwrap();
    if let Token::Identifier(name) = &tokens[0] {
        assert_eq!(name, "myVariable", "read_identifier must return exact name");
        assert_ne!(name, "", "read_identifier must not return empty string");
        assert_ne!(name, "xyzzy", "read_identifier must not return garbage");
    } else {
        panic!("Expected identifier, got {:?}", tokens[0]);
    }
}

#[test]
fn test_mutable_tokenize_identifier_boundary() {
    // Catches: replace < with == in Lexer::read_identifier (line 337)
    // Catches: replace || with && in Lexer::read_identifier (line 339)
    // Catches: replace == with != in Lexer::read_identifier (line 339)
    // Identifier at end of input — boundary condition
    let code = "x";
    let mut lexer = Lexer::new(code);
    let tokens = lexer.tokenize().unwrap();
    assert_eq!(tokens.len(), 2); // identifier + EOF
    if let Token::Identifier(name) = &tokens[0] {
        assert_eq!(name, "x");
    } else {
        panic!("Expected identifier, got {:?}", tokens[0]);
    }
}

#[test]
fn test_mutable_tokenize_identifier_with_underscore() {
    // Catches: replace == with != in Lexer::read_identifier (line 339)
    // The _ character check: ch == '_'
    let code = "has_underscore_here";
    let mut lexer = Lexer::new(code);
    let tokens = lexer.tokenize().unwrap();
    if let Token::Identifier(name) = &tokens[0] {
        assert_eq!(name, "has_underscore_here");
    } else {
        panic!("Expected identifier, got {:?}", tokens[0]);
    }
}

#[test]
fn test_mutable_tokenize_unterminated_string() {
    // Catches: boundary mutations in read_string loop
    let code = r#""unterminated"#;
    let mut lexer = Lexer::new(code);
    let result = lexer.tokenize();
    assert!(
        result.is_err(),
        "Unterminated string should fail in mutable tokenize"
    );
}

#[test]
fn test_mutable_tokenize_multitoken_statement() {
    // Comprehensive test exercising all 3 readers in one pass
    let code = r#"let name = "hello";"#;
    let mut lexer = Lexer::new(code);
    let tokens = lexer.tokenize().unwrap();
    // let, name, =, "hello", ;, EOF
    assert!(tokens.len() >= 5);
    assert!(matches!(tokens[0], Token::Keyword(Keyword::Let)));
    if let Token::Identifier(name) = &tokens[1] {
        assert_eq!(name, "name");
    }
    if let Token::Literal(Literal::String(value)) = &tokens[3] {
        assert_eq!(value, "hello");
    }
}