summaryrefslogtreecommitdiff
path: root/src/test/DBusVariantTest.cpp
blob: a05ae03463db3f24aa72b9dbe0f8cbeb91221e7e (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
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
174
/* Copyright (C) 2013 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <gtest/gtest.h>
#include <CommonAPI/SerializableVariant.h>

using namespace CommonAPI;

class VariantTest: public ::testing::Test {

  protected:
    typedef Variant<int, double, std::string> BasicVariantType;

    void SetUp() {
        fromInt = 5;
        fromDouble = 12.344;
        fromString = "123abcsadfaljkawlöfasklöerklöfjasklfjysklfjaskfjsklösdfdko4jdfasdjioögjopefgip3rtgjiprg!";
    }

    void TearDown() {
    }

    int fromInt;
    double fromDouble;
    std::string fromString;
};

TEST_F(VariantTest, HandlesInts) {
    BasicVariantType myVariant(fromInt);

    EXPECT_TRUE(myVariant.isType<int>());
    EXPECT_FALSE(myVariant.isType<double>());
    EXPECT_FALSE(myVariant.isType<std::string>());

    const int myInt = myVariant.get<int>();
    EXPECT_EQ(myInt, fromInt);

    EXPECT_ANY_THROW(myVariant.get<double>());
    EXPECT_ANY_THROW(myVariant.get<std::string>());
}

TEST_F(VariantTest, HandlesDoubles) {
    BasicVariantType myVariant(fromDouble);

    EXPECT_FALSE(myVariant.isType<int>());
    EXPECT_TRUE(myVariant.isType<double>());
    EXPECT_FALSE(myVariant.isType<std::string>());

    EXPECT_ANY_THROW(myVariant.get<int>());

    const double myDouble = myVariant.get<double>();
    EXPECT_EQ(myDouble, fromDouble);

    EXPECT_ANY_THROW(myVariant.get<std::string>());
}

TEST_F(VariantTest, HandlesStrings) {
    BasicVariantType myVariant(fromString);

    EXPECT_FALSE(myVariant.isType<int>());
    EXPECT_FALSE(myVariant.isType<double>());
    EXPECT_TRUE(myVariant.isType<std::string>());

    EXPECT_ANY_THROW(myVariant.get<int>());
    EXPECT_ANY_THROW(myVariant.get<double>());

    const std::string myString = myVariant.get<std::string>();
    EXPECT_EQ(myString, fromString);
}

TEST_F(VariantTest, HandlesStringVectors) {
    typedef Variant<int, double, std::vector<std::string>> VectorVariantType;

    std::vector<std::string> testVector;
    for(int i = 0; i < 10; i++) {
        testVector.push_back(fromString);
    }

    VectorVariantType vectorVariant(testVector);
    const std::vector<std::string> resultVector = vectorVariant.get<std::vector<std::string>>();
    EXPECT_EQ(resultVector, testVector);
}

TEST_F(VariantTest, HandlesAssignment) {
    Variant<int, double, std::string> myVariant = fromInt;

    myVariant = fromString;

    EXPECT_FALSE(myVariant.isType<int>());
    EXPECT_FALSE(myVariant.isType<double>());
    EXPECT_TRUE(myVariant.isType<std::string>());

    EXPECT_ANY_THROW(myVariant.get<int>());

    const std::string myString = myVariant.get<std::string>();
    EXPECT_EQ(myString, fromString);
}

TEST_F(VariantTest, HandlesVariantConstructionByAssignment) {
    Variant<int, double, std::string> myVariant = fromInt;

    EXPECT_TRUE(myVariant.isType<int>());
    EXPECT_FALSE(myVariant.isType<double>());
    EXPECT_FALSE(myVariant.isType<std::string>());

    const int myInt = myVariant.get<int>();
    EXPECT_EQ(myInt, fromInt);
}

TEST_F(VariantTest, HandlesVariantCopy) {
    BasicVariantType myVariant(fromInt);
    Variant<int, double, std::string> myVariantAssigned = myVariant;

    EXPECT_TRUE(myVariantAssigned.isType<int>());
    EXPECT_FALSE(myVariantAssigned.isType<double>());
    EXPECT_FALSE(myVariantAssigned.isType<std::string>());

    const int myInt2 = myVariantAssigned.get<int>();
    EXPECT_EQ(myInt2, fromInt);

    Variant<int, double, std::string> myVariantCopied(myVariant);
    EXPECT_EQ(myVariant, myVariantCopied);

    EXPECT_TRUE(myVariantCopied.isType<int>());
    EXPECT_FALSE(myVariantCopied.isType<double>());
    EXPECT_FALSE(myVariantCopied.isType<std::string>());

    const int& myIntCopy = myVariantCopied.get<int>();
    EXPECT_EQ(myIntCopy, fromInt);
}

TEST_F(VariantTest, HandlesVariantsWithinVariants) {
    typedef Variant<int, double, BasicVariantType > VariantInVariantType;
    BasicVariantType fromInnerVariant(fromInt);
    VariantInVariantType myOuterVariant(fromInnerVariant);

    EXPECT_FALSE(myOuterVariant.isType<int>());
    EXPECT_FALSE(myOuterVariant.isType<double>());
    EXPECT_TRUE(myOuterVariant.isType<BasicVariantType>());

    EXPECT_ANY_THROW(myOuterVariant.get<int>());
    EXPECT_ANY_THROW(myOuterVariant.get<double>());

    const BasicVariantType myInnerVariant = myOuterVariant.get<BasicVariantType>();
    EXPECT_EQ(fromInnerVariant, myInnerVariant);
}


TEST_F(VariantTest, VariantStringArray) {

    std::vector<std::string> testVector;
    testVector.push_back("Test 1");
    testVector.push_back("Test 2");
    testVector.push_back("Test 3");
    testVector.push_back("Test 4");

    CommonAPI::Variant<int, double, std::vector<std::string>>* vectorVariant = new CommonAPI::Variant<int, double, std::vector<std::string>>(testVector);

    std::vector<std::string> readVector = vectorVariant->get<std::vector<std::string> >();
    EXPECT_EQ(readVector, testVector);

    delete vectorVariant;
}

#ifndef WIN32
int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
#endif