TEST(GTestXmlDom, TestElement) { Document::CPtr doc = Document::parse(sample); ASSERT_TRUE(!doc->asAttr()); ASSERT_TRUE(!doc->asElement()); ASSERT_TRUE(!!doc->asDocument()); Node::CPtr node = doc->firstChild(); ASSERT_TRUE(!node->asAttr()); ASSERT_TRUE(!!node->asElement()); ASSERT_TRUE(!node->asDocument()); Element::CPtr element = node->asElement(); String::CPtr tag = element->tagName(); String::CPtr attrVal1 = element->getAttribute(String::create("name")); String::CPtr attrVal2 = element->getAttribute(String::create("foo")); String::CPtr attrVal3 = element->getAttribute(String::null()); ASSERT_TRUE(tag->equals(String::create("team"))); ASSERT_TRUE(attrVal1->equals(String::create("foo"))); ASSERT_TRUE(attrVal2->equals(String::create())); ASSERT_TRUE(!attrVal3); ASSERT_TRUE(element->hasAttribute(String::create("name"))); ASSERT_FALSE(element->hasAttribute(String::create("foo"))); ASSERT_FALSE(element->hasAttribute(String::null())); Attr::CPtr attr1 = element->getAttributeNode(String::create("name")); Attr::CPtr attr2 = element->getAttributeNode(String::create("foo")); Attr::CPtr attr3 = element->getAttributeNode(String::null()); ASSERT_TRUE(!!attr1); ASSERT_TRUE(!attr2); ASSERT_TRUE(!attr3); }
TEST(GTestValue, TestTo10) { Value v = String::create("abc"); String::CPtr s; ASSERT_TRUE(to<String::CPtr>(v, &s)); ASSERT_TRUE(s->equals(String::create("abc"))); Immutable::CPtr i1; ASSERT_FALSE(to<Immutable::CPtr>(v, &i1)); Immutable::CPtr i2 = String::create("123"); v = i2; Immutable::CPtr i3; ASSERT_TRUE(to<Immutable::CPtr>(v, &i3)); ASSERT_TRUE(i3->equals(String::create("123"))); }
TEST(GTestJsRegExp, TestExec) { JsRegExp::Ptr re = JsRegExp::create(String::create("a+(b*)(c)")); JsArray::Ptr a = re->exec(String::create("xaacz")); ASSERT_EQ(3, a->length()); ASSERT_TRUE(toCPtr<String>(a->get(0))->equals(String::create("aac"))); ASSERT_TRUE(a->get(1).isUndefined()); ASSERT_TRUE(toCPtr<String>(a->get(2))->equals(String::create("c"))); Int index = -1; to<Int>(a->getProperty(String::create("index")), &index); ASSERT_EQ(1, index); String::CPtr input = toCPtr<String>(a->getProperty(String::create("input"))); ASSERT_TRUE(input->equals(String::create("xaacz"))); ASSERT_FALSE(re->exec(String::create("bc"))); }
TEST(GTestJsRegExp, TestExec2) { JsRegExp::Ptr re = JsRegExp::create(String::create("^([0-9]+)\\.([0-9]+)$")); JsArray::Ptr a = re->exec(String::create("1.23")); ASSERT_EQ(3, a->length()); ASSERT_TRUE(toCPtr<String>(a->get(0))->equals(String::create("1.23"))); ASSERT_TRUE(toCPtr<String>(a->get(1))->equals(String::create("1"))); ASSERT_TRUE(toCPtr<String>(a->get(2))->equals(String::create("23"))); Int index = -1; to<Int>(a->getProperty(String::create("index")), &index); ASSERT_EQ(0, index); String::CPtr input = toCPtr<String>(a->getProperty(String::create("input"))); ASSERT_TRUE(input->equals(String::create("1.23"))); ASSERT_FALSE(re->exec(String::create("1x23"))); ASSERT_FALSE(re->exec(String::create("v1.23"))); }
String::CPtr normalize(String::CPtr path) { LIBJ_STATIC_SYMBOL_DEF(symCurrent, "."); LIBJ_STATIC_SYMBOL_DEF(symParent, ".."); LIBJ_STATIC_SYMBOL_DEF(symNull, "null"); if (!path) return symNull; Boolean absolute = false; Boolean endsWithSep = false; typedef TypedJsArray<String::CPtr> StringArray; StringArray::Ptr dirs = StringArray::create(); Size len = path->length(); for (Size i = 0; i < len;) { Size idx = indexOfSep(path, i); if (idx == 0) { absolute = true; } else if (idx != i) { String::CPtr dir; if (idx == NO_POS) { dir = path->substring(i); } else { dir = path->substring(i, idx); } if (dir->equals(symParent)) { Size numDirs = dirs->size(); if (numDirs > 0 && !dirs->getTyped(numDirs - 1)->equals(symParent)) { dirs->removeTyped(numDirs - 1); } else { dirs->addTyped(dir); } } else if (!dir->equals(symCurrent)) { dirs->addTyped(dir); } } if (idx == NO_POS) { endsWithSep = false; i = len; } else { endsWithSep = true; i = idx + 1; } } StringBuilder::Ptr normal = StringBuilder::create(); if (absolute) normal->appendChar(SEP); Size numDirs = dirs->size(); for (Size i = 0; i < numDirs; i++) { if (i) normal->appendChar(SEP); normal->appendStr(dirs->getTyped(i)); } if (numDirs > 0 && endsWithSep) normal->appendChar(SEP); if (normal->length() == 0) { return symCurrent; } else { return normal->toString(); } }
TEST(GTestQueryString, TestStringify) { JsObject::Ptr obj = JsObject::create(); String::CPtr query = querystring::stringify(obj); ASSERT_TRUE(query->equals(String::create())); obj = JsObject::create(); obj->put(String::create("a"), String::create("b")); query = querystring::stringify(obj); ASSERT_TRUE(query->equals(String::create("a=b"))); obj = JsObject::create(); obj->put(String::create("a"), String::create("b")); obj->put(String::create("x"), 1); query = querystring::stringify(obj); ASSERT_TRUE(query->equals(String::create("a=b&x=1"))); obj = JsObject::create(); obj->put(String::create("a"), JsObject::create()); query = querystring::stringify(obj); ASSERT_TRUE(query->equals(String::create("a="))); obj = JsObject::create(); obj->put(String::create("a"), JsArray::create()); query = querystring::stringify(obj); ASSERT_TRUE(query->equals(String::create())); obj = JsObject::create(); obj->put(String::create("a"), Object::null()); query = querystring::stringify(obj); ASSERT_TRUE(query->equals(String::create("a="))); obj = JsObject::create(); obj->put(String::create("a"), UNDEFINED); query = querystring::stringify(obj); ASSERT_TRUE(query->equals(String::create("a="))); obj = JsObject::create(); obj->put(String::create(), String::create()); query = querystring::stringify(obj); ASSERT_TRUE(query->equals(String::create("="))); obj = JsObject::create(); obj->put(Object::null(), Object::null()); query = querystring::stringify(obj); ASSERT_TRUE(query->equals(String::create("null="))); obj = JsObject::create(); JsArray::Ptr ary = JsArray::create(); ary->add(String::create("xyz")); ary->add(123); obj->put(UNDEFINED, ary); query = querystring::stringify(obj); ASSERT_TRUE(query->equals( String::create("undefined=xyz&undefined=123"))); obj = JsObject::create(); obj->put(String::create(" "), String::create(" ")); query = querystring::stringify(obj); ASSERT_TRUE(query->equals(String::create("%20=%20"))); }