build fix
[LibreOffice.git] / basic / qa / cppunit / basictest.cxx
blob2c4e86f0ea17179876b3988d176a12cc67a4f97a
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 */
10 #include "basictest.hxx"
11 #include <cppunit/TestFixture.h>
12 #include <cppunit/extensions/HelperMacros.h>
13 #include <cppunit/plugin/TestPlugIn.h>
14 #include <basic/sbstar.hxx>
15 #include <basic/basrdll.hxx>
16 #include <basic/sbmod.hxx>
17 #include <basic/sbmeth.hxx>
18 #include <basic/sbuno.hxx>
19 #include <osl/file.hxx>
21 void MacroSnippet::InitSnippet()
23 CPPUNIT_ASSERT_MESSAGE( "No resource manager", maDll.GetBasResMgr() != nullptr );
24 mpBasic = new StarBASIC();
25 StarBASIC::SetGlobalErrorHdl( LINK( this, MacroSnippet, BasicErrorHdl ) );
28 void MacroSnippet::MakeModule( const OUString& sSource )
30 mpMod = mpBasic->MakeModule( "TestModule", sSource );
33 MacroSnippet::MacroSnippet( const OUString& sSource )
34 : mbError(false)
36 InitSnippet();
37 MakeModule( sSource );
40 MacroSnippet::MacroSnippet()
41 : mbError(false)
43 InitSnippet();
46 void MacroSnippet::LoadSourceFromFile( const OUString& sMacroFileURL )
48 OUString sSource;
49 fprintf(stderr,"loadSource opening macro file %s\n", OUStringToOString( sMacroFileURL, RTL_TEXTENCODING_UTF8 ).getStr() );
51 osl::File aFile(sMacroFileURL);
52 if(osl::FileBase::E_None == aFile.open(osl_File_OpenFlag_Read))
54 sal_uInt64 size;
55 sal_uInt64 size_read;
56 if(osl::FileBase::E_None == aFile.getSize(size))
58 void* buffer = calloc(1, size+1);
59 CPPUNIT_ASSERT(buffer);
60 if(osl::FileBase::E_None == aFile.read( buffer, size, size_read))
62 if(size == size_read)
64 OUString sCode(static_cast<sal_Char*>(buffer), size, RTL_TEXTENCODING_UTF8);
65 sSource = sCode;
69 free(buffer);
72 CPPUNIT_ASSERT_MESSAGE( "Source is empty", ( sSource.getLength() > 0 ) );
73 MakeModule( sSource );
76 SbxVariableRef MacroSnippet::Run( const css::uno::Sequence< css::uno::Any >& rArgs )
78 SbxVariableRef pReturn = nullptr;
79 if ( !Compile() )
80 return pReturn;
81 SbMethod* pMeth = mpMod.Is() ? static_cast<SbMethod*>(mpMod->Find( "doUnitTest", SbxClassType::Method )) : nullptr;
82 if ( pMeth )
84 if ( rArgs.getLength() )
86 SbxArrayRef aArgs = new SbxArray;
87 for ( int i=0; i < rArgs.getLength(); ++i )
89 SbxVariable* pVar = new SbxVariable();
90 unoToSbxValue( pVar, rArgs[ i ] );
91 aArgs->Put( pVar, i + 1 );
93 pMeth->SetParameters( aArgs.get() );
95 pReturn = new SbxMethod( *static_cast<SbxMethod*>(pMeth));
97 return pReturn;
100 SbxVariableRef MacroSnippet::Run()
102 css::uno::Sequence< css::uno::Any > aArgs;
103 return Run( aArgs );
106 bool MacroSnippet::Compile()
108 CPPUNIT_ASSERT_MESSAGE("module is NULL", mpMod.get() != nullptr );
109 mpMod->Compile();
110 return !mbError;
113 bool MacroSnippet::HasError() { return mbError; }
115 IMPL_LINK( MacroSnippet, BasicErrorHdl, StarBASIC *, /*pBasic*/, bool)
117 fprintf(stderr,"(%d:%d)\n",
118 StarBASIC::GetLine(), StarBASIC::GetCol1());
119 fprintf(stderr,"Basic error: %s\n", OUStringToOString( StarBASIC::GetErrorText(), RTL_TEXTENCODING_UTF8 ).getStr() );
120 mbError = true;
121 return false;
124 CPPUNIT_PLUGIN_IMPLEMENT();
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */