File: catch2_attr_manip.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 (105 lines) | stat: -rw-r--r-- 3,304 bytes parent folder | download | duplicates (4)
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
#include "catch2_common.h"

static constexpr double SERVER_VALUE = 8.888;

template <class Base>
class AttrManipDev : public Base
{
  public:
    using Base::Base;

    ~AttrManipDev() override { }

    void init_device() override { }

    void read_attribute(Tango::Attribute &att)
    {
        attr_dq_double = SERVER_VALUE;
        att.set_value_date_quality(&attr_dq_double, std::chrono::system_clock::now(), Tango::ATTR_VALID);
    }

    static void attribute_factory(std::vector<Tango::Attr *> &attrs)
    {
        attrs.push_back(new TangoTest::AutoAttr<&AttrManipDev::read_attribute>("attr_dq_db", Tango::DEV_DOUBLE));
    }

  private:
    Tango::DevDouble attr_dq_double;
};

TANGO_TEST_AUTO_DEV_TMPL_INSTANTIATE(AttrManipDev, 3)

SCENARIO("attribute formatting can be controlled")
{
    int idlver = GENERATE(TangoTest::idlversion(3));
    GIVEN("a device proxy to a simple IDLv" << idlver << " device")
    {
        TangoTest::Context ctx{"attr_manip", "AttrManipDev", idlver};
        auto device = ctx.get_proxy();

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

        AND_GIVEN("an attribute name and configuration")
        {
            std::string att{"attr_dq_db"};

            Tango::AttributeInfo sta_ai;
            REQUIRE_NOTHROW(sta_ai = device->get_attribute_config(att));

            WHEN("we read the attribute")
            {
                Tango::DeviceAttribute da;
                REQUIRE_NOTHROW(da = device->read_attribute(att));

                THEN("the read value matches the value on the server")
                {
                    double att_value;
                    da >> att_value;
                    REQUIRE(att_value == SERVER_VALUE);
                }
            }

            struct TestData
            {
                const char *name;
                const char *format;
                const char *expected;
            };

            auto data = GENERATE((TestData{"scientfic", "scientific;uppercase;setprecision(2)", "8.89E+00"}),
                                 (TestData{"fixed-width", "fixed;setprecision(2)", "8.89"}));

            AND_GIVEN("a " << data.name << " format specification")
            {
                std::string format{data.format};

                WHEN("we set the attribute configuration with the format")
                {
                    Tango::AttributeInfoList new_ai;
                    new_ai.push_back(sta_ai);
                    new_ai[0].format = format;

                    REQUIRE_NOTHROW(device->set_attribute_config(new_ai));

                    THEN("the format read back matches")
                    {
                        Tango::AttributeInfo sta_ai_2;
                        REQUIRE_NOTHROW(sta_ai_2 = device->get_attribute_config(att));
                        REQUIRE(sta_ai_2.format == format);
                    }
                }

                WHEN("we format a value")
                {
                    std::stringstream out;
                    REQUIRE_NOTHROW(out << Tango::AttrManip(format) << SERVER_VALUE);

                    THEN("the rendered string is in " << data.name << " notation")
                    {
                        REQUIRE(out.str() == data.expected);
                    }
                }
            }
        }
    }
}