summaryrefslogtreecommitdiff
path: root/test/core/script/VariantTest.cpp
blob: f229e3a41122cc62a40a8cacbe3be32303aec856 (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
/*
    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/script/Variant.hpp>
#include <core/script/Function.hpp>
#include <core/script/Object.hpp>

namespace ousia {
namespace script {

TEST(Variant, getBooleanValue)
{
	ASSERT_TRUE((Variant{true}).getBooleanValue());
	ASSERT_FALSE((Variant{false}).getBooleanValue());
	ASSERT_FALSE((Variant{(int64_t)0}).getBooleanValue());
	ASSERT_TRUE((Variant{(int64_t)1}).getBooleanValue());
	ASSERT_FALSE((Variant{0.0}).getBooleanValue());
	ASSERT_TRUE((Variant{1.2}).getBooleanValue());
	ASSERT_FALSE((Variant{""}).getBooleanValue());
}

TEST(Variant, getIntegerValue)
{
	Variant vi{(int64_t)42};
	Variant vf{42.0};

	ASSERT_EQ(42, vi.getIntegerValue());
	ASSERT_EQ(42, vf.getIntegerValue());
	ASSERT_EQ(1, (Variant{true}).getIntegerValue());
	ASSERT_EQ(0, (Variant{false}).getIntegerValue());
}

TEST(Variant, getNumberValue)
{
	Variant vi{(int64_t)42};
	Variant vf{42.5};

	ASSERT_EQ(42, vi.getNumberValue());
	ASSERT_EQ(42.5, vf.getNumberValue());
	ASSERT_EQ(1.0, (Variant{true}).getIntegerValue());
	ASSERT_EQ(0.0, (Variant{false}).getIntegerValue());
}

TEST(Variant, getStringValue)
{
	Variant v{"hello world"};
	ASSERT_EQ("hello world", v.getStringValue());
}

TEST(Variant, getArrayValue)
{
	Variant v{{"test1", (int64_t)42}};
	ASSERT_EQ(2, v.getArrayValue().size());
	ASSERT_EQ("test1", v.getArrayValue()[0].getStringValue());
	ASSERT_EQ(42, v.getArrayValue()[1].getIntegerValue());
}

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

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

	ASSERT_EQ("entry1", (*map.find("key1")).second.getStringValue());
	ASSERT_EQ("entry2", (*map.find("key2")).second.getStringValue());
}

TEST(Variant, getFunctionValue)
{
	int64_t i = 0;
	HostFunction f{[](const std::vector<Variant> &args, void *data) {
		*((int64_t*)data) = args[0].getIntegerValue();
		return Variant{"Hello World"};
	}, &i};

	Variant v{&f};
	ASSERT_TRUE(v.getFunctionValue() != nullptr);
	ASSERT_EQ("Hello World", v.getFunctionValue()->call({{(int64_t)42}}).getStringValue());
	ASSERT_EQ(42, i);
}


}
}