tdf#161411 - UI: Add Better wording for ASCII-only characters
[LibreOffice.git] / basic / qa / cppunit / test_vba.cxx
blobdf0ceffefe8144b1056e4f3ba3819b1d36dac1d7
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 <unotools/syslocaleoptions.hxx>
12 #ifdef _WIN32
13 #include <string.h>
14 #include <comphelper/processfactory.hxx>
15 #include <o3tl/char16_t2wchar_t.hxx>
17 #include <systools/win32/odbccp32.hxx>
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 "chrw.vb",
63 "abs.vb",
64 "array.vb",
65 "asc.vb",
66 "atn.vb",
67 "booltypename.vb",
68 "cbool.vb",
69 "cdate.vb",
70 "cdbl.vb",
71 "choose.vb",
72 "cos.vb",
73 "cint.vb",
74 "clng.vb",
75 "collection.vb",
76 "csng.vb",
77 "cstr.vb",
78 "cvdate.vb",
79 "cverr.vb",
80 "dateadd.vb",
81 "datediff.vb",
82 "datepart.vb",
83 "day.vb",
84 "enum.vb",
85 "error.vb",
86 "error_message.vb",
87 "Err.Raise.vb",
88 "exp.vb",
89 "fix.vb",
90 "gosub_goto.vb",
91 "hex.vb",
92 "hour.vb",
93 "for.vb",
94 "formatnumber.vb",
95 "formatpercent.vb",
96 "if.vb",
97 "iif.vb",
98 "instr.vb",
99 "instrrev.vb",
100 "int.vb",
101 "iserror.vb",
102 "ismissing.vb",
103 "isnull.vb",
104 "isobject.vb",
105 "join.vb",
106 "lbound.vb",
107 "isarray.vb",
108 "isdate.vb",
109 "isempty.vb",
110 "isnumeric.vb",
111 "lcase.vb",
112 "left.vb",
113 "len.vb",
114 "log.vb",
115 "ltrim.vb",
116 "mid.vb",
117 "minute.vb",
118 "month.vb",
119 "monthname.vb",
120 "like.vb",
121 "oct.vb",
122 "optional_paramters.vb",
123 "qbcolor.vb",
124 "rgb.vb",
125 "rtrim.vb",
126 "right.vb",
127 "second.vb",
128 "sgn.vb",
129 "sin.vb",
130 "space.vb",
131 "split.vb",
132 "sqr.vb",
133 "str.vb",
134 "strcomp.vb",
135 "string.vb",
136 "strreverse.vb",
137 "switch.vb",
138 "tdf147089_idiv.vb",
139 "tdf147529_optional_parameters_msgbox.vb",
140 "tdf148358_non_ascii_names.vb",
141 "timeserial.vb",
142 "timevalue.vb",
143 "trim.vb",
144 "typename.vb",
145 "ubound.vb",
146 "ucase.vb",
147 "val.vb",
148 "vartype.vb",
149 "weekday.vb",
150 "weekdayname.vb",
151 "year.vb",
152 #ifndef _WIN32 // missing 64bit Currency marshalling.
153 "win32compat.vb", // windows compatibility hooks.
154 #endif
155 "win32compatb.vb" // same methods, different signatures.
157 OUString sMacroPathURL = m_directories.getURLFromSrc(u"/basic/qa/vba_tests/");
158 OUString sMacroUtilsURL = m_directories.getURLFromSrc(u"/basic/qa/cppunit/_test_asserts.vb");
159 // Some test data expects the uk locale
160 LanguageTag aLocale(LANGUAGE_ENGLISH_UK);
161 SvtSysLocaleOptions aLocalOptions;
162 aLocalOptions.SetLocaleConfigString( aLocale.getBcp47() );
164 for ( size_t i=0; i<std::size( macroSource ); ++i )
166 OUString sMacroURL = sMacroPathURL
167 + OUString::createFromAscii( macroSource[ i ] );
169 MacroSnippet myMacro;
170 myMacro.LoadSourceFromFile(u"TestUtil"_ustr, sMacroUtilsURL);
171 myMacro.LoadSourceFromFile(u"TestModule"_ustr, sMacroURL);
172 SbxVariableRef pReturn = myMacro.Run();
173 CPPUNIT_ASSERT_MESSAGE("No return variable huh?", pReturn.is());
174 fprintf(stderr, "macro result for %s\n", macroSource[i]);
175 fprintf(stderr, "macro returned:\n%s\n",
176 OUStringToOString(pReturn->GetOUString(), RTL_TEXTENCODING_UTF8).getStr());
177 CPPUNIT_ASSERT_EQUAL_MESSAGE("Result not as expected", u"OK"_ustr,
178 pReturn->GetOUString());
182 void VBATest::testMiscOLEStuff()
184 // Not much point even trying to run except on Windows.
185 // (Without Excel doesn't really do anything anyway,
186 // see "so skip test" below.)
188 // Since some time, on a properly updated Windows 10, this works
189 // only with a 64-bit LibreOffice
191 #if defined _WIN32 && defined _ARM64_
192 // skip for windows arm64 build
193 // Avoid "this method is empty and should be removed" warning
194 (void) 42;
195 #elif defined(_WIN64)
196 // test if we have the necessary runtime environment
197 // to run the OLE tests.
198 uno::Reference< lang::XMultiServiceFactory > xOLEFactory;
199 uno::Reference< uno::XComponentContext > xContext(
200 comphelper::getProcessComponentContext() );
201 if( xContext.is() )
203 uno::Reference<lang::XMultiComponentFactory> xSMgr = xContext->getServiceManager();
204 xOLEFactory.set( xSMgr->createInstanceWithContext( "com.sun.star.bridge.OleObjectFactory", xContext ),
205 uno::UNO_QUERY );
207 bool bOk = false;
208 if( xOLEFactory.is() )
210 uno::Reference< uno::XInterface > xADODB = xOLEFactory->createInstance( "ADODB.Connection" );
211 bOk = xADODB.is();
213 if ( !bOk )
214 return; // can't do anything, skip test
216 const int nBufSize = 1024 * 4;
217 wchar_t sBuf[nBufSize];
218 if (!sal::systools::odbccp32().SQLGetInstalledDrivers(sBuf, nBufSize))
219 return;
221 const wchar_t *pODBCDriverName = sBuf;
222 bool bFound = false;
223 for (; wcslen( pODBCDriverName ) != 0; pODBCDriverName += wcslen( pODBCDriverName ) + 1 ) {
224 if( wcscmp( pODBCDriverName, L"Microsoft Excel Driver (*.xls)" ) == 0 ||
225 wcscmp( pODBCDriverName, L"Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)" ) == 0 ) {
226 bFound = true;
227 break;
230 if ( !bFound )
231 return; // can't find ODBC driver needed test, so skip test
233 const char* macroSource[] = {
234 "ole_ObjAssignNoDflt.vb",
235 "ole_ObjAssignToNothing.vb",
238 OUString sMacroPathURL = m_directories.getURLFromSrc(u"/basic/qa/vba_tests/");
240 // path to test document
241 OUString sPath = m_directories.getPathFromSrc(u"/basic/qa/vba_tests/data/ADODBdata.xls");
242 sPath = sPath.replaceAll( "/", "\\" );
244 uno::Sequence< uno::Any > aArgs
246 uno::Any(sPath),
247 uno::Any(OUString(o3tl::toU(pODBCDriverName)))
250 for ( sal_uInt32 i=0; i<std::size( macroSource ); ++i )
252 OUString sMacroURL = sMacroPathURL
253 + OUString::createFromAscii( macroSource[ i ] );
254 MacroSnippet myMacro;
255 myMacro.LoadSourceFromFile("TestModule", sMacroURL);
256 SbxVariableRef pReturn = myMacro.Run( aArgs );
257 CPPUNIT_ASSERT_MESSAGE("No return variable huh?", pReturn.is());
258 fprintf(stderr, "macro result for %s\n", macroSource[i]);
259 fprintf(stderr, "macro returned:\n%s\n",
260 OUStringToOString(pReturn->GetOUString(), RTL_TEXTENCODING_UTF8).getStr());
261 CPPUNIT_ASSERT_EQUAL_MESSAGE("Result not as expected", OUString("OK"),
262 pReturn->GetOUString());
264 #else
265 // Avoid "this method is empty and should be removed" warning
266 (void) 42;
267 #endif
270 // Put the test suite in the registry
271 CPPUNIT_TEST_SUITE_REGISTRATION(VBATest);
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */