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