File: stl_codec_test.cpp

package info (click to toggle)
dbus-cpp 5.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,208 kB
  • sloc: cpp: 9,427; ansic: 2,012; xml: 1,156; makefile: 14; sh: 2
file content (102 lines) | stat: -rw-r--r-- 3,053 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
/*
 * Copyright © 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */

#include <core/dbus/dbus.h>
#include <core/dbus/message_streaming_operators.h>

#include <core/dbus/types/variant.h>

#include <core/dbus/types/stl/map.h>
#include <core/dbus/types/stl/string.h>
#include <core/dbus/types/stl/tuple.h>

#include <gtest/gtest.h>

#include <memory>

namespace dbus = core::dbus;

namespace
{
std::shared_ptr<core::dbus::Message> a_method_call()
{
    return dbus::Message::make_method_call(
                dbus::DBus::name(),
                dbus::DBus::path(),
                dbus::DBus::interface(),
                "ListNames");
}
}

TEST(CodecForTuple, encoding_of_tuples_works)
{
    typedef std::tuple<int, int> TupleType;
    auto msg = a_method_call();
    auto writer = msg->writer();
    TupleType t1(42, 42);
    core::dbus::Codec<TupleType>::encode_argument(writer, t1);
    EXPECT_EQ(msg->signature(),
              core::dbus::helper::TypeMapper<TupleType>::signature());
    TupleType t2;
    auto reader = msg->reader();
    core::dbus::Codec<TupleType>::decode_argument(reader, t2);

    EXPECT_EQ(std::get<0>(t1), std::get<0>(t2));
    EXPECT_EQ(std::get<1>(t1), std::get<1>(t2));
}

TEST(CodecForMaps, DictionaryMappingToVariantsIsEncodedAndDecodedCorrectly)
{
    namespace dbus = core::dbus;

    auto msg = a_method_call();

    {
        auto writer = msg->writer();
        auto array = writer.open_array(dbus::types::Signature{"{sv}"});
        for(unsigned int i = 0; i < 5; i++)
        {
            auto entry = array.open_dict_entry();
            {
                auto key = std::to_string(i);
                entry.push_stringn(key.c_str(), key.size());
                auto variant = entry.open_variant(dbus::types::Signature{dbus::helper::TypeMapper<std::uint32_t>::signature()});
                {
                    variant.push_uint32(i);

                } entry.close_variant(std::move(variant));
            } array.close_dict_entry(std::move(entry));
        } writer.close_array(std::move(array));
    }

    unsigned int counter = 0;

    std::map<std::string, dbus::types::Variant> result;
    msg->reader() >> result;

    EXPECT_EQ(std::uint32_t(5), result.size());

    for (const auto& element : result)
    {
        EXPECT_EQ(std::to_string(counter), element.first);
        EXPECT_EQ(counter, element.second.as<std::uint32_t>());

        counter++;
    }
}