build fix
[LibreOffice.git] / basic / qa / cppunit / test_vba.cxx
blob4028ec461a40b94b7b02427416adcafa6017f61e
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 */
9 #include "basictest.hxx"
10 #include <comphelper/processfactory.hxx>
11 #include <unotools/syslocaleoptions.hxx>
13 #ifdef WIN32
14 #include <string.h>
16 #include <windows.h>
17 #include <odbcinst.h>
18 #endif
20 using namespace ::com::sun::star;
22 namespace
24 class VBATest : public test::BootstrapFixture
26 public:
27 VBATest() : BootstrapFixture(true, false) {}
28 void testMiscVBAFunctions();
29 void testMiscOLEStuff();
30 // Adds code needed to register the test suite
31 CPPUNIT_TEST_SUITE(VBATest);
33 // Declares the method as a test to call
34 CPPUNIT_TEST(testMiscVBAFunctions);
35 CPPUNIT_TEST(testMiscOLEStuff);
37 // End of test suite definition
38 CPPUNIT_TEST_SUITE_END();
42 void VBATest::testMiscVBAFunctions()
44 const char* macroSource[] = {
45 "bytearraystring.vb",
46 "cdec.vb",
47 // datevalue test seems to depend on both locale and language
48 // settings, should try and rewrite the test to deal with that
49 // for some reason tinderboxes don't seem to complain leaving enabled
50 // for the moment
51 "datevalue.vb",
52 "partition.vb",
53 "strconv.vb",
54 "dateserial.vb",
55 "format.vb",
56 "replace.vb",
57 "stringplusdouble.vb",
58 #ifndef WIN32 // missing 64bit Currency marshalling.
59 "win32compat.vb", // windows compatibility hooks.
60 #endif
61 "win32compatb.vb" // same methods, different signatures.
63 OUString sMacroPathURL = m_directories.getURLFromSrc("/basic/qa/vba_tests/");
64 // Some test data expects the uk locale
65 LanguageTag aLocale(LANGUAGE_ENGLISH_UK);
66 SvtSysLocaleOptions aLocalOptions;
67 aLocalOptions.SetLocaleConfigString( aLocale.getBcp47() );
69 for ( sal_uInt32 i=0; i<SAL_N_ELEMENTS( macroSource ); ++i )
71 OUString sMacroURL = sMacroPathURL
72 + OUString::createFromAscii( macroSource[ i ] );
74 MacroSnippet myMacro;
75 myMacro.LoadSourceFromFile( sMacroURL );
76 SbxVariableRef pReturn = myMacro.Run();
77 if ( pReturn.Is() )
79 fprintf(stderr, "macro result for %s\n", macroSource[ i ] );
80 fprintf(stderr, "macro returned:\n%s\n", OUStringToOString( pReturn->GetOUString(), RTL_TEXTENCODING_UTF8 ).getStr() );
82 CPPUNIT_ASSERT_MESSAGE("No return variable huh?", pReturn.get() != nullptr );
83 CPPUNIT_ASSERT_EQUAL_MESSAGE("Result not as expected", OUString("OK"), pReturn->GetOUString() );
87 void VBATest::testMiscOLEStuff()
89 // Not much point even trying to run except on Windows. Does not work
90 // on 64-bit Windows with Excel installed. (Without Excel doesn't
91 // really do anything anyway, see "so skip test" below.)
92 #if defined(_WIN32) && !defined(_WIN64)
93 // test if we have the necessary runtime environment
94 // to run the OLE tests.
95 uno::Reference< lang::XMultiServiceFactory > xOLEFactory;
96 uno::Reference< uno::XComponentContext > xContext(
97 comphelper::getProcessComponentContext() );
98 if( xContext.is() )
100 uno::Reference<lang::XMultiComponentFactory> xSMgr = xContext->getServiceManager();
101 xOLEFactory.set( xSMgr->createInstanceWithContext( "com.sun.star.bridge.OleObjectFactory", xContext ),
102 uno::UNO_QUERY );
104 bool bOk = false;
105 if( xOLEFactory.is() )
107 uno::Reference< uno::XInterface > xADODB = xOLEFactory->createInstance( "ADODB.Connection" );
108 bOk = xADODB.is();
110 if ( !bOk )
111 return; // can't do anything, skip test
113 sal_Unicode sBuf[1024*4];
114 SQLGetInstalledDriversW( sBuf, sizeof( sBuf ), nullptr );
116 const sal_Unicode *pODBCDriverName = sBuf;
117 bool bFound = false;
118 for (; wcslen( pODBCDriverName ) != 0; pODBCDriverName += wcslen( pODBCDriverName ) + 1 ) {
119 if ( wcsstr( pODBCDriverName, L"Microsoft Excel Driver" ) != nullptr ) {
120 bFound = true;
121 break;
124 if ( !bFound )
125 return; // can't find ODBC driver needed test, so skip test
127 const char* macroSource[] = {
128 "ole_ObjAssignNoDflt.vb",
129 "ole_ObjAssignToNothing.vb",
130 "ole_dfltObjDflMethod.vb",
133 OUString sMacroPathURL = m_directories.getURLFromSrc("/basic/qa/vba_tests/");
135 uno::Sequence< uno::Any > aArgs(1);
136 // path to test document
137 OUString sPath = m_directories.getPathFromSrc("/basic/qa/vba_tests/data/")
138 + "ADODBdata.xls";
139 sPath = sPath.replaceAll( "/", "\\" );
141 aArgs[ 0 ] = uno::makeAny( sPath );
143 for ( sal_uInt32 i=0; i<SAL_N_ELEMENTS( macroSource ); ++i )
145 OUString sMacroURL = sMacroPathURL
146 + OUString::createFromAscii( macroSource[ i ] );
147 MacroSnippet myMacro;
148 myMacro.LoadSourceFromFile( sMacroURL );
149 SbxVariableRef pReturn = myMacro.Run( aArgs );
150 if ( pReturn.Is() )
152 fprintf(stderr, "macro result for %s\n", macroSource[ i ] );
153 fprintf(stderr, "macro returned:\n%s\n", OUStringToOString( pReturn->GetOUString(), RTL_TEXTENCODING_UTF8 ).getStr() );
155 CPPUNIT_ASSERT_MESSAGE("No return variable huh?", pReturn.get() != nullptr );
156 CPPUNIT_ASSERT_MESSAGE("Result not as expected", pReturn->GetOUString() == "OK" );
158 #else
159 // Avoid "this method is empty and should be removed" warning
160 (void) 42;
161 #endif
164 // Put the test suite in the registry
165 CPPUNIT_TEST_SUITE_REGISTRATION(VBATest);
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */