LCOV - code coverage report
Current view: top level - libreoffice/basic/qa/cppunit - test_nested_struct.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 74 74 100.0 %
Date: 2012-12-27 Functions: 13 14 92.9 %
Legend: Lines: hit not hit

          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           8 :     class Nested_Struct : public BasicTestBase
      18             :     {
      19             :         public:
      20           4 :         Nested_Struct() {};
      21             :         void testAssign1();
      22             :         void testOldAssign();
      23             :         void testUnfixedVarAssign();
      24             :         void testFixedVarAssign();
      25             :         // Adds code needed to register the test suite
      26           2 :         CPPUNIT_TEST_SUITE(Nested_Struct);
      27             : 
      28             :         // Declares the method as a test to call
      29           1 :         CPPUNIT_TEST(testAssign1);
      30           1 :         CPPUNIT_TEST(testOldAssign);
      31           1 :         CPPUNIT_TEST(testUnfixedVarAssign);
      32           1 :         CPPUNIT_TEST(testFixedVarAssign);
      33             : 
      34             :         // End of test suite definition
      35           2 :         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           2 : 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           1 : );
      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           2 : 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           1 : );
      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           2 : 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           1 : );
      79             : 
      80             : // nearly the same as above but this time for a fixed type
      81             : // variable
      82           2 : 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           1 : );
      92             : 
      93           1 : void Nested_Struct::testAssign1()
      94             : {
      95           1 :     CPPUNIT_ASSERT_MESSAGE( "No resource manager", basicDLL().GetBasResMgr() != NULL );
      96           1 :     StarBASICRef pBasic = new StarBASIC();
      97           1 :     ResetError();
      98           1 :     StarBASIC::SetGlobalErrorHdl( LINK( this, Nested_Struct, BasicErrorHdl ) );
      99             : 
     100           1 :     SbModule* pMod = pBasic->MakeModule( rtl::OUString( "TestModule" ), sTestSource1 );
     101           1 :     pMod->Compile();
     102           1 :     CPPUNIT_ASSERT_MESSAGE("testAssign1 fails with compile error",!HasError() );
     103           1 :     SbMethod* pMeth = static_cast<SbMethod*>(pMod->Find( rtl::OUString("simpleNestStructAccess"),  SbxCLASS_METHOD ));
     104           1 :     CPPUNIT_ASSERT_MESSAGE("testAssign1 no method found", pMeth );
     105           1 :     SbxVariableRef refTemp = pMeth;
     106             :     // forces a broadcast
     107           1 :     SbxVariableRef pNew = new  SbxMethod( *((SbxMethod*)pMeth));
     108           1 :     CPPUNIT_ASSERT(pNew->GetInteger() == 9 );
     109           1 : }
     110             : 
     111           1 : void Nested_Struct::testOldAssign()
     112             : {
     113           1 :     CPPUNIT_ASSERT_MESSAGE( "No resource manager", basicDLL().GetBasResMgr() != NULL );
     114           1 :     StarBASICRef pBasic = new StarBASIC();
     115           1 :     ResetError();
     116           1 :     StarBASIC::SetGlobalErrorHdl( LINK( this, Nested_Struct, BasicErrorHdl ) );
     117             : 
     118           1 :     SbModule* pMod = pBasic->MakeModule( rtl::OUString( "TestModule" ), sTestSource2 );
     119           1 :     pMod->Compile();
     120           1 :     CPPUNIT_ASSERT_MESSAGE("testOldAssign fails with compile error",!HasError() );
     121           1 :     SbMethod* pMeth = static_cast<SbMethod*>(pMod->Find( rtl::OUString("simpleRegressionTestOld"),  SbxCLASS_METHOD ));
     122           1 :     CPPUNIT_ASSERT_MESSAGE("testOldAssign no method found", pMeth );
     123           1 :     SbxVariableRef refTemp = pMeth;
     124             :     // forces a broadcast
     125           1 :     SbxVariableRef pNew = new  SbxMethod( *((SbxMethod*)pMeth));
     126           1 :     CPPUNIT_ASSERT(pNew->GetInteger() == 9 );
     127           1 : }
     128             : 
     129           1 : void Nested_Struct::testUnfixedVarAssign()
     130             : {
     131           1 :     CPPUNIT_ASSERT_MESSAGE( "No resource manager", basicDLL().GetBasResMgr() != NULL );
     132           1 :     StarBASICRef pBasic = new StarBASIC();
     133           1 :     ResetError();
     134           1 :     StarBASIC::SetGlobalErrorHdl( LINK( this, Nested_Struct, BasicErrorHdl ) );
     135             : 
     136           1 :     SbModule* pMod = pBasic->MakeModule( rtl::OUString( "TestModule" ), sTestSource3 );
     137           1 :     pMod->Compile();
     138           1 :     CPPUNIT_ASSERT_MESSAGE("testUnfixedVarAssign fails with compile error",!HasError() );
     139           1 :     SbMethod* pMeth = static_cast<SbMethod*>(pMod->Find( rtl::OUString("testUnfixedVarAssign"),  SbxCLASS_METHOD ));
     140           1 :     CPPUNIT_ASSERT_MESSAGE("testUnfixedVarAssign no method found", pMeth );
     141           1 :     SbxVariableRef refTemp = pMeth;
     142             :     // forces a broadcast
     143           1 :     SbxVariableRef pNew = new SbxMethod( *((SbxMethod*)pMeth));
     144           1 :     CPPUNIT_ASSERT(pNew->GetInteger() == 13 );
     145           1 : }
     146             : 
     147           1 : void Nested_Struct::testFixedVarAssign()
     148             : {
     149           1 :     CPPUNIT_ASSERT_MESSAGE( "No resource manager", basicDLL().GetBasResMgr() != NULL );
     150           1 :     StarBASICRef pBasic = new StarBASIC();
     151           1 :     ResetError();
     152           1 :     StarBASIC::SetGlobalErrorHdl( LINK( this, Nested_Struct, BasicErrorHdl ) );
     153             : 
     154           1 :     SbModule* pMod = pBasic->MakeModule( rtl::OUString( "TestModule" ), sTestSource4 );
     155           1 :     pMod->Compile();
     156           1 :     CPPUNIT_ASSERT_MESSAGE("testFixedVarAssign fails with compile error",!HasError() );
     157           1 :     SbMethod* pMeth = static_cast<SbMethod*>(pMod->Find( rtl::OUString("testFixedVarAssign"),  SbxCLASS_METHOD ));
     158           1 :     CPPUNIT_ASSERT_MESSAGE("testFixedVarAssign no method found", pMeth );
     159           1 :     SbxVariableRef refTemp = pMeth;
     160             :     // forces a broadcast
     161           1 :     SbxVariableRef pNew = new SbxMethod( *((SbxMethod*)pMeth));
     162           1 :     CPPUNIT_ASSERT(pNew->GetInteger() == 13 );
     163           1 : }
     164             : 
     165             :   // Put the test suite in the registry
     166           1 :   CPPUNIT_TEST_SUITE_REGISTRATION(Nested_Struct);
     167             : } // namespace
     168           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     169             : 
     170             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10