Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Copyright 2012 LibreOffice contributors.
4 : *
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"
10 : #include "osl/file.hxx"
11 : #include "osl/process.h"
12 :
13 : #include "basic/sbmod.hxx"
14 : #include "basic/sbmeth.hxx"
15 : namespace
16 : {
17 16 : class Nested_Struct : public BasicTestBase
18 : {
19 : public:
20 8 : Nested_Struct() {};
21 : void testAssign1();
22 : void testOldAssign();
23 : void testUnfixedVarAssign();
24 : void testFixedVarAssign();
25 : // Adds code needed to register the test suite
26 4 : CPPUNIT_TEST_SUITE(Nested_Struct);
27 :
28 : // Declares the method as a test to call
29 2 : CPPUNIT_TEST(testAssign1);
30 2 : CPPUNIT_TEST(testOldAssign);
31 2 : CPPUNIT_TEST(testUnfixedVarAssign);
32 2 : CPPUNIT_TEST(testFixedVarAssign);
33 :
34 : // End of test suite definition
35 4 : CPPUNIT_TEST_SUITE_END();
36 : };
37 :
38 : // tests the new behaviour, we should be able to
39 : // directly modify the value of the nested 'HorizontalLine' struct
40 4 : rtl::OUString sTestSource1(
41 : "Function simpleNestStructAccess() as Integer\n"
42 : "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
43 : "b0.HorizontalLine.OuterLineWidth = 9\n"
44 : "simpleNestStructAccess = b0.HorizontalLine.OuterLineWidth\n"
45 : "End Function\n"
46 2 : );
47 :
48 : // tests the old behaviour, we should still be able
49 : // to use the old workaround of
50 : // a) creating a new instance BorderLine,
51 : // b) cloning the new instance with the value of b0.HorizontalLine
52 : // c) modifying the new instance
53 : // d) setting b0.HorizontalLine with the value of the new instance
54 4 : rtl::OUString sTestSource2(
55 : "Function simpleRegressionTestOld()\n"
56 : "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
57 : "l = b0.HorizontalLine\n"
58 : "l.OuterLineWidth = 9\n"
59 : "b0.HorizontalLine = l\n"
60 : "simpleRegressionTestOld = b0.HorizontalLine.OuterLineWidth\n"
61 : "End Function\n"
62 2 : );
63 :
64 : // it should be legal to assign a variant to a struct ( and copy by val )
65 : // make sure we aren't copying by reference, we make sure that l is not
66 : // a reference copy of b0.HorizontalLine, each one should have an
67 : // OuterLineWidth of 4 & 9 respectively and we should be returning
68 : // 13 the sum of the two ( hopefully unique values if we haven't copied by reference )
69 4 : rtl::OUString sTestSource3(
70 : "Function testUnfixedVarAssign()\n"
71 : "Dim b0 as new \"com.sun.star.table.TableBorder\"\n"
72 : "l = b0.HorizontalLine\n"
73 : "l.OuterLineWidth = 9\n"
74 : "b0.HorizontalLine = l\n"
75 : "l.OuterLineWidth = 4\n"
76 : "testUnfixedVarAssign = b0.HorizontalLine.OuterLineWidth + l.OuterLineWidth\n"
77 : "End Function\n"
78 2 : );
79 :
80 : // nearly the same as above but this time for a fixed type
81 : // variable
82 4 : rtl::OUString sTestSource4(
83 : "Function testFixedVarAssign()\n"
84 : "Dim b0 as new \"com.sun.star.table.TableBorder\", l as new \"com.sun.star.table.BorderLine\"\n"
85 : "l = b0.HorizontalLine\n"
86 : "l.OuterLineWidth = 9\n"
87 : "b0.HorizontalLine = l\n"
88 : "l.OuterLineWidth = 4\n"
89 : "testFixedVarAssign = b0.HorizontalLine.OuterLineWidth + l.OuterLineWidth\n"
90 : "End Function\n"
91 2 : );
92 :
93 2 : void Nested_Struct::testAssign1()
94 : {
95 2 : CPPUNIT_ASSERT_MESSAGE( "No resource manager", basicDLL().GetBasResMgr() != NULL );
96 2 : StarBASICRef pBasic = new StarBASIC();
97 2 : ResetError();
98 2 : StarBASIC::SetGlobalErrorHdl( LINK( this, Nested_Struct, BasicErrorHdl ) );
99 :
100 2 : SbModule* pMod = pBasic->MakeModule( rtl::OUString( "TestModule" ), sTestSource1 );
101 2 : pMod->Compile();
102 2 : CPPUNIT_ASSERT_MESSAGE("testAssign1 fails with compile error",!HasError() );
103 2 : SbMethod* pMeth = static_cast<SbMethod*>(pMod->Find( rtl::OUString("simpleNestStructAccess"), SbxCLASS_METHOD ));
104 2 : CPPUNIT_ASSERT_MESSAGE("testAssign1 no method found", pMeth );
105 2 : SbxVariableRef refTemp = pMeth;
106 : // forces a broadcast
107 2 : SbxVariableRef pNew = new SbxMethod( *((SbxMethod*)pMeth));
108 2 : CPPUNIT_ASSERT(pNew->GetInteger() == 9 );
109 2 : }
110 :
111 2 : void Nested_Struct::testOldAssign()
112 : {
113 2 : CPPUNIT_ASSERT_MESSAGE( "No resource manager", basicDLL().GetBasResMgr() != NULL );
114 2 : StarBASICRef pBasic = new StarBASIC();
115 2 : ResetError();
116 2 : StarBASIC::SetGlobalErrorHdl( LINK( this, Nested_Struct, BasicErrorHdl ) );
117 :
118 2 : SbModule* pMod = pBasic->MakeModule( rtl::OUString( "TestModule" ), sTestSource2 );
119 2 : pMod->Compile();
120 2 : CPPUNIT_ASSERT_MESSAGE("testOldAssign fails with compile error",!HasError() );
121 2 : SbMethod* pMeth = static_cast<SbMethod*>(pMod->Find( rtl::OUString("simpleRegressionTestOld"), SbxCLASS_METHOD ));
122 2 : CPPUNIT_ASSERT_MESSAGE("testOldAssign no method found", pMeth );
123 2 : SbxVariableRef refTemp = pMeth;
124 : // forces a broadcast
125 2 : SbxVariableRef pNew = new SbxMethod( *((SbxMethod*)pMeth));
126 2 : CPPUNIT_ASSERT(pNew->GetInteger() == 9 );
127 2 : }
128 :
129 2 : void Nested_Struct::testUnfixedVarAssign()
130 : {
131 2 : CPPUNIT_ASSERT_MESSAGE( "No resource manager", basicDLL().GetBasResMgr() != NULL );
132 2 : StarBASICRef pBasic = new StarBASIC();
133 2 : ResetError();
134 2 : StarBASIC::SetGlobalErrorHdl( LINK( this, Nested_Struct, BasicErrorHdl ) );
135 :
136 2 : SbModule* pMod = pBasic->MakeModule( rtl::OUString( "TestModule" ), sTestSource3 );
137 2 : pMod->Compile();
138 2 : CPPUNIT_ASSERT_MESSAGE("testUnfixedVarAssign fails with compile error",!HasError() );
139 2 : SbMethod* pMeth = static_cast<SbMethod*>(pMod->Find( rtl::OUString("testUnfixedVarAssign"), SbxCLASS_METHOD ));
140 2 : CPPUNIT_ASSERT_MESSAGE("testUnfixedVarAssign no method found", pMeth );
141 2 : SbxVariableRef refTemp = pMeth;
142 : // forces a broadcast
143 2 : SbxVariableRef pNew = new SbxMethod( *((SbxMethod*)pMeth));
144 2 : CPPUNIT_ASSERT(pNew->GetInteger() == 13 );
145 2 : }
146 :
147 2 : void Nested_Struct::testFixedVarAssign()
148 : {
149 2 : CPPUNIT_ASSERT_MESSAGE( "No resource manager", basicDLL().GetBasResMgr() != NULL );
150 2 : StarBASICRef pBasic = new StarBASIC();
151 2 : ResetError();
152 2 : StarBASIC::SetGlobalErrorHdl( LINK( this, Nested_Struct, BasicErrorHdl ) );
153 :
154 2 : SbModule* pMod = pBasic->MakeModule( rtl::OUString( "TestModule" ), sTestSource4 );
155 2 : pMod->Compile();
156 2 : CPPUNIT_ASSERT_MESSAGE("testFixedVarAssign fails with compile error",!HasError() );
157 2 : SbMethod* pMeth = static_cast<SbMethod*>(pMod->Find( rtl::OUString("testFixedVarAssign"), SbxCLASS_METHOD ));
158 2 : CPPUNIT_ASSERT_MESSAGE("testFixedVarAssign no method found", pMeth );
159 2 : SbxVariableRef refTemp = pMeth;
160 : // forces a broadcast
161 2 : SbxVariableRef pNew = new SbxMethod( *((SbxMethod*)pMeth));
162 2 : CPPUNIT_ASSERT(pNew->GetInteger() == 13 );
163 2 : }
164 :
165 : // Put the test suite in the registry
166 2 : CPPUNIT_TEST_SUITE_REGISTRATION(Nested_Struct);
167 : } // namespace
168 8 : CPPUNIT_PLUGIN_IMPLEMENT();
169 :
170 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|