Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / basic / qa / cppunit / test_vba.cxx
blob8b46d739f81da2fe3f215384c4674ec996f72b55
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 #ifdef _WIN32
47 "cdec.vb", // currently CDec is implemented only on Windows
48 #endif
49 "constants.vb",
50 // datevalue test seems to depend on both locale and language
51 // settings, should try and rewrite the test to deal with that
52 // for some reason tinderboxes don't seem to complain leaving enabled
53 // for the moment
54 "datevalue.vb",
55 "partition.vb",
56 "strconv.vb",
57 "dateserial.vb",
58 "format.vb",
59 "replace.vb",
60 "stringplusdouble.vb",
61 "chr.vb",
62 "abs.vb",
63 "array.vb",
64 "asc.vb",
65 "atn.vb",
66 "cbool.vb",
67 "cdate.vb",
68 "cdbl.vb",
69 "choose.vb",
70 "cos.vb",
71 "cint.vb",
72 "clng.vb",
73 "csng.vb",
74 "cstr.vb",
75 "cvdate.vb",
76 "cverr.vb",
77 "dateadd.vb",
78 "datediff.vb",
79 "datepart.vb",
80 "day.vb",
81 "error.vb",
82 "exp.vb",
83 "fix.vb",
84 "hex.vb",
85 "hour.vb",
86 "formatnumber.vb",
87 "iif.vb",
88 "instr.vb",
89 "instrrev.vb",
90 "int.vb",
91 "iserror.vb",
92 "ismissing.vb",
93 "isnull.vb",
94 "isobject.vb",
95 "join.vb",
96 "lbound.vb",
97 "isarray.vb",
98 "isdate.vb",
99 "isempty.vb",
100 "isnumeric.vb",
101 "lcase.vb",
102 "left.vb",
103 "len.vb",
104 "log.vb",
105 "ltrim.vb",
106 "mid.vb",
107 "minute.vb",
108 "mirr.vb",
109 "month.vb",
110 "monthname.vb",
111 "oct.vb",
112 "nper.vb",
113 "npv.vb",
114 #ifndef WIN32 // missing 64bit Currency marshalling.
115 "win32compat.vb", // windows compatibility hooks.
116 #endif
117 "win32compatb.vb" // same methods, different signatures.
119 OUString sMacroPathURL = m_directories.getURLFromSrc("/basic/qa/vba_tests/");
120 // Some test data expects the uk locale
121 LanguageTag aLocale(LANGUAGE_ENGLISH_UK);
122 SvtSysLocaleOptions aLocalOptions;
123 aLocalOptions.SetLocaleConfigString( aLocale.getBcp47() );
125 for ( sal_uInt32 i=0; i<SAL_N_ELEMENTS( macroSource ); ++i )
127 OUString sMacroURL = sMacroPathURL
128 + OUString::createFromAscii( macroSource[ i ] );
130 MacroSnippet myMacro;
131 myMacro.LoadSourceFromFile( sMacroURL );
132 SbxVariableRef pReturn = myMacro.Run();
133 if ( pReturn.is() )
135 fprintf(stderr, "macro result for %s\n", macroSource[ i ] );
136 fprintf(stderr, "macro returned:\n%s\n", OUStringToOString( pReturn->GetOUString(), RTL_TEXTENCODING_UTF8 ).getStr() );
138 CPPUNIT_ASSERT_MESSAGE("No return variable huh?", pReturn.get() != nullptr );
139 CPPUNIT_ASSERT_EQUAL_MESSAGE("Result not as expected", OUString("OK"), pReturn->GetOUString() );
143 void VBATest::testMiscOLEStuff()
145 // Not much point even trying to run except on Windows.
146 // (Without Excel doesn't really do anything anyway,
147 // see "so skip test" below.)
148 #if defined(_WIN32)
149 // test if we have the necessary runtime environment
150 // to run the OLE tests.
151 uno::Reference< lang::XMultiServiceFactory > xOLEFactory;
152 uno::Reference< uno::XComponentContext > xContext(
153 comphelper::getProcessComponentContext() );
154 if( xContext.is() )
156 uno::Reference<lang::XMultiComponentFactory> xSMgr = xContext->getServiceManager();
157 xOLEFactory.set( xSMgr->createInstanceWithContext( "com.sun.star.bridge.OleObjectFactory", xContext ),
158 uno::UNO_QUERY );
160 bool bOk = false;
161 if( xOLEFactory.is() )
163 uno::Reference< uno::XInterface > xADODB = xOLEFactory->createInstance( "ADODB.Connection" );
164 bOk = xADODB.is();
166 if ( !bOk )
167 return; // can't do anything, skip test
169 const int nBufSize = 1024 * 4;
170 wchar_t sBuf[nBufSize];
171 SQLGetInstalledDriversW( sBuf, nBufSize, nullptr );
173 const wchar_t *pODBCDriverName = sBuf;
174 bool bFound = false;
175 for (; wcslen( pODBCDriverName ) != 0; pODBCDriverName += wcslen( pODBCDriverName ) + 1 ) {
176 if( wcscmp( pODBCDriverName, L"Microsoft Excel Driver (*.xls)" ) == 0 ||
177 wcscmp( pODBCDriverName, L"Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)" ) == 0 ) {
178 bFound = true;
179 break;
182 if ( !bFound )
183 return; // can't find ODBC driver needed test, so skip test
185 const char* macroSource[] = {
186 "ole_ObjAssignNoDflt.vb",
187 "ole_ObjAssignToNothing.vb",
188 #if !defined(_WIN64)
189 // This test uses Microsoft.Jet.OLEDB.4.0 Provider, that is unavailable on Win64
190 "ole_dfltObjDflMethod.vb",
191 #endif
194 OUString sMacroPathURL = m_directories.getURLFromSrc("/basic/qa/vba_tests/");
196 uno::Sequence< uno::Any > aArgs(2);
197 // path to test document
198 OUString sPath = m_directories.getPathFromSrc("/basic/qa/vba_tests/data/ADODBdata.xls");
199 sPath = sPath.replaceAll( "/", "\\" );
201 aArgs[ 0 ] <<= sPath;
202 aArgs[ 1 ] <<= OUString(
203 reinterpret_cast<sal_Unicode const *>(pODBCDriverName));
205 for ( sal_uInt32 i=0; i<SAL_N_ELEMENTS( macroSource ); ++i )
207 OUString sMacroURL = sMacroPathURL
208 + OUString::createFromAscii( macroSource[ i ] );
209 MacroSnippet myMacro;
210 myMacro.LoadSourceFromFile( sMacroURL );
211 SbxVariableRef pReturn = myMacro.Run( aArgs );
212 if ( pReturn.is() )
214 fprintf(stderr, "macro result for %s\n", macroSource[ i ] );
215 fprintf(stderr, "macro returned:\n%s\n", OUStringToOString( pReturn->GetOUString(), RTL_TEXTENCODING_UTF8 ).getStr() );
217 CPPUNIT_ASSERT_MESSAGE("No return variable huh?", pReturn.get() != nullptr );
218 CPPUNIT_ASSERT_EQUAL_MESSAGE("Result not as expected", OUString("OK"), pReturn->GetOUString() );
220 #else
221 // Avoid "this method is empty and should be removed" warning
222 (void) 42;
223 #endif
226 // Put the test suite in the registry
227 CPPUNIT_TEST_SUITE_REGISTRATION(VBATest);
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */