From 6bcc6f80a96b04ab69d0a1269a47dbcc59be7f7d Mon Sep 17 00:00:00 2001 From: fpagliughi Date: Sat, 24 Oct 2020 11:19:11 -0400 Subject: [PATCH] Moved disconnect_options unit tests to Catch2 --- src/mqtt/disconnect_options.h | 6 + test/cppunit/disconnect_options_test.h | 145 ------------------------- test/cppunit/test.cpp | 3 +- test/unit/CMakeLists.txt | 1 + test/unit/test_disconnect_options.cpp | 125 +++++++++++++++++++++ 5 files changed, 133 insertions(+), 147 deletions(-) delete mode 100644 test/cppunit/disconnect_options_test.h create mode 100644 test/unit/test_disconnect_options.cpp diff --git a/src/mqtt/disconnect_options.h b/src/mqtt/disconnect_options.h index f37ee4a..44e03b0 100644 --- a/src/mqtt/disconnect_options.h +++ b/src/mqtt/disconnect_options.h @@ -95,6 +95,12 @@ public: * @param opt Another object to move into this new one. */ 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. * @return The timeout for disconnecting (in milliseconds). diff --git a/test/cppunit/disconnect_options_test.h b/test/cppunit/disconnect_options_test.h deleted file mode 100644 index 4984e80..0000000 --- a/test/cppunit/disconnect_options_test.h +++ /dev/null @@ -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 - * - * 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 -#include - -#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 diff --git a/test/cppunit/test.cpp b/test/cppunit/test.cpp index d1a7ff3..bef8e63 100644 --- a/test/cppunit/test.cpp +++ b/test/cppunit/test.cpp @@ -20,7 +20,6 @@ #include "async_client_test.h" #include "async_client_v3_test.h" #include "client_test.h" -#include "disconnect_options_test.h" #include "response_options_test.h" #include "delivery_response_options_test.h" #include "iclient_persistence_test.h" @@ -32,7 +31,6 @@ using namespace CppUnit; 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::delivery_response_options_test ); @@ -49,3 +47,4 @@ int main(int argc, char* argv[]) runner.addTest(registry.makeTest()); return runner.run() ? 0 : 1; } + diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index ae1788b..c04f37f 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -32,6 +32,7 @@ add_executable(unit_tests unit_tests.cpp test_buffer_ref.cpp test_connect_options.cpp test_create_options.cpp + test_disconnect_options.cpp test_exception.cpp test_message.cpp test_properties.cpp diff --git a/test/unit/test_disconnect_options.cpp b/test/unit/test_disconnect_options.cpp new file mode 100644 index 0000000..4e9c2ba --- /dev/null +++ b/test/unit/test_disconnect_options.cpp @@ -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 + * + * 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 +#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()); +} +