1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
10 #include "sal/config.h"
12 #include <cppunit/TestAssert.h>
13 #include <cppunit/TestFixture.h>
14 #include <cppunit/extensions/HelperMacros.h>
15 #include <cppunit/plugin/TestPlugIn.h>
17 #include "cppunitassertequals.hxx"
19 #define TEST1 CPPUNIT_ASSERT(b1 == b2)
23 bool b1
, bool b2
, OUString
const & s1
, OUString
const & s2
, T t
, void * p
, std::nullptr_t n
)
25 CppUnit::Asserter::failIf(b1
,"");
26 #if 0 // TODO: enable later
27 CPPUNIT_ASSERT(b1
&& b2
); // expected-error {{rather split into two CPPUNIT_ASSERT [loplugin:cppunitassertequals]}}
28 CPPUNIT_ASSERT((b1
&& b2
)); // expected-error {{rather split into two CPPUNIT_ASSERT [loplugin:cppunitassertequals]}}
29 CPPUNIT_ASSERT(!(b1
|| b2
)); // expected-error {{rather split into two CPPUNIT_ASSERT [loplugin:cppunitassertequals]}}
30 CPPUNIT_ASSERT(!(b1
&& b2
));
31 CPPUNIT_ASSERT(!!(b1
&& b2
)); // expected-error {{rather split into two CPPUNIT_ASSERT [loplugin:cppunitassertequals]}}
32 CPPUNIT_ASSERT_MESSAGE("", b1
&& b2
); // expected-error {{rather split into two CPPUNIT_ASSERT_MESSAGE [loplugin:cppunitassertequals]}}
34 CPPUNIT_ASSERT(b1
== b2
); // expected-error {{rather call CPPUNIT_ASSERT_EQUAL (or rewrite as an explicit operator == call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
35 CPPUNIT_ASSERT(b1
!= b2
);
36 CPPUNIT_ASSERT((b1
== b2
)); // expected-error {{rather call CPPUNIT_ASSERT_EQUAL (or rewrite as an explicit operator == call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
37 CPPUNIT_ASSERT(!(b1
!= b2
)); // expected-error {{rather call CPPUNIT_ASSERT_EQUAL (or rewrite as an explicit operator != call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
38 CPPUNIT_ASSERT(!(b1
== b2
));
39 CPPUNIT_ASSERT(!!(b1
== b2
)); // expected-error {{rather call CPPUNIT_ASSERT_EQUAL (or rewrite as an explicit operator == call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
40 CPPUNIT_ASSERT_MESSAGE("", b1
== b2
); // expected-error {{rather call CPPUNIT_ASSERT_EQUAL_MESSAGE (or rewrite as an explicit operator == call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
41 CPPUNIT_ASSERT(s1
== s2
); // expected-error {{rather call CPPUNIT_ASSERT_EQUAL (or rewrite as an explicit operator == call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
42 CPPUNIT_ASSERT(s1
!= s2
);
43 CPPUNIT_ASSERT((s1
== s2
)); // expected-error {{rather call CPPUNIT_ASSERT_EQUAL (or rewrite as an explicit operator == call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
44 CPPUNIT_ASSERT(!(s1
!= s2
)); // expected-error {{rather call CPPUNIT_ASSERT_EQUAL (or rewrite as an explicit operator != call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
45 CPPUNIT_ASSERT(!(s1
== s2
));
46 CPPUNIT_ASSERT(!!(s1
== s2
)); // expected-error {{rather call CPPUNIT_ASSERT_EQUAL (or rewrite as an explicit operator == call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
47 TEST1
; // expected-error {{rather call CPPUNIT_ASSERT_EQUAL (or rewrite as an explicit operator == call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
48 TEST2(CPPUNIT_ASSERT(b1
== b2
)); // expected-error {{rather call CPPUNIT_ASSERT_EQUAL (or rewrite as an explicit operator == call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
49 TEST2(TEST1
); // expected-error {{rather call CPPUNIT_ASSERT_EQUAL (or rewrite as an explicit operator == call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
51 // Useful when testing an equality iterator itself:
52 CPPUNIT_ASSERT(operator ==(s1
, s1
));
53 CPPUNIT_ASSERT(t
.operator ==(t
));
55 // `P == nullptr` for P of pointer type is awkward to write with CPPUNIT_ASSERT_EQUAL, and the
56 // actual pointer values that would be printed if CPPUNIT_ASSERT_EQUAL failed would likely not be
57 // very meaningful, so let it use CPPUNIT_ASSERT (but stick to CPPUNIT_ASSERT_EQUAL for
58 // consistency in the unlikely case that P is of type std::nullptr_t):
59 CPPUNIT_ASSERT(p
== nullptr);
60 CPPUNIT_ASSERT(n
== nullptr); // expected-error {{rather call CPPUNIT_ASSERT_EQUAL (or rewrite as an explicit operator == call when the operator itself is the topic) [loplugin:cppunitassertequals]}}
62 // There might even be good reasons(?) not to warn inside explicit casts:
63 CPPUNIT_ASSERT(bool(b1
&& b2
));
64 CPPUNIT_ASSERT(bool(b1
== b2
));
65 CPPUNIT_ASSERT(bool(s1
== s2
));
67 CPPUNIT_ASSERT_EQUAL(b1
, true); // expected-error {{CPPUNIT_ASSERT_EQUALS parameters look switched, expected value should be first param [loplugin:cppunitassertequals]}}
68 CPPUNIT_ASSERT_EQUAL_MESSAGE("foo", b1
, true); // expected-error {{CPPUNIT_ASSERT_EQUALS parameters look switched, expected value should be first param [loplugin:cppunitassertequals]}}
69 CPPUNIT_ASSERT_EQUAL(true, b1
);
70 CPPUNIT_ASSERT_EQUAL_MESSAGE("foo", true, b1
);
71 CPPUNIT_ASSERT_EQUAL(s1
, OUString("xxx")); // expected-error {{CPPUNIT_ASSERT_EQUALS parameters look switched, expected value should be first param [loplugin:cppunitassertequals]}}
72 CPPUNIT_ASSERT_EQUAL_MESSAGE("foo", s1
, OUString("xxx")); // expected-error {{CPPUNIT_ASSERT_EQUALS parameters look switched, expected value should be first param [loplugin:cppunitassertequals]}}
73 CPPUNIT_ASSERT_EQUAL(OUString("xxx"), s1
);
74 CPPUNIT_ASSERT_EQUAL_MESSAGE("foo", OUString("xxx"), s1
);
77 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */