Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / basic / qa / cppunit / test_global_as_new.cxx
blob868f62d3ec15e12520ee8abed587dc8cf29d0e9b
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 GlobalAsNewTest : public CppUnit::TestFixture
19 void testMaintainsValueAcrossCalls();
21 CPPUNIT_TEST_SUITE(GlobalAsNewTest);
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("GlobalAsNew", R"BAS(
32 Global aDate As New "com.sun.star.util.Date"
34 Function GetDateAsString As String
35 DIM local_Date As New "com.sun.star.util.Date"
36 GetDateAsString = TRIM(STR(aDate.Year)) + "-" + TRIM(STR(local_Date.Year)) + TRIM(STR(aDate.Month)) + "-" + TRIM(STR(aDate.Day))
37 End Function
39 Function SetDate
40 aDate.Month = 6
41 aDate.Day = 30
42 aDate.Year = 2019
43 SetDate = GetDateAsString()
44 End Function
46 )BAS");
47 CPPUNIT_ASSERT(mod->Compile());
48 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, StarBASIC::GetErrBasic());
49 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, SbxBase::GetError());
50 CPPUNIT_ASSERT(mod->IsCompiled());
51 return mod;
55 void GlobalAsNewTest::testMaintainsValueAcrossCalls()
57 auto m = Module();
58 auto GetDateAsString = m->FindMethod("GetDateAsString", SbxClassType::Method);
59 CPPUNIT_ASSERT_MESSAGE("Could not Find GetDateAsString in module", GetDateAsString != nullptr);
61 // There is no SbxMethod::call(), the basic code is exercised here in the copy ctor
62 SbxVariableRef returned = new SbxMethod{ *GetDateAsString };
63 CPPUNIT_ASSERT(returned->IsString());
64 //0-00-0 is the result of reading the default-initialized date
65 CPPUNIT_ASSERT_EQUAL(OUString{ "0-00-0" }, returned->GetOUString());
67 auto SetDate = m->FindMethod("SetDate", SbxClassType::Method);
68 CPPUNIT_ASSERT_MESSAGE("Could not Find SetDate in module", SetDate != nullptr);
69 returned = new SbxMethod{ *SetDate };
70 CPPUNIT_ASSERT(returned->IsString());
71 OUString set_val("2019-06-30");
72 CPPUNIT_ASSERT_EQUAL(set_val, returned->GetOUString());
74 returned = new SbxMethod{ *GetDateAsString };
75 CPPUNIT_ASSERT(returned->IsString());
76 //tdf#88442 The global should have maintained its state!
77 CPPUNIT_ASSERT_EQUAL(set_val, returned->GetOUString());
80 // Put the test suite in the registry
81 CPPUNIT_TEST_SUITE_REGISTRATION(GlobalAsNewTest);
83 } // namespace