void JSONConfigurationTest::testSetArrayElement()
{
	JSONConfiguration config;

	std::string json = "{ \"config\" : "
							" { \"prop1\" : \"value1\", "
							" \"prop2\" : 10, "
							" \"prop3\" : [ \"element1\", \"element2\" ], "
							" \"prop4\" : { \"prop5\" : false, "
											" \"prop6\" : null } "
							" }"
						"}";

	std::istringstream iss(json);
	config.load(iss);

	// config.prop3[0] = "foo"
	config.setString("config.prop3[0]", "foo");
	assert(config.getString("config.prop3[0]") == "foo");

	// config.prop3[1] = "bar"
	config.setString("config.prop3[1]", "bar");
	assert(config.getString("config.prop3[1]") == "bar");

	// config.prop3[3] = "baz"
	config.setString("config.prop3[3]", "baz");
	assert(config.getString("config.prop3[3]") == "baz");
}
Exemplo n.º 2
0
void JSONConfigurationTest::testLoad()
{
	JSONConfiguration config;

	std::string json = "{ \"config\" : "
							" { \"prop1\" : \"value1\", "
							" \"prop2\" : 10, "
							" \"prop3\" : [ \"element1\", \"element2\" ], "
							" \"prop4\" : { \"prop5\" : false, "
											" \"prop6\" : null } "
							" }"
						"}";

	std::istringstream iss(json);
	try
	{
		config.load(iss);
	}
	catch(JSONException jsone)
	{
		std::cout << jsone.message() << std::endl;
		assert(false);
	}

	std::string property1 = config.getString("config.prop1");
	assert(property1.compare("value1") == 0);

	int property2 = config.getInt("config.prop2");
	assert(property2 == 10);

	int nonExistingProperty = config.getInt("config.prop7", 5);
	assert(nonExistingProperty == 5);

	std::string arrProperty = config.getString("config.prop3[1]");
	assert(arrProperty.compare("element2") == 0);

	bool property35 = config.getBool("config.prop4.prop5");
	assert(! property35);

	try
	{
		config.getString("propertyUnknown");
		assert(true);
	}
	catch(NotFoundException nfe)
	{
	}

}