1
0
mirror of https://github.com/eclipse/paho.mqtt.cpp.git synced 2025-05-10 03:39:07 +08:00

Moved disconnect_options unit tests to Catch2

This commit is contained in:
fpagliughi 2020-10-24 11:19:11 -04:00
parent 4a5de0db4f
commit 6bcc6f80a9
5 changed files with 133 additions and 147 deletions

View File

@ -95,6 +95,12 @@ public:
* @param opt Another object to move into this new one. * @param opt Another object to move into this new one.
*/ */
disconnect_options& operator=(disconnect_options&& opt); disconnect_options& operator=(disconnect_options&& opt);
/**
* Expose the underlying C struct for the unit tests.
*/
#if defined(UNIT_TESTS)
const MQTTAsync_disconnectOptions& c_struct() const { return opts_; }
#endif
/** /**
* Gets the timeout used for disconnecting. * Gets the timeout used for disconnecting.
* @return The timeout for disconnecting (in milliseconds). * @return The timeout for disconnecting (in milliseconds).

View File

@ -1,145 +0,0 @@
// disconnect_options_test.h
// Unit tests for the disconnect_options class in the Paho MQTT C++ library.
/*******************************************************************************
* Copyright (c) 2016 Guilherme M. Ferreira <guilherme.maciel.ferreira@gmail.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Guilherme M. Ferreira - initial implementation and documentation
*******************************************************************************/
#ifndef __mqtt_disconnect_options_test_h
#define __mqtt_disconnect_options_test_h
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include "mqtt/disconnect_options.h"
#include "dummy_async_client.h"
namespace mqtt {
/////////////////////////////////////////////////////////////////////////////
class disconnect_options_test : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( disconnect_options_test );
CPPUNIT_TEST( test_dflt_constructor );
CPPUNIT_TEST( test_user_constructor );
CPPUNIT_TEST( test_set_timeout );
CPPUNIT_TEST( test_set_token );
CPPUNIT_TEST_SUITE_END();
const int DFLT_TIMEOUT = 0;
const std::string EMPTY_STR;
mqtt::test::dummy_async_client cli;
static constexpr token::Type TOKEN_TYPE = token::Type::DISCONNECT;
public:
void setUp() {}
void tearDown() {}
// ----------------------------------------------------------------------
// Test default constructor
// ----------------------------------------------------------------------
void test_dflt_constructor() {
mqtt::disconnect_options opts;
CPPUNIT_ASSERT_EQUAL(DFLT_TIMEOUT, (int) opts.get_timeout().count());
CPPUNIT_ASSERT(!opts.get_token());
const auto& c_struct = opts.opts_;
CPPUNIT_ASSERT(nullptr == c_struct.onSuccess);
CPPUNIT_ASSERT(nullptr == c_struct.onFailure);
CPPUNIT_ASSERT_EQUAL(DFLT_TIMEOUT, c_struct.timeout);
}
// ----------------------------------------------------------------------
// Test user constructor
// ----------------------------------------------------------------------
void test_user_constructor() {
const int TIMEOUT = 10;
auto tok = token::create(TOKEN_TYPE, cli);
mqtt::disconnect_options opts { TIMEOUT };
opts.set_token(tok, MQTTVERSION_DEFAULT);
const auto& c_struct = opts.opts_;
CPPUNIT_ASSERT(nullptr != c_struct.onSuccess);
CPPUNIT_ASSERT(nullptr != c_struct.onFailure);
CPPUNIT_ASSERT_EQUAL(TIMEOUT, (int) opts.get_timeout().count());
CPPUNIT_ASSERT_EQUAL(tok, opts.get_token());
}
// ----------------------------------------------------------------------
// Test set timeout
// ----------------------------------------------------------------------
void test_set_timeout() {
mqtt::disconnect_options opts;
const auto& c_struct = opts.opts_;
const int TIMEOUT = 5000; // ms
// Set with integer
opts.set_timeout(TIMEOUT);
CPPUNIT_ASSERT_EQUAL(TIMEOUT, (int) opts.get_timeout().count());
CPPUNIT_ASSERT_EQUAL(TIMEOUT, c_struct.timeout);
// Set with chrono duration
opts.set_timeout(std::chrono::seconds(2*TIMEOUT/1000));
CPPUNIT_ASSERT_EQUAL(2*TIMEOUT, (int) opts.get_timeout().count());
CPPUNIT_ASSERT_EQUAL(2*TIMEOUT, c_struct.timeout);
}
// ----------------------------------------------------------------------
// Test set contect token
// ----------------------------------------------------------------------
void test_set_token() {
auto tok = token::create(TOKEN_TYPE, cli);
mqtt::disconnect_options opts;
const auto& c_struct = opts.opts_;
CPPUNIT_ASSERT(nullptr == c_struct.onSuccess);
CPPUNIT_ASSERT(nullptr == c_struct.onFailure);
opts.set_token(mqtt::token_ptr(), MQTTVERSION_DEFAULT);
CPPUNIT_ASSERT(nullptr == c_struct.onSuccess);
CPPUNIT_ASSERT(nullptr == c_struct.onFailure);
opts.set_token(tok, MQTTVERSION_DEFAULT);
CPPUNIT_ASSERT(nullptr != c_struct.onSuccess);
CPPUNIT_ASSERT(nullptr != c_struct.onFailure);
CPPUNIT_ASSERT_EQUAL(tok, opts.get_token());
}
};
/////////////////////////////////////////////////////////////////////////////
// end namespace 'mqtt'
}
#endif // __mqtt_disconnect_options_test_h

View File

@ -20,7 +20,6 @@
#include "async_client_test.h" #include "async_client_test.h"
#include "async_client_v3_test.h" #include "async_client_v3_test.h"
#include "client_test.h" #include "client_test.h"
#include "disconnect_options_test.h"
#include "response_options_test.h" #include "response_options_test.h"
#include "delivery_response_options_test.h" #include "delivery_response_options_test.h"
#include "iclient_persistence_test.h" #include "iclient_persistence_test.h"
@ -32,7 +31,6 @@ using namespace CppUnit;
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::disconnect_options_test );
CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::response_options_test ); CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::response_options_test );
CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::delivery_response_options_test ); CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::delivery_response_options_test );
@ -49,3 +47,4 @@ int main(int argc, char* argv[])
runner.addTest(registry.makeTest()); runner.addTest(registry.makeTest());
return runner.run() ? 0 : 1; return runner.run() ? 0 : 1;
} }

View File

@ -32,6 +32,7 @@ add_executable(unit_tests unit_tests.cpp
test_buffer_ref.cpp test_buffer_ref.cpp
test_connect_options.cpp test_connect_options.cpp
test_create_options.cpp test_create_options.cpp
test_disconnect_options.cpp
test_exception.cpp test_exception.cpp
test_message.cpp test_message.cpp
test_properties.cpp test_properties.cpp

View File

@ -0,0 +1,125 @@
// disconnect_options_test.h
// Unit tests for the disconnect_options class in the Paho MQTT C++ library.
/*******************************************************************************
* Copyright (c) 2016 Guilherme M. Ferreira <guilherme.maciel.ferreira@gmail.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Guilherme M. Ferreira - initial implementation and documentation
*******************************************************************************/
#define UNIT_TESTS
#include <cstring>
#include "catch2/catch.hpp"
#include "mqtt/disconnect_options.h"
#include "mock_async_client.h"
using namespace mqtt;
/////////////////////////////////////////////////////////////////////////////
static const int DFLT_TIMEOUT = 0;
static const std::string EMPTY_STR;
static constexpr token::Type TOKEN_TYPE = token::Type::DISCONNECT;
static mqtt::test::mock_async_client cli;
// ----------------------------------------------------------------------
// Test default constructor
// ----------------------------------------------------------------------
TEST_CASE("disconnect_options dflt constructor", "[options]")
{
mqtt::disconnect_options opts;
REQUIRE(DFLT_TIMEOUT == (int) opts.get_timeout().count());
REQUIRE(!opts.get_token());
const auto& c_struct = opts.c_struct();
REQUIRE(nullptr == c_struct.onSuccess);
REQUIRE(nullptr == c_struct.onFailure);
REQUIRE(DFLT_TIMEOUT == c_struct.timeout);
}
// ----------------------------------------------------------------------
// Test user constructor
// ----------------------------------------------------------------------
TEST_CASE("disconnect_options user constructor", "[options]")
{
const int TIMEOUT = 10;
auto tok = token::create(TOKEN_TYPE, cli);
mqtt::disconnect_options opts { TIMEOUT };
opts.set_token(tok, MQTTVERSION_DEFAULT);
const auto& c_struct = opts.c_struct();
REQUIRE(nullptr != c_struct.onSuccess);
REQUIRE(nullptr != c_struct.onFailure);
REQUIRE(TIMEOUT == (int) opts.get_timeout().count());
REQUIRE(tok == opts.get_token());
}
// ----------------------------------------------------------------------
// Test set timeout
// ----------------------------------------------------------------------
TEST_CASE("disconnect_options set timeout", "[options]")
{
mqtt::disconnect_options opts;
const auto& c_struct = opts.c_struct();
const int TIMEOUT = 5000; // ms
// Set with integer
opts.set_timeout(TIMEOUT);
REQUIRE(TIMEOUT == (int) opts.get_timeout().count());
REQUIRE(TIMEOUT == c_struct.timeout);
// Set with chrono duration
opts.set_timeout(std::chrono::seconds(2*TIMEOUT/1000));
REQUIRE(2*TIMEOUT == (int) opts.get_timeout().count());
REQUIRE(2*TIMEOUT == c_struct.timeout);
}
// ----------------------------------------------------------------------
// Test set contect token
// ----------------------------------------------------------------------
TEST_CASE("disconnect_options set token", "[options]")
{
auto tok = token::create(TOKEN_TYPE, cli);
mqtt::disconnect_options opts;
const auto& c_struct = opts.c_struct();
REQUIRE(nullptr == c_struct.onSuccess);
REQUIRE(nullptr == c_struct.onFailure);
opts.set_token(mqtt::token_ptr(), MQTTVERSION_DEFAULT);
REQUIRE(nullptr == c_struct.onSuccess);
REQUIRE(nullptr == c_struct.onFailure);
opts.set_token(tok, MQTTVERSION_DEFAULT);
REQUIRE(nullptr != c_struct.onSuccess);
REQUIRE(nullptr != c_struct.onFailure);
REQUIRE(tok == opts.get_token());
}