File: Exception_test.cpp

package info (click to toggle)
sqlitecpp 3.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,608 kB
  • sloc: ansic: 166,965; cpp: 3,720; python: 2,374; xml: 14; sh: 12; makefile: 8
file content (89 lines) | stat: -rw-r--r-- 2,537 bytes parent folder | download | duplicates (5)
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
/**
 * @file    Transaction_test.cpp
 * @ingroup tests
 * @brief   Test of a SQLite Transaction.
 *
 * Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com)
 *
 * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
 * or copy at http://opensource.org/licenses/MIT)
 */

#include <SQLiteCpp/Exception.h>

#include <gtest/gtest.h>

#include <string>

TEST(Exception, copy)
{
    const SQLite::Exception ex1("some error", 2);
    const SQLite::Exception ex2 = ex1;
    EXPECT_STREQ(ex1.what(), ex2.what());
    EXPECT_EQ(ex1.getErrorCode(), ex2.getErrorCode());
    EXPECT_EQ(ex1.getExtendedErrorCode(), ex2.getExtendedErrorCode());
}

// see http://eel.is/c++draft/exception#2 or http://www.cplusplus.com/reference/exception/exception/operator=/
// an assignment operator is expected to be avaiable
TEST(Exception, assignment)
{
    const char message[] = "some error";
    const SQLite::Exception ex1(message, 1);
    SQLite::Exception ex2("another error", 2);

    ex2 = ex1;

    EXPECT_STREQ(ex2.what(), message);
    EXPECT_EQ(ex2.getErrorCode(), 1);
    EXPECT_EQ(ex2.getExtendedErrorCode(), -1);
    EXPECT_STREQ(ex2.getErrorStr(), "SQL logic error");
}

TEST(Exception, throw_catch)
{
    const char message[] = "some error";
    try
    {
        throw SQLite::Exception(message);
    }
    catch (const std::runtime_error& ex)
    {
        EXPECT_STREQ(ex.what(), message);
    }
}


TEST(Exception, constructor)
{
    const char msg1[] = "some error";
    std::string msg2 = "another error";
    {
        const SQLite::Exception ex(msg1);
        EXPECT_STREQ(ex.what(), msg1);
        EXPECT_EQ(ex.getErrorCode(), -1);
        EXPECT_EQ(ex.getExtendedErrorCode(), -1);
        EXPECT_STREQ("unknown error", ex.getErrorStr());
    }
    {
        const SQLite::Exception ex(msg2);
        EXPECT_STREQ(ex.what(), msg2.c_str());
        EXPECT_EQ(ex.getErrorCode(), -1);
        EXPECT_EQ(ex.getExtendedErrorCode(), -1);
        EXPECT_STREQ("unknown error", ex.getErrorStr());
    }
    {
        const SQLite::Exception ex(msg1, 1);
        EXPECT_STREQ(ex.what(), msg1);
        EXPECT_EQ(ex.getErrorCode(), 1);
        EXPECT_EQ(ex.getExtendedErrorCode(), -1);
        EXPECT_STREQ(ex.getErrorStr(), "SQL logic error");
    }
    {
        const SQLite::Exception ex(msg2, 2);
        EXPECT_STREQ(ex.what(), msg2.c_str());
        EXPECT_EQ(ex.getErrorCode(), 2);
        EXPECT_EQ(ex.getExtendedErrorCode(), -1);
        EXPECT_STREQ(ex.getErrorStr(), "unknown error");
    }
}