| 117 | } |
| 118 | |
| 119 | void TestMICommand::testQObjectCommandHandler() |
| 120 | { |
| 121 | KDevMI::MI::UserCommand command(KDevMI::MI::NonMI, "command"); |
| 122 | command.setToken(1); |
| 123 | |
| 124 | // set the handler and invoke |
| 125 | auto recordsHandled = 0; |
| 126 | auto resultHandler = std::make_unique<TestCommandResultHandler>(recordsHandled); |
| 127 | command.setHandler(resultHandler.get(), &TestCommandResultHandler::handleResult); |
| 128 | |
| 129 | KDevMI::MI::ResultRecord resultRecord1("reason"); |
| 130 | bool success = command.invokeHandler(resultRecord1); |
| 131 | |
| 132 | // check |
| 133 | QVERIFY(success); |
| 134 | QCOMPARE(recordsHandled, 1); |
| 135 | |
| 136 | // set the same resultHandler again, delete it, and verify that invoking does not crash |
| 137 | command.setHandler(resultHandler.get(), &TestCommandResultHandler::handleResult); |
| 138 | resultHandler.reset(); |
| 139 | |
| 140 | const KDevMI::MI::ResultRecord resultRecord2("reason"); |
| 141 | success = command.invokeHandler(resultRecord2); |
| 142 | |
| 143 | // setHandler() created an internal handler wrapper that still exists, hence the invocation succeeds |
| 144 | QVERIFY(success); |
| 145 | // the TestCommandResultHandler instance was destroyed before the invocation, so recordsHandled is unchanged |
| 146 | QCOMPARE(recordsHandled, 1); |
| 147 | |
| 148 | // invoke once more, this time without a handler (each invocation resets the command's handler to nullptr) |
| 149 | success = command.invokeHandler(resultRecord2); |
| 150 | |
| 151 | // invoking a null handler fails |
| 152 | QVERIFY(!success); |
| 153 | QCOMPARE(recordsHandled, 1); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | QTEST_GUILESS_MAIN(TestMICommand) |
nothing calls this directly
no test coverage detected