~aleteoryx/muditaos

ref: d558e60a4d5bc26a982f0ba85f20631d7efac5df muditaos/module-services/service-db/test/test-settings-Settings/test-settings-Interface.cpp -rw-r--r-- 1.8 KiB
d558e60a — Lefucjusz [CP-2013] Add time sync endpoint 2 years ago
                                                                                
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>
#include <memory>
#include <Service/ServiceProxy.hpp>

namespace sys
{
    // mockup
    class Service
    {};

} // namespace sys

class Interface_imp : public service::ServiceProxy
{
  public:
    using ServiceProxy::getService;
    Interface_imp() = default;
    explicit Interface_imp(std::weak_ptr<sys::Service> s) : service::ServiceProxy(std::move(s))
    {}
};

TEST_CASE("Interface - not initialized")
{
    SECTION("base api")
    {
        auto interface = service::ServiceProxy();
        REQUIRE(!interface.isValid());
    }

    SECTION("inherited api")
    {
        auto interface = Interface_imp();
        REQUIRE_THROWS_AS(interface.getService(), std::runtime_error);
    }
}

TEST_CASE("Interface - initialized")
{
    auto service   = std::make_shared<sys::Service>();
    auto interface = Interface_imp(service);
    REQUIRE(interface.isValid());
    REQUIRE_NOTHROW(interface.getService());
}

TEST_CASE("Interface - expired")
{
    Interface_imp *interface;
    std::shared_ptr<sys::Service> expired;
    {
        auto service = std::make_shared<sys::Service>();
        interface    = new Interface_imp(service);
    }
    REQUIRE(!interface->isValid());
    REQUIRE_THROWS_AS(interface->getService(), std::runtime_error);
    delete interface;
}

TEST_CASE("Interface - copied")
{
    auto service    = std::make_shared<sys::Service>();
    auto interface  = Interface_imp(service);
    auto interface2 = interface;

    REQUIRE(interface.isValid());
    REQUIRE_NOTHROW(interface.getService());
    REQUIRE(interface2.isValid());
    REQUIRE_NOTHROW(interface2.getService());
    REQUIRE(interface2.getService() == interface.getService());
}