std::string GD_API VariableStructureToJSON(const gd::Variable & variable) { if ( !variable.IsStructure() ) { if ( variable.IsNumber() ) return ToString(variable.GetValue()); else return StringToQuotedJSONString(variable.GetString().c_str()); } std::string str = "{"; bool firstChild = true; for(std::map<std::string, gd::Variable>::const_iterator i = variable.GetAllChildren().begin(); i != variable.GetAllChildren().end();++i) { if ( !firstChild ) str += ","; str += StringToQuotedJSONString(i->first.c_str())+": "+VariableStructureToJSON(i->second); firstChild = false; } str += "}"; return str; }
#include "catch.hpp" #include <algorithm> #include <initializer_list> #include <map> #include "GDCore/CommonTools.h" #include "GDCore/Project/VariablesContainer.h" TEST_CASE("Variable", "[common][variables]") { SECTION("Basics") { gd::Variable variable; variable.SetValue(50); REQUIRE(variable.GetValue() == 50); REQUIRE(variable == 50); REQUIRE(variable.IsNumber() == true); REQUIRE(variable.IsStructure() == false); variable.SetString("MyString"); REQUIRE(variable.GetString() == "MyString"); REQUIRE(variable == "MyString"); REQUIRE(variable.IsNumber() == false); REQUIRE(variable.IsStructure() == false); } SECTION("Conversions") { gd::Variable variable; variable.SetValue(50); REQUIRE(variable.GetString() == "50"); // Used as a string... REQUIRE(variable.IsNumber() == false); //...so consider as a string variable.SetString("MyString");