Index: test/cctest/test-api.cc |
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc |
index fbb6e56e9270e2651dad911d2f10f91b47cb042a..f2cadaf5f12505a3701befebb6e3c708e1ff7fc7 100644 |
--- a/test/cctest/test-api.cc |
+++ b/test/cctest/test-api.cc |
@@ -22113,3 +22113,97 @@ TEST(EventLogging) { |
CHECK_EQ("V8.Test", last_event_message); |
CHECK_EQ(1, last_event_status); |
} |
+ |
+ |
+TEST(Promises) { |
+ i::FLAG_harmony_promises = true; |
+ |
+ LocalContext context; |
+ v8::Isolate* isolate = context->GetIsolate(); |
+ v8::HandleScope scope(isolate); |
+ Handle<Object> global = context->Global(); |
+ |
+ // Creation. |
+ Handle<v8::Promise> p = v8::Promise::New(isolate); |
+ Handle<v8::Promise> r = v8::Promise::New(isolate); |
+ |
+ // IsPromise predicate. |
+ CHECK(p->IsPromise()); |
+ CHECK(r->IsPromise()); |
+ Handle<Value> o = v8::Object::New(isolate); |
+ CHECK(!o->IsPromise()); |
+ |
+ // Resolution and rejection. |
+ p->Resolve(v8::Integer::New(isolate, 1)); |
+ CHECK(p->IsPromise()); |
+ r->Reject(v8::Integer::New(isolate, 2)); |
+ CHECK(r->IsPromise()); |
+ |
+ // Chaining non-pending promises. |
+ CompileRun( |
+ "var x1 = 0;\n" |
+ "var x2 = 0;\n" |
+ "function f1(x) { x1 = x; return x+1 };\n" |
+ "function f2(x) { x2 = x; return x+1 };\n"); |
+ Handle<Function> f1 = Handle<Function>::Cast(global->Get(v8_str("f1"))); |
+ Handle<Function> f2 = Handle<Function>::Cast(global->Get(v8_str("f2"))); |
+ |
+ p->Chain(f1); |
+ CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); |
+ V8::RunMicrotasks(isolate); |
+ CHECK_EQ(1, global->Get(v8_str("x1"))->Int32Value()); |
+ |
+ p->Catch(f2); |
+ V8::RunMicrotasks(isolate); |
+ CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); |
+ |
+ r->Catch(f2); |
+ CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); |
+ V8::RunMicrotasks(isolate); |
+ CHECK_EQ(2, global->Get(v8_str("x2"))->Int32Value()); |
+ |
+ r->Chain(f1); |
+ V8::RunMicrotasks(isolate); |
+ CHECK_EQ(1, global->Get(v8_str("x1"))->Int32Value()); |
+ |
+ // Chaining pending promises. |
+ CompileRun("x1 = x2 = 0;"); |
+ p = v8::Promise::New(isolate); |
+ r = v8::Promise::New(isolate); |
+ |
+ p->Chain(f1); |
+ r->Catch(f2); |
+ V8::RunMicrotasks(isolate); |
+ CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); |
+ CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); |
+ |
+ p->Resolve(v8::Integer::New(isolate, 1)); |
+ r->Reject(v8::Integer::New(isolate, 2)); |
+ CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); |
+ CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); |
+ |
+ V8::RunMicrotasks(isolate); |
+ CHECK_EQ(1, global->Get(v8_str("x1"))->Int32Value()); |
+ CHECK_EQ(2, global->Get(v8_str("x2"))->Int32Value()); |
+ |
+ // Multi-chaining. |
+ CompileRun("x1 = x2 = 0;"); |
+ p = v8::Promise::New(isolate); |
+ p->Chain(f1)->Chain(f2); |
+ p->Resolve(v8::Integer::New(isolate, 3)); |
+ CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); |
+ CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); |
+ V8::RunMicrotasks(isolate); |
+ CHECK_EQ(3, global->Get(v8_str("x1"))->Int32Value()); |
+ CHECK_EQ(4, global->Get(v8_str("x2"))->Int32Value()); |
+ |
+ CompileRun("x1 = x2 = 0;"); |
+ r = v8::Promise::New(isolate); |
+ r->Catch(f1)->Chain(f2); |
+ r->Reject(v8::Integer::New(isolate, 3)); |
+ CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); |
+ CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); |
+ V8::RunMicrotasks(isolate); |
+ CHECK_EQ(3, global->Get(v8_str("x1"))->Int32Value()); |
+ CHECK_EQ(4, global->Get(v8_str("x2"))->Int32Value()); |
+} |