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
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "utils/JSONVariantWriter.h"
#include "utils/Variant.h"
#include <gtest/gtest.h>
TEST(TestJSONVariantWriter, CanWriteNull)
{
CVariant variant;
std::string str;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("null", str.c_str());
}
TEST(TestJSONVariantWriter, CanWriteBoolean)
{
CVariant variant(true);
std::string str;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("true", str.c_str());
variant = false;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("false", str.c_str());
}
TEST(TestJSONVariantWriter, CanWriteSignedInteger)
{
CVariant variant(-1);
std::string str;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("-1", str.c_str());
}
TEST(TestJSONVariantWriter, CanWriteUnsignedInteger)
{
CVariant variant(0);
std::string str;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("0", str.c_str());
variant = 1;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("1", str.c_str());
}
TEST(TestJSONVariantWriter, CanWriteSignedInteger64)
{
CVariant variant(static_cast<int64_t>(-4294967296LL));
std::string str;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("-4294967296", str.c_str());
}
TEST(TestJSONVariantWriter, CanWriteUnsignedInteger64)
{
CVariant variant(static_cast<int64_t>(4294967296LL));
std::string str;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("4294967296", str.c_str());
}
TEST(TestJSONVariantWriter, CanWriteDouble)
{
CVariant variant(0.0);
std::string str;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("0.0", str.c_str());
variant = 1.0;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("1.0", str.c_str());
variant = -1.0;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("-1.0", str.c_str());
}
TEST(TestJSONVariantWriter, CanWriteString)
{
CVariant variant("");
std::string str;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("\"\"", str.c_str());
variant = "foo";
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("\"foo\"", str.c_str());
variant = "foo bar";
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("\"foo bar\"", str.c_str());
}
TEST(TestJSONVariantWriter, CanWriteObject)
{
CVariant variant(CVariant::VariantTypeObject);
std::string str;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("{}", str.c_str());
variant.clear();
variant["foo"] = "bar";
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("{\n\t\"foo\": \"bar\"\n}", str.c_str());
variant.clear();
variant["foo"] = "bar";
variant["bar"] = true;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("{\n\t\"bar\": true,\n\t\"foo\": \"bar\"\n}", str.c_str());
variant.clear();
variant["foo"]["sub-foo"] = "bar";
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("{\n\t\"foo\": {\n\t\t\"sub-foo\": \"bar\"\n\t}\n}", str.c_str());
}
TEST(TestJSONVariantWriter, CanWriteArray)
{
CVariant variant(CVariant::VariantTypeArray);
std::string str;
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("[]", str.c_str());
variant.clear();
variant.push_back(true);
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("[\n\ttrue\n]", str.c_str());
variant.clear();
variant.push_back(true);
variant.push_back("foo");
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("[\n\ttrue,\n\t\"foo\"\n]", str.c_str());
variant.clear();
CVariant obj(CVariant::VariantTypeObject);
obj["foo"] = "bar";
variant.push_back(obj);
ASSERT_TRUE(CJSONVariantWriter::Write(variant, str, false));
ASSERT_STREQ("[\n\t{\n\t\t\"foo\": \"bar\"\n\t}\n]", str.c_str());
}
|