tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / basic / qa / basic_coverage / test_optional_paramters_basic.bas
blob82880e11bdded226ccc931c2f6dc9621bae186cd
1 ' This file is part of the LibreOffice project.
3 ' This Source Code Form is subject to the terms of the Mozilla Public
4 ' License, v. 2.0. If a copy of the MPL was not distributed with this
5 ' file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 Option Explicit
10 Type testObject
11 testInt As Integer
12 End Type
14 Function doUnitTest() As String
15 TestUtil.TestInit
16 verify_testOptionalsBasic
17 doUnitTest = TestUtil.GetResult()
18 End Function
20 ' tdf#36737 - Test optionals with different datatypes. In LO Basic, optional
21 ' parameters are allowed, but without any default values. Missing optional
22 ' parameters will not be initialized to their respective default values of
23 ' its datatype, either.
24 Sub verify_testOptionalsBasic()
25 On Error GoTo errorHandler
27 ' optionals with variant datatypes
28 TestUtil.AssertEqual(TestOptVariant(), 0, "TestOptVariant()")
29 TestUtil.AssertEqual(TestOptVariant(123), 123, "TestOptVariant(123)")
30 TestUtil.AssertEqual(TestOptVariant(, 456), 456, "TestOptVariant(, 456)")
31 TestUtil.AssertEqual(TestOptVariant(123, 456), 579, "TestOptVariant(123, 456)")
33 ' optionals with variant datatypes (ByRef and ByVal)
34 TestUtil.AssertEqual(TestOptVariantByRefByVal(), 0, "TestOptVariantByRefByVal()")
35 TestUtil.AssertEqualApprox(TestOptVariantByRefByVal(123), 123, "TestOptVariantByRefByVal(123)")
36 TestUtil.AssertEqualApprox(TestOptVariantByRefByVal(, 456), 456, "TestOptVariantByRefByVal(, 456)")
37 TestUtil.AssertEqualApprox(TestOptVariantByRefByVal(123, 456), 579, "TestOptVariantByRefByVal(123, 456)")
39 ' optionals with double datatypes
40 TestUtil.AssertEqual(TestOptDouble(), 0, "TestOptDouble()")
41 TestUtil.AssertEqualApprox(TestOptDouble(123.4), 123.4, 1E-5, "TestOptDouble(123.4)")
42 TestUtil.AssertEqualApprox(TestOptDouble(, 567.8), 567.8, 1E-5, "TestOptDouble(, 567.8)")
43 TestUtil.AssertEqualApprox(TestOptDouble(123.4, 567.8), 691.2, 1E-5, "TestOptDouble(123.4, 567.8)")
45 ' optionals with double datatypes (ByRef and ByVal)
46 TestUtil.AssertEqual(TestOptDoubleByRefByVal(), 0, "TestOptDoubleByRefByVal()")
47 TestUtil.AssertEqualApprox(TestOptDoubleByRefByVal(123.4), 123.4, 1E-5, "TestOptDoubleByRefByVal(123.4)")
48 TestUtil.AssertEqualApprox(TestOptDoubleByRefByVal(, 567.8), 567.8, 1E-5, "TestOptDoubleByRefByVal(, 567.8)")
49 TestUtil.AssertEqualApprox(TestOptDoubleByRefByVal(123.4, 567.8), 691.2, 1E-5, "TestOptDoubleByRefByVal(123.4, 567.8)")
51 ' optionals with integer datatypes
52 TestUtil.AssertEqual(TestOptInteger(), 0, "TestOptInteger()")
53 TestUtil.AssertEqual(TestOptInteger(123), 123, "TestOptInteger(123)")
54 TestUtil.AssertEqual(TestOptInteger(, 456), 456, "TestOptInteger(, 456)")
55 TestUtil.AssertEqual(TestOptInteger(123, 456), 579, "TestOptInteger(123, 456)")
57 ' optionals with integer datatypes (ByRef and ByVal)
58 TestUtil.AssertEqual(TestOptIntegerByRefByVal(), 0, "TestOptIntegerByRefByVal()")
59 TestUtil.AssertEqual(TestOptIntegerByRefByVal(123), 123, "TestOptIntegerByRefByVal(123)")
60 TestUtil.AssertEqual(TestOptIntegerByRefByVal(, 456), 456, "TestOptIntegerByRefByVal(, 456)")
61 TestUtil.AssertEqual(TestOptIntegerByRefByVal(123, 456), 579, "TestOptIntegerByRefByVal(123, 456)")
63 ' optionals with string datatypes
64 TestUtil.AssertEqual(TestOptString(), "", "TestOptString()")
65 TestUtil.AssertEqual(TestOptString("123"), "123", "TestOptString(""123"")")
66 TestUtil.AssertEqual(TestOptString(, "456"), "456", "TestOptString(, ""456"")")
67 TestUtil.AssertEqual(TestOptString("123", "456"), "123456", "TestOptString(""123"", ""456"")")
69 ' optionals with string datatypes (ByRef and ByVal)
70 TestUtil.AssertEqual(TestOptStringByRefByVal(), "", "TestOptStringByRefByVal()")
71 TestUtil.AssertEqual(TestOptStringByRefByVal("123"), "123", "TestOptStringByRefByVal(""123"")")
72 TestUtil.AssertEqual(TestOptStringByRefByVal(, "456"), "456", "TestOptStringByRefByVal(, ""456"")")
73 TestUtil.AssertEqual(TestOptStringByRefByVal("123", "456"), "123456", "TestOptStringByRefByVal(""123"", ""456"")")
75 ' optionals with object datatypes
76 Dim cA As New Collection
77 cA.Add (123)
78 cA.Add (456)
79 Dim cB As New Collection
80 cB.Add (123.4)
81 cB.Add (567.8)
82 TestUtil.AssertEqual(TestOptObject(), 0, "TestOptObject()")
83 TestUtil.AssertEqual(TestOptObject(cA), 579, "TestOptObject(A)")
84 TestUtil.AssertEqualApprox(TestOptObject(, cB), 691.2, 1E-5, "TestOptObject(, B)")
85 TestUtil.AssertEqualApprox(TestOptObject(cA, cB), 1270.2, 1E-5, "TestOptObject(A, B)")
87 ' optionals with object datatypes (ByRef and ByVal)
88 TestUtil.AssertEqual(TestOptObjectByRefByVal(), 0, "TestOptObjectByRefByVal()")
89 TestUtil.AssertEqual(TestOptObjectByRefByVal(cA), 579, "TestOptObjectByRefByVal(A)")
90 TestUtil.AssertEqualApprox(TestOptObjectByRefByVal(, cB), 691.2, 1E-5, "TestOptObjectByRefByVal(, B)")
91 TestUtil.AssertEqualApprox(TestOptObjectByRefByVal(cA, cB), 1270.2, 1E-5, "TestOptObjectByRefByVal(A, B)")
93 ' optionals with array datatypes
94 Dim aA(0 To 1) As Integer
95 aA(0) = 123
96 aA(1) = 456
97 Dim aB(0 To 1) As Variant
98 aB(0) = 123.4
99 aB(1) = 567.8
100 TestUtil.AssertEqual(TestOptArray(), 0, "TestOptArray()")
101 TestUtil.AssertEqual(TestOptArray(aA), 579, "TestOptArray(A)")
102 TestUtil.AssertEqualApprox(TestOptArray(, aB), 691.2, 1E-5, "TestOptArray(, B)")
103 TestUtil.AssertEqualApprox(TestOptArray(aA, aB), 1270.2, 1E-5, "TestOptArray(A, B)")
105 ' optionals with array datatypes (ByRef and ByVal)
106 TestUtil.AssertEqual(TestOptArrayByRefByVal(), 0, "TestOptArrayByRefByVal()")
107 TestUtil.AssertEqual(TestOptArrayByRefByVal(aA), 579, "TestOptArrayByRefByVal(A)")
108 TestUtil.AssertEqualApprox(TestOptArrayByRefByVal(, aB), 691.2, 1E-5, "TestOptArrayByRefByVal(, B)")
109 TestUtil.AssertEqualApprox(TestOptArrayByRefByVal(aA, aB), 1270.2, 1E-5, "TestOptArrayByRefByVal(A, B)")
111 ' tdf#144353 - error handling of missing optional parameters (arithmetic operator)
112 ' Without the fix in place, this test would have failed with:
113 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
114 ' - Actual : 549 (Actual value of the variable)
115 TestUtil.AssertEqual(TestArithmeticOperator, 449, "TestArithmeticOperator")
117 ' tdf#144353 - error handling of missing optional parameters (unary operator)
118 ' Without the fix in place, this test would have failed with:
119 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
120 ' - Actual : 100 (Actual value of the variable)
121 TestUtil.AssertEqual(TestUnaryOperator, 449, "TestUnaryOperator")
123 ' tdf#144353 - error handling of missing optional parameters (assigning to a collection)
124 ' Without the fix in place, this test would have failed with:
125 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
126 ' - Actual : 549 (Actual value of the variable)
127 TestUtil.AssertEqual(TestCollection, 449, "TestCollection")
129 ' tdf#144353 - error handling of missing optional parameters
130 ' Without the fix in place, this test would have failed with:
131 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
132 ' - Actual : 448 (Actual value of the variable)
133 TestUtil.AssertEqual(TestObjectError, 449, "TestObjectError")
135 ' tdf#151503 - error handling of missing optional parameters (boolean operations)
136 ' Without the fix in place, this test would have failed with:
137 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
138 ' - Actual : 0 (No error code since a missing parameter evaluates to true)
139 TestUtil.AssertEqual(TestBooleanOperations, 449, "TestBooleanOperations")
141 Exit Sub
142 errorHandler:
143 TestUtil.ReportErrorHandler("verify_testOptionalsBasic", Err, Error$, Erl)
144 End Sub
146 Function TestOptVariant(Optional A, Optional B As Variant)
147 TestOptVariant = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
148 End Function
150 Function TestOptVariantByRefByVal(Optional ByRef A, Optional ByVal B As Variant)
151 TestOptVariantByRefByVal = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
152 End Function
154 Function TestOptDouble(Optional A As Double, Optional B As Double)
155 TestOptDouble = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
156 End Function
158 Function TestOptDoubleByRefByVal(Optional ByRef A As Double, Optional ByVal B As Double)
159 TestOptDoubleByRefByVal = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
160 End Function
162 Function TestOptInteger(Optional A As Integer, Optional B As Integer)
163 TestOptInteger = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
164 End Function
166 Function TestOptIntegerByRefByVal(Optional ByRef A As Integer, Optional ByVal B As Integer)
167 TestOptIntegerByRefByVal = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
168 End Function
170 Function TestOptString(Optional A As String, Optional B As String)
171 TestOptString = OptStringConcat(IsMissing(A), A, IsMissing(B), B)
172 End Function
174 Function TestOptStringByRefByVal(Optional ByRef A As String, Optional ByVal B As String)
175 TestOptStringByRefByVal = OptStringConcat(IsMissing(A), A, IsMissing(B), B)
176 End Function
178 Function TestOptObject(Optional A As Collection, Optional B As Collection)
179 TestOptObject = 0
180 If Not IsMissing(A) Then TestOptObject = CollectionSum(A)
181 If Not IsMissing(B) Then TestOptObject = TestOptObject + CollectionSum(B)
182 End Function
184 Function TestOptObjectByRefByVal(Optional ByRef A As Collection, Optional ByVal B As Collection)
185 TestOptObjectByRefByVal = 0
186 If Not IsMissing(A) Then TestOptObjectByRefByVal = CollectionSum(A)
187 If Not IsMissing(B) Then TestOptObjectByRefByVal = TestOptObjectByRefByVal + CollectionSum(B)
188 End Function
190 Function TestOptArray(Optional A() As Integer, Optional B() As Variant)
191 TestOptArray = ArraySum(IsMissing(A), A) + ArraySum(IsMissing(B), B)
192 End Function
194 Function TestOptArrayByRefByVal(Optional ByRef A() As Integer, Optional ByVal B() As Variant)
195 TestOptArrayByRefByVal = ArraySum(IsMissing(A), A) + ArraySum(IsMissing(B), B)
196 End Function
198 Function OptNumberSum(is_missingA As Boolean, A, is_missingB As Boolean, B)
199 OptNumberSum = 0
200 If Not is_missingA Then OptNumberSum = A
201 If Not is_missingB Then OptNumberSum = OptNumberSum + B
202 End Function
204 Function OptStringConcat(is_missingA As Boolean, A, is_missingB As Boolean, B)
205 OptStringConcat = ""
206 If Not is_missingA Then OptStringConcat = A
207 If Not is_missingB Then OptStringConcat = OptStringConcat & B
208 End Function
210 Function TestArithmeticOperator(Optional optInt)
211 On Error GoTo errorHandler
212 optInt = optInt + 100
213 TestArithmeticOperator = optInt
214 errorHandler:
215 TestArithmeticOperator = Err()
216 End Function
218 Function TestUnaryOperator(Optional optInt)
219 On Error GoTo errorHandler
220 If (Not optInt) Then optInt = 100
221 TestUnaryOperator = optInt
222 errorHandler:
223 TestUnaryOperator = Err()
224 End Function
226 Function TestCollection(Optional optInt)
227 On Error GoTo errorHandler
228 Dim cA As New Collection
229 cA.Add(optInt)
230 TestCollection = cA.Item(1) + 100
231 errorHandler:
232 TestCollection = Err()
233 End Function
235 Function TestObjectError(Optional optInt)
236 On Error GoTo errorHandler
237 Dim aTestObject As Variant
238 aTestObject = CreateObject("testObject")
239 aTestObject.testInt = optInt
240 TestObjectError = optInt
241 errorHandler:
242 TestObjectError = Err()
243 End Function
245 Function TestBooleanOperations(Optional optBool As Boolean)
246 On Error GoTo errorHandler
247 if optBool then
248 TestBooleanOperations = 0
249 end if
250 errorHandler:
251 TestBooleanOperations = Err()
252 End Function
254 Function CollectionSum(C)
255 Dim idx As Integer
256 CollectionSum = 0
257 For idx = 1 To C.Count
258 CollectionSum = CollectionSum + C.Item(idx)
259 Next idx
260 End Function
262 Function ArraySum(is_missingC As Boolean, C)
263 Dim idx As Integer
264 ArraySum = 0
265 If Not is_missingC Then
266 For idx = LBound(C) To UBound(C)
267 ArraySum = ArraySum + C(idx)
268 Next idx
269 End If
270 End Function