Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / basic / qa / cppunit / test_global_array.cxx
blobd2dafcfbda009c827051b751f820c3ca06f1f4d3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 <basic/sbstar.hxx>
11 #include <basic/sbmeth.hxx>
12 #include <basic/basrdll.hxx>
13 #include <cppunit/extensions/HelperMacros.h>
15 namespace
17 class GlobalArrayTest : public CppUnit::TestFixture
19 void testMaintainsValueAcrossCalls();
21 CPPUNIT_TEST_SUITE(GlobalArrayTest);
22 CPPUNIT_TEST(testMaintainsValueAcrossCalls);
23 CPPUNIT_TEST_SUITE_END();
25 BasicDLL lib;
26 StarBASICRef interpreter;
28 SbModuleRef Module()
30 interpreter = new StarBASIC();
31 auto mod = interpreter->MakeModule("GlobalArray", R"BAS(
33 Type testType
34 iNr As Integer
35 sType As String
36 End Type
38 Global aTestTypes(2) As New testType
40 Function Macro1 As String
41 aTestTypes(0).iNr = 1
42 aTestTypes(0).sType = "A"
43 Macro1 = aTestTypes(0).iNr & aTestTypes(0).sType
44 End Function
46 Function Macro2 As String
47 aTestTypes(1).iNr = 2
48 aTestTypes(1).sType = "B"
49 Macro2 = aTestTypes(0).iNr & aTestTypes(0).sType & aTestTypes(1).iNr & aTestTypes(1).sType
50 End Function
52 )BAS");
53 CPPUNIT_ASSERT(mod->Compile());
54 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, StarBASIC::GetErrBasic());
55 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, SbxBase::GetError());
56 CPPUNIT_ASSERT(mod->IsCompiled());
57 return mod;
61 void GlobalArrayTest::testMaintainsValueAcrossCalls()
63 auto m = Module();
64 auto Macro1 = m->FindMethod("Macro1", SbxClassType::Method);
65 CPPUNIT_ASSERT_MESSAGE("Could not Find Macro1 in module", Macro1 != nullptr);
67 // There is no SbxMethod::call(), the basic code is exercised here in the copy ctor
68 SbxVariableRef returned = new SbxMethod{ *Macro1 };
69 CPPUNIT_ASSERT(returned->IsString());
70 CPPUNIT_ASSERT_EQUAL(OUString{ "1A" }, returned->GetOUString());
72 auto Macro2 = m->FindMethod("Macro2", SbxClassType::Method);
73 CPPUNIT_ASSERT_MESSAGE("Could not Find Macro2 in module", Macro2 != nullptr);
74 returned = new SbxMethod{ *Macro2 };
75 CPPUNIT_ASSERT(returned->IsString());
76 // tdf#145371 - check if the global array has maintained its state
77 // Without the fix in place, this test would have failed with:
78 // - Expected: 1A2B
79 // - Actual : 02B
80 CPPUNIT_ASSERT_EQUAL(OUString("1A2B"), returned->GetOUString());
83 // Put the test suite in the registry
84 CPPUNIT_TEST_SUITE_REGISTRATION(GlobalArrayTest);
86 } // namespace