File: ut_dexpected.cpp

package info (click to toggle)
dtkcore 5.7.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,728 kB
  • sloc: cpp: 22,021; ansic: 183; python: 68; xml: 58; makefile: 27; sh: 15
file content (59 lines) | stat: -rw-r--r-- 1,391 bytes parent folder | download
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
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <gtest/gtest.h>

#include "dexpected.h"

DCORE_USE_NAMESPACE

TEST(ut_DExpected, unexpected)
{
    DExpected<void> exp_void { DUnexpected { emplace_tag::USE_EMPLACE, 404, "Not Found"} };

    EXPECT_FALSE(exp_void.hasValue());
    EXPECT_EQ(exp_void.error().getErrorCode(), 404);
    EXPECT_EQ(exp_void.error().getErrorMessage(), "Not Found");
}

TEST(ut_DExpected, expected)
{
    DExpected<void> exp_void {};
    EXPECT_TRUE(exp_void.hasValue()); // void

    DExpected<int> exp_int {200};
    EXPECT_TRUE(exp_int.hasValue());
    EXPECT_EQ(exp_int.value(), 200);
}

TEST(ut_DError, defaultError)
{
    DError error;
    EXPECT_EQ(error.getErrorCode(), -1);
    EXPECT_EQ(error.getErrorMessage(), QString());
}

TEST(ut_DError, setError)
{
    DError error(404, "Not Found");
    EXPECT_EQ(error.getErrorCode(), 404);
    EXPECT_EQ(error.getErrorMessage(), "Not Found");

    error.setErrorCode(502);
    error.setErrorMessage("Bad Gateway");
    EXPECT_EQ(error.getErrorCode(), 502);
    EXPECT_EQ(error.getErrorMessage(), "Bad Gateway");
}

TEST(ut_DError, operator)
{
    DError error(404, "Not Found");
    DError copy = error;
    EXPECT_EQ(copy, error);

    copy.setErrorCode(502);
    EXPECT_NE(copy, error);

    qDebug() << error; // operator <<
}