vb6parse 1.0.0

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
//! # `Hex` Function
//!
//! Returns a `String` representing the hexadecimal value of a number.
//!
//! ## Syntax
//!
//! ```vb
//! Hex(number)
//! ```
//!
//! ## Parameters
//!
//! - `number` (Required): Any valid numeric expression or string expression. If `number` is not already a whole number, it is rounded to the nearest whole number before being evaluated.
//!
//! ## Return Value
//!
//! Returns a `String` representing the hexadecimal value of the number. The return value contains hexadecimal digits (0-9, A-F) without the "&H" prefix.
//!
//! ## Remarks
//!
//! The `Hex` function converts a decimal number to its hexadecimal (base 16) string representation:
//!
//! - If `number` is `Null`, `Hex` returns `Null`
//! - If `number` is `Empty`, `Hex` returns "0"
//! - Negative numbers are represented in two's complement form
//! - For Byte values: Returns up to 2 hexadecimal digits
//! - For Integer values: Returns up to 4 hexadecimal digits
//! - For Long values: Returns up to 8 hexadecimal digits
//! - Fractional values are rounded to the nearest integer before conversion
//! - The result does not include the "&H" prefix (use "&H" & `Hex(n)` to include it)
//! - Leading zeros are not included in the result (e.g., 15 returns "F", not "0F")
//! - For hexadecimal to decimal conversion, use the `CLng` or `CInt` functions with "&H" prefix
//!
//! ## Typical Uses
//!
//! 1. **Color Values**: Convert RGB color values to hexadecimal for HTML/CSS
//! 2. **Memory Addresses**: Display memory addresses in hexadecimal format
//! 3. **Debugging**: Show binary data in readable hexadecimal format
//! 4. **Low-Level Programming**: Work with hardware registers and bit patterns
//! 5. **File I/O**: Display byte values when working with binary files
//! 6. **Cryptography**: Show hash values and checksums in hex format
//!
//! ## Basic Usage Examples
//!
//! ```vb
//! ' Example 1: Simple conversion
//! Debug.Print Hex(255)        ' Prints: FF
//! Debug.Print Hex(16)         ' Prints: 10
//! Debug.Print Hex(0)          ' Prints: 0
//!
//! ' Example 2: Color conversion
//! Dim red As Long
//! red = RGB(255, 0, 0)
//! Debug.Print Hex(red)        ' Prints: FF (on little-endian systems)
//!
//! ' Example 3: Negative numbers (two's complement)
//! Debug.Print Hex(-1)         ' Prints: FFFFFFFF (Long)
//! Debug.Print Hex(-256)       ' Prints: FFFFFF00
//!
//! ' Example 4: With "&H" prefix
//! Dim value As Long
//! value = 42
//! Debug.Print "&H" & Hex(value)  ' Prints: &H2A
//! ```
//!
//! ## Common Patterns
//!
//! ```vb
//! ' Pattern 1: RGB to hex color string
//! Function RGBToHex(r As Integer, g As Integer, b As Integer) As String
//!     RGBToHex = Right$("0" & Hex(r), 2) & _
//!                Right$("0" & Hex(g), 2) & _
//!                Right$("0" & Hex(b), 2)
//! End Function
//!
//! ' Pattern 2: Pad hex string to specific length
//! Function HexPad(value As Long, length As Integer) As String
//!     HexPad = Right$(String$(length, "0") & Hex(value), length)
//! End Function
//!
//! ' Pattern 3: Display memory dump
//! Sub ShowMemoryDump(bytes() As Byte)
//!     Dim i As Long
//!     Dim line As String
//!     For i = LBound(bytes) To UBound(bytes)
//!         line = line & Right$("0" & Hex(bytes(i)), 2) & " "
//!         If (i + 1) Mod 16 = 0 Then
//!             Debug.Print line
//!             line = ""
//!         End If
//!     Next i
//!     If Len(line) > 0 Then Debug.Print line
//! End Sub
//!
//! ' Pattern 4: Format address with hex
//! Function FormatAddress(addr As Long) As String
//!     FormatAddress = "0x" & Right$("00000000" & Hex(addr), 8)
//! End Function
//!
//! ' Pattern 5: Convert byte array to hex string
//! Function BytesToHex(bytes() As Byte) As String
//!     Dim result As String
//!     Dim i As Long
//!     For i = LBound(bytes) To UBound(bytes)
//!         result = result & Right$("0" & Hex(bytes(i)), 2)
//!     Next i
//!     BytesToHex = result
//! End Function
//!
//! ' Pattern 6: Parse hex string back to number
//! Function HexToLong(hexStr As String) As Long
//!     If Left$(hexStr, 2) = "&H" Then
//!         HexToLong = CLng(hexStr)
//!     Else
//!         HexToLong = CLng("&H" & hexStr)
//!     End If
//! End Function
//!
//! ' Pattern 7: Show bit pattern
//! Sub ShowBitPattern(value As Long)
//!     Debug.Print "Hex: " & Hex(value)
//!     Debug.Print "Dec: " & value
//! End Sub
//!
//! ' Pattern 8: Color component extraction
//! Function GetRedComponent(color As Long) As Integer
//!     GetRedComponent = color And &HFF
//!     Debug.Print "Red: " & Hex(GetRedComponent)
//! End Function
//!
//! ' Pattern 9: Build lookup table
//! Dim hexLookup(0 To 255) As String
//! Sub InitHexLookup()
//!     Dim i As Long
//!     For i = 0 To 255
//!         hexLookup(i) = Right$("0" & Hex(i), 2)
//!     Next i
//! End Sub
//!
//! ' Pattern 10: Format hex with separator
//! Function HexWithSeparator(value As Long, separator As String) As String
//!     Dim hexStr As String
//!     Dim i As Integer
//!     hexStr = Right$("00000000" & Hex(value), 8)
//!     For i = 1 To 7 Step 2
//!         HexWithSeparator = HexWithSeparator & Mid$(hexStr, i, 2)
//!         If i < 7 Then HexWithSeparator = HexWithSeparator & separator
//!     Next i
//! End Function
//! ```
//!
//! ## Advanced Usage Examples
//!
//! ```vb
//! ' Example 1: Hex dump utility class
//! Public Class HexDumper
//!     Private Const BYTES_PER_LINE As Integer = 16
//!     
//!     Public Function DumpToString(data() As Byte) As String
//!         Dim result As String
//!         Dim i As Long
//!         Dim offset As Long
//!         Dim hexPart As String
//!         Dim asciiPart As String
//!         Dim b As Byte
//!         
//!         For i = LBound(data) To UBound(data)
//!             If i Mod BYTES_PER_LINE = 0 Then
//!                 If i > 0 Then
//!                     result = result & hexPart & "  " & asciiPart & vbCrLf
//!                 End If
//!                 hexPart = Right$("00000000" & Hex(i), 8) & ": "
//!                 asciiPart = ""
//!             End If
//!             
//!             b = data(i)
//!             hexPart = hexPart & Right$("0" & Hex(b), 2) & " "
//!             
//!             If b >= 32 And b <= 126 Then
//!                 asciiPart = asciiPart & Chr$(b)
//!             Else
//!                 asciiPart = asciiPart & "."
//!             End If
//!         Next i
//!         
//!         ' Pad last line
//!         If Len(hexPart) > 10 Then
//!             hexPart = hexPart & String$((BYTES_PER_LINE - Len(asciiPart)) * 3, " ")
//!             result = result & hexPart & "  " & asciiPart
//!         End If
//!         
//!         DumpToString = result
//!     End Function
//! End Class
//!
//! ' Example 2: HTML color converter
//! Public Class ColorConverter
//!     Public Function VBColorToHTML(vbColor As Long) As String
//!         Dim r As Integer, g As Integer, b As Integer
//!         r = vbColor And &HFF
//!         g = (vbColor \ &H100) And &HFF
//!         b = (vbColor \ &H10000) And &HFF
//!         
//!         VBColorToHTML = "#" & _
//!             Right$("0" & Hex(r), 2) & _
//!             Right$("0" & Hex(g), 2) & _
//!             Right$("0" & Hex(b), 2)
//!     End Function
//!     
//!     Public Function HTMLToVBColor(htmlColor As String) As Long
//!         Dim r As Long, g As Long, b As Long
//!         If Left$(htmlColor, 1) = "#" Then htmlColor = Mid$(htmlColor, 2)
//!         
//!         r = CLng("&H" & Mid$(htmlColor, 1, 2))
//!         g = CLng("&H" & Mid$(htmlColor, 3, 2))
//!         b = CLng("&H" & Mid$(htmlColor, 5, 2))
//!         
//!         HTMLToVBColor = RGB(r, g, b)
//!     End Function
//! End Class
//!
//! ' Example 3: Checksum calculator
//! Public Function CalculateChecksum(data() As Byte) As String
//!     Dim checksum As Long
//!     Dim i As Long
//!     
//!     For i = LBound(data) To UBound(data)
//!         checksum = checksum Xor data(i)
//!         checksum = ((checksum And &H7FFFFFFF) * 2) Or (checksum And &H80000000) \ &H80000000
//!     Next i
//!     
//!     CalculateChecksum = Right$("00000000" & Hex(checksum), 8)
//! End Function
//!
//! ' Example 4: Binary file viewer
//! Public Class BinaryFileViewer
//!     Public Function LoadAndDisplayFile(filePath As String) As String
//!         Dim fileNum As Integer
//!         Dim fileData() As Byte
//!         Dim fileSize As Long
//!         Dim result As String
//!         Dim i As Long
//!         
//!         fileNum = FreeFile
//!         Open filePath For Binary As #fileNum
//!         fileSize = LOF(fileNum)
//!         ReDim fileData(0 To fileSize - 1)
//!         Get #fileNum, , fileData
//!         Close #fileNum
//!         
//!         For i = 0 To UBound(fileData)
//!             If i Mod 16 = 0 Then
//!                 result = result & vbCrLf & Right$("00000000" & Hex(i), 8) & ": "
//!             End If
//!             result = result & Right$("0" & Hex(fileData(i)), 2) & " "
//!         Next i
//!         
//!         LoadAndDisplayFile = result
//!     End Function
//! End Class
//! ```
//!
//! ## Error Handling
//!
//! The Hex function generally doesn't raise errors, but type conversion can:
//!
//! - **Type Mismatch (Error 13)**: If the argument cannot be converted to a number
//! - **Overflow (Error 6)**: If the value exceeds Long range before conversion
//! - **Null Propagation**: If the argument is Null, Hex returns Null
//!
//! ```vb
//! On Error Resume Next
//! Dim result As String
//! result = Hex(someValue)
//! If Err.Number <> 0 Then
//!     Debug.Print "Error converting to hex: " & Err.Description
//!     Err.Clear
//! End If
//! On Error GoTo 0
//! ```
//!
//! ## Performance Considerations
//!
//! - **Fast Operation**: Hex conversion is a fast, native operation
//! - **String Building**: When converting many values, use string builder pattern to avoid repeated concatenation
//! - **Lookup Tables**: For repeated conversions of small numbers (0-255), consider pre-building a lookup table
//! - **Memory**: The returned string length depends on the magnitude of the number
//!
//! ## Best Practices
//!
//! 1. **Padding**: Use Right$() or Format$() to pad hex values to fixed width
//! 2. **Prefixes**: Add "&H" prefix when the value will be parsed back to numeric
//! 3. **Case**: VB6 Hex returns uppercase (A-F); convert to lowercase if needed
//! 4. **Validation**: Validate input before conversion to avoid type mismatch errors
//! 5. **Documentation**: Document whether hex strings include prefixes ("0x", "&H")
//! 6. **Null Handling**: Check for Null before calling Hex if working with Variants
//!
//! ## Comparison with Other Functions
//!
//! | Function | Purpose | Example |
//! |----------|---------|---------|
//! | Hex | Decimal to hexadecimal string | Hex(255) → "FF" |
//! | Oct | Decimal to octal string | Oct(8) → "10" |
//! | Str | Number to decimal string | Str(255) → " 255" |
//! | Format | Number to formatted string | Format(255, "00") → "255" |
//! | CLng("&H...") | Hexadecimal string to Long | CLng("&HFF") → 255 |
//!
//! ## Platform and Version Notes
//!
//! - Available in all VB6 versions
//! - Behavior consistent across Windows platforms
//! - Integer size (2 bytes) and Long size (4 bytes) are fixed
//! - Two's complement representation for negative numbers is standard
//!
//! ## Limitations
//!
//! - Cannot directly convert Currency or Decimal types (convert to Long first)
//! - No built-in support for padding with leading zeros (use Right$ or Format$)
//! - Returns uppercase letters only (A-F, not a-f)
//! - No control over prefix inclusion (always excludes "&H")
//! - Maximum value is Long range (−2,147,483,648 to 2,147,483,647)
//! - Fractional parts are rounded, not truncated
//!
//! ## Related Functions
//!
//! - `Oct`: Returns a String representing the octal value of a number
//! - `Str`: Returns a String representation of a number
//! - `Format`: Returns a Variant (String) formatted according to instructions
//! - `CLng`: Converts an expression to a Long
//! - `CInt`: Converts an expression to an Integer
//! - `Val`: Returns the numbers contained in a string as a numeric value

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

    #[test]
    fn hex_basic() {
        let source = r"
Sub Test()
    result = Hex(255)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_in_function() {
        let source = r"
Function ToHexString(value As Long) As String
    ToHexString = Hex(value)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_with_prefix() {
        let source = r#"
Sub Test()
    hexStr = "&H" & Hex(42)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_debug_print() {
        let source = r"
Sub Test()
    Debug.Print Hex(255)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_if_statement() {
        let source = r#"
Sub Test()
    If Hex(value) = "FF" Then
        Debug.Print "Maximum byte value"
    End If
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_for_loop() {
        let source = r"
Sub Test()
    Dim i As Integer
    For i = 0 To 255
        hexValues(i) = Hex(i)
    Next i
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_select_case() {
        let source = r#"
Sub Test()
    Select Case Hex(errorCode)
        Case "FF"
            HandleError
        Case "0"
            Success
    End Select
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_do_loop() {
        let source = r"
Sub Test()
    Do While Len(Hex(counter)) < 8
        counter = counter + 1
    Loop
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_class_member() {
        let source = r"
Private Sub Class_Initialize()
    m_hexValue = Hex(initialValue)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_type_field() {
        let source = r"
Sub Test()
    Dim config As ConfigType
    config.hexCode = Hex(statusValue)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_collection_add() {
        let source = r#"
Sub Test()
    Dim col As New Collection
    col.Add Hex(value), "HexValue"
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_with_statement() {
        let source = r"
Sub Test()
    With myObject
        .HexString = Hex(.Value)
    End With
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_msgbox() {
        let source = r#"
Sub Test()
    MsgBox "Hex value: " & Hex(errorCode)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_property() {
        let source = r"
Property Get HexValue() As String
    HexValue = Hex(m_value)
End Property
";
        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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_concatenation() {
        let source = r#"
Sub Test()
    result = "0x" & Hex(address)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_for_each() {
        let source = r"
Sub Test()
    Dim item As Variant
    For Each item In collection
        Debug.Print Hex(item)
    Next
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_error_handling() {
        let source = r"
Sub Test()
    On Error Resume Next
    hexStr = Hex(value)
    If Err.Number <> 0 Then Err.Clear
    On Error GoTo 0
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_right_function() {
        let source = r#"
Sub Test()
    padded = Right$("00" & Hex(value), 2)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_array_assignment() {
        let source = r"
Sub Test()
    Dim hexArray(1 To 10) As String
    hexArray(1) = Hex(255)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_function_argument() {
        let source = r"
Sub Test()
    DisplayHexValue Hex(colorValue)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_nested_call() {
        let source = r"
Sub Test()
    length = Len(Hex(value))
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_iif() {
        let source = r"
Sub Test()
    display = IIf(showHex, Hex(value), Str(value))
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_color_conversion() {
        let source = r#"
Function RGBToHex(r As Integer, g As Integer, b As Integer) As String
    RGBToHex = Right$("0" & Hex(r), 2) & Right$("0" & Hex(g), 2) & Right$("0" & Hex(b), 2)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_byte_array() {
        let source = r#"
Sub Test()
    Dim bytes(0 To 15) As Byte
    Dim i As Integer
    For i = 0 To 15
        Debug.Print Right$("0" & Hex(bytes(i)), 2);
    Next i
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_parentheses() {
        let source = r"
Sub Test()
    value = (Hex(number))
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_format_address() {
        let source = r#"
Function FormatAddress(addr As Long) As String
    FormatAddress = "0x" & Right$("00000000" & Hex(addr), 8)
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn hex_negative_value() {
        let source = r"
Sub Test()
    Dim negHex As String
    negHex = Hex(-1)
    Debug.Print negHex
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/conversion/hex");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}