Skip to content

Instantly share code, notes, and snippets.

@fictorial
Created December 1, 2009 04:41
Show Gist options
  • Save fictorial/246061 to your computer and use it in GitHub Desktop.
Save fictorial/246061 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var promise_chain = require("../promise_chain"),
test = require("mjsunit"),
sys = require("sys");
function test_single_chain() {
callback_called = false;
var chain = new promise_chain.PromiseChain();
var link_0_promise = new process.Promise();
var link_0 = function () {
test.assertEquals(0, arguments.length);
return link_0_promise;
};
var length = chain.push(link_0);
test.assertEquals(1, length);
chain.promise.addCallback(function (result) {
test.assertEquals("link_0", result);
callback_called = true;
}).addErrback(function () {
test.assertTrue(false);
});
chain.run();
link_0_promise.emitSuccess("link_0");
}
function test_double_chain() {
callback_called = false;
var chain = new promise_chain.PromiseChain();
var link_0_promise = new process.Promise();
var link_0 = function (start_value) {
test.assertEquals("hello", start_value);
return link_0_promise;
};
var link_1_promise = new process.Promise();
var link_1 = function (value_from_prev_link) {
test.assertEquals("link_0", value_from_prev_link);
return link_1_promise;
};
var length = chain.push(link_0);
test.assertEquals(1, length);
length = chain.push(link_1);
test.assertEquals(2, length);
chain.promise.addCallback(function (result) {
test.assertEquals("link_1", result);
callback_called = true;
}).addErrback(function (e) {
sys.puts(e);
test.assertTrue(false);
});
chain.run("hello");
link_0_promise.emitSuccess("link_0");
link_1_promise.emitSuccess("link_1");
}
function test_triple_chain() {
callback_called = false;
var chain = new promise_chain.PromiseChain();
var link_0_promise = new process.Promise();
var link_0 = function (start_value) {
test.assertEquals("hello", start_value);
return link_0_promise;
};
var link_1_promise = new process.Promise();
var link_1 = function (value_from_prev_link) {
test.assertEquals("link_0", value_from_prev_link);
return link_1_promise;
};
var link_2_promise = new process.Promise();
var link_2 = function (value_from_prev_link) {
test.assertEquals("link_1", value_from_prev_link);
return link_2_promise;
};
var length = chain.push(link_0);
test.assertEquals(1, length);
length = chain.push(link_1);
test.assertEquals(2, length);
length = chain.push(link_2);
test.assertEquals(3, length);
chain.promise.addCallback(function (result) {
test.assertEquals("link_2", result);
callback_called = true;
}).addErrback(function (e) {
sys.puts(e);
test.assertTrue(false);
});
chain.run("hello");
link_0_promise.emitSuccess("link_0");
link_1_promise.emitSuccess("link_1");
link_2_promise.emitSuccess("link_2");
}
test_single_chain();
test_double_chain();
test_triple_chain();
process.addListener("exit", function () {
test.assertTrue(callback_called);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment