2013-10-29 21:51:11 +01:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <valijson/adapters/property_tree_adapter.hpp>
|
|
|
|
|
|
|
|
class TestPropertyTreeAdapter : public testing::Test
|
|
|
|
{
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(TestPropertyTreeAdapter, BasicArrayIteration)
|
|
|
|
{
|
|
|
|
const unsigned int numElements = 10;
|
|
|
|
|
2019-09-26 02:58:38 +02:00
|
|
|
// Create a boost property tree that is equivalent to a JSON array containing a
|
2013-10-29 21:51:11 +01:00
|
|
|
// list of numbers.
|
|
|
|
boost::property_tree::ptree document;
|
|
|
|
for (unsigned int i = 0; i < numElements; i++) {
|
|
|
|
document.push_back(std::make_pair(std::string(),
|
2016-08-01 22:09:31 +02:00
|
|
|
boost::property_tree::ptree(std::to_string(i))));
|
2013-10-29 21:51:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that wrapping the document preserves the array and does not allow
|
|
|
|
// it to be cast to other types
|
|
|
|
valijson::adapters::PropertyTreeAdapter adapter(document);
|
2020-11-07 03:17:36 +01:00
|
|
|
#if VALIJSON_USE_EXCEPTIONS
|
2013-10-29 21:51:11 +01:00
|
|
|
ASSERT_NO_THROW( adapter.getArray() );
|
|
|
|
ASSERT_ANY_THROW( adapter.getBool() );
|
|
|
|
ASSERT_ANY_THROW( adapter.getDouble() );
|
|
|
|
ASSERT_ANY_THROW( adapter.getObject() );
|
|
|
|
ASSERT_ANY_THROW( adapter.getString() );
|
2020-11-07 03:17:36 +01:00
|
|
|
#endif
|
2014-02-28 23:03:39 +01:00
|
|
|
|
2013-10-29 21:51:11 +01:00
|
|
|
// Ensure that the array contains the expected number of elements
|
|
|
|
EXPECT_EQ( numElements, adapter.getArray().size() );
|
2014-02-28 23:03:39 +01:00
|
|
|
|
2013-10-29 21:51:11 +01:00
|
|
|
// Ensure that the elements are returned in the order they were inserted
|
|
|
|
unsigned int expectedValue = 0;
|
2016-08-10 17:22:06 +02:00
|
|
|
for (const valijson::adapters::PropertyTreeAdapter value : adapter.getArray()) {
|
2013-10-29 21:51:11 +01:00
|
|
|
ASSERT_TRUE( value.isString() );
|
|
|
|
ASSERT_FALSE( value.isNumber() );
|
|
|
|
ASSERT_TRUE( value.maybeDouble() );
|
|
|
|
EXPECT_EQ( double(expectedValue), value.asDouble() );
|
|
|
|
expectedValue++;
|
|
|
|
}
|
2014-02-28 23:03:39 +01:00
|
|
|
|
2013-10-29 21:51:11 +01:00
|
|
|
// Ensure that the correct number of elements were iterated over
|
|
|
|
EXPECT_EQ(numElements, expectedValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TestPropertyTreeAdapter, BasicObjectIteration)
|
|
|
|
{
|
|
|
|
const unsigned int numElements = 10;
|
|
|
|
|
2019-09-26 02:58:38 +02:00
|
|
|
// Create a boost property tree that consists of an object that maps numeric
|
2013-10-29 21:51:11 +01:00
|
|
|
// strings their corresponding numeric values
|
|
|
|
boost::property_tree::ptree document;
|
|
|
|
for (unsigned int i = 0; i < numElements; i++) {
|
2016-08-01 22:09:31 +02:00
|
|
|
std::string name(std::to_string(i));
|
2013-10-29 21:51:11 +01:00
|
|
|
document.push_back(std::make_pair(name, boost::property_tree::ptree(
|
2016-08-01 22:09:31 +02:00
|
|
|
std::to_string(double(i)))));
|
2013-10-29 21:51:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that wrapping the document preserves the object and does not
|
|
|
|
// allow it to be cast to other types
|
|
|
|
valijson::adapters::PropertyTreeAdapter adapter(document);
|
2020-11-07 03:17:36 +01:00
|
|
|
#if VALIJSON_USE_EXCEPTIONS
|
2013-10-29 21:51:11 +01:00
|
|
|
ASSERT_NO_THROW( adapter.getObject() );
|
|
|
|
ASSERT_ANY_THROW( adapter.getArray() );
|
|
|
|
ASSERT_ANY_THROW( adapter.getBool() );
|
|
|
|
ASSERT_ANY_THROW( adapter.getDouble() );
|
|
|
|
ASSERT_ANY_THROW( adapter.getString() );
|
2020-11-07 03:17:36 +01:00
|
|
|
#endif
|
2014-02-28 23:03:39 +01:00
|
|
|
|
2013-10-29 21:51:11 +01:00
|
|
|
// Ensure that the object contains the expected number of members
|
|
|
|
EXPECT_EQ( numElements, adapter.getObject().size() );
|
2014-02-28 23:03:39 +01:00
|
|
|
|
2013-10-29 21:51:11 +01:00
|
|
|
// Ensure that the members are returned in the order they were inserted
|
|
|
|
unsigned int expectedValue = 0;
|
2016-08-10 17:22:06 +02:00
|
|
|
for (const valijson::adapters::PropertyTreeAdapter::ObjectMember member : adapter.getObject()) {
|
2013-10-29 21:51:11 +01:00
|
|
|
ASSERT_TRUE( member.second.isString() );
|
|
|
|
ASSERT_FALSE( member.second.isNumber() );
|
|
|
|
ASSERT_TRUE( member.second.maybeDouble() );
|
2016-08-01 22:09:31 +02:00
|
|
|
EXPECT_EQ( std::to_string(expectedValue), member.first );
|
2013-10-29 21:51:11 +01:00
|
|
|
EXPECT_EQ( double(expectedValue), member.second.asDouble() );
|
|
|
|
expectedValue++;
|
|
|
|
}
|
2014-02-28 23:03:39 +01:00
|
|
|
|
2013-10-29 21:51:11 +01:00
|
|
|
// Ensure that the correct number of elements were iterated over
|
|
|
|
EXPECT_EQ( numElements, expectedValue );
|
|
|
|
}
|