vb6interpret 0.1.0

VB6 interpreter for executing VB6 code directly
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
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
//! Integration tests for the `vb6interpret` tree-walking interpreter.

use vb6interpret::run_source;
use vb6interpret::Interpreter;
use vb6parse::files::ModuleFile;
use vb6parse::io::SourceFile;
use vb6runtime::state::settings as settings_state;

use std::sync::Mutex;

/// Serializes tests that install an environment on the shared runtime
/// snapshot; every `run_module` resets it, so parallel runs would stomp each
/// other's assignments.
static ENV_TEST_LOCK: Mutex<()> = Mutex::new(());

/// Run a module body and return the captured `Debug.Print` output.
fn run(body: &str) -> Vec<String> {
    let source = format!("Attribute VB_Name = \"M\"\nSub Main()\n{}\nEnd Sub\n", body);
    run_source(&source).expect("interpretation failed")
}

/// Run a module like the playground's plain run (no trace snapshots) and
/// return the final reported execution line.
fn run_final_line(source: &str) -> usize {
    let source_file = SourceFile::from_string("scratch.bas", source);
    let module = ModuleFile::parse(&source_file).unwrap_or_fail();
    let mut interpreter = Interpreter::new();
    let _ = interpreter.run_module(&module);
    interpreter.current_line()
}

#[test]
fn arithmetic_and_concat() {
    let out = run("    Debug.Print 2 + 3 * 4\n\
         Debug.Print 10 \\ 3\n\
         Debug.Print 10 Mod 3\n\
         Debug.Print 2 ^ 10\n\
         Debug.Print \"a\" & \"b\" & 1\n");
    assert_eq!(out, vec!["14", "3", "1", "1024", "ab1"]);
}

#[test]
fn integer_types() {
    let out = run("    Dim i As Integer\n\
         Dim l As Long\n\
         i = 32767\n\
         l = i + 1\n\
         Debug.Print i\n\
         Debug.Print l\n");
    assert_eq!(out, vec!["32767", "32768"]);
}

#[test]
fn string_functions() {
    let out = run("    Dim s As String\n\
         s = \"  Hello World  \"\n\
         Debug.Print Trim(s)\n\
         Debug.Print UCase(s)\n\
         Debug.Print Left(s, 5)\n\
         Debug.Print Right(s, 5)\n\
         Debug.Print Mid(s, 8, 5)\n\
         Debug.Print Len(s)\n\
         Debug.Print InStr(\"hello\", \"ll\")\n\
         Debug.Print Chr(65) & Chr(66)\n\
         Debug.Print Space(3) & \"!\"\n");
    assert_eq!(
        out,
        vec![
            "Hello World",
            "  HELLO WORLD  ",
            "  Hel",
            "rld  ",
            " Worl",
            "15",
            "3",
            "AB",
            "   !",
        ]
    );
}

#[test]
fn string_function_suffix_variants() {
    let out = run("    Debug.Print Chr$(65) & Chr$(66)\n\
         Debug.Print ChrB$(65) & ChrB$(66)\n\
         Debug.Print ChrW$(65) & ChrW$(66)\n\
         Debug.Print Trim$(\"  hi  \")\n");
    assert_eq!(out, vec!["AB", "AB", "AB", "hi"]);
}

#[test]
fn lset_statement_pads_on_the_right() {
    let out = run("    Dim s As String\n\
         s = String(10, \"X\")\n\
         LSet s = \"Left\"\n\
         Debug.Print \"[\" & s & \"]\"\n");
    assert_eq!(out, vec!["[Left      ]"]);
}

#[test]
fn lset_statement_truncates_long_sources() {
    let out = run("    Dim s As String\n\
         s = \"ABCDEFGH\"\n\
         LSet s = \"1234567890\"\n\
         Debug.Print s\n");
    assert_eq!(out, vec!["12345678"]);
}

#[test]
fn lset_statement_accepts_a_variable_source() {
    let out = run("    Dim s As String\n\
         Dim t As String\n\
         s = String(6, \"*\")\n\
         t = \"abc\"\n\
         LSet s = t\n\
         Debug.Print \"[\" & s & \"]\"\n");
    assert_eq!(out, vec!["[abc   ]"]);
}

#[test]
fn lset_statement_keeps_exact_fit_unchanged() {
    let out = run("    Dim s As String\n\
         s = \"abc\"\n\
         LSet s = \"xyz\"\n\
         Debug.Print s\n");
    assert_eq!(out, vec!["xyz"]);
}

#[test]
fn rset_statement_pads_on_the_left() {
    let out = run("    Dim s As String\n\
         s = String(10, \"X\")\n\
         RSet s = \"Right\"\n\
         Debug.Print \"[\" & s & \"]\"\n");
    assert_eq!(out, vec!["[     Right]"]);
}

#[test]
fn rset_statement_truncates_long_sources() {
    let out = run("    Dim s As String\n\
         s = \"ABCDEFGH\"\n\
         RSet s = \"1234567890\"\n\
         Debug.Print s\n");
    assert_eq!(out, vec!["34567890"]);
}

#[test]
fn rset_statement_accepts_a_variable_source() {
    let out = run("    Dim s As String\n\
         Dim t As String\n\
         s = String(6, \"*\")\n\
         t = \"abc\"\n\
         RSet s = t\n\
         Debug.Print \"[\" & s & \"]\"\n");
    assert_eq!(out, vec!["[   abc]"]);
}

#[test]
fn rset_statement_keeps_exact_fit_unchanged() {
    let out = run("    Dim s As String\n\
         s = \"abc\"\n\
         RSet s = \"xyz\"\n\
         Debug.Print s\n");
    assert_eq!(out, vec!["xyz"]);
}

#[test]
fn mid_statement_replaces_with_explicit_length() {
    let out = run("    Dim s As String\n\
         s = \"Hello World\"\n\
         Mid(s, 7, 5) = \"VB6!!\"\n\
         Debug.Print s\n");
    assert_eq!(out, vec!["Hello VB6!!"]);
}

#[test]
fn mid_statement_without_length_replaces_to_the_end() {
    let out = run("    Dim s As String\n\
         s = \"ABCDEFGH\"\n\
         Mid(s, 3) = \"123\"\n\
         Debug.Print s\n");
    assert_eq!(out, vec!["AB123FGH"]);
}

#[test]
fn mid_statement_shorter_replacement_keeps_the_tail() {
    let out = run("    Dim s As String\n\
         s = \"Test\"\n\
         Mid(s, 2, 2) = \"XX\"\n\
         Debug.Print s\n");
    assert_eq!(out, vec!["TXXt"]);
}

#[test]
fn midb_statement_replaces_bytes() {
    let out = run("    Dim s As String\n\
         s = \"ABCDEFGH\"\n\
         MidB(s, 3, 4) = \"12\"\n\
         Debug.Print s\n");
    assert_eq!(out, vec!["A12DEFGH"]);
}

#[test]
fn math_functions() {
    let out = run("    Debug.Print Abs(-5)\n\
         Debug.Print Sqr(16)\n\
         Debug.Print Int(3.7)\n\
         Debug.Print Fix(-3.7)\n\
         Debug.Print Sgn(-7)\n\
         Debug.Print Round(123.456, 2)\n\
         Debug.Print Exp(0)\n\
         Debug.Print Log(1)\n");
    assert_eq!(out, vec!["5", "4", "3", "-3", "-1", "123.46", "1", "0"]);
}

#[test]
fn if_elseif_else_block() {
    let out = run("    Dim n As Integer\n\
         n = 7\n\
         If n < 5 Then\n\
         Debug.Print \"low\"\n\
         ElseIf n < 10 Then\n\
         Debug.Print \"mid\"\n\
         Else\n\
         Debug.Print \"high\"\n\
         End If\n\
         If n < 5 Then Debug.Print \"one\" Else Debug.Print \"two\"\n");
    assert_eq!(out, vec!["mid", "two"]);
}

#[test]
fn select_case() {
    let out = run("    Dim a As Long\n\
         a = 4\n\
         Select Case a\n\
         Case 1, 2\n\
         Debug.Print \"low\"\n\
         Case 3 To 5\n\
         Debug.Print \"three-to-five\"\n\
         Case Is > 100\n\
         Debug.Print \"big\"\n\
         Case Else\n\
         Debug.Print \"other\"\n\
         End Select\n");
    assert_eq!(out, vec!["three-to-five"]);
}

#[test]
fn for_loop_with_step() {
    let out = run("    Dim total As Long\n\
         Dim i As Integer\n\
         For i = 1 To 10 Step 2\n\
         total = total + i\n\
         Next i\n\
         Debug.Print total\n\
         Dim j As Integer\n\
         For j = 5 To 1 Step -1\n\
         Debug.Print j\n\
         Next j\n");
    assert_eq!(out, vec!["25", "5", "4", "3", "2", "1"]);
}

#[test]
fn do_and_while_loops() {
    let out = run("    Dim x As Long\n\
         x = 1\n\
         Do While x < 1000\n\
         x = x * 2\n\
         Loop\n\
         Debug.Print x\n\
         x = 1\n\
         Do\n\
         x = x + 1\n\
         Loop Until x >= 5\n\
         Debug.Print x\n\
         Dim n As Integer\n\
         n = 0\n\
         While n < 4\n\
         n = n + 1\n\
         Wend\n\
         Debug.Print n\n");
    assert_eq!(out, vec!["1024", "5", "4"]);
}

#[test]
fn recursion_factorial() {
    let source = "Attribute VB_Name = \"M\"\n\
Function Factorial(n As Integer) As Long\n\
    If n <= 1 Then\n\
        Factorial = 1\n\
    Else\n\
        Factorial = n * Factorial(n - 1)\n\
    End If\n\
End Function\n\
Sub Main()\n\
    Debug.Print Factorial(6)\n\
    Debug.Print Factorial(0)\n\
End Sub\n";
    let out = run_source(source).expect("interpretation failed");
    assert_eq!(out, vec!["720", "1"]);
}

#[test]
fn arrays_and_dim_const() {
    let source = "Attribute VB_Name = \"M\"\n\
Const MAX As Integer = 3\n\
Sub Main()\n\
    Dim a(1 To MAX) As Integer\n\
    Dim total As Long\n\
    a(1) = 10\n\
    a(2) = 20\n\
    a(3) = 30\n\
    For i = 1 To MAX\n\
        total = total + a(i)\n\
    Next i\n\
    Debug.Print total\n\
End Sub\n";
    let out = run_source(source).expect("interpretation failed");
    assert_eq!(out, vec!["60"]);
}

#[test]
fn function_returns_default_when_unset() {
    let source = "Attribute VB_Name = \"M\"\n\
Function Unset() As Integer\n\
End Function\n\
Sub Main()\n\
    Debug.Print Unset()\n\
End Sub\n";
    let out = run_source(source).expect("interpretation failed");
    assert_eq!(out, vec!["0"]);
}

#[test]
fn global_const_and_module_level_init() {
    let source = "Attribute VB_Name = \"M\"\n\
Dim gCount As Integer\n\
Const BASE As Long = 100\n\
Sub Main()\n\
    Debug.Print gCount\n\
    Debug.Print BASE\n\
End Sub\n";
    let out = run_source(source).expect("interpretation failed");
    assert_eq!(out, vec!["0", "100"]);
}

#[test]
fn division_by_zero_reports_line() {
    let source = "Attribute VB_Name = \"M\"\n\
Sub Main()\n\
    Dim x As Double\n\
    x = 1 / 0\n\
End Sub\n";
    let error = run_source(source).expect_err("expected division by zero");
    assert_eq!(error.error.number, 11);
    assert!(error.to_string().contains("line 3"));
}

/// Plan C1: a boundary conversion failure raised while dispatching an
/// *expression-position* builtin (`eval::call_builtin` -> `error_at`) must
/// report the offending source line. `CVErr` produces an Error variant that
/// the `Long` conversion of `Chr$` re-raises verbatim.
#[test]
fn expression_builtin_conversion_failure_reports_line() {
    let source = "Attribute VB_Name = \"M\"\n\
Sub Main()\n\
    Debug.Print Chr$(CVErr(31337))\n\
End Sub\n";
    let error = run_source(source).expect_err("expected CVErr propagation");
    assert_eq!(error.error.number, 31337);
    assert!(error.to_string().contains("line 2"));
}

/// Plan C1: same guarantee for the statement-execution path (`exec_mid_set`
/// converts the `start` operand to `Long` and wraps failures with
/// `error_here`). The Error variant arrives through a variable because Mid
/// operands are flat tokens.
#[test]
fn statement_conversion_failure_reports_line() {
    let source = "Attribute VB_Name = \"M\"\n\
Sub Main()\n\
    Dim s As String\n\
    Dim v As Variant\n\
    v = CVErr(4242)\n\
    Mid(s, v) = \"x\"\n\
End Sub\n";
    let error = run_source(source).expect_err("expected CVErr propagation");
    assert_eq!(error.error.number, 4242);
    assert!(error.to_string().contains("line 5"));
}

/// Plan C1: same guarantee for the flat token-run fallback
/// (`eval_flat_expression` -> `call_builtin` -> `error_here`), reachable via
/// `Set` statements whose right-hand side is dispatched from raw tokens.
#[test]
fn flat_statement_builtin_conversion_failure_reports_line() {
    let source = "Attribute VB_Name = \"M\"\n\
Sub Main()\n\
    Dim v As Variant\n\
    Set v = Chr$(CVErr(777))\n\
End Sub\n";
    let error = run_source(source).expect_err("expected CVErr propagation");
    assert_eq!(error.error.number, 777);
    assert!(error.to_string().contains("line 3"));
}

#[test]
fn print_separators() {
    let out = run("    Debug.Print \"a\"; \"b\"\n    Debug.Print \"c\"\n    Debug.Print \"x\";\n    Debug.Print \"y\"\n");
    assert_eq!(out, vec!["ab", "c", "xy"]);
}

#[test]
fn like_operator() {
    let out = run("    Debug.Print \"Hello\" Like \"H*\"\n\
         Debug.Print \"hello\" Like \"H*\"\n\
         Debug.Print \"abc123\" Like \"???###\"\n\
         Debug.Print \"cat\" Like \"[a-d]at\"\n\
         Debug.Print \"eat\" Like \"[!a-d]at\"\n\
         Debug.Print \"x?y\" Like \"x[?]y\"\n\
         Debug.Print \"[a\" Like \"[[]a\"\n\
         Debug.Print \"hello\" Like \"h[eo]l?o\"\n");
    assert_eq!(
        out,
        vec!["True", "True", "True", "True", "True", "True", "True", "True"]
    );
}

#[test]
fn is_operator() {
    let out = run("    Dim v As Variant\n\
         Debug.Print v Is Nothing\n\
         v = Nothing\n\
         Debug.Print v Is Nothing\n\
         Dim w As Variant\n\
         w = \"x\"\n\
         Debug.Print w Is Nothing\n");
    assert_eq!(out, vec!["False", "True", "False"]);
}

#[test]
fn bitwise_logical_operators() {
    let out = run("    Debug.Print 5 And 3\n\
         Debug.Print 5 Or 2\n\
         Debug.Print 5 Xor 1\n\
         Debug.Print 5 Eqv 3\n\
         Debug.Print True And False\n\
         Debug.Print True Imp False\n");
    assert_eq!(out, vec!["1", "7", "4", "-7", "False", "False"]);
}

#[test]
fn run_final_line_lands_on_end_sub_after_loop() {
    let source = "Attribute VB_Name = \"M\"\n\n\
Sub Main()\n\
    For i = 1 To 2\n\
        Debug.Print i\n\
    Next i\n\
End Sub\n";
    // A normally-completing program ends with the highlight on the `End Sub`
    // line, not the loop's closing keyword or header.
    assert_eq!(run_final_line(source), 7);
}

#[test]
fn run_final_line_lands_on_end_sub_after_while() {
    let source = "Attribute VB_Name = \"M\"\n\n\
Sub Main()\n\
    i = 0\n\
    While i < 2\n\
        i = i + 1\n\
    Wend\n\
End Sub\n";
    assert_eq!(run_final_line(source), 8);
}

#[test]
fn run_final_line_lands_on_end_sub_after_do() {
    let source = "Attribute VB_Name = \"M\"\n\n\
Sub Main()\n\
    i = 0\n\
    Do While i < 2\n\
        i = i + 1\n\
    Loop\n\
End Sub\n";
    assert_eq!(run_final_line(source), 8);
}

#[test]
fn run_final_line_lands_on_end_sub_after_post_test_loop() {
    let source = "Attribute VB_Name = \"M\"\n\n\
Sub Main()\n\
    i = 0\n\
    Do\n\
        i = i + 1\n\
    Loop While i < 2\n\
End Sub\n";
    assert_eq!(run_final_line(source), 8);
}

#[test]
fn run_final_line_lands_on_end_sub_after_function_entry() {
    let source = "Attribute VB_Name = \"M\"\n\n\
Function Answer() As Integer\n\
    Answer = 42\n\
End Function\n\
Sub Main()\n\
    Debug.Print Answer\n\
End Sub\n";
    assert_eq!(run_final_line(source), 8);
}

#[test]
fn irr_passes_whole_array_with_empty_parens() {
    let source = "Attribute VB_Name = \"M\"\n\
Sub Main()\n\
    Dim Guess, RetRate\n\
    Dim Values(5) As Double\n\
    Guess = .1\n\
    Values(0) = -70000\n\
    Values(1) = 22000\n\
    Values(2) = 25000\n\
    Values(3) = 28000\n\
    Values(4) = 31000\n\
    RetRate = IRR(Values(), Guess) * 100\n\
    Debug.Print Format(RetRate, \"0.0\")\n\
End Sub\n";
    let out = run_source(source).expect("interpretation failed");
    assert_eq!(out, vec!["17.7"]);
}

#[test]
fn irr_default_guess_with_empty_parens() {
    let source = "Attribute VB_Name = \"M\"\n\
Sub Main()\n\
    Dim cfs(0 To 3) As Double\n\
    cfs(0) = -1000\n\
    cfs(1) = 400\n\
    cfs(2) = 400\n\
    cfs(3) = 400\n\
    Debug.Print Format(IRR(cfs()), \"0.00\")\n\
End Sub\n";
    let out = run_source(source).expect("interpretation failed");
    assert_eq!(out, vec!["0.10"]);
}

#[test]
fn array_element_indexing_still_works() {
    let source = "Attribute VB_Name = \"M\"\n\
Sub Main()\n\
    Dim a(3) As Integer\n\
    a(0) = 10\n\
    a(1) = 20\n\
    a(2) = 30\n\
    Debug.Print a(1)\n\
    Debug.Print a(0)\n\
End Sub\n";
    let out = run_source(source).expect("interpretation failed");
    assert_eq!(out, vec!["20", "10"]);
}

/// Run a module body with an environment variable assigned before the run.
fn run_with_env(body: &str, name: &str, value: &str) -> Vec<String> {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let source = format!("Attribute VB_Name = \"M\"\nSub Main()\n{body}\nEnd Sub\n");
    let mut interpreter = Interpreter::new();
    interpreter.set_environment(name, value);
    interpreter
        .run_source(&source)
        .expect("interpretation failed");
    interpreter.output().to_vec()
}

#[test]
fn environ_reads_variable_assigned_before_running() {
    let out = run_with_env(
        "    Debug.Print Environ(\"VB6_TEST_VAR\")\n",
        "VB6_TEST_VAR",
        "hello",
    );
    assert_eq!(out, vec!["hello"]);
}

#[test]
fn environ_numeric_argument_enumerates_the_table() {
    let out = run_with_env(
        "    Dim i As Integer\n\
         i = 1\n\
         Do While Environ(i) <> \"\"\n\
             Debug.Print Environ(i)\n\
             i = i + 1\n\
         Loop\n",
        "VB6_TEST_VAR",
        "hello",
    );
    // The interpreter's overrides are appended at the end of the table, so the
    // assigned variable is the final entry and appears in the enumeration.
    assert!(!out.is_empty());
    for entry in &out {
        assert!(entry.contains('='));
    }
    assert_eq!(out.last().map(String::as_str), Some("VB6_TEST_VAR=hello"));
}

#[test]
fn environ_error_for_bad_index() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let source = "Attribute VB_Name = \"M\"\n\
Sub Main()\n\
    Debug.Print Environ(0)\n\
End Sub\n";
    let mut interpreter = Interpreter::new();
    interpreter.set_environment("VB6_TEST_VAR", "hello");
    let error = interpreter
        .run_source(source)
        .expect_err("expected error 5");
    assert_eq!(error.error.number, 5);
}

#[test]
fn environ_dollar_reads_variable_assigned_before_running() {
    let out = run_with_env(
        "    Debug.Print Environ$(\"VB6_TEST_VAR\")\n",
        "VB6_TEST_VAR",
        "hello",
    );
    assert_eq!(out, vec!["hello"]);
}

#[test]
fn environ_dollar_lookup_is_case_insensitive() {
    let out = run_with_env(
        "    Debug.Print Environ$(\"vb6_test_var\")\n",
        "VB6_TEST_VAR",
        "hello",
    );
    assert_eq!(out, vec!["hello"]);
}

#[test]
fn environ_dollar_returns_empty_for_unset_variable() {
    let out = run_with_env(
        "    Debug.Print \"[\" & Environ$(\"VB6_TEST_MISSING\") & \"]\"\n",
        "VB6_TEST_VAR",
        "hello",
    );
    assert_eq!(out, vec!["[]"]);
}

#[test]
fn environ_dollar_numeric_argument_enumerates_the_table() {
    let out = run_with_env(
        "    Dim i As Integer\n\
         i = 1\n\
         Do While Environ$(i) <> \"\"\n\
             Debug.Print Environ$(i)\n\
             i = i + 1\n\
         Loop\n",
        "VB6_TEST_VAR",
        "hello",
    );
    // The interpreter's overrides are appended at the end of the table, so the
    // assigned variable is the final entry and appears in the enumeration.
    assert!(!out.is_empty());
    for entry in &out {
        assert!(entry.contains('='));
    }
    assert_eq!(out.last().map(String::as_str), Some("VB6_TEST_VAR=hello"));
}

#[test]
fn environ_dollar_assignment_survives_repeated_runs() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let mut interpreter = Interpreter::new();
    interpreter.set_environment("VB6_TEST_VAR", "one");
    interpreter
        .run_source(
            "Attribute VB_Name = \"M\"\nSub Main()\n    Debug.Print Environ$(\"VB6_TEST_VAR\")\nEnd Sub\n",
        )
        .expect("interpretation failed");
    assert_eq!(interpreter.output().to_vec(), vec!["one"]);

    interpreter
        .run_source(
            "Attribute VB_Name = \"M\"\nSub Main()\n    Debug.Print Environ$(\"VB6_TEST_VAR\")\nEnd Sub\n",
        )
        .expect("interpretation failed");
    assert_eq!(interpreter.output().to_vec(), vec!["one"]);
}

#[test]
fn environ_dollar_error_for_bad_index() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let source = "Attribute VB_Name = \"M\"\n\
Sub Main()\n\
    Debug.Print Environ$(0)\n\
End Sub\n";
    let mut interpreter = Interpreter::new();
    interpreter.set_environment("VB6_TEST_VAR", "hello");
    let error = interpreter
        .run_source(source)
        .expect_err("expected error 5");
    assert_eq!(error.error.number, 5);
}

#[test]
fn get_setting_reads_from_the_settings_store() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let dir = tempfile::tempdir().expect("failed to create temp dir");
    settings_state::set_store_root(dir.path());
    settings_state::set("MyApp", "Startup", "Left", "150").unwrap();

    let out = run(
        "    Debug.Print GetSetting(\"MyApp\", \"Startup\", \"Left\", \"0\")\n\
         Debug.Print GetSetting(\"MyApp\", \"Startup\", \"Missing\", \"42\")\n\
         Debug.Print GetSetting(\"myapp\", \"startup\", \"left\")\n",
    );
    assert_eq!(out, vec!["150", "42", "150"]);

    settings_state::reset_store_root();
}

/// Redirect the shared settings store to a fresh temp directory for the
/// duration of the test, restoring the default root on drop so later tests
/// never touch the user's real settings.
struct TempSettingsStore {
    _dir: tempfile::TempDir,
}

impl TempSettingsStore {
    fn new() -> Self {
        let dir = tempfile::tempdir().expect("failed to create temp dir");
        settings_state::set_store_root(dir.path());
        Self { _dir: dir }
    }
}

impl Drop for TempSettingsStore {
    fn drop(&mut self) {
        settings_state::reset_store_root();
    }
}

/// Run a module body in a fresh interpreter whose settings were staged
/// beforehand, and return the captured `Debug.Print` output.
fn run_with_settings(body: &str, setup: impl FnOnce(&mut Interpreter)) -> Vec<String> {
    let source = format!("Attribute VB_Name = \"M\"\nSub Main()\n{body}\nEnd Sub\n");
    let mut interpreter = Interpreter::new();
    setup(&mut interpreter);
    interpreter
        .run_source(&source)
        .expect("interpretation failed");
    interpreter.output().to_vec()
}

#[test]
fn staged_settings_are_visible_to_getsetting_during_a_run() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let _store = TempSettingsStore::new();
    let out = run_with_settings(
        "    Debug.Print GetSetting(\"MyApp\", \"Startup\", \"Left\", \"0\")\n",
        |i| i.set_setting("MyApp", "Startup", "Left", "150"),
    );
    assert_eq!(out, vec!["150"]);
}

#[test]
fn get_setting_returns_staged_values_before_and_after_a_run() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let _store = TempSettingsStore::new();
    let mut interpreter = Interpreter::new();
    interpreter.set_setting("MyApp", "Startup", "Left", "150");
    assert_eq!(
        interpreter.get_setting("MyApp", "Startup", "Left"),
        Some("150".to_string())
    );
    assert_eq!(interpreter.get_setting("MyApp", "Startup", "Missing"), None);
    interpreter
        .run_source("Attribute VB_Name = \"M\"\nSub Main()\nEnd Sub\n")
        .expect("interpretation failed");
    assert_eq!(
        interpreter.get_setting("myapp", "startup", "left"),
        Some("150".to_string())
    );
}

#[test]
fn staged_settings_override_values_already_in_the_store() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let _store = TempSettingsStore::new();
    settings_state::set("MyApp", "Startup", "Left", "150").unwrap();
    let out = run_with_settings(
        "    Debug.Print GetSetting(\"MyApp\", \"Startup\", \"Left\", \"0\")\n",
        |i| i.set_setting("MyApp", "Startup", "Left", "200"),
    );
    assert_eq!(out, vec!["200"]);
}

#[test]
fn remove_setting_removes_staged_and_store_values() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let _store = TempSettingsStore::new();
    let mut interpreter = Interpreter::new();
    interpreter.set_setting("MyApp", "Startup", "Left", "150");
    interpreter.set_setting("MyApp", "Startup", "Right", "300");
    interpreter.remove_setting("MyApp", "Startup", "Left");
    assert_eq!(interpreter.get_setting("MyApp", "Startup", "Left"), None);
    assert_eq!(
        interpreter.get_setting("MyApp", "Startup", "Right"),
        Some("300".to_string())
    );
    // The store value was written during a run, so it must be gone too.
    interpreter
        .run_source("Attribute VB_Name = \"M\"\nSub Main()\nEnd Sub\n")
        .expect("interpretation failed");
    interpreter.remove_setting("MyApp", "Startup", "Right");
    assert_eq!(interpreter.get_setting("MyApp", "Startup", "Right"), None);
}

#[test]
fn clear_settings_removes_staged_and_store_values() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let _store = TempSettingsStore::new();
    let mut interpreter = Interpreter::new();
    interpreter.set_setting("MyApp", "Startup", "Left", "150");
    interpreter.set_setting("MyApp", "Startup", "Right", "300");
    interpreter.clear_settings();
    assert_eq!(interpreter.get_setting("MyApp", "Startup", "Left"), None);
    assert_eq!(interpreter.get_setting("MyApp", "Startup", "Right"), None);
}

#[test]
fn staged_settings_survive_clear() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let _store = TempSettingsStore::new();
    let mut interpreter = Interpreter::new();
    interpreter.set_setting("MyApp", "Startup", "Left", "150");
    interpreter.clear();
    assert_eq!(
        interpreter.get_setting("MyApp", "Startup", "Left"),
        Some("150".to_string())
    );
    interpreter
        .run_source(
            "Attribute VB_Name = \"M\"\n\
             Sub Main()\n\
                 Debug.Print GetSetting(\"MyApp\", \"Startup\", \"Left\", \"0\")\n\
             End Sub\n",
        )
        .expect("interpretation failed");
    assert_eq!(interpreter.output(), &["150"]);
}

#[test]
fn set_settings_backend_switches_to_new_backend() {
    use vb6runtime::state::settings::memory::MemoryBackend;

    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let interpreter = Interpreter::new();

    // Switch to memory backend
    interpreter.set_settings_backend(Box::new(MemoryBackend::new()));

    // Set a value in the memory backend
    settings_state::set("MyApp", "TestSection", "TestKey", "MemValue").unwrap();

    // Verify it's accessible
    let out = run_with_settings(
        "    Debug.Print GetSetting(\"MyApp\", \"TestSection\", \"TestKey\", \"default\")\n",
        |_| {},
    );
    assert_eq!(out, vec!["MemValue"]);

    // Reset backend
    interpreter.reset_settings_backend();
}

#[test]
fn reset_settings_backend_restores_default() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let _store = TempSettingsStore::new();
    let interpreter = Interpreter::new();

    // Set a value in the file backend
    settings_state::set("MyApp", "TestSection", "TestKey", "FileValue").unwrap();

    // Verify it works
    let out = run_with_settings(
        "    Debug.Print GetSetting(\"MyApp\", \"TestSection\", \"TestKey\", \"default\")\n",
        |_| {},
    );
    assert_eq!(out, vec!["FileValue"]);

    // Reset backend (should restore default)
    interpreter.reset_settings_backend();
}

/// The `Stop` test module: a global is set and printed before `Stop`, and
/// another print follows it (which must never run).
const STOP_SOURCE: &str = "Attribute VB_Name = \"M\"\n\
     Dim gX As Integer\n\
     Sub Main()\n        \
     gX = 42\n        \
     Debug.Print \"before\"\n        \
     Stop\n        \
     Debug.Print \"after\"\n    \
     End Sub\n";

#[test]
fn stop_terminates_like_end_outside_a_debugger() {
    let source_file = SourceFile::from_string("scratch.bas", STOP_SOURCE);
    let module = ModuleFile::parse(&source_file).unwrap_or_fail();
    let mut interpreter = Interpreter::new();
    interpreter.run_module(&module).unwrap();

    // Compiled-`.exe` behavior: `Stop` acts like `End`.
    assert!(interpreter.is_terminated());
    assert_eq!(interpreter.output(), vec!["before".to_string()]);
}

#[test]
fn stop_enters_break_mode_with_a_debugger_attached() {
    let source_file = SourceFile::from_string("scratch.bas", STOP_SOURCE);
    let module = ModuleFile::parse(&source_file).unwrap_or_fail();
    let mut interpreter = Interpreter::new();
    interpreter.set_record_debug_snapshots(true);
    let error = interpreter.run_module(&module).unwrap_err();

    // Development-environment behavior: suspend execution (break mode).
    assert!(error.is_debug_pause());
    assert_eq!(error.line, Some(5));
    assert_eq!(error.procedure.as_deref(), Some("Main"));

    // Unlike `End`, no files are closed and no variables cleared.
    assert_eq!(
        interpreter.global("gX").and_then(|v| v.as_i32().ok()),
        Some(42)
    );
    assert_eq!(interpreter.output(), vec!["before".to_string()]);
}

// ---- MsgBox ----

use vb6runtime::state::interaction::{
    self, memory::MemoryBackend as InteractionMemory, MsgBoxButton,
};

/// Run a module with a scripted interaction backend installed; returns the
/// output lines and the recorded MsgBox requests.
fn run_with_msgbox_responses(
    body: &str,
    responses: Vec<MsgBoxButton>,
) -> (
    Vec<String>,
    Vec<vb6runtime::state::interaction::MsgBoxRecord>,
) {
    let source = format!("Attribute VB_Name = \"M\"\nSub Main()\n{}\nEnd Sub\n", body);
    let source_file = SourceFile::from_string("scratch.bas", source);
    let module = ModuleFile::parse(&source_file).unwrap_or_fail();
    let mut interpreter = Interpreter::new();
    interpreter.set_interaction_backend(Box::new(InteractionMemory::with_msgbox_responses(
        responses,
    )));
    interpreter
        .run_module(&module)
        .expect("interpretation failed");
    let out = interpreter.output().to_vec();
    let requests = interaction::with_memory_backend(|m| m.take_msgbox_requests())
        .expect("memory interaction backend installed");
    interpreter.reset_interaction_backend();
    (out, requests)
}

#[test]
fn msgbox_function_returns_scripted_buttons_in_order() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let (out, requests) = run_with_msgbox_responses(
        "    Dim r As Integer\n\
         \x20   r = MsgBox(\"Save changes?\", vbYesNo + vbQuestion, \"Confirm\")\n\
         \x20   Debug.Print \"r=\" & r\n\
         \x20   r = MsgBox(\"Really quit?\", vbYesNo)\n\
         \x20   Debug.Print \"r=\" & r\n",
        vec![MsgBoxButton::Yes, MsgBoxButton::No],
    );
    assert_eq!(out, vec!["r=6", "r=7"]); // vbYes, then vbNo

    // Both dialogs were recorded with their offered buttons.
    assert_eq!(requests.len(), 2);
    assert_eq!(requests[0].prompt, "Save changes?");
    assert_eq!(requests[0].title.as_deref(), Some("Confirm"));
    assert_eq!(
        requests[0].offered_buttons,
        vec![MsgBoxButton::Yes, MsgBoxButton::No]
    );
    assert_eq!(requests[1].prompt, "Really quit?");
}

#[test]
fn msgbox_statement_defaults_without_scripting() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    // No queued responses: the dialog auto-answers with its default button
    // so non-interactive runs keep going.
    let (out, requests) = run_with_msgbox_responses(
        "    MsgBox \"Done.\", vbInformation\n\
         \x20   Debug.Print \"after\"\n",
        vec![],
    );
    assert_eq!(out, vec!["after"]);
    assert_eq!(requests.len(), 1);
    assert_eq!(requests[0].default_button, MsgBoxButton::Ok);
}

#[test]
fn msgbox_mismatched_scripted_response_is_a_runtime_error() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let source = "Attribute VB_Name = \"M\"\nSub Main()\n\
         \x20   Dim r As Integer\n\
         \x20   r = MsgBox(\"Overwrite file?\", vbYesNo)\n\
         \x20   Debug.Print \"r=\" & r\n\
         End Sub\n";
    let source_file = SourceFile::from_string("scratch.bas", source);
    let module = ModuleFile::parse(&source_file).unwrap_or_fail();
    let mut interpreter = Interpreter::new();
    // The dialog offers Yes/No but the script answers Cancel: error 5.
    interpreter.set_interaction_backend(Box::new(InteractionMemory::with_msgbox_responses([
        MsgBoxButton::Cancel,
    ])));
    let error = interpreter.run_module(&module).unwrap_err();
    interpreter.reset_interaction_backend();

    assert_eq!(error.error.number, 5);
    assert!(error.error.description.contains("Cancel"));
    // The program stopped before printing.
    assert_eq!(interpreter.output(), Vec::<String>::new());
}

// ---- SendKeys ----

use vb6runtime::state::interaction::SendKeysRecord;

/// Run a module with a scripted interaction backend installed; returns the
/// output lines and the recorded `SendKeys` requests.
fn run_with_sendkeys(body: &str, responses: Vec<bool>) -> (Vec<String>, Vec<SendKeysRecord>) {
    let source = format!("Attribute VB_Name = \"M\"\nSub Main()\n{}\nEnd Sub\n", body);
    let source_file = SourceFile::from_string("scratch.bas", source);
    let module = ModuleFile::parse(&source_file).unwrap_or_fail();
    let mut interpreter = Interpreter::new();
    interpreter.set_interaction_backend(Box::new(InteractionMemory::with_sendkeys_responses(
        responses,
    )));
    interpreter
        .run_module(&module)
        .expect("interpretation failed");
    let out = interpreter.output().to_vec();
    let requests = interaction::with_memory_backend(|m| m.take_sendkeys_requests())
        .expect("memory interaction backend installed");
    interpreter.reset_interaction_backend();
    (out, requests)
}

#[test]
fn sendkeys_statement_reaches_the_memory_backend_verbatim() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let (out, requests) = run_with_sendkeys(
        "    AppActivate \"Notepad\"\n\
         \x20   SendKeys \"John Doe{TAB}555-1234{ENTER}\", True\n\
         \x20   SendKeys \"^s\"\n\
         \x20   Debug.Print \"sent\"\n",
        vec![],
    );
    assert_eq!(out, vec!["sent"]);

    assert_eq!(requests.len(), 2);
    assert_eq!(requests[0].keys, "John Doe{TAB}555-1234{ENTER}");
    assert!(requests[0].wait);
    assert_eq!(requests[1].keys, "^s");
    assert!(!requests[1].wait);
}

#[test]
fn sendkeys_malformed_key_string_is_a_runtime_error() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let source = "Attribute VB_Name = \"M\"\nSub Main()\n\
         \x20   SendKeys \"{BOGUS}\"\n\
         \x20   Debug.Print \"after\"\n\
         End Sub\n";
    let source_file = SourceFile::from_string("scratch.bas", source);
    let module = ModuleFile::parse(&source_file).unwrap_or_fail();
    let mut interpreter = Interpreter::new();
    interpreter.set_interaction_backend(Box::new(InteractionMemory::new()));
    let error = interpreter.run_module(&module).unwrap_err();
    interpreter.reset_interaction_backend();

    assert_eq!(error.error.number, 5);
    assert!(error.error.description.contains("{BOGUS}"));
    // The program stopped before printing.
    assert_eq!(interpreter.output(), Vec::<String>::new());
}

#[test]
fn appactivate_accepts_a_bare_true_wait_argument() {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let source = "Attribute VB_Name = \"M\"\nSub Main()\n\
         \x20   AppActivate \"Calculator\", True\n\
         End Sub\n";
    let source_file = SourceFile::from_string("scratch.bas", source);
    let module = ModuleFile::parse(&source_file).unwrap_or_fail();
    let mut interpreter = Interpreter::new();
    interpreter.set_interaction_backend(Box::new(InteractionMemory::new()));
    interpreter
        .run_module(&module)
        .expect("interpretation failed");
    let requests = interaction::with_memory_backend(|m| m.take_appactivate_requests())
        .expect("memory interaction backend installed");
    interpreter.reset_interaction_backend();

    assert_eq!(requests.len(), 1);
    assert_eq!(requests[0].title, "Calculator");
    assert!(requests[0].wait);
}

// ---- SavePicture ----

use vb6runtime::state::file as file_state;

/// A minimal valid 1x1 24-bit BMP, as `SavePicture` itself would produce.
fn one_pixel_bmp() -> Vec<u8> {
    let mut bytes = Vec::new();
    bytes.extend_from_slice(b"BM");
    bytes.extend_from_slice(&58u32.to_le_bytes()); // file size
    bytes.extend_from_slice(&0u32.to_le_bytes()); // reserved
    bytes.extend_from_slice(&54u32.to_le_bytes()); // pixel data offset
    bytes.extend_from_slice(&40u32.to_le_bytes()); // info header size
    bytes.extend_from_slice(&1u32.to_le_bytes()); // width
    bytes.extend_from_slice(&1u32.to_le_bytes()); // height
    bytes.extend_from_slice(&1u16.to_le_bytes()); // planes
    bytes.extend_from_slice(&24u16.to_le_bytes()); // bpp
    bytes.extend_from_slice(&0u32.to_le_bytes()); // compression
    bytes.extend_from_slice(&4u32.to_le_bytes()); // image size
    bytes.extend_from_slice(&2835u32.to_le_bytes());
    bytes.extend_from_slice(&2835u32.to_le_bytes());
    bytes.extend_from_slice(&0u32.to_le_bytes());
    bytes.extend_from_slice(&0u32.to_le_bytes());
    bytes.extend_from_slice(&[0xFF, 0xFF, 0xFF, 0x00]); // padded pixel row
    bytes
}

/// Installs a temporary directory as the file root for the duration of `f`,
/// serialized against the other shared-snapshot tests.
fn with_temp_file_root<T>(f: impl FnOnce(&std::path::Path) -> T) -> T {
    let _guard = ENV_TEST_LOCK.lock().unwrap();
    let dir = tempfile::tempdir().expect("failed to create temp dir");
    file_state::reset_with_root(dir.path());
    let result = f(dir.path());
    file_state::reset();
    result
}

#[test]
fn savepicture_statement_writes_a_bitmap_file() {
    with_temp_file_root(|dir| {
        std::fs::write(dir.join("in.bmp"), one_pixel_bmp()).unwrap();

        let out = run("    Dim p As Object\n\
             \x20   Set p = LoadPicture(\"in.bmp\")\n\
             \x20   SavePicture p, \"out.bmp\"\n\
             \x20   Debug.Print Dir(\"out.bmp\") <> \"\"\n");
        assert_eq!(out, vec!["True"]);

        let bytes = std::fs::read(dir.join("out.bmp")).unwrap();
        assert_eq!(&bytes[0..2], b"BM");
        assert_eq!(bytes.len(), 54 + 4); // header + 1 padded row of 1 px
    });
}

#[test]
fn savepicture_statement_overwrites_an_existing_file() {
    with_temp_file_root(|dir| {
        std::fs::write(dir.join("in.bmp"), one_pixel_bmp()).unwrap();
        std::fs::write(dir.join("out.bmp"), b"stale").unwrap();

        let out = run("    Dim p As Object\n\
             \x20   Set p = LoadPicture(\"in.bmp\")\n\
             \x20   SavePicture p, \"out.bmp\"\n\
             \x20   Debug.Print FileLen(\"out.bmp\") > 4\n");
        assert_eq!(out, vec!["True"]);
    });
}

#[test]
fn savepicture_statement_with_a_non_picture_value_is_a_type_mismatch() {
    let source = "Attribute VB_Name = \"M\"\nSub Main()\n\
         \x20   SavePicture 42, \"out.bmp\"\n\
         \x20   Debug.Print \"after\"\n\
         End Sub\n";
    let error = vb6interpret::run_source(source).unwrap_err();

    assert_eq!(error.error.number, 13);
}

#[test]
fn savepicture_statement_with_nothing_raises_object_variable_not_set() {
    with_temp_file_root(|_| {
        let source = "Attribute VB_Name = \"M\"\nSub Main()\n\
             \x20   Dim p As Object\n\
             \x20   SavePicture p, \"out.bmp\"\n\
             \x20   Debug.Print \"after\"\n\
             End Sub\n";
        let error = vb6interpret::run_source(source).unwrap_err();

        assert_eq!(error.error.number, 91);
    });
}