summaryrefslogtreecommitdiffstats
path: root/basic/qa/cppunit/test_vba.cxx
blob: f048fee6dfda43e00cbc2ed8becfb64103201ebc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* -*- 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 <unotools/syslocaleoptions.hxx>

#ifdef _WIN32
#include <string.h>
#include <comphelper/processfactory.hxx>
#include <o3tl/char16_t2wchar_t.hxx>

#if !defined WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <odbcinst.h>
#endif

using namespace ::com::sun::star;

namespace
{
    class VBATest : public test::BootstrapFixture
    {
        public:
        VBATest() : BootstrapFixture(true, false) {}
        void testMiscVBAFunctions();
        void testMiscOLEStuff();
        // Adds code needed to register the test suite
        CPPUNIT_TEST_SUITE(VBATest);

        // Declares the method as a test to call
        CPPUNIT_TEST(testMiscVBAFunctions);
        CPPUNIT_TEST(testMiscOLEStuff);

        // End of test suite definition
        CPPUNIT_TEST_SUITE_END();

    };

void VBATest::testMiscVBAFunctions()
{
    const char* macroSource[] = {
        "bytearraystring.vb",
#ifdef _WIN32
        "cdec.vb", // currently CDec is implemented only on Windows
#endif
        "constants.vb",
// datevalue test seems to depend on both locale and language
// settings, should try and rewrite the test to deal with that
// for some reason tinderboxes don't seem to complain leaving enabled
// for the moment
        "datevalue.vb",
        "partition.vb",
        "strconv.vb",
        "dateserial.vb",
        "format.vb",
        "replace.vb",
        "stringplusdouble.vb",
        "chr.vb",
        "chrw.vb",
        "abs.vb",
        "array.vb",
        "asc.vb",
        "atn.vb",
        "booltypename.vb",
        "cbool.vb",
        "cdate.vb",
        "cdbl.vb",
        "choose.vb",
        "cos.vb",
        "cint.vb",
        "clng.vb",
        "collection.vb",
        "csng.vb",
        "cstr.vb",
        "cvdate.vb",
        "cverr.vb",
        "dateadd.vb",
        "datediff.vb",
        "datepart.vb",
        "day.vb",
        "enum.vb",
        "error.vb",
        "error_message.vb",
        "Err.Raise.vb",
        "exp.vb",
        "fix.vb",
        "hex.vb",
        "hour.vb",
        "formatnumber.vb",
        "formatpercent.vb",
        "iif.vb",
        "instr.vb",
        "instrrev.vb",
        "int.vb",
        "iserror.vb",
        "ismissing.vb",
        "isnull.vb",
        "isobject.vb",
        "join.vb",
        "lbound.vb",
        "isarray.vb",
        "isdate.vb",
        "isempty.vb",
        "isnumeric.vb",
        "lcase.vb",
        "left.vb",
        "len.vb",
        "log.vb",
        "ltrim.vb",
        "mid.vb",
        "minute.vb",
        "month.vb",
        "monthname.vb",
        "oct.vb",
        "optional_paramters.vb",
        "qbcolor.vb",
        "rgb.vb",
        "rtrim.vb",
        "right.vb",
        "second.vb",
        "sgn.vb",
        "sin.vb",
        "space.vb",
        "split.vb",
        "sqr.vb",
        "str.vb",
        "strcomp.vb",
        "string.vb",
        "strreverse.vb",
        "switch.vb",
        "tdf147089_idiv.vb",
        "tdf147529_optional_parameters_msgbox.vb",
        "tdf148358_non_ascii_names.vb",
        "timeserial.vb",
        "timevalue.vb",
        "trim.vb",
        "typename.vb",
        "ubound.vb",
        "ucase.vb",
        "val.vb",
        "vartype.vb",
        "weekday.vb",
        "weekdayname.vb",
        "year.vb",
#ifndef _WIN32 // missing 64bit Currency marshalling.
        "win32compat.vb", // windows compatibility hooks.
#endif
        "win32compatb.vb" // same methods, different signatures.
    };
    OUString sMacroPathURL = m_directories.getURLFromSrc(u"/basic/qa/vba_tests/");
    OUString sMacroUtilsURL = m_directories.getURLFromSrc(u"/basic/qa/cppunit/_test_asserts.vb");
    // Some test data expects the uk locale
    LanguageTag aLocale(LANGUAGE_ENGLISH_UK);
    SvtSysLocaleOptions aLocalOptions;
    aLocalOptions.SetLocaleConfigString( aLocale.getBcp47() );

    for ( size_t  i=0; i<std::size( macroSource ); ++i )
    {
        OUString sMacroURL = sMacroPathURL
                           + OUString::createFromAscii( macroSource[ i ] );

        MacroSnippet myMacro;
        myMacro.LoadSourceFromFile("TestUtil", sMacroUtilsURL);
        myMacro.LoadSourceFromFile("TestModule", sMacroURL);
        SbxVariableRef pReturn = myMacro.Run();
        CPPUNIT_ASSERT_MESSAGE("No return variable huh?", pReturn.is());
        fprintf(stderr, "macro result for %s\n", macroSource[i]);
        fprintf(stderr, "macro returned:\n%s\n",
                OUStringToOString(pReturn->GetOUString(), RTL_TEXTENCODING_UTF8).getStr());
        CPPUNIT_ASSERT_EQUAL_MESSAGE("Result not as expected", OUString("OK"),
                                     pReturn->GetOUString());
    }
}

void VBATest::testMiscOLEStuff()
{
// Not much point even trying to run except on Windows.
// (Without Excel doesn't really do anything anyway,
// see "so skip test" below.)

// Since some time, on a properly updated Windows 10, this works
// only with a 64-bit LibreOffice

#if defined(_WIN64)
    // test if we have the necessary runtime environment
    // to run the OLE tests.
    uno::Reference< lang::XMultiServiceFactory > xOLEFactory;
    uno::Reference< uno::XComponentContext > xContext(
        comphelper::getProcessComponentContext() );
    if( xContext.is() )
    {
        uno::Reference<lang::XMultiComponentFactory> xSMgr = xContext->getServiceManager();
        xOLEFactory.set( xSMgr->createInstanceWithContext( "com.sun.star.bridge.OleObjectFactory", xContext ),
                         uno::UNO_QUERY );
    }
    bool bOk = false;
    if( xOLEFactory.is() )
    {
        uno::Reference< uno::XInterface > xADODB = xOLEFactory->createInstance( "ADODB.Connection" );
        bOk = xADODB.is();
    }
    if ( !bOk )
        return; // can't do anything, skip test

    const int nBufSize = 1024 * 4;
    wchar_t sBuf[nBufSize];
    SQLGetInstalledDriversW( sBuf, nBufSize, nullptr );

    const wchar_t *pODBCDriverName = sBuf;
    bool bFound = false;
    for (; wcslen( pODBCDriverName ) != 0; pODBCDriverName += wcslen( pODBCDriverName ) + 1 ) {
        if( wcscmp( pODBCDriverName, L"Microsoft Excel Driver (*.xls)" ) == 0 ||
            wcscmp( pODBCDriverName, L"Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)" ) == 0 ) {
            bFound = true;
            break;
        }
    }
    if ( !bFound )
        return; // can't find ODBC driver needed test, so skip test

    const char* macroSource[] = {
        "ole_ObjAssignNoDflt.vb",
        "ole_ObjAssignToNothing.vb",
    };

    OUString sMacroPathURL = m_directories.getURLFromSrc(u"/basic/qa/vba_tests/");

    // path to test document
    OUString sPath = m_directories.getPathFromSrc(u"/basic/qa/vba_tests/data/ADODBdata.xls");
    sPath = sPath.replaceAll( "/", "\\" );

    uno::Sequence< uno::Any > aArgs
    {
        uno::Any(sPath),
        uno::Any(OUString(o3tl::toU(pODBCDriverName)))
    };

    for ( sal_uInt32  i=0; i<std::size( macroSource ); ++i )
    {
        OUString sMacroURL = sMacroPathURL
                           + OUString::createFromAscii( macroSource[ i ] );
        MacroSnippet myMacro;
        myMacro.LoadSourceFromFile("TestModule", sMacroURL);
        SbxVariableRef pReturn = myMacro.Run( aArgs );
        CPPUNIT_ASSERT_MESSAGE("No return variable huh?", pReturn.is());
        fprintf(stderr, "macro result for %s\n", macroSource[i]);
        fprintf(stderr, "macro returned:\n%s\n",
                OUStringToOString(pReturn->GetOUString(), RTL_TEXTENCODING_UTF8).getStr());
        CPPUNIT_ASSERT_EQUAL_MESSAGE("Result not as expected", OUString("OK"),
                                     pReturn->GetOUString());
    }
#else
    // Avoid "this method is empty and should be removed" warning
    (void) 42;
#endif
}

  // Put the test suite in the registry
  CPPUNIT_TEST_SUITE_REGISTRATION(VBATest);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */