vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
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
//! # `CVErr` Function
//!
//! Returns a `Variant` of subtype Error containing an error number.
//!
//! ## Syntax
//!
//! ```vb
//! CVErr(errornumber)
//! ```
//!
//! ## Parameters
//!
//! - **`errornumber`**: Required. `Long` integer that identifies an error. The valid range is from
//!   0 to 65535, though application-defined errors are typically in the range 513-65535 (VB6
//!   uses 1-512 for system errors).
//!
//! ## Return Value
//!
//! Returns a `Variant` of subtype `Error` (`VarType = 10`) containing the specified error number.
//! This is not the same as raising an error with `Err.Raise`; instead, it creates an `Error`
//! value that can be assigned to variables and returned from functions.
//!
//! ## Remarks
//!
//! The `CVErr` function is used to create user-defined error values that can be returned from
//! functions or assigned to `Variant` variables. This is particularly useful for:
//!
//! - Returning error conditions from functions without raising exceptions
//! - Creating functions that behave like Excel worksheet functions (returning error values)
//! - Signaling invalid results that should propagate through calculations
//! - Implementing error handling in data processing pipelines
//!
//! **Important Characteristics:**
//!
//! - Returns a `Variant` of subtype `Error` (not an exception)
//! - `Error` values propagate through expressions
//! - Can be tested with `IsError()` function
//! - Not the same as `Err` object or `Err.Raise`
//! - Commonly used with VBA functions called from Excel
//! - `Error` values cannot be used in arithmetic operations
//! - `VarType` of `CVErr` result is 10 (vbError)
//!
//! ## Error Number Ranges
//!
//! | Range | Description |
//! |-------|-------------|
//! | 0-512 | Reserved for VB6 system errors |
//! | 513-65535 | Available for application-defined errors |
//! | 2007-2042 | Excel error values (when used in Excel automation) |
//!
//! ## Excel Error Constants
//!
//! When creating functions for Excel, these error values are commonly used:
//!
//! | Constant | Value | Excel Display | Meaning |
//! |----------|-------|---------------|---------|
//! | xlErrDiv0 | 2007 | #DIV/0! | Division by zero |
//! | xlErrNA | 2042 | #N/A | Value not available |
//! | xlErrName | 2029 | #NAME? | Invalid name |
//! | xlErrNull | 2000 | #NULL! | Null intersection |
//! | xlErrNum | 2036 | #NUM! | Invalid number |
//! | xlErrRef | 2023 | #REF! | Invalid reference |
//! | xlErrValue | 2015 | #VALUE! | Wrong type |
//!
//! ## Examples
//!
//! ### Basic Usage
//!
//! ```vb
//! ' Create an error value
//! Dim result As Variant
//! result = CVErr(2042)  ' Create #N/A error
//!
//! ' Test if value is an error
//! If IsError(result) Then
//!     MsgBox "Result is an error"
//! End If
//! ```
//!
//! ### Function Returning Error on Invalid Input
//!
//! ```vb
//! Function SafeDivide(numerator As Double, denominator As Double) As Variant
//!     If denominator = 0 Then
//!         SafeDivide = CVErr(2007)  ' #DIV/0!
//!     Else
//!         SafeDivide = numerator / denominator
//!     End If
//! End Function
//!
//! ' Usage
//! Dim result As Variant
//! result = SafeDivide(10, 0)
//! If IsError(result) Then
//!     MsgBox "Division error occurred"
//! Else
//!     MsgBox "Result: " & result
//! End If
//! ```
//!
//! ### Excel UDF with Error Handling
//!
//! ```vb
//! Function Lookup(value As Variant, table As Range) As Variant
//!     Dim cell As Range
//!     
//!     ' Validate input
//!     If IsEmpty(value) Then
//!         Lookup = CVErr(2042)  ' #N/A
//!         Exit Function
//!     End If
//!     
//!     ' Search for value
//!     For Each cell In table
//!         If cell.Value = value Then
//!             Lookup = cell.Offset(0, 1).Value
//!             Exit Function
//!         End If
//!     Next cell
//!     
//!     ' Not found
//!     Lookup = CVErr(2042)  ' #N/A
//! End Function
//! ```
//!
//! ## Common Patterns
//!
//! ### Error Constants Definition
//!
//! ```vb
//! ' Define Excel error constants
//! Public Const xlErrDiv0 As Long = 2007   ' #DIV/0!
//! Public Const xlErrNA As Long = 2042     ' #N/A
//! Public Const xlErrName As Long = 2029   ' #NAME?
//! Public Const xlErrNull As Long = 2000   ' #NULL!
//! Public Const xlErrNum As Long = 2036    ' #NUM!
//! Public Const xlErrRef As Long = 2023    ' #REF!
//! Public Const xlErrValue As Long = 2015  ' #VALUE!
//!
//! ' Use in functions
//! Function MyFunction(input As Variant) As Variant
//!     If Not IsNumeric(input) Then
//!         MyFunction = CVErr(xlErrValue)
//!     Else
//!         MyFunction = input * 2
//!     End If
//! End Function
//! ```
//!
//! ### Validation Functions
//!
//! ```vb
//! Function ValidateRange(value As Double, min As Double, max As Double) As Variant
//!     If value < min Or value > max Then
//!         ValidateRange = CVErr(2036)  ' #NUM! - Number out of range
//!     Else
//!         ValidateRange = value
//!     End If
//! End Function
//! ```
//!
//! ### Error Propagation
//!
//! ```vb
//! Function Calculate(input As Variant) As Variant
//!     ' Check if input is already an error
//!     If IsError(input) Then
//!         Calculate = input  ' Propagate the error
//!         Exit Function
//!     End If
//!     
//!     ' Perform calculation
//!     If input < 0 Then
//!         Calculate = CVErr(2036)  ' #NUM!
//!     Else
//!         Calculate = Sqr(input)
//!     End If
//! End Function
//! ```
//!
//! ### Database Lookup with Error
//!
//! ```vb
//! Function GetEmployeeName(employeeID As Long) As Variant
//!     Dim rs As ADODB.Recordset
//!     
//!     On Error GoTo ErrorHandler
//!     
//!     Set rs = New ADODB.Recordset
//!     rs.Open "SELECT Name FROM Employees WHERE ID = " & employeeID, conn
//!     
//!     If rs.EOF Then
//!         GetEmployeeName = CVErr(2042)  ' #N/A - Not found
//!     Else
//!         GetEmployeeName = rs("Name")
//!     End If
//!     
//!     rs.Close
//!     Set rs = Nothing
//!     Exit Function
//!     
//! ErrorHandler:
//!     GetEmployeeName = CVErr(2042)  ' #N/A
//! End Function
//! ```
//!
//! ### Array Formula with Errors
//!
//! ```vb
//! Function ProcessArray(values As Variant) As Variant
//!     Dim results() As Variant
//!     Dim i As Long
//!     
//!     If Not IsArray(values) Then
//!         ProcessArray = CVErr(2015)  ' #VALUE!
//!         Exit Function
//!     End If
//!     
//!     ReDim results(LBound(values) To UBound(values))
//!     
//!     For i = LBound(values) To UBound(values)
//!         If IsError(values(i)) Then
//!             results(i) = values(i)  ' Propagate error
//!         ElseIf Not IsNumeric(values(i)) Then
//!             results(i) = CVErr(2015)  ' #VALUE!
//!         Else
//!             results(i) = values(i) * 2
//!         End If
//!     Next i
//!     
//!     ProcessArray = results
//! End Function
//! ```
//!
//! ### Type Conversion with Error
//!
//! ```vb
//! Function SafeCLng(value As Variant) As Variant
//!     On Error GoTo ErrorHandler
//!     
//!     If IsError(value) Then
//!         SafeCLng = value  ' Propagate error
//!     ElseIf IsEmpty(value) Then
//!         SafeCLng = CVErr(2042)  ' #N/A
//!     ElseIf Not IsNumeric(value) Then
//!         SafeCLng = CVErr(2015)  ' #VALUE!
//!     Else
//!         Dim temp As Double
//!         temp = CDbl(value)
//!         If temp < -2147483648# Or temp > 2147483647# Then
//!             SafeCLng = CVErr(2036)  ' #NUM!
//!         Else
//!             SafeCLng = CLng(value)
//!         End If
//!     End If
//!     Exit Function
//!     
//! ErrorHandler:
//!     SafeCLng = CVErr(2015)  ' #VALUE!
//! End Function
//! ```
//!
//! ### Conditional Error Return
//!
//! ```vb
//! Function GetDiscount(totalSales As Double) As Variant
//!     Select Case totalSales
//!         Case Is < 0
//!             GetDiscount = CVErr(2036)  ' #NUM! - Negative sales
//!         Case 0 To 999.99
//!             GetDiscount = 0
//!         Case 1000 To 4999.99
//!             GetDiscount = 0.05
//!         Case 5000 To 9999.99
//!             GetDiscount = 0.1
//!         Case Is >= 10000
//!             GetDiscount = 0.15
//!         Case Else
//!             GetDiscount = CVErr(2042)  ' #N/A
//!     End Select
//! End Function
//! ```
//!
//! ### Error Checking Helper
//!
//! ```vb
//! Function GetErrorNumber(value As Variant) As Long
//!     ' Returns error number or 0 if not an error
//!     If IsError(value) Then
//!         ' There's no direct way to extract error number in VB6
//!         ' Would need to compare against known error values
//!         GetErrorNumber = -1  ' Indicates error present
//!     Else
//!         GetErrorNumber = 0
//!     End If
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### Custom Error Type System
//!
//! ```vb
//! ' Application-specific error codes
//! Public Const APP_ERR_INVALID_USER As Long = 1000
//! Public Const APP_ERR_DATABASE As Long = 1001
//! Public Const APP_ERR_NETWORK As Long = 1002
//! Public Const APP_ERR_PERMISSION As Long = 1003
//!
//! Function AuthenticateUser(username As String, password As String) As Variant
//!     If Len(username) = 0 Then
//!         AuthenticateUser = CVErr(APP_ERR_INVALID_USER)
//!         Exit Function
//!     End If
//!     
//!     ' Check credentials
//!     If Not ValidateCredentials(username, password) Then
//!         AuthenticateUser = CVErr(APP_ERR_PERMISSION)
//!         Exit Function
//!     End If
//!     
//!     ' Return user object on success
//!     AuthenticateUser = GetUserObject(username)
//! End Function
//! ```
//!
//! ### Error Value in Collections
//!
//! ```vb
//! Function ProcessRecords() As Collection
//!     Dim results As New Collection
//!     Dim rs As ADODB.Recordset
//!     Dim i As Long
//!     
//!     Set rs = GetRecords()
//!     
//!     While Not rs.EOF
//!         On Error Resume Next
//!         results.Add ProcessRecord(rs)
//!         
//!         If Err.Number <> 0 Then
//!             results.Add CVErr(2015)  ' Add error marker
//!             Err.Clear
//!         End If
//!         
//!         rs.MoveNext
//!     Wend
//!     
//!     Set ProcessRecords = results
//! End Function
//! ```
//!
//! ### Chainable Operations with Error Propagation
//!
//! ```vb
//! Function Step1(input As Variant) As Variant
//!     If IsError(input) Then
//!         Step1 = input
//!     ElseIf input < 0 Then
//!         Step1 = CVErr(2036)
//!     Else
//!         Step1 = Sqr(input)
//!     End If
//! End Function
//!
//! Function Step2(input As Variant) As Variant
//!     If IsError(input) Then
//!         Step2 = input
//!     ElseIf input = 0 Then
//!         Step2 = CVErr(2007)
//!     Else
//!         Step2 = 100 / input
//!     End If
//! End Function
//!
//! ' Chain operations
//! result = Step2(Step1(value))
//! ```
//!
//! ## Error Handling
//!
//! ```vb
//! Function CreateErrorSafe(errorNum As Long) As Variant
//!     On Error GoTo ErrorHandler
//!     
//!     If errorNum < 0 Or errorNum > 65535 Then
//!         CreateErrorSafe = CVErr(2036)  ' Invalid error number
//!     Else
//!         CreateErrorSafe = CVErr(errorNum)
//!     End If
//!     
//!     Exit Function
//!     
//! ErrorHandler:
//!     CreateErrorSafe = CVErr(2042)  ' Generic error
//! End Function
//! ```
//!
//! ### Common Errors
//!
//! - **Error 13** (Type mismatch): Error number is not a valid Long integer
//! - **Error 6** (Overflow): Error number is outside valid range (0-65535)
//!
//! ## Performance Considerations
//!
//! - `CVErr` is a fast function with minimal overhead
//! - `Error` values are lightweight `Variant` subtypes
//! - Using `CVErr` is more efficient than raising and catching exceptions
//! - `Error` propagation through calculations is automatic
//! - No significant memory overhead compared to other `Variant` values
//!
//! ## Comparison with Other Error Mechanisms
//!
//! ### `CVErr` vs `Err.Raise`
//!
//! ```vb
//! ' CVErr - Returns error value (doesn't stop execution)
//! Function Method1(x As Double) As Variant
//!     If x < 0 Then
//!         Method1 = CVErr(2036)  ' Returns error, continues
//!     Else
//!         Method1 = Sqr(x)
//!     End If
//! End Function
//!
//! ' Err.Raise - Throws exception (stops execution)
//! Function Method2(x As Double) As Double
//!     If x < 0 Then
//!         Err.Raise 5, , "Invalid argument"  ' Stops execution
//!     Else
//!         Method2 = Sqr(x)
//!     End If
//! End Function
//! ```
//!
//! **`CVErr` advantages:**
//! - Doesn't interrupt program flow
//! - Can be used in expressions
//! - Natural for functional-style programming
//! - Compatible with Excel worksheet functions
//!
//! **`Err.Raise` advantages:**
//! - Forces immediate attention to errors
//! - Provides error description and source
//! - Traditional exception handling model
//! - Better for critical errors
//!
//! ## Best Practices
//!
//! ### Always Check for Errors Before Using Values
//!
//! ```vb
//! Dim result As Variant
//! result = SomeFunction()
//!
//! If IsError(result) Then
//!     MsgBox "Error occurred"
//! Else
//!     ' Safe to use result
//!     Debug.Print result
//! End If
//! ```
//!
//! ### Use Meaningful Error Numbers
//!
//! ```vb
//! ' Good - Use named constants
//! Const ERR_INVALID_INPUT As Long = 1000
//! result = CVErr(ERR_INVALID_INPUT)
//!
//! ' Avoid - Magic numbers
//! result = CVErr(42)  ' What does 42 mean?
//! ```
//!
//! ### Document Custom Error Codes
//!
//! ```vb
//! ' Application Error Codes (1000-1999)
//! Public Const ERR_INVALID_USER As Long = 1000    ' Invalid username
//! Public Const ERR_EXPIRED_SESSION As Long = 1001  ' Session expired
//! Public Const ERR_INSUFFICIENT_RIGHTS As Long = 1002  ' Access denied
//! ```
//!
//! ## Limitations
//!
//! - Cannot extract error number from error value directly in VB6
//! - `Error` values cannot be used in arithmetic operations
//! - Limited to Long integer error numbers (0-65535)
//! - No built-in error description with `CVErr` (unlike `Err` object)
//! - `VarType` test required to detect errors (`IsError` function)
//! - Not all VB6 functions handle error values gracefully
//!
//! ## Related Functions
//!
//! - `IsError`: Tests if a Variant contains an error value
//! - `Err.Raise`: Raises a runtime error (different from `CVErr`)
//! - `Error`: Returns error message for an error number
//! - `Error$`: `String` version of `Error` function
//! - `VarType`: Returns the subtype of a Variant (10 for `Error`)

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn cverr_basic() {
        let source = r"
result = CVErr(2042)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_division_by_zero() {
        let source = r"
error = CVErr(2007)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    #[allow(clippy::too_many_lines)]
    fn cverr_in_function() {
        let source = r"
Function SafeDivide(a As Double, b As Double) As Variant
    If b = 0 Then
        SafeDivide = CVErr(2007)
    Else
        SafeDivide = a / b
    End If
End Function
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_with_constant() {
        let source = r"
Const xlErrNA As Long = 2042
result = CVErr(xlErrNA)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_value_error() {
        let source = r"
err = CVErr(2015)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_with_iserror() {
        let source = r#"
result = CVErr(2042)
If IsError(result) Then
    MsgBox "Error"
End If
"#;
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_in_assignment() {
        let source = r"
Dim myError As Variant
myError = CVErr(2036)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_custom_error() {
        let source = r"
Const APP_ERR_INVALID As Long = 1000
result = CVErr(APP_ERR_INVALID)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_ref_error() {
        let source = r"
refError = CVErr(2023)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_num_error() {
        let source = r"
numError = CVErr(2036)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_in_select_case() {
        let source = r"
Select Case value
    Case Is < 0
        result = CVErr(2036)
    Case Else
        result = value
End Select
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_propagation() {
        let source = r"
If IsError(input) Then
    output = input
Else
    output = CVErr(2042)
End If
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_name_error() {
        let source = r"
nameErr = CVErr(2029)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_null_error() {
        let source = r"
nullErr = CVErr(2000)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_in_array() {
        let source = r"
results(i) = CVErr(2042)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_with_variable() {
        let source = r"
Dim errorNum As Long
errorNum = 2042
result = CVErr(errorNum)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_in_if_condition() {
        let source = r"
If value < 0 Then
    result = CVErr(2036)
End If
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_multiple_errors() {
        let source = r"
err1 = CVErr(2007)
err2 = CVErr(2042)
err3 = CVErr(2015)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    #[allow(clippy::too_many_lines)]
    fn cverr_in_loop() {
        let source = r"
For i = 1 To 10
    If arr(i) < 0 Then
        arr(i) = CVErr(2036)
    End If
Next i
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_return_value() {
        let source = r"
Function Validate(x As Variant) As Variant
    Validate = CVErr(2015)
End Function
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_in_do_loop() {
        let source = r#"
Do While Not rs.EOF
    If IsNull(rs("Value")) Then
        result = CVErr(2042)
    End If
    rs.MoveNext
Loop
"#;
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_with_expression() {
        let source = r"
errCode = baseError + offset
result = CVErr(errCode)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_in_collection() {
        let source = r"
errors.Add CVErr(2042)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_zero() {
        let source = r"
err = CVErr(0)
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn cverr_with_whitespace() {
        let source = r"
result = CVErr( 2042 )
";
        let (cst_opt, _failure) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path(
            "../../../../../snapshots/syntax/library/functions/conversion/cverr",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}