TEST(GTestJsClosure, TestClosure2) { String::CPtr abc = String::create("abc"); String::CPtr xyz = String::create("xyz"); JsClosure::Ptr concat = JsClosure::create( [abc, xyz] (JsArray::Ptr args) -> Value { return abc->concat(xyz); }); ASSERT_TRUE(concat->call().equals(String::create("abcxyz"))); }
JsObject::Ptr parse(String::CPtr urlStr) { static const String::CPtr colon = String::create(":"); static const String::CPtr slash = String::create("/"); static const String::CPtr sharp = String::create("#"); static const String::CPtr question = String::create("?"); if (!urlStr) { LIBJ_NULL_PTR(JsObject, nullp); return nullp; } struct parsed_url* url = parse_url(urlStr->toStdString().c_str()); JsObject::Ptr obj = JsObject::create(); obj->put(HREF, urlStr); if (url->scheme) { obj->put(PROTOCOL, String::create(url->scheme)->toLowerCase()); } LIBJ_NULL_CPTR(String, port); if (url->port) { port = String::create(url->port); obj->put(PORT, port); } if (url->host) { String::CPtr hostname = String::create(url->host)->toLowerCase(); obj->put(HOSTNAME, hostname); if (port) { obj->put(HOST, hostname->concat(colon)->concat(port)); } else { obj->put(HOST, hostname); } } LIBJ_NULL_CPTR(String, query); if (url->query) { query = String::create(url->query); obj->put(QUERY, query); } if (url->path) { String::CPtr pathname = slash->concat(String::create(url->path)); obj->put(PATHNAME, pathname); if (query) { obj->put(PATH, pathname->concat(question)->concat(query)); } else { obj->put(PATH, pathname); } } if (url->username && url->password) { String::CPtr auth = String::create(url->username); auth = auth->concat(colon); auth = auth->concat(String::create(url->password)); obj->put(AUTH, auth); } if (url->fragment) { String::CPtr hash = sharp->concat(String::create(url->fragment)); obj->put(HASH, hash); } parsed_url_free(url); return obj; }
TEST(GTestStringBuffer, TestAppendManyTimes) { const char a[] = "abcde"; const char u[] = { 0xe7, 0x8c, 0xab, 0xe3, 0x81, 0xa8, 0xe6, 0x9a, 0xae, 0xe3, 0x82, 0x89, 0xe3, 0x81, 0x97, 0xe3, 0x81, 0x9f, 0xe3, 0x81, 0x84, // 猫と暮らしたい 0 }; String::CPtr s1 = String::create(a); String::CPtr s2 = String::create(u, String::UTF8); String::CPtr exp = String::create(""); StringBuffer::Ptr sb = StringBuffer::create(); for (int i = 0; i < 100; i++) { sb->append(s1); sb->append(s2); exp = exp->concat(s1)->concat(s2); } ASSERT_TRUE(sb->toString()->equals(exp)); }