File: test_auto_command.cpp

package info (click to toggle)
tango 10.0.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 89,936 kB
  • sloc: cpp: 201,786; sh: 1,645; python: 953; java: 800; perl: 467; javascript: 447; xml: 325; makefile: 272; sql: 72; ruby: 24
file content (173 lines) | stat: -rw-r--r-- 5,152 bytes parent folder | download | duplicates (3)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <tango/tango.h>

#include <memory>

#include "utils/utils.h"

static constexpr double CMD_RET_VOID_RETURN_VALUE = 42.0;
static constexpr double CMD_VOID_ARG_TEST_VALUE = 84.0;
static constexpr double CMD_RET_ARG_TEST_VALUE = 168.0;

// Test device class
template <class Base>
class AutoCmdDev : public Base
{
  public:
    using Base::Base;

    ~AutoCmdDev() override { }

    void init_device() override
    {
        cmd_run = false;
        value = 0;
    }

    // a command that doesn't accept arguments and doesn't return anything
    void cmd_void_void()
    {
        cmd_run = true;
    }

    // a command that returns a value and doesn't accept arguments
    double cmd_ret_void()
    {
        return CMD_RET_VOID_RETURN_VALUE;
    }

    // a command that accepts an argument and doesn't return anything
    void cmd_void_arg(double v)
    {
        value = v;
    }

    // a command that accepts an argument and returns a value
    double cmd_ret_arg(double v)
    {
        return v;
    }

    // a flag attribute for void-void command
    void read_cmd_run(Tango::Attribute &att)
    {
        att.set_value_date_quality(&cmd_run, std::chrono::system_clock::now(), Tango::ATTR_VALID);
    }

    // value attribute for void-arg command
    void read_value(Tango::Attribute &att)
    {
        att.set_value_date_quality(&value, std::chrono::system_clock::now(), Tango::ATTR_VALID);
    }

    static void attribute_factory(std::vector<Tango::Attr *> &attrs)
    {
        attrs.push_back(new TangoTest::AutoAttr<&AutoCmdDev::read_cmd_run>("cmd_run", Tango::DEV_BOOLEAN));
        attrs.push_back(new TangoTest::AutoAttr<&AutoCmdDev::read_value>("value", Tango::DEV_DOUBLE));
    }

    static void command_factory(std::vector<Tango::Command *> &cmds)
    {
        cmds.push_back(new TangoTest::AutoCommand<&AutoCmdDev::cmd_void_void>("void_void"));
        cmds.push_back(new TangoTest::AutoCommand<&AutoCmdDev::cmd_ret_void>("ret_void"));
        cmds.push_back(new TangoTest::AutoCommand<&AutoCmdDev::cmd_void_arg>("void_arg"));
        cmds.push_back(new TangoTest::AutoCommand<&AutoCmdDev::cmd_ret_arg>("ret_arg"));
    }

  private:
    Tango::DevBoolean cmd_run;
    Tango::DevDouble value;
};

TANGO_TEST_AUTO_DEV_TMPL_INSTANTIATE(AutoCmdDev, 3)

SCENARIO("AutoCommand executes correctly")
{
    int idlver = GENERATE(TangoTest::idlversion(3));
    GIVEN("a device proxy to a simple IDLv" << idlver << " device")
    {
        TangoTest::Context ctx{"auto_command", "AutoCmdDev", idlver};
        auto device = ctx.get_proxy();

        REQUIRE(idlver == device->get_idl_version());

        AND_GIVEN("a no-arg, no-return command name and flag attribute name")
        {
            std::string cmd{"void_void"};
            std::string att{"cmd_run"};

            WHEN("we execute the command")
            {
                REQUIRE_NOTHROW(device->command_inout(cmd));

                THEN("we see that the flag was set")
                {
                    Tango::DeviceAttribute da;
                    REQUIRE_NOTHROW(da = device->read_attribute(att));
                    bool flag_value;
                    da >> flag_value;
                    REQUIRE(flag_value == true);
                }
            }
        }

        AND_GIVEN("a no-arg command that returns a value")
        {
            std::string cmd{"ret_void"};

            WHEN("we execute the command")
            {
                Tango::DeviceData dd;
                REQUIRE_NOTHROW(dd = device->command_inout(cmd));

                THEN("we get back the expected value")
                {
                    double r;
                    dd >> r;
                    REQUIRE(r == CMD_RET_VOID_RETURN_VALUE);
                }
            }
        }

        AND_GIVEN("a no-return command that accepts an argument and a value attribute")
        {
            std::string cmd{"void_arg"};
            std::string att{"value"};

            WHEN("we execute the command")
            {
                Tango::DeviceData dd;
                dd << CMD_VOID_ARG_TEST_VALUE;
                REQUIRE_NOTHROW(device->command_inout(cmd, dd));

                THEN("we get back the expected value")
                {
                    Tango::DeviceAttribute da;
                    REQUIRE_NOTHROW(da = device->read_attribute(att));
                    double att_value;
                    da >> att_value;
                    REQUIRE(att_value == CMD_VOID_ARG_TEST_VALUE);
                }
            }
        }

        AND_GIVEN("a command that returns a value passed as argument")
        {
            std::string cmd{"ret_arg"};

            WHEN("we execute the command")
            {
                Tango::DeviceData in;
                Tango::DeviceData out;
                in << CMD_RET_ARG_TEST_VALUE;
                REQUIRE_NOTHROW(out = device->command_inout(cmd, in));

                THEN("we get back the expected value")
                {
                    double r;
                    out >> r;
                    REQUIRE(r == CMD_RET_ARG_TEST_VALUE);
                }
            }
        }
    }
}