summaryrefslogtreecommitdiff
path: root/comphelper/qa/unit/variadictemplates.cxx
blob: 6b62204f487c5473ab8ade030d4f590364f61541 (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
/* -*- 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 <optional>
#include <sal/types.h>
#include <comphelper/unwrapargs.hxx>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>

#include <sstream>

class VariadicTemplatesTest : public CppUnit::TestFixture
{
public:
    void testUnwrapArgs();

    CPPUNIT_TEST_SUITE(VariadicTemplatesTest);
    CPPUNIT_TEST(testUnwrapArgs);
    CPPUNIT_TEST_SUITE_END();
};

namespace {

namespace detail {

template <typename T>
void extract(
    ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> const& seq,
    sal_Int32 nArg, T & v,
    ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>
    const& xErrorContext )
{
    if (nArg >= seq.getLength()) {
        throw ::com::sun::star::lang::IllegalArgumentException(
            "No such argument available!",
            xErrorContext, static_cast<sal_Int16>(nArg) );
    }
    if (! fromAny(seq[nArg], &v)) {
        throw ::com::sun::star::lang::IllegalArgumentException(
            "Cannot extract ANY { "
            + seq[nArg].getValueType().getTypeName()
            + " } to " + ::cppu::UnoType<T>::get().getTypeName(),
            xErrorContext,
            static_cast<sal_Int16>(nArg) );
    }
}

template <typename T>
void extract(
    ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> const& seq,
    sal_Int32 nArg, ::std::optional<T> & v,
    ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>
    const& xErrorContext )
{
    if (nArg < seq.getLength()) {
        T t;
        extract( seq, nArg, t, xErrorContext );
        v = t;
    }
}

} // namespace detail

template < typename T0, typename T1, typename T2, typename T3, typename T4 >
void unwrapArgsBaseline(
    ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > const& seq,
    T0& v0, T1& v1, T2& v2, T3& v3, T4& v4,
    ::com::sun::star::uno::Reference<
    ::com::sun::star::uno::XInterface> const& xErrorContext =
    ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>() )
{
    ::detail::extract( seq, 0, v0, xErrorContext );
    ::detail::extract( seq, 1, v1, xErrorContext );
    ::detail::extract( seq, 2, v2, xErrorContext );
    ::detail::extract( seq, 3, v3, xErrorContext );
    ::detail::extract( seq, 4, v4, xErrorContext );
}

}

void VariadicTemplatesTest::testUnwrapArgs() {
    OUString tmp1   = "Test1";
    sal_Int32 tmp2  = 42;
    sal_uInt32 tmp3 = 42;
    ::com::sun::star::uno::Any tmp6(
        tmp1
        );
    ::com::sun::star::uno::Any tmp7(
        tmp2
        );
    ::com::sun::star::uno::Any tmp8(
        tmp3
        );
    ::com::sun::star::uno::Any tmp9(
        OUString("Test2")
        );
    ::std::optional< ::com::sun::star::uno::Any > tmp10(
        OUString("Test3")
        );
    ::std::optional< ::com::sun::star::uno::Any > tmp11(
        tmp1
        );

    // test equality with the baseline and template specialization with
    // std::optional< T >
    try {
        ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq1(
            static_cast< sal_uInt32 >( 5 ) );
        ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq2(
            static_cast< sal_uInt32 >( 5 ) );

        // tmp11 should be ignored as it is ::std::optional< T >
        ::comphelper::unwrapArgs( seq1, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11 );
        unwrapArgsBaseline( seq2, tmp6, tmp7, tmp8, tmp9, tmp10 );
        ::com::sun::star::uno::Any* p1 = seq1.getArray();
        ::com::sun::star::uno::Any* p2 = seq2.getArray();

        for( sal_Int32 i = 0; i < seq1.getLength() && i < seq2.getLength(); ++i ) {
            CPPUNIT_ASSERT_EQUAL_MESSAGE( "seq1 and seq2 are equal",
                                    p1[i], p2[i] );
        }
        CPPUNIT_ASSERT_MESSAGE( "seq1 and seq2 are equal",
                                bool(seq1 == seq2) );
    }
    catch( ::com::sun::star::lang::IllegalArgumentException& err ) {
        std::stringstream ss;
        ss << "IllegalArgumentException when unwrapping arguments at: " <<
            err.ArgumentPosition;
        CPPUNIT_FAIL( ss.str() );
    }

    // test argument counting
    try {
        ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq(
            static_cast< sal_uInt32 >( 4 ) );
        ::comphelper::unwrapArgs( seq, tmp6, tmp7, tmp10, tmp11, tmp10, tmp6 );
    }
    catch( ::com::sun::star::lang::IllegalArgumentException& err ) {
        CPPUNIT_ASSERT_EQUAL( static_cast< short >( 5 ), err.ArgumentPosition );
    }

    OUString test1( "Test2" );
    OUString test2( "Test2" );
    OUString test3( "Test3" );
    OUString test4( "Test4" );
    OUString test5( "Test5" );

    try {
        ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq(
            static_cast< sal_uInt32 >( 4 ) );
        ::comphelper::unwrapArgs( seq, test1, test2, test3, test4, test5 );
    }
    catch( ::com::sun::star::lang::IllegalArgumentException& err1 ) {
        try {
            ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > seq(
                static_cast< sal_uInt32 >( 4 ) );
            unwrapArgsBaseline( seq, test1, test2, test3, test4, test5 );
            CPPUNIT_FAIL( "unwrapArgs failed while the baseline did not throw" );
        }
        catch( ::com::sun::star::lang::IllegalArgumentException& err2 ) {
            CPPUNIT_ASSERT_EQUAL_MESSAGE( "err1.ArgumentPosition == err2.ArgumentPosition",
                                    err1.ArgumentPosition, err2.ArgumentPosition );
        }
    }
}

CPPUNIT_TEST_SUITE_REGISTRATION(VariadicTemplatesTest);

CPPUNIT_PLUGIN_IMPLEMENT();

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