Skip to content

Commit

Permalink
remove unused variable and merge
Browse files Browse the repository at this point in the history
  • Loading branch information
hotchemi committed Jun 12, 2013
2 parents a06f56f + c7f2e14 commit bc18d2a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 34 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ expect(fn).to.throwException(/matches the exception message/);
expect(fn2).to.not.throwException();
```

**withArgs**: creates anonymous function to call fn with arguments

```js
expect(fn).withArgs(invalid, arg).to.throwException();
expect(fn).withArgs(valid, arg).to.not.throwException();
```

**within**: asserts a number within a range

```js
Expand Down
97 changes: 64 additions & 33 deletions expect.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@

(function (global, module) {

if ('undefined' == typeof module) {
var module = { exports: {} }
, exports = module.exports
}
var exports = module.exports;

/**
* Exports.
Expand Down Expand Up @@ -71,7 +67,7 @@
var old = this[name];
this[name] = function () {
return old.apply(self, arguments);
}
};

for (var fn in Assertion.prototype) {
if (Assertion.prototype.hasOwnProperty(fn) && fn != name) {
Expand All @@ -91,12 +87,19 @@
* @api private
*/

Assertion.prototype.assert = function (truth, msg, error) {
Assertion.prototype.assert = function (truth, msg, error, expected) {
var msg = this.flags.not ? error : msg
, ok = this.flags.not ? !truth : truth;
, ok = this.flags.not ? !truth : truth
, err;

if (!ok) {
throw new Error(msg.call(this));
err = new Error(msg.call(this));
if (arguments.length > 3) {
err.actual = this.obj;
err.expected = expected;
err.showDiff = true;
}
throw err;
}

this.and = new Assertion(this.obj);
Expand All @@ -115,6 +118,19 @@
, function(){ return 'expected ' + i(this.obj) + ' to be falsy' });
};

/**
* Creates an anonymous function which calls fn with arguments.
*
* @api public
*/

Assertion.prototype.withArgs = function() {
expect(this.obj).to.be.a('function');
var fn = this.obj;
var args = Array.prototype.slice.call(arguments);
return expect(function() { fn.apply(null, args); });
};

/**
* Assert that the function throws.
*
Expand All @@ -132,20 +148,20 @@
try {
this.obj();
} catch (e) {
if ('function' == typeof fn) {
fn(e);
} else if ('object' == typeof fn) {
if (isRegExp(fn)) {
var subject = 'string' == typeof e ? e : e.message;
if (not) {
expect(subject).to.not.match(fn);
} else {
expect(subject).to.match(fn);
}
} else if ('function' == typeof fn) {
fn(e);
}
thrown = true;
}

if ('object' == typeof fn && not) {
if (isRegExp(fn) && not) {
// in the presence of a matcher, ensure the `not` only applies to
// the matching.
this.flags.not = false;
Expand Down Expand Up @@ -212,9 +228,10 @@

Assertion.prototype.eql = function (obj) {
this.assert(
expect.eql(obj, this.obj)
expect.eql(this.obj, obj)
, function(){ return 'expected ' + i(this.obj) + ' to sort of equal ' + i(obj) }
, function(){ return 'expected ' + i(this.obj) + ' to sort of not equal ' + i(obj) });
, function(){ return 'expected ' + i(this.obj) + ' to sort of not equal ' + i(obj) }
, obj);
return this;
};

Expand Down Expand Up @@ -250,9 +267,10 @@
// typeof with support for 'array'
this.assert(
'array' == type ? isArray(this.obj) :
'object' == type
? 'object' == typeof this.obj && null !== this.obj
: type == typeof this.obj
'regexp' == type ? isRegExp(this.obj) :
'object' == type
? 'object' == typeof this.obj && null !== this.obj
: type == typeof this.obj
, function(){ return 'expected ' + i(this.obj) + ' to be a' + n + ' ' + type }
, function(){ return 'expected ' + i(this.obj) + ' not to be a' + n + ' ' + type });
} else {
Expand Down Expand Up @@ -467,8 +485,8 @@
* @api public
*/
Assertion.prototype.fail = function (msg) {
msg = msg || "explicit failure";
this.assert(false, msg, msg);
var error = function() { return msg || "explicit failure"; }
this.assert(false, error, error);
return this;
};

Expand Down Expand Up @@ -519,14 +537,13 @@
; i < j && arr[i] !== o; i++);

return j <= i ? -1 : i;
};
}

// https://gist.github.com/1044128/
var getOuterHTML = function(element) {
if ('outerHTML' in element) return element.outerHTML;
var ns = "http://www.w3.org/1999/xhtml";
var container = document.createElementNS(ns, '_');
var elemProto = (window.HTMLElement || window.Element).prototype;
var xmlSerializer = new XMLSerializer();
var html;
if (document.xmlVersion) {
Expand Down Expand Up @@ -620,6 +637,11 @@
if (isDate(value) && $keys.length === 0) {
return stylize(value.toUTCString(), 'date');
}

// Error objects can be shortcutted
if (value instanceof Error) {
return stylize("["+value.toString()+"]", 'Error');
}

var base, type, braces;
// Determine the object type
Expand Down Expand Up @@ -743,8 +765,10 @@
return format(obj, (typeof depth === 'undefined' ? 2 : depth));
}

expect.stringify = i;

function isArray (ar) {
return Object.prototype.toString.call(ar) == '[object Array]';
return Object.prototype.toString.call(ar) === '[object Array]';
}

function isRegExp(re) {
Expand Down Expand Up @@ -870,13 +894,16 @@
// equivalence is determined by ==.
} else if (typeof actual != 'object' && typeof expected != 'object') {
return actual == expected;

// 7.4. For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values for every
// corresponding key, and an identical "prototype" property. Note: this
// accounts for both named and indexed properties on Arrays.
// If both are regular expression use the special `regExpEquiv` method
// to determine equivalence.
} else if (isRegExp(actual) && isRegExp(expected)) {
return regExpEquiv(actual, expected);
// 7.4. For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values for every
// corresponding key, and an identical "prototype" property. Note: this
// accounts for both named and indexed properties on Arrays.
} else {
return objEquiv(actual, expected);
}
Expand All @@ -890,6 +917,11 @@
return Object.prototype.toString.call(object) == '[object Arguments]';
}

function regExpEquiv (a, b) {
return a.source === b.source && a.global === b.global &&
a.ignoreCase === b.ignoreCase && a.multiline === b.multiline;
}

function objEquiv (a, b) {
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
return false;
Expand Down Expand Up @@ -958,7 +990,7 @@
f(d.getUTCHours()) + ':' +
f(d.getUTCMinutes()) + ':' +
f(d.getUTCSeconds()) + 'Z' : null;
};
}

var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
Expand Down Expand Up @@ -1248,6 +1280,5 @@

})(
this
, 'undefined' != typeof module ? module : {}
, 'undefined' != typeof exports ? exports : {}
, 'undefined' != typeof module ? module : {exports: {}}
);
23 changes: 23 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ describe('expect', function () {
}, "expected '' to equal false")
});

it('should test functions with arguments', function () {
function itThrowsSometimes (first, second) {
if (first ^ second) {
throw new Error('tell');
}
}

expect(itThrowsSometimes).withArgs(false, false).to.not.throwException();
expect(itThrowsSometimes).withArgs(false, true).to.throwException(/tell/);
expect(itThrowsSometimes).withArgs(true, false).to.throwException(/tell/);
expect(itThrowsSometimes).withArgs(true, true).to.not.throwException();
});

it('should test for exceptions', function () {
function itThrows () {
a.b.c;
Expand Down Expand Up @@ -185,6 +198,15 @@ describe('expect', function () {
}, 'expected {} to be an array');
});

it('should test regex', function () {
expect(/a/).to.be.an('regexp');
expect(/a/).to.be.a('regexp');

err(function () {
expect(null).to.be.a('regexp');
}, 'expected null to be a regexp');
});

it('should test objects', function () {
expect({}).to.be.an('object');

Expand Down Expand Up @@ -288,6 +310,7 @@ describe('expect', function () {
expect({ foo: 'bar' }).to.eql({ foo: 'bar' });
expect(1).to.eql(1);
expect('4').to.eql(4);
expect(/a/gmi).to.eql(/a/mig);

err(function () {
expect(4).to.eql(3);
Expand Down
2 changes: 1 addition & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link href="/support/mocha.css" rel="stylesheet" media="screen" />
<script src="/support/jquery.js"></script>
<script src="/support/mocha.js"></script>
<script>mocha.setup('bdd');</script>
<script>mocha.setup({ ui: 'bdd', globals: ['script*']});</script>
<script src="/expect.js"></script>
<script src="/test/expect.js"></script>
<script>window.onload = mocha.run;</script>
Expand Down

0 comments on commit bc18d2a

Please sign in to comment.