summaryrefslogtreecommitdiff
path: root/test/core/variant/VariantTest.cpp
blob: 3a23887c06087c16eaedf9c33b6b5a85159d9d47 (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
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <gtest/gtest.h>

#include <core/variant/Variant.hpp>

namespace ousia {

TEST(Variant, nullValue)
{
	Variant v;
	ASSERT_TRUE(v.isNull());

	v = 1;
	ASSERT_FALSE(v.isNull());

	v = nullptr;
	ASSERT_TRUE(v.isNull());
}

TEST(Variant, booleanValue)
{
	Variant v{true};
	ASSERT_TRUE(v.isBool());
	ASSERT_TRUE(v.asBool());

	v = false;
	ASSERT_TRUE(v.isBool());
	ASSERT_FALSE(v.asBool());

	v.setBool(true);
	ASSERT_TRUE(v.isBool());
	ASSERT_TRUE(v.asBool());

	v = nullptr;
	ASSERT_FALSE(v.isBool());
}

TEST(Variant, intValue)
{
	Variant v{42};
	ASSERT_TRUE(v.isInt());
	ASSERT_EQ(42, v.asInt());

	v = 43;
	ASSERT_TRUE(v.isInt());
	ASSERT_EQ(43, v.asInt());

	v = false;
	ASSERT_FALSE(v.isInt());
}

TEST(Variant, doubleValue)
{
	Variant v{42.5};
	ASSERT_TRUE(v.isDouble());
	ASSERT_EQ(42.5, v.asDouble());

	v = 42;
	ASSERT_FALSE(v.isDouble());

	v = 43.5;
	ASSERT_TRUE(v.isDouble());
	ASSERT_EQ(43.5, v.asDouble());
}

TEST(Variant, stringValue)
{
	Variant v{"Hello World"};
	ASSERT_TRUE(v.isString());
	ASSERT_EQ("Hello World", v.asString());

	v = "Goodbye World";
	ASSERT_TRUE(v.isString());
	ASSERT_EQ("Goodbye World", v.asString());

	v = 42;
	ASSERT_FALSE(v.isString());
}

TEST(Variant, arrayValue)
{
	const Variant v{{"test1", 42}};
	ASSERT_EQ(2, v.asArray().size());
	ASSERT_EQ("test1", v.asArray()[0].asString());
	ASSERT_EQ(42, v.asArray()[1].asInt());
}

TEST(Variant, mapValue)
{
	const Variant v{{{"key1", "entry1"}, {"key2", "entry2"}}};

	auto map = v.asMap();
	ASSERT_EQ(2, map.size());

	ASSERT_EQ("entry1", map.find("key1")->second.asString());
	ASSERT_EQ("entry2", map.find("key2")->second.asString());

	const Variant v2{{{"key1", Variant::arrayType{1, 2, 3}}, {"key2", "entry2"}}};
}


}