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

Generated by: LCOV version 1.11