tdf#161412 - UI: fix warning in PDF password dialog didn't disappear
[LibreOffice.git] / basic / qa / cppunit / basictest.cxx
blobf93961deab8ab99c3172a4bf588fef3a219beb29
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/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 )
30 : mbError(false)
32 InitSnippet();
33 MakeModule(u"TestModule"_ustr, sSource);
36 MacroSnippet::MacroSnippet()
37 : mbError(false)
39 InitSnippet();
42 void MacroSnippet::LoadSourceFromFile(const OUString& sModuleName, const OUString& sMacroFileURL)
44 OUString sSource;
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)
50 sal_uInt64 size;
51 if(aFile.getSize(size) == osl::FileBase::E_None)
53 void* buffer = calloc(1, size+1);
54 CPPUNIT_ASSERT(buffer);
55 sal_uInt64 size_read;
56 if(aFile.read( buffer, size, size_read) == osl::FileBase::E_None)
58 if(size == size_read)
60 OUString sCode(static_cast<char*>(buffer), size, RTL_TEXTENCODING_UTF8);
61 sSource = sCode;
65 free(buffer);
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;
75 if ( !Compile() )
76 return pReturn;
77 SbMethod* pMeth = mpMod.is() ? static_cast<SbMethod*>(mpMod->Find( u"doUnitTest"_ustr, SbxClassType::Method )) : nullptr;
78 if ( pMeth )
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));
93 return pReturn;
96 SbxVariableRef MacroSnippet::Run()
98 css::uno::Sequence< css::uno::Any > aArgs;
99 return Run( aArgs );
102 bool MacroSnippet::Compile()
104 CPPUNIT_ASSERT_MESSAGE("module is NULL", mpMod );
105 mpMod->Compile();
106 return !mbError;
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() );
118 mbError = true;
119 maErrCode = StarBASIC::GetErrorCode();
120 return false;
123 CPPUNIT_PLUGIN_IMPLEMENT();
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */