Esempio n. 1
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(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")));
}
Esempio n. 2
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(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")));
}
Esempio n. 3
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);
}
Esempio n. 4
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());
}