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
//! `FormatPercent` Function
//!
//! Returns an expression formatted as a percentage (multiplied by 100) with a trailing % character.
//!
//! # Syntax
//!
//! ```vb
//! FormatPercent(expression[, numdigitsafterdecimal[, includeleadingdigit[, useparensfornegativenumbers[, groupdigits]]]])
//! ```
//!
//! # Parameters
//!
//! - `expression` - Required. Expression to be formatted as a percentage.
//! - `numdigitsafterdecimal` - Optional. Numeric value indicating how many places to the right of the decimal are displayed.
//!   Default value is -1, which indicates that the computer's regional settings are used.
//! - `includeleadingdigit` - Optional. Tristate constant that indicates whether or not a leading zero is displayed for fractional values.
//!   See Settings section for values.
//! - `useparensfornegativenumbers` - Optional. Tristate constant that indicates whether or not to place negative values within parentheses.
//!   See Settings section for values.
//! - `groupdigits` - Optional. Tristate constant that indicates whether or not numbers are grouped using the group delimiter
//!   specified in the computer's regional settings. See Settings section for values.
//!
//! # Settings
//!
//! The `includeleadingdigit`, `useparensfornegativenumbers`, and `groupdigits` arguments have the following settings:
//!
//! | Constant | Value | Description |
//! |----------|-------|-------------|
//! | vbTrue | -1 | True |
//! | vbFalse | 0 | False |
//! | vbUseDefault | -2 | Use the setting from the computer's regional settings |
//!
//! # Return Value
//!
//! Returns a Variant of subtype String containing the expression formatted as a percentage with a trailing % character.
//!
//! # Remarks
//!
//! - The `FormatPercent` function multiplies the numeric value by 100 and appends a percent sign.
//! - When one or more optional arguments are omitted, the computer's regional settings provide the default values.
//! - The number is formatted according to the computer's locale settings.
//! - If the expression is `Null`, `FormatPercent` returns an empty string.
//! - The default number of decimal places is 2 (from regional settings).
//!
//! # Typical Uses
//!
//! - Displaying statistical percentages (success rates, completion rates)
//! - Financial ratios (profit margins, growth rates, interest rates)
//! - Survey results and poll data
//! - Progress indicators and completion percentages
//! - Conversion rates and efficiency metrics
//! - Grade percentages and test scores
//!
//! # Basic Usage Examples
//!
//! ```vb
//! ' Default formatting (2 decimal places from regional settings)
//! result = FormatPercent(0.75)           ' Returns: "75.00%"
//! result = FormatPercent(0.5)            ' Returns: "50.00%"
//! result = FormatPercent(1.25)           ' Returns: "125.00%"
//!
//! ' Handling negative percentages
//! result = FormatPercent(-0.15)          ' Returns: "-15.00%"
//! result = FormatPercent(-0.05, 2, , vbTrue)  ' Returns: "(5.00)%"
//!
//! ' Controlling decimal places
//! result = FormatPercent(0.3333, 0)      ' Returns: "33%"
//! result = FormatPercent(0.3333, 2)      ' Returns: "33.33%"
//! result = FormatPercent(0.3333, 4)      ' Returns: "33.3300%"
//!
//! ' Leading digit control
//! result = FormatPercent(0.005, 2, vbTrue)   ' Returns: "0.50%"
//! result = FormatPercent(0.005, 2, vbFalse)  ' Returns: ".50%"
//! ```
//!
//! # Common Patterns
//!
//! ## 1. Success Rate Display
//!
//! ```vb
//! Dim successful As Long
//! Dim total As Long
//! Dim rate As Double
//!
//! successful = 85
//! total = 100
//! rate = successful / total
//!
//! MsgBox "Success Rate: " & FormatPercent(rate, 1)  ' "Success Rate: 85.0%"
//! ```
//!
//! ## 2. Financial Ratios
//!
//! ```vb
//! Dim profit As Currency
//! Dim revenue As Currency
//! Dim margin As Double
//!
//! profit = 25000
//! revenue = 100000
//! margin = profit / revenue
//!
//! lblMargin.Caption = "Profit Margin: " & FormatPercent(margin, 2)  ' "Profit Margin: 25.00%"
//! ```
//!
//! ## 3. Survey Results
//!
//! ```vb
//! Dim yesVotes As Long
//! Dim totalVotes As Long
//!
//! yesVotes = 347
//! totalVotes = 500
//!
//! txtResult.Text = FormatPercent(yesVotes / totalVotes, 1)  ' "69.4%"
//! ```
//!
//! ## 4. Progress Indicator
//!
//! ```vb
//! Dim completed As Long
//! Dim total As Long
//! Dim progress As Double
//!
//! completed = 45
//! total = 100
//! progress = completed / total
//!
//! lblProgress.Caption = "Progress: " & FormatPercent(progress, 0)  ' "Progress: 45%"
//! ```
//!
//! ## 5. Growth Rate Calculation
//!
//! ```vb
//! Dim currentValue As Double
//! Dim previousValue As Double
//! Dim growth As Double
//!
//! currentValue = 125000
//! previousValue = 100000
//! growth = (currentValue - previousValue) / previousValue
//!
//! MsgBox "Growth: " & FormatPercent(growth, 1)  ' "Growth: 25.0%"
//! ```
//!
//! ## 6. Grade Percentage
//!
//! ```vb
//! Dim score As Long
//! Dim maxScore As Long
//! Dim percentage As Double
//!
//! score = 87
//! maxScore = 100
//! percentage = score / maxScore
//!
//! lblGrade.Caption = "Score: " & FormatPercent(percentage, 0)  ' "Score: 87%"
//! ```
//!
//! ## 7. Comparison Display
//!
//! ```vb
//! Dim actual As Double
//! Dim target As Double
//! Dim achievement As Double
//!
//! actual = 95000
//! target = 100000
//! achievement = actual / target
//!
//! lblStatus.Caption = "Target Achievement: " & FormatPercent(achievement, 1)  ' "Target Achievement: 95.0%"
//! ```
//!
//! ## 8. `ListBox` Population with Percentages
//!
//! ```vb
//! Dim i As Integer
//! Dim values() As Double
//! Dim total As Double
//!
//! values = Array(15.5, 28.3, 42.1, 14.1)
//! total = 100
//!
//! For i = 0 To UBound(values)
//!     lstResults.AddItem FormatPercent(values(i) / total, 1)
//! Next i
//! ```
//!
//! ## 9. Database Field Display
//!
//! ```vb
//! Dim rs As ADODB.Recordset
//! Set rs = New ADODB.Recordset
//!
//! rs.Open "SELECT CompletionRate FROM Projects", conn
//!
//! While Not rs.EOF
//!     Debug.Print FormatPercent(rs("CompletionRate"), 0)
//!     rs.MoveNext
//! Wend
//! ```
//!
//! ## 10. Multiple Percentages in Report
//!
//! ```vb
//! Dim passed As Long, failed As Long, total As Long
//! Dim report As String
//!
//! passed = 85
//! failed = 15
//! total = passed + failed
//!
//! report = "Passed: " & FormatPercent(passed / total, 1) & vbCrLf & _
//!          "Failed: " & FormatPercent(failed / total, 1)
//! ' "Passed: 85.0%
//! '  Failed: 15.0%"
//! ```
//!
//! # Advanced Usage
//!
//! ## 1. Flexible Percentage Formatter
//!
//! ```vb
//! Function DisplayPercentage(value As Double, Optional precision As Integer = 1) As String
//!     If value < 0.01 Then
//!         DisplayPercentage = FormatPercent(value, 3)  ' More precision for small values
//!     ElseIf value > 1 Then
//!         DisplayPercentage = FormatPercent(value, 0)  ' No decimals for large percentages
//!     Else
//!         DisplayPercentage = FormatPercent(value, precision)
//!     End If
//! End Function
//! ```
//!
//! ## 2. Comparison with Color Coding
//!
//! ```vb
//! Function FormatVariance(actual As Double, target As Double) As String
//!     Dim variance As Double
//!     variance = (actual - target) / target
//!     
//!     FormatVariance = FormatPercent(variance, 1)
//!     
//!     If variance > 0 Then
//!         lblVariance.ForeColor = vbGreen  ' Positive variance
//!     ElseIf variance < 0 Then
//!         lblVariance.ForeColor = vbRed    ' Negative variance
//!     End If
//! End Function
//! ```
//!
//! ## 3. Dynamic Precision Based on Value
//!
//! ```vb
//! Function SmartFormatPercent(value As Double) As String
//!     Dim absValue As Double
//!     absValue = Abs(value)
//!     
//!     Select Case absValue
//!         Case Is < 0.0001
//!             SmartFormatPercent = FormatPercent(value, 4)
//!         Case Is < 0.01
//!             SmartFormatPercent = FormatPercent(value, 3)
//!         Case Is < 1
//!             SmartFormatPercent = FormatPercent(value, 2)
//!         Case Else
//!             SmartFormatPercent = FormatPercent(value, 1)
//!     End Select
//! End Function
//! ```
//!
//! ## 4. Table Alignment with Percentages
//!
//! ```vb
//! Function AlignedPercentage(value As Double, width As Integer) As String
//!     Dim formatted As String
//!     formatted = FormatPercent(value, 2)
//!     AlignedPercentage = Space(width - Len(formatted)) & formatted
//! End Function
//!
//! ' Usage in grid or report
//! For i = 1 To 10
//!     Debug.Print AlignedPercentage(data(i), 12)
//! Next i
//! ```
//!
//! ## 5. Statistical Analysis Display
//!
//! ```vb
//! Type StatisticsResult
//!     Mean As Double
//!     StdDev As Double
//!     Confidence As Double
//! End Type
//!
//! Function FormatStatistics(stats As StatisticsResult) As String
//!     FormatStatistics = "Mean: " & FormatPercent(stats.Mean, 2) & vbCrLf & _
//!                       "Std Dev: " & FormatPercent(stats.StdDev, 2) & vbCrLf & _
//!                       "Confidence: " & FormatPercent(stats.Confidence, 1)
//! End Function
//! ```
//!
//! ## 6. Conditional Formatting for Thresholds
//!
//! ```vb
//! Sub DisplayMetricWithThreshold(metric As Double, threshold As Double, lbl As Label)
//!     lbl.Caption = FormatPercent(metric, 1)
//!     
//!     If metric >= threshold Then
//!         lbl.BackColor = vbGreen
//!         lbl.ForeColor = vbWhite
//!     ElseIf metric >= threshold * 0.8 Then
//!         lbl.BackColor = vbYellow
//!         lbl.ForeColor = vbBlack
//!     Else
//!         lbl.BackColor = vbRed
//!         lbl.ForeColor = vbWhite
//!     End If
//! End Sub
//! ```
//!
//! # Error Handling
//!
//! ```vb
//! Function SafeFormatPercent(value As Variant) As String
//!     On Error GoTo ErrorHandler
//!     
//!     If IsNull(value) Then
//!         SafeFormatPercent = "N/A"
//!         Exit Function
//!     End If
//!     
//!     If Not IsNumeric(value) Then
//!         SafeFormatPercent = "Invalid"
//!         Exit Function
//!     End If
//!     
//!     SafeFormatPercent = FormatPercent(CDbl(value), 2)
//!     Exit Function
//!     
//! ErrorHandler:
//!     SafeFormatPercent = "Error"
//! End Function
//! ```
//!
//! Common errors:
//! - **Error 13 (Type mismatch)**: Occurs when expression cannot be converted to a numeric value.
//! - **Error 6 (Overflow)**: Can occur with extremely large or small values.
//! - **Error 5 (Invalid procedure call)**: May occur with invalid argument values.
//!
//! # Performance Considerations
//!
//! - `FormatPercent` is relatively fast for single values but can be optimized in loops
//! - For large datasets, consider caching formatted values
//! - The multiplication by 100 is automatic and efficient
//! - Regional settings lookup may have slight overhead
//!
//! # Best Practices
//!
//! 1. Use appropriate decimal places for your context (financial: 2, statistics: 1, progress: 0)
//! 2. Consider the audience when choosing precision
//! 3. Handle division by zero before calling `FormatPercent`
//! 4. Use consistent formatting throughout your application
//! 5. Remember that the input is a decimal (0.5 = 50%)
//! 6. Consider using parentheses for negative values in financial contexts
//!
//! # Comparison with Other Functions
//!
//! - **`FormatNumber`**: Does not multiply by 100 or add %, gives more control over formatting
//! - **`FormatCurrency`**: Adds currency symbol instead of %, doesn't multiply by 100
//! - **Format with "%"**: More flexible but requires manual multiplication by 100
//! - **`Str`/`CStr`**: No automatic multiplication or % symbol, no locale support
//!
//! # Limitations
//!
//! - Always multiplies by 100 (cannot be disabled)
//! - Always adds trailing % character
//! - Cannot customize the position of the % symbol
//! - Limited control over negative number formatting compared to custom Format strings
//! - Depends on regional settings which may vary across systems
//!
//! # Regional Settings Impact
//!
//! The appearance of formatted percentages varies by locale:
//!
//! - **US (English)**: 75.50%
//! - **European (many)**: 75,50%
//! - **Switzerland**: 75.50%
//!
//! # Related Functions
//!
//! - `FormatNumber` - Formats numbers without percentage conversion
//! - `FormatCurrency` - Formats as currency with symbol
//! - `Format` - General-purpose formatting function
//! - `CDbl` - Converts to Double for percentage calculations
//! - `Round` - Rounds numbers before percentage formatting

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

    #[test]
    fn formatpercent_basic() {
        let source = r"result = FormatPercent(0.75)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_with_decimals() {
        let source = r"result = FormatPercent(0.3333, 2)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_no_decimals() {
        let source = r"result = FormatPercent(0.87, 0)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_with_parentheses() {
        let source = r"result = FormatPercent(-0.05, 2, , vbTrue)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_all_parameters() {
        let source = r"result = FormatPercent(0.125, 1, vbTrue, vbFalse, vbTrue)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_in_debug_print() {
        let source = r"Debug.Print FormatPercent(0.5, 0)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_concatenation() {
        let source = r#"MsgBox "Success Rate: " & FormatPercent(rate, 1)"#;
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_in_function() {
        let source = r#"lblMargin.Caption = "Profit Margin: " & FormatPercent(margin, 2)"#;
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_division() {
        let source = r"txtResult.Text = FormatPercent(yesVotes / totalVotes, 1)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_calculation() {
        let source = r#"MsgBox "Growth: " & FormatPercent((currentValue - previousValue) / previousValue, 1)"#;
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_assignment() {
        let source = r#"lblGrade.Caption = "Score: " & FormatPercent(score / maxScore, 0)"#;
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_listbox() {
        let source = r"lstResults.AddItem FormatPercent(values(i) / total, 1)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_multiline() {
        let source = r#"report = "Passed: " & FormatPercent(passed / total, 1) & vbCrLf & "Failed: " & FormatPercent(failed / total, 1)"#;
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_in_if() {
        let source = r"If value < 0.01 Then result = FormatPercent(value, 3)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_null_check() {
        let source =
            r"If IsNull(value) Then Exit Function Else result = FormatPercent(CDbl(value), 2)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_numeric_check() {
        let source =
            r"If Not IsNumeric(value) Then Exit Function Else result = FormatPercent(value, 2)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_error_handling() {
        let source = r"On Error GoTo ErrorHandler
result = FormatPercent(value, 2)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_for_loop() {
        let source = r"For i = 1 To 10
    Debug.Print FormatPercent(data(i), 2)
Next i";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_select_case() {
        let source = r"Select Case absValue
    Case Is < 0.01
        result = FormatPercent(value, 3)
End Select";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_comparison() {
        let source = r#"lblStatus.Caption = "Achievement: " & FormatPercent(actual / target, 1)"#;
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_vbfalse() {
        let source = r"result = FormatPercent(0.005, 2, vbFalse)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_vbtrue() {
        let source = r"result = FormatPercent(0.005, 2, vbTrue)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_recordset() {
        let source = r#"Debug.Print FormatPercent(rs("CompletionRate"), 0)"#;
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_iif() {
        let source = r"result = IIf(value > 1, FormatPercent(value, 0), FormatPercent(value, 2))";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_negative() {
        let source = r"result = FormatPercent(-0.15)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn formatpercent_large_value() {
        let source = r"result = FormatPercent(1.25, 2)";
        let (cst_opt, _failures) = 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/string/formatpercent",
        );
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}