1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-09-08 10:00:17 +00:00
QB64-PE/tests/compile_tests/qb64pe/hasFunctionElement.bas
Matthew Kilgore 83533dc319 Add support for optional function arguments
Currently functions only have very limited optional argument support,
this expands it so that we can have more complex sets of optional
arguments for functions, such as multiple arguments where not all need
to be provided. This will be used in the future for some upcoming
functionality.

Note that this does not support any generic optional argument format,
the commas always have to be provided unless an optional argument is at
the end of the parameter list. Thus, if you have a format with two
optional arguments and you want to omit the second, then you need to call
it as 'foo(2, , 3)`, rather than `foo(2, 3)`. This is important for
avoiding ambiguous situations, and is how many SUBs currently function.

The two functions that violate that requirement are INSTR() and
_INSTRREV(), which use the format `[?],?,?` and allow omitting the comma
for the first argument. This format is simply handled as a special case.

Fixes: #303
2023-02-12 22:38:50 -05:00

76 lines
2 KiB
QBasic

DEFLNG A-Z
$Console:Only
Debug = -1
'$include:'../../../source/global/constants.bas'
sp = "@" ' Makes the output readable
Type TestCase
args As String
results As String
End Type
Dim tests(6) As TestCase
tests(1).args = "2"
tests(1).results = MKL$(-1) + MKL$(0) + MKL$(0) + MKL$(0)
tests(2).args = "2" + sp + "," + sp + "3"
tests(2).results = MKL$(-1) + MKL$(-1) + MKL$(0) + MKL$(0)
' (2, foo(3, 2) ) - includes two function arguments, with one of them being a function call
tests(3).args = "2" + sp + "," + sp + _
"foo" + sp + "(" + sp + "3" + sp + "," + sp + "2" + sp + ")"
tests(3).results = MKL$(-1) + MKL$(-1) + MKL$(0) + MKL$(0) + MKL$(0)
' Trailing comma at the end of argument list
tests(4).args = "2" + sp + "," + sp + "3" + sp + ","
tests(4).results = MKL$(-1) + MKL$(-1) + MKL$(0) + MKL$(0)
tests(5).args = "2" + sp + "," + sp + "," + sp + "3"
tests(5).results = MKL$(-1) + MKL$(0) + MKL$(-1) + MKL$(0)
tests(6).args = "2" + sp + "," + sp + ","
tests(6).results = MKL$(-1) + MKL$(0) + MKL$(0) + MKL$(0)
ReDim provided(10) As Long
For i = 1 To UBOUND(tests)
argStringToArray tests(i).results, provided()
Print "Test"; i; ", Args: "; tests(i).args
For k = 1 To UBOUND(provided)
result& = hasFunctionElement(tests(i).args, k)
Print " Expected:"; provided(k); ", Actual"; result&;
If provided(k) = result& Then
Print " PASS!"
Else
Print " FAIL!"
End If
Next
Print
Next
System
'$include:'../../../source/utilities/elements.bas'
SUB argStringToArray(argString As String, provided() As Long)
ReDim provided(LEN(argString) / 4) As Long
for i = 1 to UBOUND(provided)
provided(i) = CVL(MID$(argString, (i - 1) * 4 + 1, 4))
next
END SUB
FUNCTION argStringPrint$(argString As String)
res$ = ""
res$ = STR$(CVL(MID$(argString, 1, 4)))
For i = 2 To LEN(argString) / 4
res$ = res$ + ", " + STR$(CVL(MID$(argString, (i - 1) * 4 + 1, 4)))
Next I
argStringPrint$ = res$
END FUNCTION