bump product version to 4.1.6.2
[LibreOffice.git] / basic / qa / cppunit / basictest.hxx
blob5dbd69143083e14b9d3c0bf123a7cbecfba928ee
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 #ifndef _BASICTEST_HXX
10 #define _BASICTEST_HXX
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/basrdll.hxx"
22 #include "basic/sbuno.hxx"
23 #include <osl/file.hxx>
25 class MacroSnippet
27 private:
28 bool mbError;
29 SbModuleRef mpMod;
30 StarBASICRef mpBasic;
32 void InitSnippet()
34 CPPUNIT_ASSERT_MESSAGE( "No resource manager", basicDLL().GetBasResMgr() != NULL );
35 mpBasic = new StarBASIC();
36 StarBASIC::SetGlobalErrorHdl( LINK( this, MacroSnippet, BasicErrorHdl ) );
38 void MakeModule( const OUString& sSource )
40 mpMod = mpBasic->MakeModule( OUString( "TestModule" ), sSource );
42 public:
43 struct ErrorDetail
45 OUString sErrorText;
46 int nLine;
47 int nCol;
48 ErrorDetail() : nLine(0), nCol(0) {}
51 MacroSnippet( const OUString& sSource ) : mbError(false)
53 InitSnippet();
54 MakeModule( sSource );
56 MacroSnippet() : mbError(false)
58 InitSnippet();
60 void LoadSourceFromFile( const OUString& sMacroFileURL )
62 OUString sSource;
63 fprintf(stderr,"loadSource opening macro file %s\n", OUStringToOString( sMacroFileURL, RTL_TEXTENCODING_UTF8 ).getStr() );
65 osl::File aFile(sMacroFileURL);
66 if(osl::FileBase::E_None == aFile.open(osl_File_OpenFlag_Read))
68 sal_uInt64 size;
69 sal_uInt64 size_read;
70 if(osl::FileBase::E_None == aFile.getSize(size))
72 void* buffer = calloc(1, size+1);
73 CPPUNIT_ASSERT(buffer);
74 if(osl::FileBase::E_None == aFile.read( buffer, size, size_read))
76 if(size == size_read)
78 OUString sCode((sal_Char*)buffer, size, RTL_TEXTENCODING_UTF8);
79 sSource = sCode;
84 CPPUNIT_ASSERT_MESSAGE( "Source is empty", ( sSource.getLength() > 0 ) );
85 MakeModule( sSource );
88 SbxVariableRef Run( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& rArgs )
90 SbxVariableRef pReturn = NULL;
91 if ( !Compile() )
92 return pReturn;
93 SbMethod* pMeth = mpMod ? static_cast<SbMethod*>(mpMod->Find( OUString("doUnitTest"), SbxCLASS_METHOD )) : NULL;
94 if ( pMeth )
96 if ( rArgs.getLength() )
98 SbxArrayRef aArgs = new SbxArray;
99 for ( int i=0; i < rArgs.getLength(); ++i )
101 SbxVariable* pVar = new SbxVariable();
102 unoToSbxValue( pVar, rArgs[ i ] );
103 aArgs->Put( pVar, i + 1 );
105 pMeth->SetParameters( aArgs );
107 pReturn = new SbxMethod( *((SbxMethod*)pMeth));
109 return pReturn;
112 SbxVariableRef Run()
114 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > aArgs;
115 return Run( aArgs );
118 bool Compile()
120 CPPUNIT_ASSERT_MESSAGE("module is NULL", mpMod != NULL );
121 mpMod->Compile();
122 return !mbError;
125 DECL_LINK( BasicErrorHdl, StarBASIC * );
127 ErrorDetail GetError()
129 ErrorDetail aErr;
130 aErr.sErrorText = StarBASIC::GetErrorText();
131 aErr.nLine = StarBASIC::GetLine();
132 aErr.nCol = StarBASIC::GetCol1();
133 return aErr;
136 bool HasError() { return mbError; }
138 void ResetError()
140 StarBASIC::SetGlobalErrorHdl( Link() );
141 mbError = false;
144 BasicDLL& basicDLL()
146 static BasicDLL maDll; // we need a dll instance for resouce manager etc.
147 return maDll;
151 IMPL_LINK( MacroSnippet, BasicErrorHdl, StarBASIC *, /*pBasic*/)
153 fprintf(stderr,"(%d:%d)\n",
154 StarBASIC::GetLine(), StarBASIC::GetCol1());
155 fprintf(stderr,"Basic error: %s\n", OUStringToOString( StarBASIC::GetErrorText(), RTL_TEXTENCODING_UTF8 ).getStr() );
156 mbError = true;
157 return 0;
159 #endif
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */