Ejemplo n.º 1
0
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")));
}
Ejemplo n.º 2
0
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>());
}
Ejemplo n.º 3
0
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));
}
Ejemplo n.º 4
0
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));
}
Ejemplo n.º 5
0
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
}
Ejemplo n.º 6
0
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(a->getCPtr<String>(0)->equals(String::create("aac")));
    ASSERT_TRUE(a->getCPtr<String>(1)->equals(String::create()));
    ASSERT_TRUE(a->getCPtr<String>(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")));
}
Ejemplo n.º 7
0
TEST(GTestEventEmitter, TestOnAndAddListener) {
    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->addListener(str("event"), sub);

    JsArray::Ptr a = ee->listeners(event);
    ASSERT_EQ(2, a->size());

    Value v = a->get(0);
    JsFunction::Ptr f1 = toPtr<JsFunction>(v);
    ASSERT_EQ(add, f1);
    v = a->get(1);
    JsFunction::Ptr f2 = toPtr<JsFunction>(v);
    ASSERT_EQ(sub, f2);
}
Ejemplo n.º 8
0
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(a->getCPtr<String>(0)->equals(String::create("1.23")));
    ASSERT_TRUE(a->getCPtr<String>(1)->equals(String::create("1")));
    ASSERT_TRUE(a->getCPtr<String>(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")));
}
Ejemplo n.º 9
0
TEST(GTestJsRegExp, TestLastIndex) {
    JsRegExp::Ptr re =
        JsRegExp::create(String::create("(hi)?"), JsRegExp::GLOBAL);
    ASSERT_EQ(0, re->lastIndex());

    JsArray::Ptr a = re->exec(String::create("hi"));
    ASSERT_EQ(2, a->length());
    ASSERT_TRUE(a->getCPtr<String>(0)->equals(String::create("hi")));
    ASSERT_TRUE(a->getCPtr<String>(1)->equals(String::create("hi")));

    ASSERT_EQ(2, re->lastIndex());
    a = re->exec(String::create("hi"));
    ASSERT_EQ(2, a->length());
    ASSERT_EQ(0, a->getCPtr<String>(0)->length());
    ASSERT_TRUE(a->get(1).isUndefined());

    re->put(String::create("lastIndex"), static_cast<Size>(3));
    ASSERT_EQ(3, re->lastIndex());
    ASSERT_TRUE(!re->exec(String::create("hi")));
    ASSERT_EQ(0, re->lastIndex());
}
Ejemplo n.º 10
0
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)));
}