#include #include "V8Xml/WebParser.h" #include "v8tree/Instance.h" using namespace RBX; BOOST_AUTO_TEST_SUITE(WebParser) BOOST_AUTO_TEST_CASE(TestJSONEmpty) { Reflection::Variant v; BOOST_CHECK(RBX::WebParser::parseJSONObject("{}", v)); BOOST_CHECK(v.isType >()); BOOST_CHECK(v.get >()->size() == 0); BOOST_CHECK(RBX::WebParser::parseJSONObject("[]", v)); BOOST_CHECK(v.isType >()); BOOST_CHECK(v.get >()->size() == 0); } BOOST_AUTO_TEST_CASE(TestJSONBasic) { Reflection::Variant v; BOOST_CHECK(RBX::WebParser::parseJSONObject( "{ \"a\" : 0, \"b\": false, \"c\": \"True\", \"d\": {}, \"e\": [], \"f\": null}", v)); BOOST_CHECK(v.isType >()); shared_ptr table = v.get >(); BOOST_CHECK(table->size() == 6); Reflection::ValueTable::const_iterator it = table->find("a"); BOOST_CHECK(it != table->end()); BOOST_CHECK(it->second.isNumber()); BOOST_CHECK(it->second.get() == 0); it = table->find("b"); BOOST_CHECK(it != table->end()); BOOST_CHECK(it->second.isType()); BOOST_CHECK(it->second.get() == FALSE); it = table->find("c"); BOOST_CHECK(it != table->end()); BOOST_CHECK(it->second.isType()); BOOST_CHECK(it->second.get() == "True"); it = table->find("d"); BOOST_CHECK(it != table->end()); BOOST_CHECK(it->second.get >()->size() == 0); it = table->find("e"); BOOST_CHECK(it != table->end()); BOOST_CHECK(it->second.isType >()); BOOST_CHECK(it->second.get >()->size() == 0); it = table->find("f"); BOOST_CHECK(it != table->end()); BOOST_CHECK(it->second.isVoid()); } BOOST_AUTO_TEST_CASE(TestJSONSub) { Reflection::Variant v; BOOST_CHECK(RBX::WebParser::parseJSONObject( "{ \"subdict\": { \"x\": true, \"y\": 0 }, \"subarray\": [0, 1, null] }", v)); shared_ptr table = v.get >(); BOOST_CHECK(table->size() == 2); Reflection::ValueTable::const_iterator it = table->find("subdict"); BOOST_CHECK(it != table->end()); BOOST_CHECK(it->second.isType >()); shared_ptr subTable = it->second.get >(); BOOST_CHECK(subTable->size() == 2); it = subTable->find("x"); BOOST_CHECK(it->second.isType()); BOOST_CHECK(it->second.get() == TRUE); it = subTable->find("y"); BOOST_CHECK(it->second.isType()); BOOST_CHECK(it->second.get() == 0); it = table->find("subarray"); BOOST_CHECK(it != table->end()); shared_ptr subArray = it->second.get >(); BOOST_CHECK(subArray->size() == 3); BOOST_CHECK((*subArray)[0].isType()); BOOST_CHECK((*subArray)[0].get() == 0); BOOST_CHECK((*subArray)[1].isType()); BOOST_CHECK((*subArray)[1].get() == 1); BOOST_CHECK((*subArray)[2].isType()); } BOOST_AUTO_TEST_CASE(TestJSONWriteBasic) { double testValue = 4.5; shared_ptr table(rbx::make_shared()); (*table)["a"] = 2; (*table)["b"] = testValue; (*table)["c"] = false; (*table)["d"] = shared_ptr(rbx::make_shared()); (*table)["e"] = shared_ptr(rbx::make_shared()); (*table)["f"] = RBX::Vector3(); (*table)["g"] = std::string("test"); Reflection::Variant v = shared_ptr(table); std::string result; BOOST_CHECK_EQUAL(RBX::WebParser::writeJSON(v, result, RBX::WebParser::FailOnNonJSON), false); BOOST_CHECK(RBX::WebParser::writeJSON(v, result, RBX::WebParser::SkipNonJSON)); // Warning: depends on implementation of particular JSON parser, might break if parser is changed BOOST_CHECK_EQUAL(result,"{\"g\":\"test\",\"f\":null,\"e\":[],\"d\":{},\"c\":false,\"b\":4.5,\"a\":2}"); // Check roundtrip Reflection::Variant parsedV; BOOST_CHECK(RBX::WebParser::parseJSONObject(result, parsedV)); BOOST_CHECK(parsedV.isType >()); shared_ptr parsedTable = parsedV.get >(); BOOST_CHECK_EQUAL(parsedTable->find("b")->second.get(),testValue); BOOST_CHECK(parsedTable->find("f")->second.isVoid()); } BOOST_AUTO_TEST_CASE(TestJSONRountrip) { double floatValue = 4.6; // Can't be represented by double exactly int intValue = 123456789; shared_ptr table(rbx::make_shared()); (*table)["bigInt"] = intValue; (*table)["float"] = floatValue; Reflection::Variant v = shared_ptr(table); std::string result; BOOST_CHECK(RBX::WebParser::writeJSON(v, result)); Reflection::Variant parsedV; BOOST_CHECK(RBX::WebParser::parseJSONObject(result, parsedV)); BOOST_CHECK(parsedV.isType >()); shared_ptr parsedTable = parsedV.get >(); BOOST_CHECK_EQUAL(parsedTable->find("float")->second.get(),floatValue); BOOST_CHECK_EQUAL(parsedTable->find("bigInt")->second.get(),intValue); } BOOST_AUTO_TEST_CASE(TestWebParserList) { std::string response = "foobar"; RBX::Reflection::Variant value; std::auto_ptr stream(new std::istringstream(response)); BOOST_REQUIRE(RBX::WebParser::parseWebGenericResponse(*stream, value)); BOOST_CHECK(value.isType >()); } BOOST_AUTO_TEST_CASE(TestWebParserTable) { std::string response = "foofoo2barbar2
"; RBX::Reflection::Variant value; std::auto_ptr stream(new std::istringstream(response)); if(!RBX::WebParser::parseWebGenericResponse(*stream, value)){ BOOST_CHECK(false); } BOOST_CHECK(value.isType >()); } BOOST_AUTO_TEST_CASE(TestWebParserBool) { std::string response; RBX::Reflection::Variant value; { response = "true"; std::auto_ptr stream(new std::istringstream(response)); if(!RBX::WebParser::parseWebGenericResponse(*stream, value)){ BOOST_CHECK(false); } BOOST_CHECK(value.isType()); } { response = "false"; std::auto_ptr stream(new std::istringstream(response)); if(!RBX::WebParser::parseWebGenericResponse(*stream, value)){ BOOST_CHECK(false); } BOOST_CHECK(value.isType()); } } BOOST_AUTO_TEST_CASE(TestWebParserBoolWithType) { std::string response; RBX::Reflection::Variant value; { response = "true"; std::auto_ptr stream(new std::istringstream(response)); if(!RBX::WebParser::parseWebGenericResponse(*stream, value)){ BOOST_CHECK(false); } BOOST_CHECK(value.isType()); } { response = "false"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(RBX::WebParser::parseWebGenericResponse(*stream, value)); BOOST_CHECK(value.isType()); } { response = "False"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(RBX::WebParser::parseWebGenericResponse(*stream, value)); BOOST_CHECK(value.isType()); } { response = "True"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(RBX::WebParser::parseWebGenericResponse(*stream, value)); BOOST_CHECK(value.isType()); } { response = "TRUE"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(RBX::WebParser::parseWebGenericResponse(*stream, value)); BOOST_CHECK(value.isType()); } { response = "FALSE"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(RBX::WebParser::parseWebGenericResponse(*stream, value)); BOOST_CHECK(value.isType()); } { response = "foo"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(!RBX::WebParser::parseWebGenericResponse(*stream, value)); } { response = "bar"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(!RBX::WebParser::parseWebGenericResponse(*stream, value)); } } BOOST_AUTO_TEST_CASE(TestWebParserNumberWithType) { std::string response; RBX::Reflection::Variant value; { response = "0.12321"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(RBX::WebParser::parseWebGenericResponse(*stream, value)); BOOST_CHECK(value.isType()); } { response = "1241241"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(RBX::WebParser::parseWebGenericResponse(*stream, value)); BOOST_CHECK(value.isType()); } { response = "1234aba"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(RBX::WebParser::parseWebGenericResponse(*stream, value)); BOOST_CHECK(value.isType()); } { response = "fdklajsd980f"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(RBX::WebParser::parseWebGenericResponse(*stream, value)); } } BOOST_AUTO_TEST_CASE(TestWebParserString) { std::string response; RBX::Reflection::Variant value; { response = "Foo"; std::auto_ptr stream(new std::istringstream(response)); if(!RBX::WebParser::parseWebGenericResponse(*stream, value)){ BOOST_CHECK(false); } BOOST_CHECK(value.isType()); } { response = "Bar"; std::auto_ptr stream(new std::istringstream(response)); if(!RBX::WebParser::parseWebGenericResponse(*stream, value)){ BOOST_CHECK(false); } BOOST_CHECK(value.isType()); } } BOOST_AUTO_TEST_CASE(TestWebParserStringWithType) { std::string response; RBX::Reflection::Variant value; { response = "Foo"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(RBX::WebParser::parseWebGenericResponse(*stream, value)); BOOST_CHECK(value.isType()); } { response = "Bar"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(RBX::WebParser::parseWebGenericResponse(*stream, value)); BOOST_CHECK(value.isType()); } } BOOST_AUTO_TEST_CASE(TestWebParserInstanceWithType) { std::string response; RBX::Reflection::Variant value; { response = "" "" "null " "nil " " " " " " false " " -0.5 " " 0.5 " " 0 " " 0 " " -0.5 " " 0.5 " " 4 " " 0 " " 194 " " " " 0 " " 0.600000024 " " 0 " " 1 " " 0 " " 0 " " 0 " " 1 " " 0 " " 0 " " 0 " " 1 " " " " true " " false " " 0.5 " " 1 " " 0.300000012 " " -0.5 " " 0.5 " " 0 " " 0 " " -0.5 " " 0.5 " " 0 " " 0 " " false " " 256 " " Part " " 0 " " -0.5 " " 0.5 " " 0 " " 0 " " " " 0 " " 0 " " 0 " " " " -0.5 " " 0.5 " " 3 " " 0 " " 0.319999993 " " " " 0 " " 0 " " 0 " " " " true " " 1 " " " " 4 " " 1.20000005 " " 2 " " " " " " " "" ""; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(RBX::WebParser::parseWebGenericResponse(*stream, value)); BOOST_CHECK(value.isType >()); } { response = "" "" "null " "nil " " " " " " false " " -0.5 " " 0.5 " " 0 " " 0 " " -0.5 " " 0.5 " " 4 " " 0 " " 194 " " " " 0 " " 0.600000024 " " 0 " " 1 " " 0 " " 0 " " 0 " " 1 " " 0 " " 0 " " 0 " " 1 " " " " true " " false " " 0.5 " " 1 " " 0.300000012 " " -0.5 " " 0.5 " " 0 " " 0 " " -0.5 " " 0.5 " " 0 " " 0 " " false " " 256 " " Part " " 0 " " -0.5 " " 0.5 " " 0 " " 0 " " " " 0 " " 0 " " 0 " " " " -0.5 " " 0.5 " " 3 " " 0 " " 0.319999993 " " " " 0 " " 0 " " 0 " " " " true " " 1 " " " " 4 " " 1.20000005 " " 2 " " " " " " " " " " " " false " " -0.5 " " 0.5 " " 0 " " 0 " " -0.5 " " 0.5 " " 4 " " 0 " " 194 " " " " 0 " " 0.600000024 " " 0 " " 1 " " 0 " " 0 " " 0 " " 1 " " 0 " " 0 " " 0 " " 1 " " " " true " " false " " 0.5 " " 1 " " 0.300000012 " " -0.5 " " 0.5 " " 0 " " 0 " " -0.5 " " 0.5 " " 0 " " 0 " " false " " 256 " " Part " " 0 " " -0.5 " " 0.5 " " 0 " " 0 " " " " 0 " " 0 " " 0 " " " " -0.5 " " 0.5 " " 3 " " 0 " " 0.319999993 " " " " 0 " " 0 " " 0 " " " " true " " 1 " " " " 4 " " 1.20000005 " " 2 " " " " " " " "" ""; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(!RBX::WebParser::parseWebGenericResponse(*stream, value)); } { response = "Bar"; std::auto_ptr stream(new std::istringstream(response)); BOOST_CHECK(!RBX::WebParser::parseWebGenericResponse(*stream, value)); } } BOOST_AUTO_TEST_SUITE_END()