2 ' This file is part of the LibreOffice project.
4 ' This Source Code Form is subject to the terms of the Mozilla Public
5 ' License, v. 2.0. If a copy of the MPL was not distributed with this
6 ' file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 Function doUnitTest() As String
13 verify_testOptionalsVba
14 doUnitTest
= TestUtil
.GetResult()
17 ' tdf#36737 - Test optionals with different datatypes. In LO Basic
18 ' with option VBASupport, optional parameters are allowed including additional
19 ' default values. Missing optional parameters having types other than variant,
20 ' which don't have explicit default values, will be initialized to their
21 ' respective default value of its datatype
22 Sub verify_testOptionalsVba()
23 On Error GoTo errorHandler
25 ' tdf#143707 - check correct initialization of default value for optionals
26 ' Without the fix in place, this test would have failed with
29 TestUtil
.AssertEqual(TestOptVariantInit(), 123, "TestOptVariantInit()")
31 ' optionals with variant datatypes
32 TestUtil
.AssertEqual(TestOptVariant(), 123, "TestOptVariant()")
33 TestUtil
.AssertEqual(TestOptVariant(123), 246, "TestOptVariant(123)")
34 TestUtil
.AssertEqual(TestOptVariant(, 456), 456, "TestOptVariant(, 456)")
35 TestUtil
.AssertEqual(TestOptVariant(123, 456), 579, "TestOptVariant(123, 456)")
37 ' optionals with variant datatypes (ByRef and ByVal)
38 TestUtil
.AssertEqual(TestOptVariantByRefByVal(), 123, "TestOptVariantByRefByVal()")
39 TestUtil
.AssertEqual(TestOptVariantByRefByVal(123), 246, "TestOptVariantByRefByVal(123)")
40 TestUtil
.AssertEqual(TestOptVariantByRefByVal(, 456), 456, "TestOptVariantByRefByVal(, 456)")
41 TestUtil
.AssertEqual(TestOptVariantByRefByVal(123, 456), 579, "TestOptVariantByRefByVal(123, 456)")
43 ' optionals with double datatypes
44 TestUtil
.AssertEqualApprox(TestOptDouble(), 123.4, 1E-5, "TestOptDouble()")
45 TestUtil
.AssertEqualApprox(TestOptDouble(123.4), 246.8, 1E-5, "TestOptDouble(123.4)")
46 TestUtil
.AssertEqualApprox(TestOptDouble(, 567.8), 567.8, 1E-5, "TestOptDouble(, 567.8)")
47 TestUtil
.AssertEqualApprox(TestOptDouble(123.4, 567.8), 691.2, 1E-5, "TestOptDouble(123.4, 567.8)")
49 ' optionals with double datatypes (ByRef and ByVal)
50 TestUtil
.AssertEqualApprox(TestOptDoubleByRefByVal(), 123.4, 1E-5, "TestOptDoubleByRefByVal()")
51 TestUtil
.AssertEqualApprox(TestOptDoubleByRefByVal(123.4), 246.8, 1E-5, "TestOptDoubleByRefByVal(123.4)")
52 TestUtil
.AssertEqualApprox(TestOptDoubleByRefByVal(, 567.8), 567.8, 1E-5, "TestOptDoubleByRefByVal(, 567.8)")
53 TestUtil
.AssertEqualApprox(TestOptDoubleByRefByVal(123.4, 567.8), 691.2, 1E-5, "TestOptDoubleByRefByVal(123.4, 567.8)")
55 ' optionals with integer datatypes
56 TestUtil
.AssertEqual(TestOptInteger(), 123, "TestOptInteger()")
57 TestUtil
.AssertEqual(TestOptInteger(123), 246, "TestOptInteger(123)")
58 TestUtil
.AssertEqual(TestOptInteger(, 456), 456, "TestOptInteger(, 456)")
59 TestUtil
.AssertEqual(TestOptInteger(123, 456), 579, "TestOptInteger(123, 456)")
61 ' optionals with integer datatypes (ByRef and ByVal)
62 TestUtil
.AssertEqual(TestOptIntegerByRefByVal(), 123, "TestOptIntegerByRefByVal()")
63 TestUtil
.AssertEqual(TestOptIntegerByRefByVal(123), 246, "TestOptIntegerByRefByVal(123)")
64 TestUtil
.AssertEqual(TestOptIntegerByRefByVal(, 456), 456, "TestOptIntegerByRefByVal(, 456)")
65 TestUtil
.AssertEqual(TestOptIntegerByRefByVal(123, 456), 579, "TestOptIntegerByRefByVal(123, 456)")
67 ' optionals with string datatypes
68 TestUtil
.AssertEqual(TestOptString(), "123", "TestOptString()")
69 TestUtil
.AssertEqual(TestOptString("123"), "123123", "TestOptString(""123"")")
70 TestUtil
.AssertEqual(TestOptString(, "456"), "456", "TestOptString(, ""456"")")
71 TestUtil
.AssertEqual(TestOptString("123", "456"), "123456", "TestOptString(""123"", ""456"")")
73 ' optionals with string datatypes (ByRef and ByVal)
74 TestUtil
.AssertEqual(TestOptStringByRefByVal(), "123", "TestOptStringByRefByVal()")
75 TestUtil
.AssertEqual(TestOptStringByRefByVal("123"), "123123", "TestOptStringByRefByVal(""123"")")
76 TestUtil
.AssertEqual(TestOptStringByRefByVal(, "456"), "456", "TestOptStringByRefByVal(, ""456"")")
77 TestUtil
.AssertEqual(TestOptStringByRefByVal("123", "456"), "123456", "TestOptStringByRefByVal(""123"", ""456"")")
79 ' optionals with object datatypes
80 Dim cA
As New Collection
83 Dim cB
As New Collection
86 TestUtil
.AssertEqual(TestOptObject(), 0, "TestOptObject()")
87 TestUtil
.AssertEqual(TestOptObject(cA
), 579, "TestOptObject(A)")
88 TestUtil
.AssertEqualApprox(TestOptObject(, cB
), 691.2, 1E-5, "TestOptObject(, B)")
89 TestUtil
.AssertEqualApprox(TestOptObject(cA
, cB
), 1270.2, 1E-5, "TestOptObject(A, B)")
91 ' optionals with object datatypes (ByRef and ByVal)
92 TestUtil
.AssertEqual(TestOptObjectByRefByVal(), 0, "TestOptObjectByRefByVal()")
93 TestUtil
.AssertEqual(TestOptObjectByRefByVal(cA
), 579, "TestOptObjectByRefByVal(A)")
94 TestUtil
.AssertEqualApprox(TestOptObjectByRefByVal(, cB
), 691.2, 1E-5, "TestOptObjectByRefByVal(, B)")
95 TestUtil
.AssertEqualApprox(TestOptObjectByRefByVal(cA
, cB
), 1270.2, 1E-5, "TestOptObjectByRefByVal(A, B)")
97 ' optionals with array datatypes
98 Dim aA(0 To 1) As Integer
101 Dim aB(0 To 1) As Variant
104 ' TODO - New bug report? Scanner initializes variable not as an array
105 ' TestUtil.AssertEqual(TestOptArray(), 0, "TestOptArray()")
106 ' TestUtil.AssertEqual(TestOptArray(aA), 579, "TestOptArray(A)")
107 ' TestUtil.AssertEqualApprox(TestOptArray(, aB), 691.2, 1E-5, "TestOptArray(, B)")
108 TestUtil
.AssertEqualApprox(TestOptArray(aA
, aB
), 1270.2, 1E-5, "TestOptArray(A, B)")
110 ' optionals with array datatypes (ByRef and ByVal)
111 ' TODO - New bug report? Scanner initializes variable not as an array
112 ' TestUtil.AssertEqual(TestOptArrayByRefByVal(), 0, "TestOptArrayByRefByVal()")
113 ' TestUtil.AssertEqual(TestOptArrayByRefByVal(aA), 579, "TestOptArrayByRefByVal(A)")
114 ' TestUtil.AssertEqualApprox(TestOptArrayByRefByVal(, aB), 691.2, 1E-5, "TestOptArrayByRefByVal(, B)")
115 TestUtil
.AssertEqualApprox(TestOptArrayByRefByVal(aA
, aB
), 1270.2, 1E-5, "TestOptArrayByRefByVal(A, B)")
119 TestUtil
.ReportErrorHandler("verify_testOptionalsVba", Err
, Error$, Erl
)
122 Function TestOptVariantInit(Optional A
As Variant = 123)
123 TestOptVariantInit
= A
126 Function TestOptVariant(Optional A
, Optional B
As Variant = 123)
127 TestOptVariant
= OptNumberSum(IsMissing(A
), A
, IsMissing(B
), B
)
130 Function TestOptVariantByRefByVal(Optional ByRef A
, Optional ByVal B
As Variant = 123)
131 TestOptVariantByRefByVal
= OptNumberSum(IsMissing(A
), A
, IsMissing(B
), B
)
134 Function TestOptDouble(Optional A
As Double, Optional B
As Double = 123.4)
135 TestOptDouble
= OptNumberSum(IsMissing(A
), A
, IsMissing(B
), B
)
138 Function TestOptDoubleByRefByVal(Optional ByRef A
As Double, Optional ByVal B
As Double = 123.4)
139 TestOptDoubleByRefByVal
= OptNumberSum(IsMissing(A
), A
, IsMissing(B
), B
)
142 Function TestOptInteger(Optional A
As Integer, Optional B
As Integer = 123)
143 TestOptInteger
= OptNumberSum(IsMissing(A
), A
, IsMissing(B
), B
)
146 Function TestOptIntegerByRefByVal(Optional ByRef A
As Integer, Optional ByVal B
As Integer = 123)
147 TestOptIntegerByRefByVal
= OptNumberSum(IsMissing(A
), A
, IsMissing(B
), B
)
150 Function TestOptString(Optional A
As String, Optional B
As String = "123")
151 TestOptString
= OptStringConcat(IsMissing(A
), A
, IsMissing(B
), B
)
154 Function TestOptStringByRefByVal(Optional ByRef A
As String, Optional ByVal B
As String = "123")
155 TestOptStringByRefByVal
= OptStringConcat(IsMissing(A
), A
, IsMissing(B
), B
)
158 Function TestOptObject(Optional A
As Collection
, Optional B
As Collection
)
159 ' TODO - isMissing returns false even though the collection is null and is missing?
161 If Not IsNull(A
) Then TestOptObject
= CollectionSum(A
)
162 If Not IsNull(B
) Then TestOptObject
= TestOptObject
+ CollectionSum(B
)
165 Function TestOptObjectByRefByVal(Optional ByRef A
As Collection
, Optional ByVal B
As Collection
)
166 ' TODO - isMissing returns false even though the collection is null and is missing?
167 TestOptObjectByRefByVal
= 0
168 If Not IsNull(A
) Then TestOptObjectByRefByVal
= CollectionSum(A
)
169 If Not IsNull(B
) Then TestOptObjectByRefByVal
= TestOptObjectByRefByVal
+ CollectionSum(B
)
172 Function TestOptArray(Optional A() As Integer, Optional B() As Variant)
173 TestOptArray
= ArraySum(IsMissing(A
), A
) + ArraySum(IsMissing(B
), B
)
176 Function TestOptArrayByRefByVal(Optional ByRef A() As Integer, Optional ByVal B() As Variant)
177 TestOptArrayByRefByVal
= ArraySum(IsMissing(A
), A
) + ArraySum(IsMissing(B
), B
)
180 Function OptNumberSum(is_missingA
As Boolean, A
, is_missingB
As Boolean, B
)
182 If Not is_missingA
Then OptNumberSum
= A
183 If Not is_missingB
Then OptNumberSum
= OptNumberSum
+ B
186 Function OptStringConcat(is_missingA
As Boolean, A
, is_missingB
As Boolean, B
)
188 If Not is_missingA
Then OptStringConcat
= A
189 If Not is_missingB
Then OptStringConcat
= OptStringConcat
& B
192 Function CollectionSum(C
)
195 For idx
= 1 To C
.Count
196 CollectionSum
= CollectionSum
+ C
.Item(idx
)
200 Function ArraySum(is_missingC
As Boolean, C
)
203 If Not is_missingC
Then
204 For idx
= LBound(C
) To UBound(C
)
205 ArraySum
= ArraySum
+ C(idx
)