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 #include "basictest.hxx"
11 #include <cppunit/plugin/TestPlugIn.h>
12 #include <basic/sbstar.hxx>
13 #include <basic/sbmod.hxx>
14 #include <basic/sbmeth.hxx>
15 #include <basic/sbuno.hxx>
16 #include <osl/file.hxx>
18 void MacroSnippet::InitSnippet()
20 mpBasic
= new StarBASIC();
21 StarBASIC::SetGlobalErrorHdl( LINK( this, MacroSnippet
, BasicErrorHdl
) );
24 void MacroSnippet::MakeModule(const OUString
& sName
, const OUString
& sSource
)
26 mpMod
= mpBasic
->MakeModule(sName
, sSource
);
29 MacroSnippet::MacroSnippet( const OUString
& sSource
)
33 MakeModule(u
"TestModule"_ustr
, sSource
);
36 MacroSnippet::MacroSnippet()
42 void MacroSnippet::LoadSourceFromFile(const OUString
& sModuleName
, const OUString
& sMacroFileURL
)
45 fprintf(stderr
,"loadSource opening macro file %s\n", OUStringToOString( sMacroFileURL
, RTL_TEXTENCODING_UTF8
).getStr() );
47 osl::File
aFile(sMacroFileURL
);
48 if(aFile
.open(osl_File_OpenFlag_Read
) == osl::FileBase::E_None
)
51 if(aFile
.getSize(size
) == osl::FileBase::E_None
)
53 void* buffer
= calloc(1, size
+1);
54 CPPUNIT_ASSERT(buffer
);
56 if(aFile
.read( buffer
, size
, size_read
) == osl::FileBase::E_None
)
60 OUString
sCode(static_cast<char*>(buffer
), size
, RTL_TEXTENCODING_UTF8
);
68 CPPUNIT_ASSERT_MESSAGE( "Source is empty", ( sSource
.getLength() > 0 ) );
69 MakeModule(sModuleName
, sSource
);
72 SbxVariableRef
MacroSnippet::Run( const css::uno::Sequence
< css::uno::Any
>& rArgs
)
74 SbxVariableRef pReturn
;
77 SbMethod
* pMeth
= mpMod
.is() ? static_cast<SbMethod
*>(mpMod
->Find( u
"doUnitTest"_ustr
, SbxClassType::Method
)) : nullptr;
80 if ( rArgs
.hasElements() )
82 SbxArrayRef aArgs
= new SbxArray
;
83 for ( int i
=0; i
< rArgs
.getLength(); ++i
)
85 SbxVariable
* pVar
= new SbxVariable();
86 unoToSbxValue( pVar
, rArgs
[ i
] );
87 aArgs
->Put(pVar
, i
+ 1);
89 pMeth
->SetParameters( aArgs
.get() );
91 pReturn
= new SbxMethod( *static_cast<SbxMethod
*>(pMeth
));
96 SbxVariableRef
MacroSnippet::Run()
98 css::uno::Sequence
< css::uno::Any
> aArgs
;
102 bool MacroSnippet::Compile()
104 CPPUNIT_ASSERT_MESSAGE("module is NULL", mpMod
);
109 bool MacroSnippet::HasError() const { return mbError
; }
111 const ErrCodeMsg
& MacroSnippet::getError() const { return maErrCode
; }
113 IMPL_LINK( MacroSnippet
, BasicErrorHdl
, StarBASIC
*, /*pBasic*/, bool)
115 fprintf(stderr
,"(%d:%d)\n",
116 StarBASIC::GetLine(), StarBASIC::GetCol1());
117 fprintf(stderr
,"Basic error: %s\n", OUStringToOString( StarBASIC::GetErrorText(), RTL_TEXTENCODING_UTF8
).getStr() );
119 maErrCode
= StarBASIC::GetErrorCode();
123 CPPUNIT_PLUGIN_IMPLEMENT();
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */