2016-01-30 22:12:35 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "basictest.hxx"
|
|
|
|
#include <cppunit/plugin/TestPlugIn.h>
|
|
|
|
#include <basic/sbstar.hxx>
|
|
|
|
#include <basic/sbmod.hxx>
|
|
|
|
#include <basic/sbmeth.hxx>
|
|
|
|
#include <basic/sbuno.hxx>
|
|
|
|
#include <osl/file.hxx>
|
|
|
|
|
|
|
|
void MacroSnippet::InitSnippet()
|
|
|
|
{
|
|
|
|
mpBasic = new StarBASIC();
|
|
|
|
StarBASIC::SetGlobalErrorHdl( LINK( this, MacroSnippet, BasicErrorHdl ) );
|
|
|
|
}
|
|
|
|
|
2020-12-16 22:28:02 +00:00
|
|
|
void MacroSnippet::MakeModule(const OUString& sName, const OUString& sSource)
|
2016-01-30 22:12:35 +01:00
|
|
|
{
|
2020-12-16 22:28:02 +00:00
|
|
|
mpMod = mpBasic->MakeModule(sName, sSource);
|
2016-01-30 22:12:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MacroSnippet::MacroSnippet( const OUString& sSource )
|
|
|
|
: mbError(false)
|
|
|
|
{
|
|
|
|
InitSnippet();
|
2024-05-06 10:17:54 +02:00
|
|
|
MakeModule(u"TestModule"_ustr, sSource);
|
2016-01-30 22:12:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MacroSnippet::MacroSnippet()
|
|
|
|
: mbError(false)
|
|
|
|
{
|
|
|
|
InitSnippet();
|
|
|
|
}
|
|
|
|
|
2020-12-16 22:28:02 +00:00
|
|
|
void MacroSnippet::LoadSourceFromFile(const OUString& sModuleName, const OUString& sMacroFileURL)
|
2016-01-30 22:12:35 +01:00
|
|
|
{
|
|
|
|
OUString sSource;
|
2023-09-03 13:33:13 +03:00
|
|
|
fprintf(stderr,"loadSource opening macro file %s\n", OUStringToOString( sMacroFileURL, RTL_TEXTENCODING_UTF8 ).getStr() );
|
2016-01-30 22:12:35 +01:00
|
|
|
|
|
|
|
osl::File aFile(sMacroFileURL);
|
2017-05-19 11:25:32 +02:00
|
|
|
if(aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None)
|
2016-01-30 22:12:35 +01:00
|
|
|
{
|
|
|
|
sal_uInt64 size;
|
2017-05-19 11:25:32 +02:00
|
|
|
if(aFile.getSize(size) == osl::FileBase::E_None)
|
2016-01-30 22:12:35 +01:00
|
|
|
{
|
|
|
|
void* buffer = calloc(1, size+1);
|
|
|
|
CPPUNIT_ASSERT(buffer);
|
2020-10-01 14:01:16 +02:00
|
|
|
sal_uInt64 size_read;
|
2017-05-19 11:25:32 +02:00
|
|
|
if(aFile.read( buffer, size, size_read) == osl::FileBase::E_None)
|
2016-01-30 22:12:35 +01:00
|
|
|
{
|
|
|
|
if(size == size_read)
|
|
|
|
{
|
2019-12-18 11:52:01 +02:00
|
|
|
OUString sCode(static_cast<char*>(buffer), size, RTL_TEXTENCODING_UTF8);
|
2016-01-30 22:12:35 +01:00
|
|
|
sSource = sCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CPPUNIT_ASSERT_MESSAGE( "Source is empty", ( sSource.getLength() > 0 ) );
|
2020-12-16 22:28:02 +00:00
|
|
|
MakeModule(sModuleName, sSource);
|
2016-01-30 22:12:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SbxVariableRef MacroSnippet::Run( const css::uno::Sequence< css::uno::Any >& rArgs )
|
|
|
|
{
|
2018-09-04 13:44:45 +02:00
|
|
|
SbxVariableRef pReturn;
|
2016-01-30 22:12:35 +01:00
|
|
|
if ( !Compile() )
|
|
|
|
return pReturn;
|
2024-05-06 10:17:54 +02:00
|
|
|
SbMethod* pMeth = mpMod.is() ? static_cast<SbMethod*>(mpMod->Find( u"doUnitTest"_ustr, SbxClassType::Method )) : nullptr;
|
2016-01-30 22:12:35 +01:00
|
|
|
if ( pMeth )
|
|
|
|
{
|
2019-05-04 23:42:17 +03:00
|
|
|
if ( rArgs.hasElements() )
|
2016-01-30 22:12:35 +01:00
|
|
|
{
|
|
|
|
SbxArrayRef aArgs = new SbxArray;
|
|
|
|
for ( int i=0; i < rArgs.getLength(); ++i )
|
|
|
|
{
|
|
|
|
SbxVariable* pVar = new SbxVariable();
|
|
|
|
unoToSbxValue( pVar, rArgs[ i ] );
|
2021-03-08 12:29:39 +03:00
|
|
|
aArgs->Put(pVar, i + 1);
|
2016-01-30 22:12:35 +01:00
|
|
|
}
|
2016-10-05 22:00:51 +02:00
|
|
|
pMeth->SetParameters( aArgs.get() );
|
2016-01-30 22:12:35 +01:00
|
|
|
}
|
|
|
|
pReturn = new SbxMethod( *static_cast<SbxMethod*>(pMeth));
|
|
|
|
}
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
SbxVariableRef MacroSnippet::Run()
|
|
|
|
{
|
|
|
|
css::uno::Sequence< css::uno::Any > aArgs;
|
|
|
|
return Run( aArgs );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MacroSnippet::Compile()
|
|
|
|
{
|
2020-06-02 10:40:26 +02:00
|
|
|
CPPUNIT_ASSERT_MESSAGE("module is NULL", mpMod );
|
2016-01-30 22:12:35 +01:00
|
|
|
mpMod->Compile();
|
|
|
|
return !mbError;
|
|
|
|
}
|
|
|
|
|
2019-09-03 11:47:17 +02:00
|
|
|
bool MacroSnippet::HasError() const { return mbError; }
|
2016-01-30 22:12:35 +01:00
|
|
|
|
2023-09-30 20:47:46 +02:00
|
|
|
const ErrCodeMsg& MacroSnippet::getError() const { return maErrCode; }
|
2019-12-07 15:27:08 +01:00
|
|
|
|
2016-10-05 07:56:12 +02:00
|
|
|
IMPL_LINK( MacroSnippet, BasicErrorHdl, StarBASIC *, /*pBasic*/, bool)
|
2016-01-30 22:12:35 +01:00
|
|
|
{
|
2023-09-03 13:33:13 +03:00
|
|
|
fprintf(stderr,"(%d:%d)\n",
|
|
|
|
StarBASIC::GetLine(), StarBASIC::GetCol1());
|
|
|
|
fprintf(stderr,"Basic error: %s\n", OUStringToOString( StarBASIC::GetErrorText(), RTL_TEXTENCODING_UTF8 ).getStr() );
|
2016-01-30 22:12:35 +01:00
|
|
|
mbError = true;
|
2019-12-07 15:27:08 +01:00
|
|
|
maErrCode = StarBASIC::GetErrorCode();
|
2016-01-30 22:12:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPPUNIT_PLUGIN_IMPLEMENT();
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|