File indexing completed on 2025-01-05 04:59:56
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@kde.org> 0003 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0004 */ 0005 0006 0007 #include <testlib/qtest_zanshin.h> 0008 0009 #include <mockitopp/mockitopp.hpp> 0010 0011 class FakeInterface 0012 { 0013 public: 0014 virtual ~FakeInterface(); 0015 virtual void doSomething() = 0; 0016 virtual int computeMe(QString input) = 0; 0017 }; 0018 0019 using namespace mockitopp; 0020 using mockitopp::matcher::any; 0021 0022 class MockitoTest : public QObject 0023 { 0024 Q_OBJECT 0025 private slots: 0026 void testBasics() 0027 { 0028 mock_object<FakeInterface> mock; 0029 mock(&FakeInterface::doSomething).when().thenReturn(); 0030 mock(&FakeInterface::doSomething).when().thenThrow("exception"); 0031 for (int i = 0; i < 10; i++) { 0032 mock(&FakeInterface::computeMe).when(QStringLiteral("A")).thenReturn(0); 0033 mock(&FakeInterface::computeMe).when(QStringLiteral("B")).thenReturn(1); 0034 mock(&FakeInterface::computeMe).when(QStringLiteral("C")).thenReturn(-1); 0035 mock(&FakeInterface::computeMe).when(QStringLiteral("Foo")).thenReturn(-1); 0036 } 0037 FakeInterface &iface = mock.getInstance(); 0038 0039 iface.doSomething(); 0040 try { 0041 iface.doSomething(); 0042 QFAIL("No exception thrown"); 0043 } catch (...) { 0044 // We expect to catch something 0045 } 0046 0047 for (int i = 0; i < 10; i++) { 0048 QCOMPARE(iface.computeMe(QStringLiteral("A")), 0); 0049 QCOMPARE(iface.computeMe(QStringLiteral("B")), 1); 0050 QCOMPARE(iface.computeMe(QStringLiteral("C")), -1); 0051 QCOMPARE(iface.computeMe(QStringLiteral("Foo")), -1); 0052 } 0053 0054 QVERIFY(mock(&FakeInterface::doSomething).when().exactly(2)); 0055 QVERIFY(mock(&FakeInterface::computeMe).when(any<QString>()).exactly(40)); 0056 } 0057 }; 0058 0059 ZANSHIN_TEST_MAIN(MockitoTest) 0060 0061 #include "mockitotest.moc"