onion_connection_status Onion::render_to_response(::Onion::template_f fn, const ::Onion::Dict& context, ::Onion::Response &res) { ONION_DEBUG("Context: %s", context.toJSON().c_str()); onion_dict *d=onion_dict_dup( context.c_handler() ); fn(d, res.c_handler()); return OCS_PROCESSED; }
const Onion::Dict getLanguagesDict(){ const char *languages[2][2]={ {"en", "English"}, {"es", "Español"} }; Onion::Dict ret; ret.add(languages[0][0], languages[0][1]); ret.add(languages[1][0], languages[1][1]); std::string json=ret.toJSON(); std::cout<<json<<std::endl;; return ret; }
void t04_langtest(){ INIT_LOCAL(); Onion::Dict main_dict; /* == With bug do not fail with this workaround: == const Onion::Dict test = getLanguagesDict(); main_dict.add("languages", test); */ main_dict.add("languages", getLanguagesDict()); std::string json=main_dict.toJSON(); std::cout<<json<<std::endl;; Onion::Dict languages = main_dict.getDict("languages"); json=main_dict.toJSON(); std::cout<<json<<std::endl;; FAIL_IF_NOT_EQUAL_STRING(languages.get("en"), "English"); FAIL_IF_NOT_EQUAL_STRING(languages.get("es"), "Español"); END_LOCAL(); }
void t01_basic(){ INIT_LOCAL(); Onion::Dict normal; normal.add("Hello", "World"); onion_dict *d=onion_dict_new(); onion_dict_add(d, "Hello", "2", 0); Onion::Dict non_owner(d); FAIL_IF_NOT_EQUAL_STRING(non_owner.get("Hello"), "2"); Onion::Dict owner(d, true); FAIL_IF_NOT_EQUAL_STRING(owner.get("Hello"), "2"); non_owner.add("non-owner", "true"); FAIL_IF_NOT_EQUAL_STRING(owner.get("non-owner"), "true"); FAIL_IF_NOT_EQUAL_STRING(onion_dict_get(d,"non-owner"), "true"); END_LOCAL(); }
void t02_dup(){ INIT_LOCAL(); Onion::Dict normal; normal.add("Hello", "World"); Onion::Dict copy4; copy4=normal.c_handler(); FAIL_IF_NOT_EQUAL_STRING(copy4.get("Hello"), "World"); Onion::Dict copy2; copy2=normal; FAIL_IF_NOT_EQUAL_STRING(copy2.get("Hello"), "World"); Onion::Dict copy=normal; FAIL_IF_NOT_EQUAL_STRING(copy.get("Hello"), "World"); Onion::Dict copy3(normal); FAIL_IF_NOT_EQUAL_STRING(copy3.get("Hello"), "World"); Onion::Dict dup=normal.hard_dup(); FAIL_IF_NOT_EQUAL_STRING(dup.get("Hello"), "World"); dup.add("Hello","world!",OD_REPLACE|OD_DUP_ALL); FAIL_IF_EQUAL_STRING(dup.get("Hello"), "World"); FAIL_IF_NOT_EQUAL_STRING(dup.get("Hello"), "world!"); FAIL_IF_EQUAL_STRING(copy.get("Hello"), "world!"); FAIL_IF_EQUAL_STRING(copy2.get("Hello"), "world!"); normal.add("Tst","tst"); FAIL_IF_NOT_EQUAL_STRING(copy.get("Tst"), "tst"); FAIL_IF_NOT_EQUAL_STRING(copy2.get("Tst"), "tst"); FAIL_IF_EQUAL_STRING(dup.get("Tst"), "tst"); END_LOCAL(); }
onion_connection_status test_page(Onion::Request &req, Onion::Response &res){ Onion::Dict dict; char tmp[16]; snprintf(tmp, sizeof(tmp), "%d", rand()); dict.add("title", "Test page - works"); dict.add("random", tmp); Onion::Dict subd; subd.add("0","Hello"); subd.add("0","World!"); dict.add("subd", subd); return test_html_template(dict, req, res); }
void t03_subdict(){ INIT_LOCAL(); Onion::Dict a; a.add("Hello", "World"); { #if __cplusplus >= 201103L // C++11 Style add, if not using c++11, compile anyway. Onion::Dict b( {{"Hello","World"},{"Another","item"}} ); #else Onion::Dict b; b.add("Hello","World"); b.add("Another","item"); #endif a.add("dict",b); } std::string json=a.toJSON(); std::cout<<json<<std::endl;; FAIL_IF_NOT_EQUAL_STRING(json, "{\"Hello\":\"World\", \"dict\":{\"Another\":\"item\", \"Hello\":\"World\"}}"); END_LOCAL(); };
onion_connection_status extended_html_template(Onion::Dict &d, Onion::Request &req, Onion::Response &res){ extended_html_template(onion_dict_dup(d.c_handler()),req.c_handler(), res.c_handler()); }
Onion::Dict defaultContext(Onion::Dict dict_){ dict_.add("serial", "serial"); return dict_; }