Mercurial > code
view cpp/json_util/test/main.cpp @ 650:ff73f2dd1c82
json_util: split and style
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 23 Oct 2018 21:10:00 +0200 |
parents | 3129f59002d2 |
children | 6739771fba29 |
line wrap: on
line source
/* * main.cpp -- main test file for json_util * * Copyright (c) 2018 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_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_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_SUITE_END() BOOST_AUTO_TEST_SUITE_END() } // !json_util