1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
11 #include <sal/types.h>
12 #include <comphelper/unwrapargs.hxx>
13 #include <cppunit/TestAssert.h>
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 #include <cppunit/plugin/TestPlugIn.h>
20 class VariadicTemplatesTest
: public CppUnit::TestFixture
23 void testUnwrapArgs();
25 CPPUNIT_TEST_SUITE(VariadicTemplatesTest
);
26 CPPUNIT_TEST(testUnwrapArgs
);
27 CPPUNIT_TEST_SUITE_END();
36 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> const& seq
,
37 sal_Int32 nArg
, T
& v
,
38 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>
39 const& xErrorContext
)
41 if (nArg
>= seq
.getLength()) {
42 throw ::com::sun::star::lang::IllegalArgumentException(
43 "No such argument available!",
44 xErrorContext
, static_cast<sal_Int16
>(nArg
) );
46 if (! fromAny(seq
[nArg
], &v
)) {
47 throw ::com::sun::star::lang::IllegalArgumentException(
48 "Cannot extract ANY { "
49 + seq
[nArg
].getValueType().getTypeName()
50 + " } to " + ::cppu::UnoType
<T
>::get().getTypeName(),
52 static_cast<sal_Int16
>(nArg
) );
58 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> const& seq
,
59 sal_Int32 nArg
, ::std::optional
<T
> & v
,
60 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>
61 const& xErrorContext
)
63 if (nArg
< seq
.getLength()) {
65 extract( seq
, nArg
, t
, xErrorContext
);
72 template < typename T0
, typename T1
, typename T2
, typename T3
, typename T4
>
73 void unwrapArgsBaseline(
74 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> const& seq
,
75 T0
& v0
, T1
& v1
, T2
& v2
, T3
& v3
, T4
& v4
,
76 ::com::sun::star::uno::Reference
<
77 ::com::sun::star::uno::XInterface
> const& xErrorContext
=
78 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() )
80 ::detail::extract( seq
, 0, v0
, xErrorContext
);
81 ::detail::extract( seq
, 1, v1
, xErrorContext
);
82 ::detail::extract( seq
, 2, v2
, xErrorContext
);
83 ::detail::extract( seq
, 3, v3
, xErrorContext
);
84 ::detail::extract( seq
, 4, v4
, xErrorContext
);
89 void VariadicTemplatesTest::testUnwrapArgs() {
90 OUString tmp1
= "Test1";
93 ::com::sun::star::uno::Any
tmp6(
96 ::com::sun::star::uno::Any
tmp7(
99 ::com::sun::star::uno::Any
tmp8(
102 ::com::sun::star::uno::Any
tmp9(
105 ::std::optional
< ::com::sun::star::uno::Any
> tmp10(
108 ::std::optional
< ::com::sun::star::uno::Any
> tmp11(
112 // test equality with the baseline and template specialization with
113 // std::optional< T >
115 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> seq1(
116 static_cast< sal_uInt32
>( 5 ) );
117 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> seq2(
118 static_cast< sal_uInt32
>( 5 ) );
120 // tmp11 should be ignored as it is ::std::optional< T >
121 ::comphelper::unwrapArgs( seq1
, tmp6
, tmp7
, tmp8
, tmp9
, tmp10
, tmp11
);
122 unwrapArgsBaseline( seq2
, tmp6
, tmp7
, tmp8
, tmp9
, tmp10
);
123 ::com::sun::star::uno::Any
* p1
= seq1
.getArray();
124 ::com::sun::star::uno::Any
* p2
= seq2
.getArray();
126 for( sal_Int32 i
= 0; i
< seq1
.getLength() && i
< seq2
.getLength(); ++i
) {
127 CPPUNIT_ASSERT_EQUAL_MESSAGE( "seq1 and seq2 are equal",
130 CPPUNIT_ASSERT_MESSAGE( "seq1 and seq2 are equal",
131 bool(seq1
== seq2
) );
133 catch( ::com::sun::star::lang::IllegalArgumentException
& err
) {
134 std::stringstream ss
;
135 ss
<< "IllegalArgumentException when unwrapping arguments at: " <<
136 err
.ArgumentPosition
;
137 CPPUNIT_FAIL( ss
.str() );
140 // test argument counting
142 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> seq(
143 static_cast< sal_uInt32
>( 4 ) );
144 ::comphelper::unwrapArgs( seq
, tmp6
, tmp7
, tmp10
, tmp11
, tmp10
, tmp6
);
146 catch( ::com::sun::star::lang::IllegalArgumentException
& err
) {
147 CPPUNIT_ASSERT_EQUAL( static_cast< short >( 5 ), err
.ArgumentPosition
);
150 OUString
test1( "Test2" );
151 OUString
test2( "Test2" );
152 OUString
test3( "Test3" );
153 OUString
test4( "Test4" );
154 OUString
test5( "Test5" );
157 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> seq(
158 static_cast< sal_uInt32
>( 4 ) );
159 ::comphelper::unwrapArgs( seq
, test1
, test2
, test3
, test4
, test5
);
161 catch( ::com::sun::star::lang::IllegalArgumentException
& err1
) {
163 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> seq(
164 static_cast< sal_uInt32
>( 4 ) );
165 unwrapArgsBaseline( seq
, test1
, test2
, test3
, test4
, test5
);
166 CPPUNIT_FAIL( "unwrapArgs failed while the baseline did not throw" );
168 catch( ::com::sun::star::lang::IllegalArgumentException
& err2
) {
169 CPPUNIT_ASSERT_EQUAL_MESSAGE( "err1.ArgumentPosition == err2.ArgumentPosition",
170 err1
.ArgumentPosition
, err2
.ArgumentPosition
);
175 CPPUNIT_TEST_SUITE_REGISTRATION(VariadicTemplatesTest
);
177 CPPUNIT_PLUGIN_IMPLEMENT();
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */