1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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>
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
);
48 ErrorDetail() : nLine(0), nCol(0) {}
51 MacroSnippet( const OUString
& sSource
) : mbError(false)
54 MakeModule( sSource
);
56 MacroSnippet() : mbError(false)
60 void LoadSourceFromFile( const OUString
& sMacroFileURL
)
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
))
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
))
78 OUString
sCode((sal_Char
*)buffer
, size
, RTL_TEXTENCODING_UTF8
);
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
;
93 SbMethod
* pMeth
= mpMod
? static_cast<SbMethod
*>(mpMod
->Find( OUString("doUnitTest"), SbxCLASS_METHOD
)) : NULL
;
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
));
114 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> aArgs
;
120 CPPUNIT_ASSERT_MESSAGE("module is NULL", mpMod
!= NULL
);
125 DECL_LINK( BasicErrorHdl
, StarBASIC
* );
127 ErrorDetail
GetError()
130 aErr
.sErrorText
= StarBASIC::GetErrorText();
131 aErr
.nLine
= StarBASIC::GetLine();
132 aErr
.nCol
= StarBASIC::GetCol1();
136 bool HasError() { return mbError
; }
140 StarBASIC::SetGlobalErrorHdl( Link() );
146 static BasicDLL maDll
; // we need a dll instance for resouce manager etc.
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() );
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */