/*----------------------------------------------------------------------------*/ /* Modifications Copyright (c) FIRST 2017. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ /*----------------------------------------------------------------------------*/ /* __ _____ _____ _____ __| | __| | | | JSON for Modern C++ (test suite) | | |__ | | | | | | version 2.1.1 |_____|_____|_____|_|___| https://github.com/nlohmann/json Licensed under the MIT License . Copyright (c) 2013-2017 Niels Lohmann . Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "gtest/gtest.h" #include "unit-json.h" using wpi::json; using wpi::JsonTest; #include //#include #include #include #include #include template class JsonGetObjectTest : public ::testing::Test { public: JsonGetObjectTest() : j(o_reference) {} protected: json::object_t o_reference = {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, {"null", nullptr}, {"string", "Hello world"} }; json j; }; typedef ::testing::Types< json::object_t , std::map , std::multimap , std::unordered_map , std::unordered_multimap > JsonGetObjectTestTypes; TYPED_TEST_CASE(JsonGetObjectTest, JsonGetObjectTestTypes); TYPED_TEST(JsonGetObjectTest, Explicit) { TypeParam o = (this->j).template get(); EXPECT_EQ(json(o), this->j); } TYPED_TEST(JsonGetObjectTest, Implicit) { TypeParam o = this->j; EXPECT_EQ(json(o), this->j); } // exception in case of a non-object type TEST(JsonGetObjectExceptionTest, TypeError) { EXPECT_THROW_MSG(json(json::value_t::null).get(), json::type_error, "[json.exception.type_error.302] type must be object, but is null"); EXPECT_THROW_MSG(json(json::value_t::array).get(), json::type_error, "[json.exception.type_error.302] type must be object, but is array"); EXPECT_THROW_MSG(json(json::value_t::string).get(), json::type_error, "[json.exception.type_error.302] type must be object, but is string"); EXPECT_THROW_MSG(json(json::value_t::boolean).get(), json::type_error, "[json.exception.type_error.302] type must be object, but is boolean"); EXPECT_THROW_MSG(json(json::value_t::number_integer).get(), json::type_error, "[json.exception.type_error.302] type must be object, but is number"); EXPECT_THROW_MSG(json(json::value_t::number_unsigned).get(), json::type_error, "[json.exception.type_error.302] type must be object, but is number"); EXPECT_THROW_MSG(json(json::value_t::number_float).get(), json::type_error, "[json.exception.type_error.302] type must be object, but is number"); } template class JsonGetArrayTest : public ::testing::Test { public: JsonGetArrayTest() : j(a_reference) {} protected: json::array_t a_reference {json(1), json(1u), json(2.2), json(false), json("string"), json()}; json j; }; typedef ::testing::Types, /*std::forward_list,*/ std::vector, std::deque> JsonGetArrayTestTypes; TYPED_TEST_CASE(JsonGetArrayTest, JsonGetArrayTestTypes); TYPED_TEST(JsonGetArrayTest, Explicit) { TypeParam a = (this->j).template get(); EXPECT_EQ(json(a), this->j); } TYPED_TEST(JsonGetArrayTest, Implicit) { TypeParam a = this->j; EXPECT_EQ(json(a), this->j); } #if !defined(JSON_NOEXCEPTION) // reserve is called on containers that supports it TEST(JsonGetArrayAdditionalTest, ExplicitStdVectorReserve) { json::array_t a_reference {json(1), json(1u), json(2.2), json(false), json("string"), json()}; json j(a_reference); // making the call to from_json throw in order to check capacity std::vector v; EXPECT_THROW(wpi::from_json(j, v), json::type_error); EXPECT_EQ(v.capacity(), j.size()); // make sure all values are properly copied std::vector v2 = json({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); EXPECT_EQ(v2.size(), 10u); } #endif // built-in arrays TEST(JsonGetArrayAdditionalTest, ExplicitBuiltinArray) { const char str[] = "a string"; const int nbs[] = {0, 1, 2}; json j2 = nbs; json j3 = str; auto v = j2.get>(); auto s = j3.get(); EXPECT_TRUE(std::equal(v.begin(), v.end(), std::begin(nbs))); EXPECT_EQ(s, str); } #if 0 TEST(JsonGetArrayExceptionTest, ForwardList) { EXPECT_THROW_MSG(json(json::value_t::null).get>(), json::type_error, "[json.exception.type_error.302] type must be array, but is null"); } #endif TEST(JsonGetArrayExceptionTest, StdVector) { EXPECT_THROW_MSG(json(json::value_t::null).get>(), json::type_error, "[json.exception.type_error.302] type must be array, but is null"); } // exception in case of a non-array type TEST(JsonGetArrayExceptionTest, TypeError) { EXPECT_THROW_MSG(json(json::value_t::object).get>(), json::type_error, "[json.exception.type_error.302] type must be array, but is object"); EXPECT_THROW_MSG(json(json::value_t::null).get(), json::type_error, "[json.exception.type_error.302] type must be array, but is null"); EXPECT_THROW_MSG(json(json::value_t::object).get(), json::type_error, "[json.exception.type_error.302] type must be array, but is object"); EXPECT_THROW_MSG(json(json::value_t::string).get(), json::type_error, "[json.exception.type_error.302] type must be array, but is string"); EXPECT_THROW_MSG(json(json::value_t::boolean).get(), json::type_error, "[json.exception.type_error.302] type must be array, but is boolean"); EXPECT_THROW_MSG(json(json::value_t::number_integer).get(), json::type_error, "[json.exception.type_error.302] type must be array, but is number"); EXPECT_THROW_MSG(json(json::value_t::number_unsigned).get(), json::type_error, "[json.exception.type_error.302] type must be array, but is number"); EXPECT_THROW_MSG(json(json::value_t::number_float).get(), json::type_error, "[json.exception.type_error.302] type must be array, but is number"); } template class JsonGetStringTest : public ::testing::Test { public: JsonGetStringTest() : j(s_reference) {} protected: json::string_t s_reference {"Hello world"}; json j; }; typedef ::testing::Types JsonGetStringTestTypes; TYPED_TEST_CASE(JsonGetStringTest, JsonGetStringTestTypes); TYPED_TEST(JsonGetStringTest, Explicit) { TypeParam s = (this->j).template get(); EXPECT_EQ(json(s), this->j); } TYPED_TEST(JsonGetStringTest, Implicit) { TypeParam s = this->j; EXPECT_EQ(json(s), this->j); } // exception in case of a non-string type TEST(JsonGetStringExceptionTest, TypeError) { EXPECT_THROW_MSG(json(json::value_t::null).get(), json::type_error, "[json.exception.type_error.302] type must be string, but is null"); EXPECT_THROW_MSG(json(json::value_t::object).get(), json::type_error, "[json.exception.type_error.302] type must be string, but is object"); EXPECT_THROW_MSG(json(json::value_t::array).get(), json::type_error, "[json.exception.type_error.302] type must be string, but is array"); EXPECT_THROW_MSG(json(json::value_t::boolean).get(), json::type_error, "[json.exception.type_error.302] type must be string, but is boolean"); EXPECT_THROW_MSG(json(json::value_t::number_integer).get(), json::type_error, "[json.exception.type_error.302] type must be string, but is number"); EXPECT_THROW_MSG(json(json::value_t::number_unsigned).get(), json::type_error, "[json.exception.type_error.302] type must be string, but is number"); EXPECT_THROW_MSG(json(json::value_t::number_float).get(), json::type_error, "[json.exception.type_error.302] type must be string, but is number"); } template class JsonGetBooleanTest : public ::testing::Test { public: JsonGetBooleanTest() : j(b_reference) {} protected: json::boolean_t b_reference {true}; json j; }; typedef ::testing::Types JsonGetBooleanTestTypes; TYPED_TEST_CASE(JsonGetBooleanTest, JsonGetBooleanTestTypes); TYPED_TEST(JsonGetBooleanTest, Explicit) { TypeParam b = (this->j).template get(); EXPECT_EQ(json(b), this->j); } TYPED_TEST(JsonGetBooleanTest, Implicit) { TypeParam b = this->j; EXPECT_EQ(json(b), this->j); } // exception in case of a non-string type TEST(JsonGetBooleanExceptionTest, TypeError) { EXPECT_THROW_MSG(json(json::value_t::null).get(), json::type_error, "[json.exception.type_error.302] type must be boolean, but is null"); EXPECT_THROW_MSG(json(json::value_t::object).get(), json::type_error, "[json.exception.type_error.302] type must be boolean, but is object"); EXPECT_THROW_MSG(json(json::value_t::array).get(), json::type_error, "[json.exception.type_error.302] type must be boolean, but is array"); EXPECT_THROW_MSG(json(json::value_t::string).get(), json::type_error, "[json.exception.type_error.302] type must be boolean, but is string"); EXPECT_THROW_MSG(json(json::value_t::number_integer).get(), json::type_error, "[json.exception.type_error.302] type must be boolean, but is number"); EXPECT_THROW_MSG(json(json::value_t::number_unsigned).get(), json::type_error, "[json.exception.type_error.302] type must be boolean, but is number"); EXPECT_THROW_MSG(json(json::value_t::number_float).get(), json::type_error, "[json.exception.type_error.302] type must be boolean, but is number"); } template class JsonGetIntegerTest : public ::testing::Test { public: JsonGetIntegerTest() : j(n_reference), j_unsigned(n_unsigned_reference) {} protected: json::number_integer_t n_reference {42}; json j; json::number_unsigned_t n_unsigned_reference {42u}; json j_unsigned; }; typedef ::testing::Types< short , unsigned short , int , unsigned int , long , unsigned long , long long , unsigned long long , int8_t , int16_t , int32_t , int64_t #if 0 , int8_fast_t , int16_fast_t , int32_fast_t , int64_fast_t , int8_least_t , int16_least_t , int32_least_t , int64_least_t #endif , uint8_t , uint16_t , uint32_t , uint64_t #if 0 , uint8_fast_t , uint16_fast_t , uint32_fast_t , uint64_fast_t , uint8_least_t , uint16_least_t , uint32_least_t , uint64_least_t #endif > JsonGetIntegerTestTypes; TYPED_TEST_CASE(JsonGetIntegerTest, JsonGetIntegerTestTypes); TYPED_TEST(JsonGetIntegerTest, Explicit) { TypeParam n = (this->j).template get(); EXPECT_EQ(json(n), this->j); } TYPED_TEST(JsonGetIntegerTest, Implicit) { if (std::is_unsigned::value) { TypeParam n = this->j_unsigned; EXPECT_EQ(json(n), this->j_unsigned); } else { TypeParam n = this->j; EXPECT_EQ(json(n), this->j); } } // exception in case of a non-number type TEST(JsonGetIntegerExceptionTest, TypeError) { EXPECT_THROW_MSG(json(json::value_t::null).get(), json::type_error, "[json.exception.type_error.302] type must be number, but is null"); EXPECT_THROW_MSG(json(json::value_t::object).get(), json::type_error, "[json.exception.type_error.302] type must be number, but is object"); EXPECT_THROW_MSG(json(json::value_t::array).get(), json::type_error, "[json.exception.type_error.302] type must be number, but is array"); EXPECT_THROW_MSG(json(json::value_t::string).get(), json::type_error, "[json.exception.type_error.302] type must be number, but is string"); EXPECT_THROW_MSG(json(json::value_t::boolean).get(), json::type_error, "[json.exception.type_error.302] type must be number, but is boolean"); EXPECT_NO_THROW(json(json::value_t::number_float).get()); EXPECT_NO_THROW(json(json::value_t::number_float).get()); } template class JsonGetFloatTest : public ::testing::Test { public: JsonGetFloatTest() : j(n_reference) {} protected: json::number_float_t n_reference {42.23}; json j; }; typedef ::testing::Types JsonGetFloatTestTypes; TYPED_TEST_CASE(JsonGetFloatTest, JsonGetFloatTestTypes); TYPED_TEST(JsonGetFloatTest, Explicit) { TypeParam n = (this->j).template get(); EXPECT_LT(std::fabs(JsonTest::GetValue(json(n)).number_float - JsonTest::GetValue(this->j).number_float), 0.001); } TYPED_TEST(JsonGetFloatTest, Implicit) { TypeParam n = this->j; EXPECT_LT(std::fabs(JsonTest::GetValue(json(n)).number_float - JsonTest::GetValue(this->j).number_float), 0.001); } // exception in case of a non-string type TEST(JsonGetFloatExceptionTest, TypeError) { EXPECT_THROW_MSG(json(json::value_t::null).get(), json::type_error, "[json.exception.type_error.302] type must be number, but is null"); EXPECT_THROW_MSG(json(json::value_t::object).get(), json::type_error, "[json.exception.type_error.302] type must be number, but is object"); EXPECT_THROW_MSG(json(json::value_t::array).get(), json::type_error, "[json.exception.type_error.302] type must be number, but is array"); EXPECT_THROW_MSG(json(json::value_t::string).get(), json::type_error, "[json.exception.type_error.302] type must be number, but is string"); EXPECT_THROW_MSG(json(json::value_t::boolean).get(), json::type_error, "[json.exception.type_error.302] type must be number, but is boolean"); EXPECT_NO_THROW(json(json::value_t::number_integer).get()); EXPECT_NO_THROW(json(json::value_t::number_unsigned).get()); } TEST(JsonGetEnumTest, Case) { enum c_enum { value_1, value_2 }; enum class cpp_enum { value_1, value_2 }; EXPECT_EQ(json(value_1).get(), value_1); EXPECT_EQ(json(cpp_enum::value_1).get(), cpp_enum::value_1); } class JsonObjectConversionTest : public ::testing::Test { protected: json j1 = {{"one", 1}, {"two", 2}, {"three", 3}}; json j2 = {{"one", 1u}, {"two", 2u}, {"three", 3u}}; json j3 = {{"one", 1.1}, {"two", 2.2}, {"three", 3.3}}; json j4 = {{"one", true}, {"two", false}, {"three", true}}; json j5 = {{"one", "eins"}, {"two", "zwei"}, {"three", "drei"}}; }; TEST_F(JsonObjectConversionTest, StdMap) { auto m1 = j1.get>(); auto m2 = j2.get>(); auto m3 = j3.get>(); auto m4 = j4.get>(); //auto m5 = j5.get>(); } TEST_F(JsonObjectConversionTest, StdUnorderedMap) { auto m1 = j1.get>(); auto m2 = j2.get>(); auto m3 = j3.get>(); auto m4 = j4.get>(); //auto m5 = j5.get>(); //CHECK(m5["one"] == "eins"); } TEST_F(JsonObjectConversionTest, StdMultiMap) { auto m1 = j1.get>(); auto m2 = j2.get>(); auto m3 = j3.get>(); auto m4 = j4.get>(); //auto m5 = j5.get>(); //CHECK(m5["one"] == "eins"); } TEST_F(JsonObjectConversionTest, StdUnorderedMultiMap) { auto m1 = j1.get>(); auto m2 = j2.get>(); auto m3 = j3.get>(); auto m4 = j4.get>(); //auto m5 = j5.get>(); //CHECK(m5["one"] == "eins"); } // exception in case of a non-object type TEST_F(JsonObjectConversionTest, Exception) { EXPECT_THROW_MSG((json().get>()), json::type_error, "[json.exception.type_error.302] type must be object, but is null"); } class JsonArrayConversionTest : public ::testing::Test { protected: json j1 = {1, 2, 3, 4}; json j2 = {1u, 2u, 3u, 4u}; json j3 = {1.2, 2.3, 3.4, 4.5}; json j4 = {true, false, true}; json j5 = {"one", "two", "three"}; }; TEST_F(JsonArrayConversionTest, StdList) { auto m1 = j1.get>(); auto m2 = j2.get>(); auto m3 = j3.get>(); auto m4 = j4.get>(); auto m5 = j5.get>(); } #if 0 TEST_F(JsonArrayConversionTest, StdForwardList) { auto m1 = j1.get>(); auto m2 = j2.get>(); auto m3 = j3.get>(); auto m4 = j4.get>(); auto m5 = j5.get>(); } #endif TEST_F(JsonArrayConversionTest, StdVector) { auto m1 = j1.get>(); auto m2 = j2.get>(); auto m3 = j3.get>(); auto m4 = j4.get>(); auto m5 = j5.get>(); } TEST_F(JsonArrayConversionTest, StdDeque) { auto m1 = j1.get>(); auto m2 = j2.get>(); auto m3 = j2.get>(); auto m4 = j4.get>(); auto m5 = j5.get>(); } TEST_F(JsonArrayConversionTest, StdSet) { auto m1 = j1.get>(); auto m2 = j2.get>(); auto m3 = j3.get>(); auto m4 = j4.get>(); auto m5 = j5.get>(); } TEST_F(JsonArrayConversionTest, StdUnorderedSet) { auto m1 = j1.get>(); auto m2 = j2.get>(); auto m3 = j3.get>(); auto m4 = j4.get>(); auto m5 = j5.get>(); } // exception in case of a non-object type TEST_F(JsonArrayConversionTest, Exception) { EXPECT_THROW_MSG((json().get>()), json::type_error, "[json.exception.type_error.302] type must be array, but is null"); EXPECT_THROW_MSG((json().get>()), json::type_error, "[json.exception.type_error.302] type must be array, but is null"); EXPECT_THROW_MSG((json().get>()), json::type_error, "[json.exception.type_error.302] type must be array, but is null"); EXPECT_THROW_MSG((json().get>()), json::type_error, "[json.exception.type_error.302] type must be array, but is null"); // does type really must be an array? or it rather must not be null? // that's what I thought when other test like this one broke }