Permalink
Browse files

Merge branch 'develop' into feature/genmin

  • Loading branch information...
2 parents 1aa84a1 + 8fe3923 commit c73f809f166ac7709a17be145c0a3f0fff7401db @foobarfighter committed Jun 25, 2011
Showing with 72 additions and 15 deletions.
  1. +1 −1 site/www/src/layouts/home.html
  2. +20 −0 spec/shared/base_spec.js
  3. +38 −1 src/base.js
  4. +13 −13 src/matchers.js
@@ -42,7 +42,7 @@
<br><br>
var foounit = require('foounit');<br>
<br>
- describe('this is ta test', function(){<br>
+ describe('this is a test', function(){<br>
&nbsp;&nbsp;it('is only a test', function() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;expect(myTest).to(runEverywhere);<br>
&nbsp;&nbsp;});<br>
@@ -41,6 +41,26 @@ foounit.add(function (kw){ with(kw){
});
});
+ describe('.addMatcher/.removeMatcher', function (){
+ it('adds a matcher to the keywords as well as to the expectations', function (){
+ foounit.addMatcher('beRainbow', function (){
+ this.match = function (){ return true; }
+ this.notMatch = function () { throw new Error('expected'); }
+ });
+
+ expect('unicorn eating').toBeRainbow();
+ expect('unicorn eating').to(beRainbow);
+ expect(function (){ expect('x').toNotBeRainbow(); }).to(throwError, /expected/);
+ expect(function (){ expect('x').toNot(beRainbow); }).to(throwError, /expected/);
+
+ foounit.removeMatcher('beRainbow');
+
+ expect(typeof foounit.Expectation.prototype.toBeRainbow).toNot(be, 'function');
+ expect(typeof foounit.Expectation.prototype.toNotBeRainbow).toNot(be, 'function');
+ expect(typeof beRainbow).to(be, 'undefined');
+ });
+ });
+
describe('scope', function (){
it('mixes in the foounit keywords into the scope object that was passed', function (){
var obj = {};
View
@@ -283,7 +283,44 @@ foounit = typeof foounit === 'undefined' ? {} : foounit;
foounit.removeKeyword = function (keyword){
delete foounit.keywords[keyword];
if (_kwScope){ delete _kwScope[keyword]; }
- }
+ };
+
+ (function (){
+
+ // Return a matcher keyword suffix
+ // keyword = haveBeenCalled returns HaveBeenCalled
+ var sfix = function (keyword) {
+ return keyword.substr(0, 1).toUpperCase() + keyword.substr(1)
+ };
+
+ /**
+ * Adds a matcher
+ */
+ foounit.addMatcher = function (matcherKeyword, definition){
+ foounit.addKeyword(matcherKeyword, definition); // Add the keyword to the kw scope
+
+ var suffix = sfix(matcherKeyword)
+ , instance = new definition()
+ , proto = foounit.Expectation.prototype;
+
+ proto['to' + suffix] = instance.match;
+ proto['toNot' + suffix] = instance.notMatch;
+ }
+
+ /**
+ * Removes a matcher
+ */
+ foounit.removeMatcher = function (matcherKeyword){
+ foounit.removeKeyword(matcherKeyword); // Remove the keyword from the kw scope
+
+ var suffix = sfix(matcherKeyword)
+ , proto = foounit.Expectation.prototype;
+
+ delete proto['to' + suffix];
+ delete proto['toNot' + suffix];
+ }
+
+ })();
/**
* Puts all of the foounit keywords in the global scope
View
@@ -5,7 +5,7 @@ if (foounit.hostenv.type == 'node'){
/**
* Asserts that a function throws an error
*/
-foounit.addKeyword('throwError', function (){
+foounit.addMatcher('throwError', function (){
this.match = function (actual, expected){
// actual == block
// expected == error
@@ -22,7 +22,7 @@ foounit.addKeyword('throwError', function (){
/**
* Asserts type and object
*/
-foounit.addKeyword('be', function (){
+foounit.addMatcher('be', function (){
this.match = function (actual, expected){
assert.strictEqual(actual, expected);
}
@@ -35,7 +35,7 @@ foounit.addKeyword('be', function (){
/**
* Asserts that actual === null
*/
-foounit.addKeyword('beNull', function (){
+foounit.addMatcher('beNull', function (){
this.match = function (actual){
assert.strictEqual(actual, null);
}
@@ -48,7 +48,7 @@ foounit.addKeyword('beNull', function (){
/**
* Asserts that actual === undefined
*/
-foounit.addKeyword('beUndefined', function (){
+foounit.addMatcher('beUndefined', function (){
this.match = function (actual){
assert.strictEqual(actual, undefined);
}
@@ -61,7 +61,7 @@ foounit.addKeyword('beUndefined', function (){
/**
* Assert that actual is greater than expected
*/
-foounit.addKeyword('beGt', function (){
+foounit.addMatcher('beGt', function (){
this.match = function (actual, expected){
if (actual > expected){ return; }
assert.fail(actual, expected, null, '>');
@@ -76,7 +76,7 @@ foounit.addKeyword('beGt', function (){
/**
* Assert that actual is less than expected
*/
-foounit.addKeyword('beLt', function (){
+foounit.addMatcher('beLt', function (){
this.match = function (actual, expected){
if (actual < expected){ return; }
assert.fail(actual, expected, null, '<');
@@ -91,7 +91,7 @@ foounit.addKeyword('beLt', function (){
/**
* Asserts true === actual
*/
-foounit.addKeyword('beTrue', function (){
+foounit.addMatcher('beTrue', function (){
// expected is unused
this.notMatch = function (actual){
assert.notStrictEqual(actual, true);
@@ -106,7 +106,7 @@ foounit.addKeyword('beTrue', function (){
/**
* Asserts that actual is truthy
*/
-foounit.addKeyword('beTruthy', function (){
+foounit.addMatcher('beTruthy', function (){
this.notMatch = function (actual){
if (!actual){ return };
assert.fail('Expected "' + actual + '" to NOT be truthy');
@@ -121,7 +121,7 @@ foounit.addKeyword('beTruthy', function (){
/**
* Asserts true === actual
*/
-foounit.addKeyword('beFalse', function (){
+foounit.addMatcher('beFalse', function (){
// expected is unused
this.notMatch = function (actual){
assert.notStrictEqual(actual, false);
@@ -136,7 +136,7 @@ foounit.addKeyword('beFalse', function (){
/**
* Asserts that actual is falsy
*/
-foounit.addKeyword('beFalsy', function (){
+foounit.addMatcher('beFalsy', function (){
this.notMatch = function (actual){
if (actual){ return; }
assert.fail('Expected "' + actual + '" to NOT be falsy');
@@ -151,7 +151,7 @@ foounit.addKeyword('beFalsy', function (){
/**
* Asserts deep equality
*/
-foounit.addKeyword('equal', function (){
+foounit.addMatcher('equal', function (){
var pSlice = Array.prototype.slice;
var isArguments = function (value){
@@ -183,7 +183,7 @@ foounit.addKeyword('equal', function (){
/**
* Asserts that actual has an element that === expected
*/
-foounit.addKeyword('include', function (){
+foounit.addMatcher('include', function (){
var find = function (actual, expected){
if (!expected || (expected.constructor != Array && !expected.callee)){
expected = [expected];
@@ -214,7 +214,7 @@ foounit.addKeyword('include', function (){
}
});
-foounit.addKeyword('match', function (){
+foounit.addMatcher('match', function (){
this.notMatch = function (actual, expected){
if (!expected.exec(actual)){ return; }
assert.fail(actual, expected, null, expected + ' matches');

0 comments on commit c73f809

Please sign in to comment.