LCOV - code coverage report
Current view: top level - basic/qa/cppunit - basictest.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 48 60 80.0 %
Date: 2014-11-03 Functions: 10 12 83.3 %
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             :  * This file is part of the LibreOffice project.
       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             : #ifndef INCLUDED_BASIC_QA_CPPUNIT_BASICTEST_HXX
      10             : #define INCLUDED_BASIC_QA_CPPUNIT_BASICTEST_HXX
      11             : 
      12             : #include <sal/types.h>
      13             : #include <cppunit/TestFixture.h>
      14             : #include <cppunit/extensions/HelperMacros.h>
      15             : #include <cppunit/plugin/TestPlugIn.h>
      16             : #include <test/bootstrapfixture.hxx>
      17             : #include <basic/sbstar.hxx>
      18             : #include <basic/basrdll.hxx>
      19             : #include <basic/sbmod.hxx>
      20             : #include <basic/sbmeth.hxx>
      21             : #include <basic/sbuno.hxx>
      22             : #include <osl/file.hxx>
      23             : 
      24          70 : class MacroSnippet
      25             : {
      26             :     private:
      27             :     bool mbError;
      28             :     BasicDLL maDll; // we need a dll instance for resouce manager etc.
      29             :     SbModuleRef mpMod;
      30             :     StarBASICRef mpBasic;
      31             : 
      32          70 :     void InitSnippet()
      33             :     {
      34          70 :         CPPUNIT_ASSERT_MESSAGE( "No resource manager", maDll.GetBasResMgr() != NULL );
      35          70 :         mpBasic = new StarBASIC();
      36          70 :         StarBASIC::SetGlobalErrorHdl( LINK( this, MacroSnippet, BasicErrorHdl ) );
      37          70 :     }
      38          70 :     void MakeModule( const OUString& sSource )
      39             :     {
      40          70 :         mpMod = mpBasic->MakeModule( OUString( "TestModule" ), sSource );
      41          70 :     }
      42             :     public:
      43             :     struct ErrorDetail
      44             :     {
      45             :         OUString sErrorText;
      46             :         int nLine;
      47             :         int nCol;
      48             :         ErrorDetail() : nLine(0), nCol(0) {}
      49             :     };
      50             : 
      51          22 :     MacroSnippet( const OUString& sSource ) : mbError(false)
      52             :     {
      53          22 :         InitSnippet();
      54          22 :         MakeModule( sSource );
      55          22 :     };
      56          48 :     MacroSnippet() : mbError(false)
      57             :     {
      58          48 :         InitSnippet();
      59          48 :     };
      60          48 :     void LoadSourceFromFile( const OUString& sMacroFileURL )
      61             :     {
      62          48 :         OUString sSource;
      63          48 :         fprintf(stderr,"loadSource opening macro file %s\n", OUStringToOString( sMacroFileURL, RTL_TEXTENCODING_UTF8 ).getStr() );
      64             : 
      65          96 :         osl::File aFile(sMacroFileURL);
      66          48 :         if(osl::FileBase::E_None == aFile.open(osl_File_OpenFlag_Read))
      67             :         {
      68             :             sal_uInt64 size;
      69             :             sal_uInt64 size_read;
      70          48 :             if(osl::FileBase::E_None == aFile.getSize(size))
      71             :             {
      72          48 :                 void* buffer = calloc(1, size+1);
      73          48 :                 CPPUNIT_ASSERT(buffer);
      74          48 :                 if(osl::FileBase::E_None == aFile.read( buffer, size, size_read))
      75             :                 {
      76          48 :                     if(size == size_read)
      77             :                     {
      78          48 :                         OUString sCode((sal_Char*)buffer, size, RTL_TEXTENCODING_UTF8);
      79          48 :                         sSource = sCode;
      80             :                     }
      81             :                 }
      82             : 
      83          48 :                 free(buffer);
      84             :             }
      85             :         }
      86          48 :         CPPUNIT_ASSERT_MESSAGE( "Source is empty", ( sSource.getLength() > 0 ) );
      87          96 :         MakeModule( sSource );
      88          48 :     }
      89             : 
      90          68 :     SbxVariableRef Run( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& rArgs )
      91             :     {
      92          68 :         SbxVariableRef pReturn = NULL;
      93          68 :         if ( !Compile() )
      94           0 :             return pReturn;
      95          68 :         SbMethod* pMeth = mpMod ? static_cast<SbMethod*>(mpMod->Find( OUString("doUnitTest"),  SbxCLASS_METHOD )) : NULL;
      96          68 :         if ( pMeth )
      97             :         {
      98          68 :             if ( rArgs.getLength() )
      99             :             {
     100           0 :                 SbxArrayRef aArgs = new SbxArray;
     101           0 :                 for ( int i=0; i < rArgs.getLength(); ++i )
     102             :                 {
     103           0 :                     SbxVariable* pVar = new SbxVariable();
     104           0 :                     unoToSbxValue( pVar, rArgs[ i ] );
     105           0 :                     aArgs->Put(  pVar, i + 1 );
     106             :                 }
     107           0 :                 pMeth->SetParameters( aArgs );
     108             :             }
     109          68 :             pReturn = new SbxMethod( *((SbxMethod*)pMeth));
     110             :         }
     111          68 :         return pReturn;
     112             :     }
     113             : 
     114          68 :     SbxVariableRef Run()
     115             :     {
     116          68 :         ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > aArgs;
     117          68 :         return Run( aArgs );
     118             :     }
     119             : 
     120         116 :     bool Compile()
     121             :     {
     122         116 :         CPPUNIT_ASSERT_MESSAGE("module is NULL", mpMod != NULL );
     123         116 :         mpMod->Compile();
     124         116 :         return !mbError;
     125             :     }
     126             : 
     127             :     DECL_LINK( BasicErrorHdl, StarBASIC * );
     128             : 
     129             :     ErrorDetail GetError()
     130             :     {
     131             :         ErrorDetail aErr;
     132             :         aErr.sErrorText = StarBASIC::GetErrorText();
     133             :         aErr.nLine = StarBASIC::GetLine();
     134             :         aErr.nCol = StarBASIC::GetCol1();
     135             :         return aErr;
     136             :     }
     137             : 
     138          48 :     bool HasError() { return mbError; }
     139             : 
     140             :     void ResetError()
     141             :     {
     142             :         StarBASIC::SetGlobalErrorHdl( Link() );
     143             :         mbError = false;
     144             :     }
     145             : };
     146             : 
     147           0 : IMPL_LINK( MacroSnippet, BasicErrorHdl, StarBASIC *, /*pBasic*/)
     148             : {
     149             :     fprintf(stderr,"(%d:%d)\n",
     150           0 :             StarBASIC::GetLine(), StarBASIC::GetCol1());
     151           0 :     fprintf(stderr,"Basic error: %s\n", OUStringToOString( StarBASIC::GetErrorText(), RTL_TEXTENCODING_UTF8 ).getStr() );
     152           0 :     mbError = true;
     153           0 :     return 0;
     154             : }
     155             : #endif
     156             : 
     157             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10