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