view cpp/json_util/test/main.cpp @ 656:734ce3a26a58

json_util: upgrade to 3.5.0 and add string_view overload
author David Demelier <markand@malikania.fr>
date Mon, 21 Jan 2019 20:45:02 +0100
parents 87e1f4c7da76
children 66015e773085
line wrap: on
line source

/*
 * main.cpp -- main test file for json_util
 *
 * Copyright (c) 2019 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#define BOOST_TEST_MODULE "json_util"
#include <boost/test/unit_test.hpp>

#include <json_util.hpp>

namespace json_util {

class test_fixture {
protected:
	deserializer document_{
		{ "boolean",    true    },
		{ "int",        -1234   },
		{ "uint",       1234U   },
		{ "string",     "str"   }
	};
};

BOOST_FIXTURE_TEST_SUITE(test_fixture_suite, test_fixture)

BOOST_AUTO_TEST_SUITE(get)

BOOST_AUTO_TEST_CASE(boolean)
{
	const auto v = document_.get<bool>("boolean");

	BOOST_TEST(v.has_value());
	BOOST_TEST(*v);
	BOOST_TEST(!document_.get<bool>("int"));
	BOOST_TEST(!document_.get<bool>("int"));
	BOOST_TEST(!document_.get<bool>("uint"));
	BOOST_TEST(!document_.get<bool>("string"));
}

BOOST_AUTO_TEST_CASE(integer)
{
	const auto v = document_.get<int>("int");

	BOOST_TEST(v.has_value());
	BOOST_TEST(*v == -1234);
	BOOST_TEST(!document_.get<int>("bool"));
	BOOST_TEST(!document_.get<int>("string"));
}

BOOST_AUTO_TEST_CASE(unsigned_integer)
{
	const auto v = document_.get<unsigned>("uint");

	BOOST_TEST(v.has_value());
	BOOST_TEST(*v == 1234U);
	BOOST_TEST(!document_.get<unsigned>("bool"));
	BOOST_TEST(!document_.get<unsigned>("int"));
	BOOST_TEST(!document_.get<unsigned>("string"));
}

BOOST_AUTO_TEST_CASE(string)
{
	const auto v = document_.get<std::string>("string");

	BOOST_TEST(v.has_value());
	BOOST_TEST(*v == "str");
	BOOST_TEST(!document_.get<std::string>("bool"));
	BOOST_TEST(!document_.get<std::string>("int"));
	BOOST_TEST(!document_.get<std::string>("uint"));
}

BOOST_AUTO_TEST_CASE(string_view)
{
	const auto v = document_.get<std::string_view>("string");

	BOOST_TEST(v.has_value());
	BOOST_TEST(*v == "str");
	BOOST_TEST(!document_.get<std::string_view>("bool"));
	BOOST_TEST(!document_.get<std::string_view>("int"));
	BOOST_TEST(!document_.get<std::string_view>("uint"));
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(optional_not_present)

BOOST_AUTO_TEST_CASE(boolean)
{
	BOOST_TEST(*document_.optional<bool>("undefined", true));
	BOOST_TEST(!*document_.optional<bool>("undefined", false));
}

BOOST_AUTO_TEST_CASE(integer)
{
	BOOST_TEST(*document_.optional<int>("undefined", 1234) == 1234);
}

BOOST_AUTO_TEST_CASE(unsigned_integer)
{
	BOOST_TEST(*document_.optional<unsigned>("undefined", 1234U) == 1234U);
}

BOOST_AUTO_TEST_CASE(string)
{
	BOOST_TEST(*document_.optional<std::string>("undefined", "foo") == "foo");
}

BOOST_AUTO_TEST_CASE(string_view)
{
	BOOST_TEST(*document_.optional<std::string_view>("undefined", "foo") == "foo");
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(optional_invalid)

BOOST_AUTO_TEST_CASE(boolean)
{
	BOOST_TEST(!document_.optional<bool>("int", true));
	BOOST_TEST(!document_.optional<bool>("uint", true));
	BOOST_TEST(!document_.optional<bool>("string", true));
}

BOOST_AUTO_TEST_CASE(integer)
{
	BOOST_TEST(!document_.optional<int>("boolean", 1234));
	BOOST_TEST(!document_.optional<int>("string", 1234));
}

BOOST_AUTO_TEST_CASE(unsigned_integer)
{
	BOOST_TEST(!document_.optional<unsigned>("boolean", 1234));
	BOOST_TEST(!document_.optional<unsigned>("int", 1234));
	BOOST_TEST(!document_.optional<unsigned>("string", 1234));
}

BOOST_AUTO_TEST_CASE(string)
{
	BOOST_TEST(!document_.optional<std::string>("boolean", "foo"));
	BOOST_TEST(!document_.optional<std::string>("int", "foo"));
	BOOST_TEST(!document_.optional<std::string>("uint", "foo"));
}

BOOST_AUTO_TEST_CASE(string_view)
{
	BOOST_TEST(!document_.optional<std::string_view>("boolean", "foo"));
	BOOST_TEST(!document_.optional<std::string_view>("int", "foo"));
	BOOST_TEST(!document_.optional<std::string_view>("uint", "foo"));
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(get_or)

BOOST_AUTO_TEST_CASE(boolean)
{
	BOOST_TEST(document_.get_or<bool>("boolean", false));
	BOOST_TEST(document_.get_or<bool>("int", true));
	BOOST_TEST(document_.get_or<bool>("undefined", true));
}

BOOST_AUTO_TEST_CASE(integer)
{
	BOOST_TEST(document_.get_or<int>("int", 3320) == -1234);
	BOOST_TEST(document_.get_or<int>("boolean", 3320) == 3320);
	BOOST_TEST(document_.get_or<int>("undefined", 3320) == 3320);
}

BOOST_AUTO_TEST_CASE(unsigned_integer)
{
	BOOST_TEST(document_.get_or<unsigned>("uint", 3320U) == 1234U);
	BOOST_TEST(document_.get_or<unsigned>("boolean", 3320U) == 3320);
	BOOST_TEST(document_.get_or<unsigned>("undefined", 3320U) == 3320);
}

BOOST_AUTO_TEST_CASE(string)
{
	BOOST_TEST(document_.get_or<std::string>("string", "hello") == "str");
	BOOST_TEST(document_.get_or<std::string>("boolean", "hello") == "hello");
	BOOST_TEST(document_.get_or<std::string>("undefined", "hello") == "hello");
}

BOOST_AUTO_TEST_CASE(string_view)
{
	BOOST_TEST(document_.get_or<std::string_view>("string", "hello") == "str");
	BOOST_TEST(document_.get_or<std::string_view>("boolean", "hello") == "hello");
	BOOST_TEST(document_.get_or<std::string_view>("undefined", "hello") == "hello");
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()

} // !json_util