Add expect.js

This commit is contained in:
teppeis
2013-05-11 22:05:30 +09:00
parent 0a279c0e4b
commit e7707e4aca
2 changed files with 337 additions and 0 deletions
+121
View File
@@ -0,0 +1,121 @@
/// <reference path="expect.js.d.ts" />
function test_expect() {
expect();
expect(1);
expect(true);
expect({});
expect(0);
}
function test_expect_properties() {
expect(0).be.an;
expect(0).have.own;
expect(0).not.be.an;
expect(0).not.have.own;
expect(0).not.include;
expect(0).not.only.have.own;
expect(0).not.to.be;
expect(0).not.to.have.own;
expect(0).not.to.include;
expect(0).not.to.only.have.own;
expect(0).only.have.own;
expect(0).to.be.an;
expect(0).to.have.own;
expect(0).to.include;
expect(0).to.not.be.an;
expect(0).to.not.have.own;
expect(0).to.not.include;
expect(0).to.not.only.have.own;
expect(0).to.only.have.own;
expect(0).be
}
function test_ok() {
expect(true).to.be.ok();
}
function test_be() {
expect(1).to.be(1);
}
function test_equal() {
expect(1).to.equal(1);
}
function test_eql() {
expect({ a: 'b' }).to.eql({ a: 'b' });
}
function test_a() {
// string
expect(5).to.be.a('number');
expect([]).to.be.an('array');
// constructors
expect(5).to.be.a(Number);
expect([]).to.be.an(Array);
}
function test_match() {
expect('1.2.3').to.match(/[0-9]+\.[0-9]+\.[0-9]+/);
}
function test_contain() {
// string
expect('hello world').to.contain('world');
expect('hello world').to.string('world');
// any
expect([1, 2]).to.contain(1);
expect([1, 2]).to.string(1);
}
function test_length() {
expect([1,2,3]).to.have.length(3);
}
function test_empty() {
expect([]).to.be.empty();
}
function test_property() {
expect(window).to.have.property('expect');
expect(window).to.have.property('expect', expect);
}
function test_key() {
expect({ a: 'b' }).to.have.key('a');
expect({ a: 'b' }).to.include.key('a');
expect({ a: 'b', c: 'd' }).to.only.have.keys('a', 'c');
expect({ a: 'b', c: 'd' }).to.only.have.keys(['a', 'c']);
expect({ a: 'b', c: 'd' }).to.not.only.have.key('a');
}
function test_throwException() {
var fn = () => {};
expect(fn).to.throwError();
expect(fn).to.throwException(function (e) {
expect(e).to.be.a(SyntaxError);
});
expect(fn).to.throwException(/matches the exception message/);
expect(fn).to.not.throwException();
}
function test_within() {
expect(1).to.be.within(0, Infinity);
}
function test_greaterThan() {
expect(5).to.be.greaterThan(3);
expect(3).to.be.above(0);
}
function test_lessThan() {
expect(1).to.be.lessThan(3);
expect(0).to.be.below(3);
}
function test_fail() {
expect().fail();
expect().fail('Custom failure message');
}
+216
View File
@@ -0,0 +1,216 @@
// Type definitions for expect.js 0.2.0
// Project: https://github.com/LearnBoost/expect.js
// Definitions by: Teppei Sato <https://github.com/teppeis>
// DefinitelyTyped: https://github.com/borisyankov/DefinitelyTyped
declare function expect(target?: any): Expect.Root;
module Expect {
interface Assertion {
/**
* Check if the value is truthy
*/
ok();
/**
* Assert that the function throws.
*
* @param fn callback to match error string against
*/
throwError(fn?: Function);
/**
* Assert that the function throws.
*
* @param fn callback to match error string against
*/
throwException(fn?: Function);
/**
* Assert that the function throws.
*
* @param regexp regexp to match error string against
*/
throwError(regexp: RegExp);
/**
* Assert that the function throws.
*
* @param fn callback to match error string against
*/
throwException(regexp: RegExp);
/**
* Checks if the array is empty.
*/
empty(): Assertion;
/**
* Checks if the obj exactly equals another.
*/
equal(obj: any): Assertion;
/**
* Checks if the obj sortof equals another.
*/
eql(obj: any): Assertion;
/**
* Assert within start to finish (inclusive).
*
* @param start
* @param finish
*/
within(start: number, finish: number): Assertion;
/**
* Assert typeof.
*/
a(type: string): Assertion;
/**
* Assert instanceof.
*/
a(type: Function): Assertion;
/**
* Assert typeof / instanceof.
*/
an: An;
/**
* Assert numeric value above n.
*/
greaterThan(n: number): Assertion;
/**
* Assert numeric value above n.
*/
above(n: number): Assertion;
/**
* Assert numeric value below n.
*/
lessThan(n: number): Assertion;
/**
* Assert numeric value below n.
*/
below(n: number): Assertion;
/**
* Assert string value matches regexp.
*
* @param regexp
*/
match(regexp: RegExp): Assertion;
/**
* Assert property "length" exists and has value of n.
*
* @param n
*/
length(n: number): Assertion;
/**
* Assert property name exists, with optional val.
*
* @param name
* @param val
*/
property(name: string, val?: any): Assertion;
/**
* Assert that string contains str.
*/
contain(str: string): Assertion;
string(str: string): Assertion;
/**
* Assert that the array contains obj.
*/
contain(obj: any): Assertion;
string(obj: any): Assertion;
/**
* Assert exact keys or inclusion of keys by using the `.own` modifier.
*/
key(keys: string[]): Assertion;
/**
* Assert exact keys or inclusion of keys by using the `.own` modifier.
*/
key(...keys: string[]): Assertion;
/**
* Assert exact keys or inclusion of keys by using the `.own` modifier.
*/
keys(keys: string[]): Assertion;
/**
* Assert exact keys or inclusion of keys by using the `.own` modifier.
*/
keys(...keys: string[]): Assertion;
/**
* Assert a failure.
*/
fail(message?: string): Assertion;
}
interface Root extends Assertion {
not: Not;
to: To;
only: Only;
have: Have;
be: Be;
}
interface Be extends Assertion {
/**
* Checks if the obj exactly equals another.
*/
(obj: any): Assertion;
an: An;
}
interface An extends Assertion {
/**
* Assert typeof.
*/
(type: string): Assertion;
/**
* Assert instanceof.
*/
(type: Function): Assertion;
}
interface Not extends Expect.NotBase {
to: Expect.ToBase;
}
interface NotBase extends Assertion {
be: Be;
have: Have;
include: Assertion;
only: Only;
}
interface To extends Expect.ToBase {
not: Expect.NotBase;
}
interface ToBase extends Assertion {
be: Be;
have: Have;
include: Assertion;
only: Only;
}
interface Only extends Assertion {
have: Have;
}
interface Have extends Assertion {
own: Assertion;
}
}