Skip to content

Instantly share code, notes, and snippets.

@gusaaaaa
Created April 24, 2013 20:56
Show Gist options
  • Save gusaaaaa/5455502 to your computer and use it in GitHub Desktop.
Save gusaaaaa/5455502 to your computer and use it in GitHub Desktop.
Gerbil new API proposal including support for promises (Promises/A+)
if(typeof module != "undefined")
var scenario = require("../lib/gerbil.js").scenario;
scenario("Gerbil - Assertions", {
"should be able to assert": function(g) {
g.assert(true, true);
g.assert(true); // g.assert(x, x) is equivalent to g.assert(x)
g.assert(1 == 1);
g.assert(true != false);
},
"should be able to assert a false statement": function(g) {
g.assert(fail, function() {
g.assert(true, false) // notice g.assert(false) == g.assert(false, false)
})
},
"should fail when an expected succeeding function doesn't raise an exception": function(g) {
g.assert(fail, function() {
g.assert(fail, function() {
// we are not raising anything
})
});
},
"should be able to validate the type of an object": function(g) {
g.assert(isA(Function), function() {});
g.assert(isA(Number), 42);
g.assert(isA(String), "Gerbil");
g.assert(failsWith(Error), function() {
g.assert(isA(Function), 42);
});
var f = function() { return 42 };
g.assert(succeedWithType(Number), f());
},
"should be able to evaluate promises": function(g) {
var promiseToSucceed = function(succeed) {
return new RSVP.Promise(function(resolve, reject) {
return (c ? resolve(c) : reject(c));
});
};
g.assert(promiseToSucceed(true), succeedWith(true));
g.assert(promiseToSucceed(true), succeed); // succeed is an alias for succeedWith()
g.assert(promiseToSucceed(true), true); // By default, it assumes we are expecting the promise to succeed
g.assert(promiseToSucceed(false), failWith(false));
g.assert(promiseToSucceed(false), fail); // fail is an alias for failWith()
},
"should able to evaluate plain old sync functions": function(g){
var succeedFunction = function(succeed) {
if (succeed) {
return true;
} else {
throw new Error("something happened");
}
};
g.assert(succeedFunction(true), succeedWith(true));
g.assert(succeedFunction(true), succeed); // succeed is an alias for succeedWith()
g.assert(succeedFunction(true), true); // by default, it assumes we are expecting the function to succeed
g.assert(succeedFunction(false), failWith(Error));
g.assert(succeedFunction(false), fail); // fail is an alias for failWith()
g.assert(succeedFunction(false), failWith("something happened")); // for consistency purposes... not sure though!
}),
"should accept arguments in any order": function(g){
// Promises
var fulfilledPromise = new RSVP.Promise(function(resolve, reject) { resolve() });
var rejectedPromise = new RSVP.Promise(function(resolve, reject) { reject() });
g.assert(succeed, fulfilledPromise);
g.assert(fail, rejectedPromise);
// Functions
var okFunction = function() { return null };
var failFunction = function() { throw new Error() };
g.assert(succeed, okFunction);
g.assert(fail, failFunction);
},
"should rise an error if expected and thrown errors don't match": function(g) {
function CustomError1() {}
function CustomError2() {}
g.assert(fail, function() {
g.assert(failWith(CustomError1), function(){
throw new CustomError2();
});
});
},
});
scenario("Gerbil - setTimeout", {
"setup": function() {
this.name = "timeout";
},
"should access gerbil test scenario": function(g){
var a = 1;
g.setTimeout(function(){
a++;
g.assert(a, 3);
}, 1000);
a++;
},
"should access context": function(g) {
g.setTimeout(function() {
g.assert(this.name, "timeout");
}, 1000);
}
});
scenario("Gerbil - context access for tests", {
"setup": function(g) {
this.value = 1;
},
"should access context": function(g) {
g.assert(this.value, 1);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment