summaryrefslogtreecommitdiffstats
path: root/test/sensor_pluggable_unittest.cpp
blob: a15f06c1354d6caec0a035cff962a7a1d4a0c20a (plain)
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
#include "sensors/pluggable.hpp"
#include "test/readinterface_mock.hpp"
#include "test/writeinterface_mock.hpp"

#include <chrono>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

using ::testing::Invoke;

TEST(PluggableSensorTest, BoringConstructorTest)
{
    // Build a boring Pluggable Sensor.

    int64_t min = 0;
    int64_t max = 255;

    std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>();
    std::unique_ptr<WriteInterface> wi =
        std::make_unique<WriteInterfaceMock>(min, max);

    std::string name = "name";
    int64_t timeout = 1;

    PluggableSensor p(name, timeout, std::move(ri), std::move(wi));
    // Successfully created it.
}

TEST(PluggableSensorTest, TryReadingTest)
{
    // Verify calling read, calls the ReadInterface.

    int64_t min = 0;
    int64_t max = 255;

    std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>();
    std::unique_ptr<WriteInterface> wi =
        std::make_unique<WriteInterfaceMock>(min, max);

    std::string name = "name";
    int64_t timeout = 1;

    ReadInterfaceMock* rip = reinterpret_cast<ReadInterfaceMock*>(ri.get());

    PluggableSensor p(name, timeout, std::move(ri), std::move(wi));

    ReadReturn r;
    r.value = 0.1;
    r.updated = std::chrono::high_resolution_clock::now();

    EXPECT_CALL(*rip, read()).WillOnce(Invoke([&](void) { return r; }));

    // TODO(venture): Implement comparison operator for ReadReturn.
    ReadReturn v = p.read();
    EXPECT_EQ(r.value, v.value);
    EXPECT_EQ(r.updated, v.updated);
}

TEST(PluggableSensorTest, TryWritingTest)
{
    // Verify calling write, calls the WriteInterface.

    int64_t min = 0;
    int64_t max = 255;

    std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>();
    std::unique_ptr<WriteInterface> wi =
        std::make_unique<WriteInterfaceMock>(min, max);

    std::string name = "name";
    int64_t timeout = 1;

    WriteInterfaceMock* wip = reinterpret_cast<WriteInterfaceMock*>(wi.get());

    PluggableSensor p(name, timeout, std::move(ri), std::move(wi));

    double value = 0.303;

    EXPECT_CALL(*wip, write(value));
    p.write(value);
}
OpenPOWER on IntegriCloud