File: test_global_as_new.cxx

package info (click to toggle)
libreoffice 4%3A26.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,837,456 kB
  • sloc: cpp: 4,397,406; xml: 499,109; java: 254,438; python: 81,844; ansic: 33,825; perl: 30,297; javascript: 19,722; sh: 12,050; makefile: 10,852; cs: 8,865; yacc: 8,549; objc: 2,131; lex: 1,385; asm: 1,231; awk: 996; pascal: 914; csh: 20; sed: 5
file content (84 lines) | stat: -rw-r--r-- 2,880 bytes parent folder | download | duplicates (3)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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 <basic/sbstar.hxx>
#include <basic/sbmeth.hxx>
#include <basic/basrdll.hxx>
#include <comphelper/errcode.hxx>
#include <cppunit/extensions/HelperMacros.h>

namespace
{
class GlobalAsNewTest : public CppUnit::TestFixture
{
    void testMaintainsValueAcrossCalls();

    CPPUNIT_TEST_SUITE(GlobalAsNewTest);
    CPPUNIT_TEST(testMaintainsValueAcrossCalls);
    CPPUNIT_TEST_SUITE_END();

    BasicDLL lib;
    StarBASICRef interpreter;

    SbModuleRef Module()
    {
        interpreter = new StarBASIC();
        auto mod = interpreter->MakeModule(u"GlobalAsNew"_ustr, uR"BAS(
Global aDate As New "com.sun.star.util.Date"

Function GetDateAsString As String
    DIM local_Date As New "com.sun.star.util.Date"
    GetDateAsString = TRIM(STR(aDate.Year)) + "-" + TRIM(STR(local_Date.Year)) + TRIM(STR(aDate.Month)) + "-" + TRIM(STR(aDate.Day))
End Function

Function SetDate
   aDate.Month = 6
   aDate.Day   = 30
   aDate.Year  = 2019
   SetDate = GetDateAsString()
End Function

        )BAS"_ustr);
        CPPUNIT_ASSERT(mod->Compile());
        CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, StarBASIC::GetErrBasic());
        CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, SbxBase::GetError());
        CPPUNIT_ASSERT(mod->IsCompiled());
        return mod;
    }
};

void GlobalAsNewTest::testMaintainsValueAcrossCalls()
{
    auto m = Module();
    auto GetDateAsString = m->FindMethod(u"GetDateAsString"_ustr, SbxClassType::Method);
    CPPUNIT_ASSERT_MESSAGE("Could not Find GetDateAsString in module", GetDateAsString != nullptr);

    // There is no SbxMethod::call(), the basic code is exercised here in the copy ctor
    SbxVariableRef returned = new SbxMethod{ *GetDateAsString };
    CPPUNIT_ASSERT(returned->IsString());
    //0-00-0 is the result of reading the default-initialized date
    CPPUNIT_ASSERT_EQUAL(u"0-00-0"_ustr, returned->GetOUString());

    auto SetDate = m->FindMethod(u"SetDate"_ustr, SbxClassType::Method);
    CPPUNIT_ASSERT_MESSAGE("Could not Find SetDate in module", SetDate != nullptr);
    returned = new SbxMethod{ *SetDate };
    CPPUNIT_ASSERT(returned->IsString());
    OUString set_val(u"2019-06-30"_ustr);
    CPPUNIT_ASSERT_EQUAL(set_val, returned->GetOUString());

    returned = new SbxMethod{ *GetDateAsString };
    CPPUNIT_ASSERT(returned->IsString());
    //tdf#88442 The global should have maintained its state!
    CPPUNIT_ASSERT_EQUAL(set_val, returned->GetOUString());
}

// Put the test suite in the registry
CPPUNIT_TEST_SUITE_REGISTRATION(GlobalAsNewTest);

} // namespace