summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/test/TestJSONVariantParser.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 18:07:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 18:07:22 +0000
commitc04dcc2e7d834218ef2d4194331e383402495ae1 (patch)
tree7333e38d10d75386e60f336b80c2443c1166031d /xbmc/utils/test/TestJSONVariantParser.cpp
parentInitial commit. (diff)
downloadkodi-c04dcc2e7d834218ef2d4194331e383402495ae1.tar.xz
kodi-c04dcc2e7d834218ef2d4194331e383402495ae1.zip
Adding upstream version 2:20.4+dfsg.upstream/2%20.4+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'xbmc/utils/test/TestJSONVariantParser.cpp')
-rw-r--r--xbmc/utils/test/TestJSONVariantParser.cpp189
1 files changed, 189 insertions, 0 deletions
diff --git a/xbmc/utils/test/TestJSONVariantParser.cpp b/xbmc/utils/test/TestJSONVariantParser.cpp
new file mode 100644
index 0000000..b8556b0
--- /dev/null
+++ b/xbmc/utils/test/TestJSONVariantParser.cpp
@@ -0,0 +1,189 @@
+/*
+ * 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/JSONVariantParser.h"
+#include "utils/Variant.h"
+
+#include <gtest/gtest.h>
+
+TEST(TestJSONVariantParser, CannotParseNullptr)
+{
+ CVariant variant;
+ ASSERT_FALSE(CJSONVariantParser::Parse(nullptr, variant));
+}
+
+TEST(TestJSONVariantParser, CannotParseEmptyString)
+{
+ CVariant variant;
+ ASSERT_FALSE(CJSONVariantParser::Parse("", variant));
+ ASSERT_FALSE(CJSONVariantParser::Parse(std::string(), variant));
+}
+
+TEST(TestJSONVariantParser, CannotParseInvalidJson)
+{
+ CVariant variant;
+ ASSERT_FALSE(CJSONVariantParser::Parse("{", variant));
+ ASSERT_FALSE(CJSONVariantParser::Parse("}", variant));
+ ASSERT_FALSE(CJSONVariantParser::Parse("[", variant));
+ ASSERT_FALSE(CJSONVariantParser::Parse("]", variant));
+ ASSERT_FALSE(CJSONVariantParser::Parse("foo", variant));
+}
+
+TEST(TestJSONVariantParser, CanParseNull)
+{
+ CVariant variant;
+ ASSERT_TRUE(CJSONVariantParser::Parse("null", variant));
+ ASSERT_TRUE(variant.isNull());
+}
+
+TEST(TestJSONVariantParser, CanParseBoolean)
+{
+ CVariant variant;
+ ASSERT_TRUE(CJSONVariantParser::Parse("true", variant));
+ ASSERT_TRUE(variant.isBoolean());
+ ASSERT_TRUE(variant.asBoolean());
+
+ ASSERT_TRUE(CJSONVariantParser::Parse("false", variant));
+ ASSERT_TRUE(variant.isBoolean());
+ ASSERT_FALSE(variant.asBoolean());
+}
+
+TEST(TestJSONVariantParser, CanParseSignedInteger)
+{
+ CVariant variant;
+ ASSERT_TRUE(CJSONVariantParser::Parse("-1", variant));
+ ASSERT_TRUE(variant.isInteger());
+ ASSERT_EQ(-1, variant.asInteger());
+}
+
+TEST(TestJSONVariantParser, CanParseUnsignedInteger)
+{
+ CVariant variant;
+ ASSERT_TRUE(CJSONVariantParser::Parse("0", variant));
+ ASSERT_TRUE(variant.isUnsignedInteger());
+ ASSERT_EQ(0U, variant.asUnsignedInteger());
+
+ ASSERT_TRUE(CJSONVariantParser::Parse("1", variant));
+ ASSERT_TRUE(variant.isUnsignedInteger());
+ ASSERT_EQ(1U, variant.asUnsignedInteger());
+}
+
+TEST(TestJSONVariantParser, CanParseSignedInteger64)
+{
+ CVariant variant;
+ ASSERT_TRUE(CJSONVariantParser::Parse("-4294967296", variant));
+ ASSERT_TRUE(variant.isInteger());
+ ASSERT_EQ(-4294967296, variant.asInteger());
+}
+
+TEST(TestJSONVariantParser, CanParseUnsignedInteger64)
+{
+ CVariant variant;
+ ASSERT_TRUE(CJSONVariantParser::Parse("4294967296", variant));
+ ASSERT_TRUE(variant.isUnsignedInteger());
+ ASSERT_EQ(4294967296U, variant.asUnsignedInteger());
+}
+
+TEST(TestJSONVariantParser, CanParseDouble)
+{
+ CVariant variant;
+ ASSERT_TRUE(CJSONVariantParser::Parse("0.0", variant));
+ ASSERT_TRUE(variant.isDouble());
+ ASSERT_EQ(0.0, variant.asDouble());
+
+ ASSERT_TRUE(CJSONVariantParser::Parse("1.0", variant));
+ ASSERT_TRUE(variant.isDouble());
+ ASSERT_EQ(1.0, variant.asDouble());
+
+ ASSERT_TRUE(CJSONVariantParser::Parse("-1.0", variant));
+ ASSERT_TRUE(variant.isDouble());
+ ASSERT_EQ(-1.0, variant.asDouble());
+}
+
+TEST(TestJSONVariantParser, CanParseString)
+{
+ CVariant variant;
+ ASSERT_TRUE(CJSONVariantParser::Parse("\"\"", variant));
+ ASSERT_TRUE(variant.isString());
+ ASSERT_TRUE(variant.empty());
+
+ ASSERT_TRUE(CJSONVariantParser::Parse("\"foo\"", variant));
+ ASSERT_TRUE(variant.isString());
+ ASSERT_STREQ("foo", variant.asString().c_str());
+
+ ASSERT_TRUE(CJSONVariantParser::Parse("\"foo bar\"", variant));
+ ASSERT_TRUE(variant.isString());
+ ASSERT_STREQ("foo bar", variant.asString().c_str());
+}
+
+TEST(TestJSONVariantParser, CanParseObject)
+{
+ CVariant variant;
+ ASSERT_TRUE(CJSONVariantParser::Parse("{}", variant));
+ ASSERT_TRUE(variant.isObject());
+ ASSERT_TRUE(variant.empty());
+
+ variant.clear();
+ ASSERT_TRUE(CJSONVariantParser::Parse("{ \"foo\": \"bar\" }", variant));
+ ASSERT_TRUE(variant.isObject());
+ ASSERT_TRUE(variant.isMember("foo"));
+ ASSERT_TRUE(variant["foo"].isString());
+ ASSERT_STREQ("bar", variant["foo"].asString().c_str());
+
+ variant.clear();
+ ASSERT_TRUE(CJSONVariantParser::Parse("{ \"foo\": \"bar\", \"bar\": true }", variant));
+ ASSERT_TRUE(variant.isObject());
+ ASSERT_TRUE(variant.isMember("foo"));
+ ASSERT_TRUE(variant["foo"].isString());
+ ASSERT_STREQ("bar", variant["foo"].asString().c_str());
+ ASSERT_TRUE(variant.isMember("bar"));
+ ASSERT_TRUE(variant["bar"].isBoolean());
+ ASSERT_TRUE(variant["bar"].asBoolean());
+
+ variant.clear();
+ ASSERT_TRUE(CJSONVariantParser::Parse("{ \"foo\": { \"sub-foo\": \"bar\" } }", variant));
+ ASSERT_TRUE(variant.isObject());
+ ASSERT_TRUE(variant.isMember("foo"));
+ ASSERT_TRUE(variant["foo"].isObject());
+ ASSERT_TRUE(variant["foo"].isMember("sub-foo"));
+ ASSERT_TRUE(variant["foo"]["sub-foo"].isString());
+ ASSERT_STREQ("bar", variant["foo"]["sub-foo"].asString().c_str());
+}
+
+TEST(TestJSONVariantParser, CanParseArray)
+{
+ CVariant variant;
+ ASSERT_TRUE(CJSONVariantParser::Parse("[]", variant));
+ ASSERT_TRUE(variant.isArray());
+ ASSERT_TRUE(variant.empty());
+
+ variant.clear();
+ ASSERT_TRUE(CJSONVariantParser::Parse("[ true ]", variant));
+ ASSERT_TRUE(variant.isArray());
+ ASSERT_EQ(1U, variant.size());
+ ASSERT_TRUE(variant[0].isBoolean());
+ ASSERT_TRUE(variant[0].asBoolean());
+
+ variant.clear();
+ ASSERT_TRUE(CJSONVariantParser::Parse("[ true, \"foo\" ]", variant));
+ ASSERT_TRUE(variant.isArray());
+ ASSERT_EQ(2U, variant.size());
+ ASSERT_TRUE(variant[0].isBoolean());
+ ASSERT_TRUE(variant[0].asBoolean());
+ ASSERT_TRUE(variant[1].isString());
+ ASSERT_STREQ("foo", variant[1].asString().c_str());
+
+ variant.clear();
+ ASSERT_TRUE(CJSONVariantParser::Parse("[ { \"foo\": \"bar\" } ]", variant));
+ ASSERT_TRUE(variant.isArray());
+ ASSERT_EQ(1U, variant.size());
+ ASSERT_TRUE(variant[0].isObject());
+ ASSERT_TRUE(variant[0].isMember("foo"));
+ ASSERT_TRUE(variant[0]["foo"].isString());
+ ASSERT_STREQ("bar", variant[0]["foo"].asString().c_str());
+}