File: test_exception.cpp

package info (click to toggle)
paho.mqtt.cpp 1.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,672 kB
  • sloc: cpp: 13,068; ansic: 113; sh: 55; makefile: 22
file content (91 lines) | stat: -rw-r--r-- 3,067 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// test_exception_test.cpp
//
// Unit tests for the exception class in the Paho MQTT C++ library.
//

/*******************************************************************************
 * Copyright (c) 2016-2017 Guilherme M. Ferreira <guilherme.maciel.ferreira@gmail.com>
 * Copyright (c) 2020 Frank Pagliughi <fpagliughi@mindspring.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v20.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Guilherme M. Ferreira - original CppUnit implementation and documentation
 *    Frank Pagliughi - Conversion to Catch2 and expansion
 *******************************************************************************/

#define UNIT_TESTS

#include <cstring>

#include "catch2_version.h"
#include "mqtt/exception.h"

using namespace mqtt;

static const std::string ERR_MSG{"Some MQTT error"};

// ----------------------------------------------------------------------
// Test user constructor
// ----------------------------------------------------------------------

TEST_CASE("user constructor", "[exception]")
{
    mqtt::exception ex1(MQTTASYNC_FAILURE);
    REQUIRE(MQTTASYNC_FAILURE == ex1.get_return_code());

    mqtt::exception ex2(MQTTASYNC_PERSISTENCE_ERROR);
    REQUIRE(MQTTASYNC_PERSISTENCE_ERROR == ex2.get_return_code());

    mqtt::exception ex3(MQTTASYNC_OPERATION_INCOMPLETE);
    REQUIRE(MQTTASYNC_OPERATION_INCOMPLETE == ex3.get_return_code());
}

// ----------------------------------------------------------------------
// Test get_message()
// ----------------------------------------------------------------------

TEST_CASE("get message", "[exception]")
{
    mqtt::exception ex(MQTTASYNC_FAILURE, ERR_MSG);
    REQUIRE(ERR_MSG == ex.get_message());
}

// ----------------------------------------------------------------------
// Test get_return_code()
// ----------------------------------------------------------------------

TEST_CASE("get return code", "[exception]")
{
    mqtt::exception ex1(MQTTASYNC_FAILURE);
    REQUIRE(MQTTASYNC_FAILURE == ex1.get_return_code());
}

// ----------------------------------------------------------------------
// Test to string
// ----------------------------------------------------------------------

TEST_CASE("to_str", "[exception]")
{
    mqtt::exception ex1(MQTTASYNC_FAILURE);
    std::string msg1{"MQTT error [-1]"};
    REQUIRE(msg1 == ex1.to_string().substr(0, 15));
}

// ----------------------------------------------------------------------
// Test what
// ----------------------------------------------------------------------

TEST_CASE("what", "[exception]")
{
    mqtt::exception ex1(MQTTASYNC_FAILURE);
    const char* msg1 = "MQTT error [-1]";
    REQUIRE(memcmp(msg1, ex1.what(), 15) == 0);
}