LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / basic / qa / basic_coverage / test_optional_paramters_basic.bas
bloba684fc256054ffbce2b8f340e2de91d05485c940
1 Option Explicit
3 Type testObject
4 testInt As Integer
5 End Type
7 Function doUnitTest() As String
8 TestUtil.TestInit
9 verify_testOptionalsBasic
10 doUnitTest = TestUtil.GetResult()
11 End Function
13 ' tdf#36737 - Test optionals with different datatypes. In LO Basic, optional
14 ' parameters are allowed, but without any default values. Missing optional
15 ' parameters will not be initialized to their respective default values of
16 ' its datatype, either.
17 Sub verify_testOptionalsBasic()
18 On Error GoTo errorHandler
20 ' optionals with variant datatypes
21 TestUtil.AssertEqual(TestOptVariant(), 0, "TestOptVariant()")
22 TestUtil.AssertEqual(TestOptVariant(123), 123, "TestOptVariant(123)")
23 TestUtil.AssertEqual(TestOptVariant(, 456), 456, "TestOptVariant(, 456)")
24 TestUtil.AssertEqual(TestOptVariant(123, 456), 579, "TestOptVariant(123, 456)")
26 ' optionals with variant datatypes (ByRef and ByVal)
27 TestUtil.AssertEqual(TestOptVariantByRefByVal(), 0, "TestOptVariantByRefByVal()")
28 TestUtil.AssertEqualApprox(TestOptVariantByRefByVal(123), 123, "TestOptVariantByRefByVal(123)")
29 TestUtil.AssertEqualApprox(TestOptVariantByRefByVal(, 456), 456, "TestOptVariantByRefByVal(, 456)")
30 TestUtil.AssertEqualApprox(TestOptVariantByRefByVal(123, 456), 579, "TestOptVariantByRefByVal(123, 456)")
32 ' optionals with double datatypes
33 TestUtil.AssertEqual(TestOptDouble(), 0, "TestOptDouble()")
34 TestUtil.AssertEqualApprox(TestOptDouble(123.4), 123.4, 1E-5, "TestOptDouble(123.4)")
35 TestUtil.AssertEqualApprox(TestOptDouble(, 567.8), 567.8, 1E-5, "TestOptDouble(, 567.8)")
36 TestUtil.AssertEqualApprox(TestOptDouble(123.4, 567.8), 691.2, 1E-5, "TestOptDouble(123.4, 567.8)")
38 ' optionals with double datatypes (ByRef and ByVal)
39 TestUtil.AssertEqual(TestOptDoubleByRefByVal(), 0, "TestOptDoubleByRefByVal()")
40 TestUtil.AssertEqualApprox(TestOptDoubleByRefByVal(123.4), 123.4, 1E-5, "TestOptDoubleByRefByVal(123.4)")
41 TestUtil.AssertEqualApprox(TestOptDoubleByRefByVal(, 567.8), 567.8, 1E-5, "TestOptDoubleByRefByVal(, 567.8)")
42 TestUtil.AssertEqualApprox(TestOptDoubleByRefByVal(123.4, 567.8), 691.2, 1E-5, "TestOptDoubleByRefByVal(123.4, 567.8)")
44 ' optionals with integer datatypes
45 TestUtil.AssertEqual(TestOptInteger(), 0, "TestOptInteger()")
46 TestUtil.AssertEqual(TestOptInteger(123), 123, "TestOptInteger(123)")
47 TestUtil.AssertEqual(TestOptInteger(, 456), 456, "TestOptInteger(, 456)")
48 TestUtil.AssertEqual(TestOptInteger(123, 456), 579, "TestOptInteger(123, 456)")
50 ' optionals with integer datatypes (ByRef and ByVal)
51 TestUtil.AssertEqual(TestOptIntegerByRefByVal(), 0, "TestOptIntegerByRefByVal()")
52 TestUtil.AssertEqual(TestOptIntegerByRefByVal(123), 123, "TestOptIntegerByRefByVal(123)")
53 TestUtil.AssertEqual(TestOptIntegerByRefByVal(, 456), 456, "TestOptIntegerByRefByVal(, 456)")
54 TestUtil.AssertEqual(TestOptIntegerByRefByVal(123, 456), 579, "TestOptIntegerByRefByVal(123, 456)")
56 ' optionals with string datatypes
57 TestUtil.AssertEqual(TestOptString(), "", "TestOptString()")
58 TestUtil.AssertEqual(TestOptString("123"), "123", "TestOptString(""123"")")
59 TestUtil.AssertEqual(TestOptString(, "456"), "456", "TestOptString(, ""456"")")
60 TestUtil.AssertEqual(TestOptString("123", "456"), "123456", "TestOptString(""123"", ""456"")")
62 ' optionals with string datatypes (ByRef and ByVal)
63 TestUtil.AssertEqual(TestOptStringByRefByVal(), "", "TestOptStringByRefByVal()")
64 TestUtil.AssertEqual(TestOptStringByRefByVal("123"), "123", "TestOptStringByRefByVal(""123"")")
65 TestUtil.AssertEqual(TestOptStringByRefByVal(, "456"), "456", "TestOptStringByRefByVal(, ""456"")")
66 TestUtil.AssertEqual(TestOptStringByRefByVal("123", "456"), "123456", "TestOptStringByRefByVal(""123"", ""456"")")
68 ' optionals with object datatypes
69 Dim cA As New Collection
70 cA.Add (123)
71 cA.Add (456)
72 Dim cB As New Collection
73 cB.Add (123.4)
74 cB.Add (567.8)
75 TestUtil.AssertEqual(TestOptObject(), 0, "TestOptObject()")
76 TestUtil.AssertEqual(TestOptObject(cA), 579, "TestOptObject(A)")
77 TestUtil.AssertEqualApprox(TestOptObject(, cB), 691.2, 1E-5, "TestOptObject(, B)")
78 TestUtil.AssertEqualApprox(TestOptObject(cA, cB), 1270.2, 1E-5, "TestOptObject(A, B)")
80 ' optionals with object datatypes (ByRef and ByVal)
81 TestUtil.AssertEqual(TestOptObjectByRefByVal(), 0, "TestOptObjectByRefByVal()")
82 TestUtil.AssertEqual(TestOptObjectByRefByVal(cA), 579, "TestOptObjectByRefByVal(A)")
83 TestUtil.AssertEqualApprox(TestOptObjectByRefByVal(, cB), 691.2, 1E-5, "TestOptObjectByRefByVal(, B)")
84 TestUtil.AssertEqualApprox(TestOptObjectByRefByVal(cA, cB), 1270.2, 1E-5, "TestOptObjectByRefByVal(A, B)")
86 ' optionals with array datatypes
87 Dim aA(0 To 1) As Integer
88 aA(0) = 123
89 aA(1) = 456
90 Dim aB(0 To 1) As Variant
91 aB(0) = 123.4
92 aB(1) = 567.8
93 TestUtil.AssertEqual(TestOptArray(), 0, "TestOptArray()")
94 TestUtil.AssertEqual(TestOptArray(aA), 579, "TestOptArray(A)")
95 TestUtil.AssertEqualApprox(TestOptArray(, aB), 691.2, 1E-5, "TestOptArray(, B)")
96 TestUtil.AssertEqualApprox(TestOptArray(aA, aB), 1270.2, 1E-5, "TestOptArray(A, B)")
98 ' optionals with array datatypes (ByRef and ByVal)
99 TestUtil.AssertEqual(TestOptArrayByRefByVal(), 0, "TestOptArrayByRefByVal()")
100 TestUtil.AssertEqual(TestOptArrayByRefByVal(aA), 579, "TestOptArrayByRefByVal(A)")
101 TestUtil.AssertEqualApprox(TestOptArrayByRefByVal(, aB), 691.2, 1E-5, "TestOptArrayByRefByVal(, B)")
102 TestUtil.AssertEqualApprox(TestOptArrayByRefByVal(aA, aB), 1270.2, 1E-5, "TestOptArrayByRefByVal(A, B)")
104 ' tdf#144353 - error handling of missing optional parameters (arithmetic operator)
105 ' Without the fix in place, this test would have failed with:
106 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
107 ' - Actual : 549 (Actual value of the variable)
108 TestUtil.AssertEqual(TestArithmeticOperator, 449, "TestArithmeticOperator")
110 ' tdf#144353 - error handling of missing optional parameters (unary operator)
111 ' Without the fix in place, this test would have failed with:
112 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
113 ' - Actual : 100 (Actual value of the variable)
114 TestUtil.AssertEqual(TestUnaryOperator, 449, "TestUnaryOperator")
116 ' tdf#144353 - error handling of missing optional parameters (assigning to a collection)
117 ' Without the fix in place, this test would have failed with:
118 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
119 ' - Actual : 549 (Actual value of the variable)
120 TestUtil.AssertEqual(TestCollection, 449, "TestCollection")
122 ' tdf#144353 - error handling of missing optional parameters
123 ' Without the fix in place, this test would have failed with:
124 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
125 ' - Actual : 448 (Actual value of the variable)
126 TestUtil.AssertEqual(TestObjectError, 449, "TestObjectError")
128 Exit Sub
129 errorHandler:
130 TestUtil.ReportErrorHandler("verify_testOptionalsBasic", Err, Error$, Erl)
131 End Sub
133 Function TestOptVariant(Optional A, Optional B As Variant)
134 TestOptVariant = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
135 End Function
137 Function TestOptVariantByRefByVal(Optional ByRef A, Optional ByVal B As Variant)
138 TestOptVariantByRefByVal = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
139 End Function
141 Function TestOptDouble(Optional A As Double, Optional B As Double)
142 TestOptDouble = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
143 End Function
145 Function TestOptDoubleByRefByVal(Optional ByRef A As Double, Optional ByVal B As Double)
146 TestOptDoubleByRefByVal = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
147 End Function
149 Function TestOptInteger(Optional A As Integer, Optional B As Integer)
150 TestOptInteger = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
151 End Function
153 Function TestOptIntegerByRefByVal(Optional ByRef A As Integer, Optional ByVal B As Integer)
154 TestOptIntegerByRefByVal = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
155 End Function
157 Function TestOptString(Optional A As String, Optional B As String)
158 TestOptString = OptStringConcat(IsMissing(A), A, IsMissing(B), B)
159 End Function
161 Function TestOptStringByRefByVal(Optional ByRef A As String, Optional ByVal B As String)
162 TestOptStringByRefByVal = OptStringConcat(IsMissing(A), A, IsMissing(B), B)
163 End Function
165 Function TestOptObject(Optional A As Collection, Optional B As Collection)
166 TestOptObject = 0
167 If Not IsMissing(A) Then TestOptObject = CollectionSum(A)
168 If Not IsMissing(B) Then TestOptObject = TestOptObject + CollectionSum(B)
169 End Function
171 Function TestOptObjectByRefByVal(Optional ByRef A As Collection, Optional ByVal B As Collection)
172 TestOptObjectByRefByVal = 0
173 If Not IsMissing(A) Then TestOptObjectByRefByVal = CollectionSum(A)
174 If Not IsMissing(B) Then TestOptObjectByRefByVal = TestOptObjectByRefByVal + CollectionSum(B)
175 End Function
177 Function TestOptArray(Optional A() As Integer, Optional B() As Variant)
178 TestOptArray = ArraySum(IsMissing(A), A) + ArraySum(IsMissing(B), B)
179 End Function
181 Function TestOptArrayByRefByVal(Optional ByRef A() As Integer, Optional ByVal B() As Variant)
182 TestOptArrayByRefByVal = ArraySum(IsMissing(A), A) + ArraySum(IsMissing(B), B)
183 End Function
185 Function OptNumberSum(is_missingA As Boolean, A, is_missingB As Boolean, B)
186 OptNumberSum = 0
187 If Not is_missingA Then OptNumberSum = A
188 If Not is_missingB Then OptNumberSum = OptNumberSum + B
189 End Function
191 Function OptStringConcat(is_missingA As Boolean, A, is_missingB As Boolean, B)
192 OptStringConcat = ""
193 If Not is_missingA Then OptStringConcat = A
194 If Not is_missingB Then OptStringConcat = OptStringConcat & B
195 End Function
197 Function TestArithmeticOperator(Optional optInt)
198 On Error GoTo errorHandler
199 optInt = optInt + 100
200 TestArithmeticOperator = optInt
201 errorHandler:
202 TestArithmeticOperator = Err()
203 End Function
205 Function TestUnaryOperator(Optional optInt)
206 On Error GoTo errorHandler
207 If (Not optInt) Then optInt = 100
208 TestUnaryOperator = optInt
209 errorHandler:
210 TestUnaryOperator = Err()
211 End Function
213 Function TestCollection(Optional optInt)
214 On Error GoTo errorHandler
215 Dim cA As New Collection
216 cA.Add(optInt)
217 TestCollection = cA.Item(1) + 100
218 errorHandler:
219 TestCollection = Err()
220 End Function
222 Function TestObjectError(Optional optInt)
223 On Error GoTo errorHandler
224 Dim aTestObject As Variant
225 aTestObject = CreateObject("testObject")
226 aTestObject.testInt = optInt
227 TestObjectError = optInt
228 errorHandler:
229 TestObjectError = Err()
230 End Function
232 Function CollectionSum(C)
233 Dim idx As Integer
234 CollectionSum = 0
235 For idx = 1 To C.Count
236 CollectionSum = CollectionSum + C.Item(idx)
237 Next idx
238 End Function
240 Function ArraySum(is_missingC As Boolean, C)
241 Dim idx As Integer
242 ArraySum = 0
243 If Not is_missingC Then
244 For idx = LBound(C) To UBound(C)
245 ArraySum = ArraySum + C(idx)
246 Next idx
247 End If
248 End Function