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
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
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
//! # Rate Function
//!
//! Returns a Double specifying the interest rate per period for an annuity.
//!
//! ## Syntax
//!
//! ```vb
//! Rate(nper, pmt, pv, [fv], [type], [guess])
//! ```
//!
//! ## Parameters
//!
//! - `nper` - Required. Double specifying total number of payment periods in the annuity. For example, if you make monthly payments on a 4-year car loan, your loan has 4 * 12 (or 48) payment periods.
//! - `pmt` - Required. Double specifying payment to be made each period. Payments usually contain principal and interest that doesn't change over the life of the annuity.
//! - `pv` - Required. Double specifying present value, or value today, of a series of future payments or receipts. For example, when you borrow money to buy a car, the loan amount is the present value to the lender of the monthly car payments you will make.
//! - `fv` - Optional. Variant specifying future value or cash balance you want after you've made the final payment. For example, the future value of a loan is $0 because that's its value after the final payment. However, if you want to save $50,000 over 18 years for your child's education, then $50,000 is the future value. If omitted, 0 is assumed.
//! - `type` - Optional. Variant specifying when payments are due. Use 0 if payments are due at the end of the payment period, or use 1 if payments are due at the beginning of the period. If omitted, 0 is assumed.
//! - `guess` - Optional. Variant specifying value you estimate will be returned by Rate. If omitted, guess is 0.1 (10 percent). Rate is calculated by iteration and can have zero or more than one solution.
//!
//! ## Return Value
//!
//! Returns a `Double` specifying the interest rate per period for an annuity. The rate is calculated using an iterative algorithm and is returned as a decimal (e.g., 0.08 for 8%).
//!
//! ## Remarks
//!
//! The `Rate` function calculates the interest rate per period for an annuity based on periodic, fixed payments and a fixed principal. This is useful when you know the loan amount, payment, and term, but need to determine what interest rate is being charged.
//!
//! An annuity is a series of fixed cash payments made over a period of time. An annuity can be a loan (such as a home mortgage) or an investment (such as a monthly savings plan).
//!
//! For all arguments, cash paid out (such as deposits to savings) is represented by negative numbers; cash received (such as dividend checks) is represented by positive numbers.
//!
//! **Important Notes**:
//! - The `Rate` function uses an iterative technique to calculate the interest rate
//! - If `Rate` cannot find a result after 20 iterations, it fails and returns an error
//! - Different values for `guess` can result in different solutions or no solution
//! - The rate returned is per period - multiply by periods per year for annual rate
//! - Be consistent with units: if `nper` is in months, the result is monthly rate
//!
//! **Calculation Method**:
//! The Rate function solves the present value equation for the rate:
//! ```text
//! PV + PMT * ((1 - (1 + rate)^-nper) / rate) + FV / (1 + rate)^nper = 0
//! ```
//!
//! ## Typical Uses
//!
//! 1. **Loan Analysis**: Determine the interest rate on a loan given payment and terms
//! 2. **APR Calculation**: Calculate Annual Percentage Rate from payment information
//! 3. **Investment Returns**: Find the rate of return on an investment
//! 4. **Lease Rate Discovery**: Determine implicit interest rate in a lease
//! 5. **Loan Comparison**: Compare effective rates between different loan offers
//! 6. **Reverse Engineering**: Find the rate when only payment details are known
//! 7. **Financial Planning**: Calculate required rate of return for goals
//! 8. **Credit Card Analysis**: Determine effective rate from minimum payments
//!
//! ## Basic Examples
//!
//! ### Example 1: Find Loan Interest Rate
//! ```vb
//! ' You borrowed $10,000, pay $200/month for 5 years. What's the monthly rate?
//! Dim monthlyRate As Double
//! Dim annualRate As Double
//! monthlyRate = Rate(60, -200, 10000)
//! annualRate = monthlyRate * 12
//! ' monthlyRate ≈ 0.00618 (0.618% per month)
//! ' annualRate ≈ 0.0742 (7.42% APR)
//! ```
//!
//! ### Example 2: Investment Rate of Return
//! ```vb
//! ' Invested $5,000, withdrew $100/month for 5 years, ended with $3,000. What was the rate?
//! Dim monthlyReturn As Double
//! monthlyReturn = Rate(60, 100, -5000, 3000)
//! ' Returns the monthly rate of return
//! ```
//!
//! ### Example 3: Find Rate with Guess
//! ```vb
//! ' Sometimes need to provide a guess to help convergence
//! Dim rate As Double
//! rate = Rate(48, -250, 10000, 0, 0, 0.08)  ' Guess 8% annual (0.08/12 monthly)
//! ```
//!
//! ### Example 4: Annual Rate from Monthly Terms
//! ```vb
//! ' Calculate APR from monthly payment information
//! Dim monthlyRate As Double
//! Dim apr As Double
//! monthlyRate = Rate(360, -1000, 150000)  ' 30-year mortgage
//! apr = monthlyRate * 12 * 100  ' Convert to annual percentage
//! MsgBox "APR: " & Format(apr, "0.00") & "%"
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: `CalculateAPR`
//! ```vb
//! Function CalculateAPR(loanAmount As Double, monthlyPayment As Double, _
//!                       months As Integer) As Double
//!     ' Calculate Annual Percentage Rate from loan terms
//!     Dim monthlyRate As Double
//!     
//!     On Error Resume Next
//!     monthlyRate = Rate(months, -monthlyPayment, loanAmount)
//!     
//!     If Err.Number = 0 Then
//!         CalculateAPR = monthlyRate * 12  ' Convert to annual rate
//!     Else
//!         CalculateAPR = -1  ' Error indicator
//!         Err.Clear
//!     End If
//!     On Error GoTo 0
//! End Function
//! ```
//!
//! ### Pattern 2: `CompareEffectiveRates`
//! ```vb
//! Function CompareEffectiveRates(loan1PV As Double, loan1PMT As Double, loan1Nper As Integer, _
//!                                loan2PV As Double, loan2PMT As Double, loan2Nper As Integer) As String
//!     Dim rate1 As Double, rate2 As Double
//!     
//!     On Error Resume Next
//!     rate1 = Rate(loan1Nper, -loan1PMT, loan1PV) * 12
//!     rate2 = Rate(loan2Nper, -loan2PMT, loan2PV) * 12
//!     
//!     If Err.Number <> 0 Then
//!         CompareEffectiveRates = "Error calculating rates"
//!         Err.Clear
//!         Exit Function
//!     End If
//!     On Error GoTo 0
//!     
//!     If rate1 < rate2 Then
//!         CompareEffectiveRates = "Loan 1 has lower rate: " & Format(rate1 * 100, "0.00") & "%"
//!     ElseIf rate2 < rate1 Then
//!         CompareEffectiveRates = "Loan 2 has lower rate: " & Format(rate2 * 100, "0.00") & "%"
//!     Else
//!         CompareEffectiveRates = "Both loans have same rate: " & Format(rate1 * 100, "0.00") & "%"
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 3: `ValidateRateParameters`
//! ```vb
//! Function ValidateRateParameters(nper As Integer, pmt As Double, pv As Double) As Boolean
//!     ValidateRateParameters = False
//!     
//!     If nper <= 0 Then
//!         MsgBox "Number of periods must be positive"
//!         Exit Function
//!     End If
//!     
//!     If pmt = 0 Then
//!         MsgBox "Payment cannot be zero"
//!         Exit Function
//!     End If
//!     
//!     If pv = 0 Then
//!         MsgBox "Present value cannot be zero"
//!         Exit Function
//!     End If
//!     
//!     ' Check if payment and PV have proper sign relationship
//!     If (pmt > 0 And pv > 0) Or (pmt < 0 And pv < 0) Then
//!         MsgBox "Payment and present value must have opposite signs"
//!         Exit Function
//!     End If
//!     
//!     ValidateRateParameters = True
//! End Function
//! ```
//!
//! ### Pattern 4: `FindRateWithRetry`
//! ```vb
//! Function FindRateWithRetry(nper As Integer, pmt As Double, pv As Double, _
//!                            Optional fv As Double = 0) As Double
//!     ' Try multiple guess values if initial attempt fails
//!     Dim guesses As Variant
//!     Dim i As Integer
//!     Dim rate As Double
//!     
//!     guesses = Array(0.1, 0.05, 0.15, 0.01, 0.2, 0.001)
//!     
//!     For i = LBound(guesses) To UBound(guesses)
//!         On Error Resume Next
//!         rate = Rate(nper, pmt, pv, fv, 0, guesses(i))
//!         
//!         If Err.Number = 0 Then
//!             FindRateWithRetry = rate
//!             Exit Function
//!         End If
//!         
//!         Err.Clear
//!     Next i
//!     
//!     On Error GoTo 0
//!     FindRateWithRetry = -999  ' Error code
//! End Function
//! ```
//!
//! ### Pattern 5: `CalculateEffectiveAPR`
//! ```vb
//! Function CalculateEffectiveAPR(loanAmount As Double, payment As Double, _
//!                                years As Integer, fees As Double) As Double
//!     ' Calculate APR including fees
//!     Dim months As Integer
//!     Dim netLoanAmount As Double
//!     Dim monthlyRate As Double
//!     
//!     months = years * 12
//!     netLoanAmount = loanAmount - fees  ' Reduce by fees paid upfront
//!     
//!     monthlyRate = Rate(months, -payment, netLoanAmount)
//!     CalculateEffectiveAPR = monthlyRate * 12
//! End Function
//! ```
//!
//! ### Pattern 6: `GetLeaseImplicitRate`
//! ```vb
//! Function GetLeaseImplicitRate(vehiclePrice As Double, monthlyPayment As Double, _
//!                               leaseTerm As Integer, residualValue As Double) As Double
//!     ' Find the implicit interest rate in a lease
//!     Dim monthlyRate As Double
//!     
//!     ' For a lease, the PV is the vehicle price, FV is the residual value
//!     monthlyRate = Rate(leaseTerm, -monthlyPayment, vehiclePrice, -residualValue)
//!     GetLeaseImplicitRate = monthlyRate * 12  ' Annual rate
//! End Function
//! ```
//!
//! ### Pattern 7: `CalculateRealRate`
//! ```vb
//! Function CalculateRealRate(nper As Integer, pmt As Double, pv As Double, _
//!                            inflationRate As Double) As Double
//!     ' Calculate real (inflation-adjusted) rate of return
//!     Dim nominalRate As Double
//!     Dim realRate As Double
//!     
//!     nominalRate = Rate(nper, pmt, pv)
//!     
//!     ' Fisher equation: (1 + nominal) = (1 + real)(1 + inflation)
//!     realRate = ((1 + nominalRate) / (1 + inflationRate / 12)) - 1
//!     
//!     CalculateRealRate = realRate
//! End Function
//! ```
//!
//! ### Pattern 8: `ConvertToAPY`
//! ```vb
//! Function ConvertToAPY(periodicRate As Double, periodsPerYear As Integer) As Double
//!     ' Convert periodic rate to Annual Percentage Yield (with compounding)
//!     ConvertToAPY = ((1 + periodicRate) ^ periodsPerYear) - 1
//! End Function
//! ```
//!
//! ### Pattern 9: `BackoutRate`
//! ```vb
//! Function BackoutRate(payment As Double, principal As Double, _
//!                      years As Integer, paymentType As Integer) As Double
//!     ' Reverse engineer the rate from payment information
//!     Dim periods As Integer
//!     Dim rate As Double
//!     
//!     periods = years * 12
//!     
//!     On Error Resume Next
//!     rate = Rate(periods, -payment, principal, 0, paymentType)
//!     
//!     If Err.Number = 0 Then
//!         BackoutRate = rate * 12  ' Annual rate
//!     Else
//!         BackoutRate = -1
//!         Err.Clear
//!     End If
//!     On Error GoTo 0
//! End Function
//! ```
//!
//! ### Pattern 10: `IsRateReasonable`
//! ```vb
//! Function IsRateReasonable(calculatedRate As Double) As Boolean
//!     ' Validate that calculated rate is within reasonable bounds
//!     Dim annualRate As Double
//!     
//!     annualRate = calculatedRate * 12
//!     
//!     ' Check if annual rate is between -50% and +50%
//!     IsRateReasonable = (annualRate >= -0.5 And annualRate <= 0.5)
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: Comprehensive Loan Rate Analyzer
//! ```vb
//! ' Analyze and compare loan rates with detailed calculations
//! Class LoanRateAnalyzer
//!     Private m_loanAmount As Double
//!     Private m_monthlyPayment As Double
//!     Private m_numPayments As Integer
//!     Private m_fees As Double
//!     Private m_calculatedRate As Double
//!     Private m_effectiveRate As Double
//!     
//!     Public Sub Initialize(loanAmount As Double, monthlyPayment As Double, _
//!                          years As Integer, Optional fees As Double = 0)
//!         m_loanAmount = loanAmount
//!         m_monthlyPayment = monthlyPayment
//!         m_numPayments = years * 12
//!         m_fees = fees
//!     End Sub
//!     
//!     Public Function CalculateNominalRate() As Double
//!         ' Calculate the stated interest rate
//!         Dim monthlyRate As Double
//!         
//!         On Error Resume Next
//!         monthlyRate = Rate(m_numPayments, -m_monthlyPayment, m_loanAmount)
//!         
//!         If Err.Number = 0 Then
//!             m_calculatedRate = monthlyRate * 12
//!             CalculateNominalRate = m_calculatedRate
//!         Else
//!             CalculateNominalRate = -1
//!             Err.Clear
//!         End If
//!         On Error GoTo 0
//!     End Function
//!     
//!     Public Function CalculateEffectiveRate() As Double
//!         ' Calculate APR including fees
//!         Dim netAmount As Double
//!         Dim monthlyRate As Double
//!         
//!         netAmount = m_loanAmount - m_fees
//!         
//!         On Error Resume Next
//!         monthlyRate = Rate(m_numPayments, -m_monthlyPayment, netAmount)
//!         
//!         If Err.Number = 0 Then
//!             m_effectiveRate = monthlyRate * 12
//!             CalculateEffectiveRate = m_effectiveRate
//!         Else
//!             CalculateEffectiveRate = -1
//!             Err.Clear
//!         End If
//!         On Error GoTo 0
//!     End Function
//!     
//!     Public Function CalculateAPY() As Double
//!         ' Calculate Annual Percentage Yield (with compounding)
//!         Dim monthlyRate As Double
//!         
//!         monthlyRate = m_calculatedRate / 12
//!         CalculateAPY = ((1 + monthlyRate) ^ 12) - 1
//!     End Function
//!     
//!     Public Function GetTotalInterestPaid() As Double
//!         ' Calculate total interest over life of loan
//!         GetTotalInterestPaid = (m_monthlyPayment * m_numPayments) - m_loanAmount
//!     End Function
//!     
//!     Public Function GetInterestPercentage() As Double
//!         ' Calculate interest as percentage of principal
//!         GetInterestPercentage = GetTotalInterestPaid() / m_loanAmount
//!     End Function
//!     
//!     Public Function GenerateRateReport() As String
//!         Dim report As String
//!         Dim nominalRate As Double
//!         Dim effectiveRate As Double
//!         Dim apy As Double
//!         
//!         nominalRate = CalculateNominalRate()
//!         effectiveRate = CalculateEffectiveRate()
//!         
//!         If nominalRate < 0 Or effectiveRate < 0 Then
//!             GenerateRateReport = "Error: Could not calculate interest rate"
//!             Exit Function
//!         End If
//!         
//!         apy = CalculateAPY()
//!         
//!         report = "Loan Rate Analysis" & vbCrLf
//!         report = report & String(50, "=") & vbCrLf
//!         report = report & "Loan Amount: $" & Format(m_loanAmount, "#,##0.00") & vbCrLf
//!         report = report & "Monthly Payment: $" & Format(m_monthlyPayment, "#,##0.00") & vbCrLf
//!         report = report & "Term: " & (m_numPayments / 12) & " years (" & m_numPayments & " months)" & vbCrLf
//!         report = report & "Fees: $" & Format(m_fees, "#,##0.00") & vbCrLf
//!         report = report & String(50, "-") & vbCrLf
//!         report = report & "Nominal APR: " & Format(nominalRate * 100, "0.00") & "%" & vbCrLf
//!         report = report & "Effective APR (with fees): " & Format(effectiveRate * 100, "0.00") & "%" & vbCrLf
//!         report = report & "APY (with compounding): " & Format(apy * 100, "0.00") & "%" & vbCrLf
//!         report = report & String(50, "-") & vbCrLf
//!         report = report & "Total Interest Paid: $" & Format(GetTotalInterestPaid(), "#,##0.00") & vbCrLf
//!         report = report & "Interest as % of Principal: " & Format(GetInterestPercentage() * 100, "0.00") & "%"
//!         
//!         GenerateRateReport = report
//!     End Function
//! End Class
//! ```
//!
//! ### Example 2: Multi-Loan Rate Comparison Tool
//! ```vb
//! ' Compare rates across multiple loan offers
//! Module LoanRateComparison
//!     Private Type LoanOffer
//!         Name As String
//!         Principal As Double
//!         Payment As Double
//!         Months As Integer
//!         Fees As Double
//!         NominalRate As Double
//!         EffectiveRate As Double
//!     End Type
//!     
//!     Public Function CompareLoans(offers() As LoanOffer) As String
//!         Dim i As Integer
//!         Dim report As String
//!         Dim bestOffer As Integer
//!         Dim lowestRate As Double
//!         
//!         lowestRate = 999
//!         bestOffer = LBound(offers)
//!         
//!         ' Calculate rates for all offers
//!         For i = LBound(offers) To UBound(offers)
//!             With offers(i)
//!                 On Error Resume Next
//!                 .NominalRate = Rate(.Months, -.Payment, .Principal) * 12
//!                 .EffectiveRate = Rate(.Months, -.Payment, .Principal - .Fees) * 12
//!                 
//!                 If Err.Number <> 0 Then
//!                     .NominalRate = -1
//!                     .EffectiveRate = -1
//!                     Err.Clear
//!                 End If
//!                 On Error GoTo 0
//!                 
//!                 If .EffectiveRate > 0 And .EffectiveRate < lowestRate Then
//!                     lowestRate = .EffectiveRate
//!                     bestOffer = i
//!                 End If
//!             End With
//!         Next i
//!         
//!         ' Generate comparison report
//!         report = "Loan Offer Comparison" & vbCrLf
//!         report = report & String(80, "=") & vbCrLf
//!         report = report & "Offer                Principal    Payment   Term   Fees      APR      Eff.APR" & vbCrLf
//!         report = report & String(80, "-") & vbCrLf
//!         
//!         For i = LBound(offers) To UBound(offers)
//!             With offers(i)
//!                 report = report & Left(.Name & Space(20), 20)
//!                 report = report & " $" & Right(Space(9) & Format(.Principal, "#,##0"), 9)
//!                 report = report & "  $" & Right(Space(7) & Format(.Payment, "#,##0"), 7)
//!                 report = report & Right(Space(5) & (.Months / 12), 5) & "y"
//!                 report = report & " $" & Right(Space(6) & Format(.Fees, "#,##0"), 6)
//!                 
//!                 If .NominalRate >= 0 Then
//!                     report = report & Right(Space(6) & Format(.NominalRate * 100, "0.00"), 6) & "%"
//!                     report = report & Right(Space(7) & Format(.EffectiveRate * 100, "0.00"), 7) & "%"
//!                 Else
//!                     report = report & "  Error   Error"
//!                 End If
//!                 
//!                 If i = bestOffer Then report = report & " *BEST*"
//!                 report = report & vbCrLf
//!             End With
//!         Next i
//!         
//!         report = report & String(80, "-") & vbCrLf
//!         report = report & "Best Offer: " & offers(bestOffer).Name & _
//!                  " (Effective APR: " & Format(offers(bestOffer).EffectiveRate * 100, "0.00") & "%)"
//!         
//!         CompareLoans = report
//!     End Function
//!     
//!     Public Function CalculateRateDifference(loan1 As LoanOffer, loan2 As LoanOffer) As String
//!         Dim diff As Double
//!         Dim savingsPerMonth As Double
//!         Dim totalSavings As Double
//!         
//!         diff = Abs(loan1.EffectiveRate - loan2.EffectiveRate)
//!         savingsPerMonth = Abs(loan1.Payment - loan2.Payment)
//!         totalSavings = savingsPerMonth * loan1.Months
//!         
//!         CalculateRateDifference = "Rate Difference: " & Format(diff * 100, "0.00") & "%" & vbCrLf & _
//!                                  "Monthly Savings: $" & Format(savingsPerMonth, "#,##0.00") & vbCrLf & _
//!                                  "Total Savings: $" & Format(totalSavings, "#,##0.00")
//!     End Function
//! End Module
//! ```
//!
//! ### Example 3: Investment Rate Calculator
//! ```vb
//! ' Calculate rate of return on investments
//! Class InvestmentRateCalculator
//!     Private m_initialInvestment As Double
//!     Private m_monthlyContribution As Double
//!     Private m_finalValue As Double
//!     Private m_months As Integer
//!     
//!     Public Sub Initialize(initialInvestment As Double, monthlyContribution As Double, _
//!                          finalValue As Double, years As Integer)
//!         m_initialInvestment = initialInvestment
//!         m_monthlyContribution = monthlyContribution
//!         m_finalValue = finalValue
//!         m_months = years * 12
//!     End Sub
//!     
//!     Public Function GetMonthlyRate() As Double
//!         ' Calculate monthly rate of return
//!         On Error Resume Next
//!         GetMonthlyRate = Rate(m_months, -m_monthlyContribution, -m_initialInvestment, m_finalValue)
//!         
//!         If Err.Number <> 0 Then
//!             GetMonthlyRate = -999
//!             Err.Clear
//!         End If
//!         On Error GoTo 0
//!     End Function
//!     
//!     Public Function GetAnnualRate() As Double
//!         Dim monthlyRate As Double
//!         monthlyRate = GetMonthlyRate()
//!         
//!         If monthlyRate = -999 Then
//!             GetAnnualRate = -999
//!         Else
//!             GetAnnualRate = monthlyRate * 12
//!         End If
//!     End Function
//!     
//!     Public Function GetEffectiveAnnualRate() As Double
//!         ' Calculate with compounding
//!         Dim monthlyRate As Double
//!         monthlyRate = GetMonthlyRate()
//!         
//!         If monthlyRate = -999 Then
//!             GetEffectiveAnnualRate = -999
//!         Else
//!             GetEffectiveAnnualRate = ((1 + monthlyRate) ^ 12) - 1
//!         End If
//!     End Function
//!     
//!     Public Function GetTotalContributed() As Double
//!         GetTotalContributed = m_initialInvestment + (m_monthlyContribution * m_months)
//!     End Function
//!     
//!     Public Function GetTotalReturn() As Double
//!         GetTotalReturn = m_finalValue - GetTotalContributed()
//!     End Function
//!     
//!     Public Function GenerateReport() As String
//!         Dim report As String
//!         Dim annualRate As Double
//!         Dim effectiveRate As Double
//!         
//!         annualRate = GetAnnualRate()
//!         effectiveRate = GetEffectiveAnnualRate()
//!         
//!         If annualRate = -999 Then
//!             GenerateReport = "Error: Could not calculate rate of return"
//!             Exit Function
//!         End If
//!         
//!         report = "Investment Rate of Return Analysis" & vbCrLf
//!         report = report & String(50, "=") & vbCrLf
//!         report = report & "Initial Investment: $" & Format(m_initialInvestment, "#,##0.00") & vbCrLf
//!         report = report & "Monthly Contribution: $" & Format(m_monthlyContribution, "#,##0.00") & vbCrLf
//!         report = report & "Investment Period: " & (m_months / 12) & " years" & vbCrLf
//!         report = report & "Final Value: $" & Format(m_finalValue, "#,##0.00") & vbCrLf
//!         report = report & String(50, "-") & vbCrLf
//!         report = report & "Total Contributed: $" & Format(GetTotalContributed(), "#,##0.00") & vbCrLf
//!         report = report & "Total Return: $" & Format(GetTotalReturn(), "#,##0.00") & vbCrLf
//!         report = report & String(50, "-") & vbCrLf
//!         report = report & "Annual Rate of Return: " & Format(annualRate * 100, "0.00") & "%" & vbCrLf
//!         report = report & "Effective Annual Rate: " & Format(effectiveRate * 100, "0.00") & "%"
//!         
//!         GenerateReport = report
//!     End Function
//! End Class
//! ```
//!
//! ### Example 4: Credit Card Rate Analyzer
//! ```vb
//! ' Analyze credit card interest rates from payment information
//! Class CreditCardRateAnalyzer
//!     Private m_balance As Double
//!     Private m_minimumPayment As Double
//!     Private m_monthsToPayoff As Integer
//!     
//!     Public Sub SetCardDetails(balance As Double, minimumPayment As Double, _
//!                              monthsToPayoff As Integer)
//!         m_balance = balance
//!         m_minimumPayment = minimumPayment
//!         m_monthsToPayoff = monthsToPayoff
//!     End Sub
//!     
//!     Public Function GetImplicitRate() As Double
//!         ' Calculate the implicit interest rate
//!         Dim monthlyRate As Double
//!         
//!         On Error Resume Next
//!         monthlyRate = Rate(m_monthsToPayoff, -m_minimumPayment, m_balance)
//!         
//!         If Err.Number = 0 Then
//!             GetImplicitRate = monthlyRate * 12  ' Annual rate
//!         Else
//!             GetImplicitRate = -1
//!             Err.Clear
//!         End If
//!         On Error GoTo 0
//!     End Function
//!     
//!     Public Function GetTotalInterest() As Double
//!         GetTotalInterest = (m_minimumPayment * m_monthsToPayoff) - m_balance
//!     End Function
//!     
//!     Public Function GetInterestAsPercent() As Double
//!         GetInterestAsPercent = GetTotalInterest() / m_balance
//!     End Function
//!     
//!     Public Function CompareToFixedPayment(fixedPayment As Double) As String
//!         Dim result As String
//!         Dim currentRate As Double
//!         Dim fixedMonths As Integer
//!         Dim savings As Double
//!         
//!         currentRate = GetImplicitRate()
//!         
//!         If currentRate < 0 Then
//!             CompareToFixedPayment = "Error calculating current rate"
//!             Exit Function
//!         End If
//!         
//!         ' Calculate months to pay off with fixed payment
//!         fixedMonths = NPer(currentRate / 12, -fixedPayment, m_balance)
//!         savings = (m_minimumPayment * m_monthsToPayoff) - (fixedPayment * fixedMonths)
//!         
//!         result = "Current Plan:" & vbCrLf
//!         result = result & "  Payment: $" & Format(m_minimumPayment, "#,##0.00") & vbCrLf
//!         result = result & "  Months: " & m_monthsToPayoff & vbCrLf
//!         result = result & "  Total: $" & Format(m_minimumPayment * m_monthsToPayoff, "#,##0.00") & vbCrLf
//!         result = result & vbCrLf & "Fixed Payment Plan:" & vbCrLf
//!         result = result & "  Payment: $" & Format(fixedPayment, "#,##0.00") & vbCrLf
//!         result = result & "  Months: " & fixedMonths & vbCrLf
//!         result = result & "  Total: $" & Format(fixedPayment * fixedMonths, "#,##0.00") & vbCrLf
//!         result = result & vbCrLf & "Savings: $" & Format(savings, "#,##0.00")
//!         
//!         CompareToFixedPayment = result
//!     End Function
//! End Class
//! ```
//!
//! ## Error Handling
//!
//! The `Rate` function can raise errors in the following situations:
//!
//! - **Invalid Procedure Call (Error 5)**: When:
//!   - The function cannot find a solution after 20 iterations
//!   - `nper` is 0 or negative
//!   - `pmt` and `pv` have the same sign (both positive or both negative)
//! - **Type Mismatch (Error 13)**: When arguments cannot be converted to numeric values
//! - **Overflow (Error 6)**: When calculated values exceed Double range
//!
//! Always use error handling when calling `Rate`:
//!
//! ```vb
//! On Error Resume Next
//! interestRate = Rate(nper, pmt, pv, fv, type, guess)
//! If Err.Number <> 0 Then
//!     MsgBox "Error calculating rate: " & Err.Description
//!     interestRate = -1  ' Error indicator
//!     Err.Clear
//! End If
//! On Error GoTo 0
//! ```
//!
//! ## Performance Considerations
//!
//! - The `Rate` function uses an iterative algorithm (Newton-Raphson method)
//! - Each call may require multiple iterations (up to 20) to converge
//! - Providing a good `guess` value can significantly speed up convergence
//! - Poor initial guesses can cause failure to converge or slower performance
//! - Consider caching results if the same parameters are used repeatedly
//!
//! ## Best Practices
//!
//! 1. **Validate Inputs**: Check that payment and PV have opposite signs
//! 2. **Use Error Handling**: Always wrap Rate calls in error handlers
//! 3. **Provide Good Guesses**: Supply reasonable guess values for faster convergence
//! 4. **Retry with Different Guesses**: If Rate fails, try different guess values
//! 5. **Convert to Annual Rate**: Multiply monthly rate by 12 for APR
//! 6. **Check for Reasonableness**: Validate that calculated rate is realistic
//! 7. **Include Fees in APR**: Calculate effective APR by including all fees
//! 8. **Use APY for Compounding**: Calculate APY when showing compound returns
//! 9. **Document Assumptions**: Clearly state what the rate represents
//! 10. **Validate Results**: Verify Rate result by using it in Pmt or PV calculation
//!
//! ## Comparison with Related Functions
//!
//! | Function | Purpose | Returns | Use Case |
//! |----------|---------|---------|----------|
//! | **Rate** | Interest rate per period | Double (rate) | Find rate from payment info |
//! | **Pmt** | Payment amount | Double (payment) | Calculate payment from rate |
//! | **PV** | Present value | Double (current value) | Find loan amount from payment |
//! | **FV** | Future value | Double (future value) | Find final value from payments |
//! | **`NPer`** | Number of periods | Double (period count) | Find term from payment/rate |
//! | **IRR** | Internal rate of return | Double (rate) | Find rate from irregular cash flows |
//!
//! ## Platform and Version Notes
//!
//! - Available in all versions of VBA and VB6
//! - Uses iterative algorithm that may not always converge
//! - Maximum 20 iterations before failure
//! - Results may vary slightly between platforms due to floating-point precision
//! - Default guess of 0.1 (10%) works well for most common scenarios
//!
//! ## Limitations
//!
//! - May fail to converge for some parameter combinations
//! - Limited to 20 iterations maximum
//! - Cannot handle multiple solutions (returns first solution found)
//! - Sensitive to initial guess value
//! - May return unrealistic rates if inputs are invalid
//! - Cannot handle variable rate scenarios
//! - Assumes constant periodic payments
//!
//! ## Related Functions
//!
//! - `Pmt`: Returns the periodic payment for an annuity
//! - `PV`: Returns the present value of an annuity
//! - `FV`: Returns the future value of an annuity
//! - `NPer`: Returns the number of periods for an annuity
//! - `IRR`: Returns internal rate of return for irregular cash flows
//! - `MIRR`: Returns modified internal rate of return

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

    #[test]
    fn rate_basic() {
        let source = r"
Dim interestRate As Double
interestRate = Rate(60, -200, 10000)
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_with_all_parameters() {
        let source = r"
Dim rate As Double
rate = Rate(48, -250, 10000, 0, 0, 0.08)
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_if_statement() {
        let source = r#"
If Rate(nper, pmt, pv) > 0.06 Then
    MsgBox "Rate is above 6%"
End If
"#;
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_function_return() {
        let source = r"
Function CalculateAPR(loan As Double, payment As Double) As Double
    Dim monthlyRate As Double
    monthlyRate = Rate(60, -payment, loan)
    CalculateAPR = monthlyRate * 12
End Function
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_variable_assignment() {
        let source = r"
Dim monthlyRate As Double
monthlyRate = Rate(months, payment, principal)
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_msgbox() {
        let source = r#"
MsgBox "APR: " & Format(Rate(60, -500, 20000) * 12 * 100, "0.00") & "%"
"#;
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_debug_print() {
        let source = r#"
Debug.Print "Monthly Rate: " & Rate(n, p, pv)
"#;
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_select_case() {
        let source = r#"
Dim apr As Double
apr = Rate(months, -payment, loan) * 12
Select Case apr
    Case Is > 0.1
        category = "High"
    Case Is > 0.05
        category = "Medium"
    Case Else
        category = "Low"
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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_class_usage() {
        let source = r"
Private m_rate As Double

Public Sub Calculate()
    m_rate = Rate(m_nper, m_pmt, m_pv)
End Sub
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_with_statement() {
        let source = r"
With loanCalc
    .InterestRate = Rate(.NumPayments, .Payment, .Principal)
End With
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_elseif() {
        let source = r"
If amount > 100000 Then
    r = 0.05
ElseIf Rate(60, -pmt, loan) > 0.08 Then
    r = 0.06
End If
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_for_loop() {
        let source = r"
For payment = 100 To 500 Step 50
    r = Rate(60, -payment, 10000)
    Debug.Print payment, r * 12
Next payment
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_do_while() {
        let source = r"
Do While Rate(nper, -pmt, pv) < targetRate
    pmt = pmt + 10
Loop
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_do_until() {
        let source = r"
Do Until Rate(n, -p, amount) >= minRate
    amount = amount - 100
Loop
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_while_wend() {
        let source = r"
While Rate(periods, payment, principal) > 0
    principal = principal + 1000
Wend
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_parentheses() {
        let source = r"
Dim result As Double
result = (Rate(nper, pmt, pv))
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_iif() {
        let source = r"
Dim annualRate As Double
annualRate = IIf(useGuess, Rate(n, p, pv, fv, t, g), Rate(n, p, pv))
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_comparison() {
        let source = r#"
If Rate(60, -200, 10000) > Rate(48, -250, 10000) Then
    MsgBox "First loan has higher rate"
End If
"#;
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_array_assignment() {
        let source = r"
Dim rates(10) As Double
rates(i) = Rate(periods(i), payments(i), principals(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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_property_assignment() {
        let source = r"
Set obj = New LoanAnalyzer
obj.InterestRate = Rate(obj.Term, obj.Payment, obj.Principal)
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_function_argument() {
        let source = r"
Call AnalyzeLoan(Rate(60, -payment, principal) * 12, loanAmount)
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_arithmetic() {
        let source = r"
Dim apr As Double
apr = Rate(months, -pmt, pv) * 12 * 100
";
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_concatenation() {
        let source = r#"
Dim msg As String
msg = "APR is: " & Format(Rate(n, p, pv) * 12, "0.00%")
"#;
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_format_function() {
        let source = r#"
Dim display As String
display = Format(Rate(60, -500, 20000) * 12 * 100, "0.00")
"#;
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_error_handling() {
        let source = r#"
On Error Resume Next
interestRate = Rate(nper, pmt, pv, fv, type, guess)
If Err.Number <> 0 Then
    MsgBox "Error calculating rate"
End If
On Error GoTo 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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rate_on_error_goto() {
        let source = r#"
Sub CalculateRate()
    On Error GoTo ErrorHandler
    Dim r As Double
    r = Rate(numMonths, monthlyPayment, loanAmount)
    Exit Sub
ErrorHandler:
    MsgBox "Error in rate calculation"
End Sub
"#;
        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/financial/rate");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}