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"))); }
TEST(GTestJsTypedArray, TestCreate2) { JsArray::Ptr a = JsArray::create(); a->add(5); JsTypedArray<Int>::Ptr ta = JsTypedArray<Int>::create(a); ASSERT_TRUE(!!ta); a->add(7.7); #ifdef LIBJ_USE_EXCEPTION ASSERT_ANY_THROW(JsTypedArray<Int>::create(a)); #else ta = JsTypedArray<Int>::create(a); ASSERT_FALSE(!!ta); #endif // LIBJ_USE_EXCEPTION }
TEST(GTestJsClosure, TestClosure) { JsClosure::Ptr add = JsClosure::create( [] (JsArray::Ptr args) -> Value { if (!args) { return Error::create(Error::ILLEGAL_ARGUMENT); } Int sum = 0; for (Size i = 0; i < args->length(); i++) { int x; if (to<Int>(args->get(i), &x)) { sum += x; } else { return Error::create(Error::ILLEGAL_ARGUMENT); } } return sum; }); JsArray::Ptr args = JsArray::create(); args->add(3); args->add(4); ASSERT_TRUE((*add)(args).equals(7)); ASSERT_TRUE(add->call().instanceof(Type<Error>::id())); ASSERT_TRUE(add->call(1).equals(1)); ASSERT_TRUE(add->call(1, 2).equals(3)); ASSERT_TRUE(add->call(1, 2, 3).equals(6)); ASSERT_TRUE(add->call(1, 2, 3, 4).equals(10)); ASSERT_TRUE(add->call(1, 2, 3, 4, 5).equals(15)); ASSERT_TRUE(add->call(1, 2, 3, 4, 5, 6).equals(21)); ASSERT_TRUE(add->call(1, 2, 3, 4, 5, 6, 7).equals(28)); ASSERT_TRUE(add->call(1, 2, 3, 4, 5, 6, 7, 8).equals(36)); ASSERT_TRUE(add->call(1, 2, 3, 4, 5, 6, 7, 8, 9).equals(45)); }
TEST(GTestEventEmitter, TestEmit) { EventEmitter::Ptr ee = EventEmitter::create(); String::CPtr event = str("event"); JsFunction::Ptr add = Add::create(); JsFunction::Ptr sub = Sub::create(); ee->on(event, add); ee->on(event, sub); results()->clear(); JsArray::Ptr args = JsArray::create(); args->add(5); args->add(3); ee->emit(event, args); ASSERT_TRUE(results()->get(0).equals(8)); ASSERT_TRUE(results()->get(1).equals(2)); ee->emit(event); ASSERT_TRUE(results()->get(2).is<Error>()); ASSERT_TRUE(results()->get(3).is<Error>()); ee->emit(event, static_cast<Value>(args)); ASSERT_TRUE(results()->get(4).is<Error>()); ASSERT_TRUE(results()->get(5).is<Error>()); }
TEST(GTestJsFunction, TestFunctor) { GTestJsFunctionAdd::Ptr add = GTestJsFunctionAdd::create(); GTestJsFunctionAdd::Ptr addx2 = GTestJsFunctionAddx2::create(); JsArray::Ptr args = JsArray::create(); args->add(3); args->add(4); ASSERT_TRUE((*add)(args).equals(7)); ASSERT_TRUE((*addx2)(args).equals(14)); }
TEST(GTestEventEmitter, TestOnce) { EventEmitter::Ptr ee = EventEmitter::create(); String::CPtr event = str("event"); JsFunction::Ptr add = Add::create(); JsFunction::Ptr sub = Sub::create(); ee->once(event, add); ee->on(event, sub); ASSERT_EQ(2, ee->listeners(event)->length()); results()->clear(); JsArray::Ptr args = JsArray::create(); args->add(6); args->add(3); ee->emit(event, args); ASSERT_EQ(1, ee->listeners(event)->length()); Value v = ee->listeners(event)->get(0); JsFunction::Ptr f = toPtr<JsFunction>(v); ASSERT_EQ(sub, f); ASSERT_EQ(9, to<Int>(results()->get(0))); ASSERT_EQ(3, to<Int>(results()->get(1))); }