tdf#161411 - UI: Add Better wording for ASCII-only characters
[LibreOffice.git] / basic / qa / cppunit / test_nested_struct.cxx
blobc7b9127592e28c555462e5c3c2f5b3fbfd2adf91
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9 #include "basictest.hxx"
11 #include <com/sun/star/awt/WindowDescriptor.hpp>
12 #include <com/sun/star/table/TableBorder.hpp>
13 #include <basic/sbuno.hxx>
15 namespace
17 using namespace com::sun::star;
18 class Nested_Struct : public test::BootstrapFixture
20 public:
21 Nested_Struct(): BootstrapFixture(true, false) {};
22 void testAssign1();
23 void testAssign1Alt(); // result is uno-ised and tested
24 void testOldAssign();
25 void testOldAssignAlt(); // result is uno-ised and tested
26 void testUnfixedVarAssign();
27 void testUnfixedVarAssignAlt(); // result is uno-ised and tested
28 void testFixedVarAssign();
29 void testFixedVarAssignAlt(); // result is uno-ised and tested
30 void testUnoAccess(); // fdo#60117 specific test
31 void testTdf134576();
33 // Adds code needed to register the test suite
34 CPPUNIT_TEST_SUITE(Nested_Struct);
36 // Declares the method as a test to call
37 CPPUNIT_TEST(testAssign1);
38 CPPUNIT_TEST(testAssign1Alt);
39 CPPUNIT_TEST(testOldAssign);
40 CPPUNIT_TEST(testOldAssignAlt);
41 CPPUNIT_TEST(testUnfixedVarAssign);
42 CPPUNIT_TEST(testUnfixedVarAssignAlt);
43 CPPUNIT_TEST(testFixedVarAssign);
44 CPPUNIT_TEST(testFixedVarAssignAlt);
45 CPPUNIT_TEST(testUnoAccess);
46 CPPUNIT_TEST(testTdf134576);
48 // End of test suite definition
49 CPPUNIT_TEST_SUITE_END();
52 // tests the new behaviour, we should be able to
53 // directly modify the value of the nested 'HorizontalLine' struct
54 OUString sTestSource1(
55 u"Function doUnitTest() as Integer\n"
56 "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
57 "b0.HorizontalLine.OuterLineWidth = 9\n"
58 "doUnitTest = b0.HorizontalLine.OuterLineWidth\n"
59 "End Function\n"_ustr
62 OUString sTestSource1Alt(
63 u"Function doUnitTest() as Object\n"
64 "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
65 "b0.HorizontalLine.OuterLineWidth = 9\n"
66 "doUnitTest = b0\n"
67 "End Function\n"_ustr
70 // tests the old behaviour, we should still be able
71 // to use the old workaround of
72 // a) creating a new instance BorderLine,
73 // b) cloning the new instance with the value of b0.HorizontalLine
74 // c) modifying the new instance
75 // d) setting b0.HorizontalLine with the value of the new instance
76 OUString sTestSource2(
77 u"Function doUnitTest()\n"
78 "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
79 "l = b0.HorizontalLine\n"
80 "l.OuterLineWidth = 9\n"
81 "b0.HorizontalLine = l\n"
82 "doUnitTest = b0.HorizontalLine.OuterLineWidth\n"
83 "End Function\n"_ustr
86 OUString sTestSource2Alt(
87 u"Function doUnitTest()\n"
88 "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
89 "l = b0.HorizontalLine\n"
90 "l.OuterLineWidth = 9\n"
91 "b0.HorizontalLine = l\n"
92 "doUnitTest = b0\n"
93 "End Function\n"_ustr
95 // it should be legal to assign a variant to a struct ( and copy by val )
96 // make sure we aren't copying by reference, we make sure that l is not
97 // a reference copy of b0.HorizontalLine, each one should have an
98 // OuterLineWidth of 4 & 9 respectively and we should be returning
99 // 13 the sum of the two ( hopefully unique values if we haven't copied by reference )
100 OUString sTestSource3(
101 u"Function doUnitTest()\n"
102 "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
103 "l = b0.HorizontalLine\n"
104 "l.OuterLineWidth = 9\n"
105 "b0.HorizontalLine = l\n"
106 "l.OuterLineWidth = 4\n"
107 "doUnitTest = b0.HorizontalLine.OuterLineWidth + l.OuterLineWidth\n"
108 "End Function\n"_ustr
111 OUString sTestSource3Alt(
112 u"Function doUnitTest()\n"
113 "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
114 "l = b0.HorizontalLine\n"
115 "l.OuterLineWidth = 9\n"
116 "b0.HorizontalLine = l\n"
117 "l.OuterLineWidth = 4\n"
118 "Dim result(1)\n"
119 "result(0) = b0\n"
120 "result(1) = l\n"
121 "doUnitTest = result\n"
122 "End Function\n"_ustr
125 // nearly the same as above but this time for a fixed type
126 // variable
127 OUString sTestSource4(
128 u"Function doUnitTest()\n"
129 "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
130 "l = b0.HorizontalLine\n"
131 "l.OuterLineWidth = 9\n"
132 "b0.HorizontalLine = l\n"
133 "l.OuterLineWidth = 4\n"
134 "doUnitTest = b0.HorizontalLine.OuterLineWidth + l.OuterLineWidth\n"
135 "End Function\n"_ustr
138 OUString sTestSource4Alt(
139 u"Function doUnitTest()\n"
140 "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
141 "l = b0.HorizontalLine\n"
142 "l.OuterLineWidth = 9\n"
143 "b0.HorizontalLine = l\n"
144 "l.OuterLineWidth = 4\n"
145 "Dim result(1)\n"
146 "result(0) = b0\n"
147 "result(1) = l\n"
148 "doUnitTest = result\n"
149 "End Function\n"_ustr
152 // Although basic might appear to correctly change nested struct elements
153 // fdo#60117 shows that basic can be fooled ( and even the watch(ed) variable
154 // in the debugger shows the expected values )
155 // We need to additionally check the actual uno struct to see if the
156 // changes made are *really* reflected in the object
157 OUString sTestSource5(
158 u"Function doUnitTest() as Object\n"
159 "Dim aWinDesc as new \"com.sun.star.awt.WindowDescriptor\"\n"
160 "Dim aRect as new \"com.sun.star.awt.Rectangle\"\n"
161 "aRect.X = 200\n"
162 "aWinDesc.Bounds = aRect\n"
163 "doUnitTest = aWinDesc\n"
164 "End Function\n"_ustr
168 void Nested_Struct::testAssign1()
170 MacroSnippet myMacro( sTestSource1 );
171 myMacro.Compile();
172 CPPUNIT_ASSERT_MESSAGE("testAssign1 fails with compile error",!myMacro.HasError() );
173 SbxVariableRef pNew = myMacro.Run();
174 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int16>(9), pNew->GetInteger());
177 void Nested_Struct::testAssign1Alt()
179 MacroSnippet myMacro( sTestSource1Alt );
180 myMacro.Compile();
181 CPPUNIT_ASSERT_MESSAGE("testAssign1Alt fails with compile error",!myMacro.HasError() );
182 SbxVariableRef pNew = myMacro.Run();
183 uno::Any aRet = sbxToUnoValue( pNew.get() );
184 table::TableBorder aBorder;
185 aRet >>= aBorder;
187 int result = aBorder.HorizontalLine.OuterLineWidth;
188 CPPUNIT_ASSERT_EQUAL( 9, result );
191 void Nested_Struct::testOldAssign()
193 MacroSnippet myMacro( sTestSource2 );
194 myMacro.Compile();
195 CPPUNIT_ASSERT_MESSAGE("testOldAssign fails with compile error",!myMacro.HasError() );
196 SbxVariableRef pNew = myMacro.Run();
197 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int16>(9), pNew->GetInteger());
200 void Nested_Struct::testOldAssignAlt()
202 MacroSnippet myMacro( sTestSource2Alt );
203 myMacro.Compile();
204 CPPUNIT_ASSERT_MESSAGE("testOldAssign fails with compile error",!myMacro.HasError() );
205 SbxVariableRef pNew = myMacro.Run();
206 uno::Any aRet = sbxToUnoValue( pNew.get() );
207 table::TableBorder aBorder;
208 aRet >>= aBorder;
210 int result = aBorder.HorizontalLine.OuterLineWidth;
211 CPPUNIT_ASSERT_EQUAL( 9, result );
214 void Nested_Struct::testUnfixedVarAssign()
216 MacroSnippet myMacro( sTestSource3 );
217 myMacro.Compile();
218 CPPUNIT_ASSERT_MESSAGE("testUnfixedVarAssign fails with compile error",!myMacro.HasError() );
219 // forces a broadcast
220 SbxVariableRef pNew = myMacro.Run();
221 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int16>(13), pNew->GetInteger());
224 void Nested_Struct::testUnfixedVarAssignAlt()
226 MacroSnippet myMacro( sTestSource3Alt );
227 myMacro.Compile();
228 CPPUNIT_ASSERT_MESSAGE("testUnfixedVarAssignAlt fails with compile error",!myMacro.HasError() );
229 SbxVariableRef pNew = myMacro.Run();
230 uno::Any aRet = sbxToUnoValue( pNew.get() );
232 uno::Sequence< uno::Any > aResult;
233 bool bRes = aRet >>= aResult;
234 CPPUNIT_ASSERT_EQUAL(true, bRes );
236 int result = aResult.getLength();
237 // should have 2 elements in a sequence returned
238 CPPUNIT_ASSERT_EQUAL(2, result );
240 table::TableBorder aBorder;
241 aResult[0] >>= aBorder;
243 table::BorderLine aBorderLine;
244 aResult[1] >>= aBorderLine;
245 result = aBorder.HorizontalLine.OuterLineWidth;
246 CPPUNIT_ASSERT_EQUAL(9, result );
247 result = aBorderLine.OuterLineWidth;
248 CPPUNIT_ASSERT_EQUAL(4, result );
251 void Nested_Struct::testFixedVarAssign()
253 MacroSnippet myMacro( sTestSource4 );
254 myMacro.Compile();
255 CPPUNIT_ASSERT_MESSAGE("testFixedVarAssign fails with compile error",!myMacro.HasError() );
256 SbxVariableRef pNew = myMacro.Run();
257 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int16>(13), pNew->GetInteger());
260 void Nested_Struct::testFixedVarAssignAlt()
262 MacroSnippet myMacro( sTestSource4Alt );
263 myMacro.Compile();
264 CPPUNIT_ASSERT_MESSAGE("testFixedVarAssignAlt fails with compile error",!myMacro.HasError() );
265 SbxVariableRef pNew = myMacro.Run();
266 uno::Any aRet = sbxToUnoValue( pNew.get() );
268 uno::Sequence< uno::Any > aResult;
269 bool bRes = aRet >>= aResult;
270 CPPUNIT_ASSERT_EQUAL(true, bRes );
272 int result = aResult.getLength();
273 // should have 2 elements in a sequence returned
274 CPPUNIT_ASSERT_EQUAL(2, result );
276 table::TableBorder aBorder;
277 aResult[0] >>= aBorder;
279 table::BorderLine aBorderLine;
280 aResult[1] >>= aBorderLine;
281 result = aBorder.HorizontalLine.OuterLineWidth;
282 CPPUNIT_ASSERT_EQUAL(9, result );
283 result = aBorderLine.OuterLineWidth;
284 CPPUNIT_ASSERT_EQUAL(4, result );
287 void Nested_Struct::testUnoAccess()
289 MacroSnippet myMacro( sTestSource5 );
290 myMacro.Compile();
291 CPPUNIT_ASSERT_MESSAGE("testUnoAccess fails with compile error",!myMacro.HasError() );
292 SbxVariableRef pNew = myMacro.Run();
293 uno::Any aRet = sbxToUnoValue( pNew.get() );
294 awt::WindowDescriptor aWinDesc;
295 aRet >>= aWinDesc;
297 int result = aWinDesc.Bounds.X;
298 CPPUNIT_ASSERT_EQUAL(200, result );
301 void Nested_Struct::testTdf134576()
303 MacroSnippet myMacro(u"Function doUnitTest()\n"
304 " On Error Resume Next\n"
305 " For Each a In b\n"
306 " c.d\n"
307 " Next\n"
308 " doUnitTest = 1\n"
309 "End Function\n"_ustr);
311 myMacro.Compile();
312 CPPUNIT_ASSERT(!myMacro.HasError());
314 // Without the fix in place, it would have crashed here
315 SbxVariableRef pNew = myMacro.Run();
316 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int16>(1), pNew->GetInteger());
319 // Put the test suite in the registry
320 CPPUNIT_TEST_SUITE_REGISTRATION(Nested_Struct);
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */