Browse files

Modularized the solution a bit more.

  • Loading branch information...
1 parent 3b0aa23 commit 81a403eb0eef3be5309ade1b41839adcda267790 @dmohl committed Dec 23, 2011
View
20 MIT.License
@@ -0,0 +1,20 @@
+Copyright (c) 2008-2011 Pivotal Labs
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
BIN lib/api.min.gz.js
Binary file not shown.
View
1 lib/api.min.js
@@ -1 +0,0 @@
-(function(a){return a.api={be:function(a){return a},to:function(a){return a},equal:function(a){return a},throwException:function(a){return{assertionType:"throw",expected:a}},greaterThan:function(a){return{assertionType:"greaterThan",expected:a}},extendApi:function(a,b){var c;return Object.prototype.should||this.init(b),c=a.toString().match(/^[^\{]*\{((.*\s*)*)\}/m)[1],c.replace(/(\^\s+|\s)+$/g,"").replace(/[(\^.(?)]/g," ").replace(/return/g," ").replace(/shouldnt/g,"shouldn't").replace(/void 0/g,"null").replace(/!= null/g,"").replace(/typeof null !== "undefined" && null !== null/g,"undefined")}},this})(expectThat||(expectThat={}))
View
BIN lib/api.pavlov.min.gz.js
Binary file not shown.
View
1 lib/api.pavlov.min.js
@@ -1 +0,0 @@
-(function(a,b){return a.api.pavlov={expectThat:function(a){var c;return c=this.extendApi(a,b.api),b.api.it(c,a)}},b.util.extend(a,a.api),b.util.extend(a,a.api.pavlov),b.util.extend(b.api,a)})(expectThat||(expectThat={}),window.pavlov)
View
36 lib/expectThat.js
@@ -53,6 +53,19 @@ expectThat = (function(expectThat) {
};
})(expectThat || (expectThat = {}));
(function(expectThat) {
+ expectThat.util = {
+ extend: function(destintation, source) {
+ var method, name;
+ for (name in source) {
+ method = source[name];
+ destintation[name] = method;
+ }
+ return destintation;
+ }
+ };
+ return this;
+})(expectThat || (expectThat = {}));
+(function(expectThat) {
expectThat.api = {
be: function(expected) {
return expected;
@@ -69,6 +82,19 @@ expectThat = (function(expectThat) {
"expected": expected
};
},
+ extendApi: function(fn, assertProvder) {
+ var description;
+ if (!Object.prototype.should) {
+ this.init(assertProvder);
+ }
+ description = fn.toString().match(/^[^\{]*\{((.*\s*)*)\}/m)[1];
+ return description.replace(/(\^\s+|\s)+$/g, "").replace(/[(\^(?)]/g, " ").replace(/.should/g, " should").replace(/return/g, " ").replace(/shouldnt/g, "shouldn't").replace(/void 0/g, "null").replace(/!= null/g, "").replace(/typeof null !== "undefined" && null !== null/g, "undefined");
+ }
+ };
+ return this;
+})(expectThat || (expectThat = {}));
+(function(expectThat) {
+ expectThat.api.customMatchers = {
greaterThan: function(expected) {
return {
"assertionType": "greaterThan",
@@ -86,16 +112,9 @@ expectThat = (function(expectThat) {
return actual < expected;
}
};
- },
- extendApi: function(fn, assertProvder) {
- var description;
- if (!Object.prototype.should) {
- this.init(assertProvder);
- }
- description = fn.toString().match(/^[^\{]*\{((.*\s*)*)\}/m)[1];
- return description.replace(/(\^\s+|\s)+$/g, "").replace(/[(\^(?)]/g, " ").replace(/.should/g, " should").replace(/return/g, " ").replace(/shouldnt/g, "shouldn't").replace(/void 0/g, "null").replace(/!= null/g, "").replace(/typeof null !== "undefined" && null !== null/g, "undefined");
}
};
+ expectThat.util.extend(expectThat.api, expectThat.customMatchers);
return this;
})(expectThat || (expectThat = {}));
(function(expectThat, pavlov) {
@@ -107,6 +126,7 @@ expectThat = (function(expectThat) {
}
};
pavlov.util.extend(expectThat, expectThat.api);
+ pavlov.util.extend(expectThat, expectThat.api.customMatchers);
pavlov.util.extend(expectThat, expectThat.api.pavlov);
return pavlov.util.extend(pavlov.api, expectThat);
})(expectThat || (expectThat = {}), window.pavlov);
View
BIN lib/expectThat.min.gz.js
Binary file not shown.
View
2 lib/expectThat.min.js
@@ -1 +1 @@
-var expectThat;expectThat=function(a){return{init:function(a){var b;return b=this,Object.prototype.should=function(c){return b.evaluateAssertion(a,!0,this,c)},Object.prototype.shouldnt=function(c){return b.evaluateAssertion(a,!1,this,c)},this},evaluateAssertion:function(a,b,c,d){var e,f,g,h;h=d,g=d,typeof d=="function"&&(g=d()),typeof g!="undefined"&&g!==null&&(f=g.assertionType,typeof g.expected!="undefined"&&(h=g.expected));if(typeof f!="undefined"){if(f==="throw")return typeof g.expected!="undefined"?a.assert(c).throwsException(h):a.assert(c).throwsException();e=a.assert(g.expr(c,h));if(b){if(e!==null)return e.isTrue()}else if(e!==null)return e.isFalse()}else{if(b)return a.assert(c).isEqualTo(h);if(!b)return a.assert(c).isNotEqualTo(h)}}}}(expectThat||(expectThat={})),function(a){return a.api={be:function(a){return a},to:function(a){return a},equal:function(a){return a},throwException:function(a){return{assertionType:"throw",expected:a}},greaterThan:function(a){return{assertionType:"greaterThan",expected:a,expr:function(a,b){return a>b}}},lessThan:function(a){return{assertionType:"lessThan",expected:a,expr:function(a,b){return a<b}}},extendApi:function(a,b){var c;return Object.prototype.should||this.init(b),c=a.toString().match(/^[^\{]*\{((.*\s*)*)\}/m)[1],c.replace(/(\^\s+|\s)+$/g,"").replace(/[(\^(?)]/g," ").replace(/.should/g," should").replace(/return/g," ").replace(/shouldnt/g,"shouldn't").replace(/void 0/g,"null").replace(/!= null/g,"").replace(/typeof null !== "undefined" && null !== null/g,"undefined")}},this}(expectThat||(expectThat={})),function(a,b){return a.api.pavlov={expectThat:function(a){var c;return c=this.extendApi(a,b.api),b.api.it(c,a)}},b.util.extend(a,a.api),b.util.extend(a,a.api.pavlov),b.util.extend(b.api,a)}(expectThat||(expectThat={}),window.pavlov)
+var expectThat;expectThat=function(a){return{init:function(a){var b;return b=this,Object.prototype.should=function(c){return b.evaluateAssertion(a,!0,this,c)},Object.prototype.shouldnt=function(c){return b.evaluateAssertion(a,!1,this,c)},this},evaluateAssertion:function(a,b,c,d){var e,f,g,h;h=d,g=d,typeof d=="function"&&(g=d()),typeof g!="undefined"&&g!==null&&(f=g.assertionType,typeof g.expected!="undefined"&&(h=g.expected));if(typeof f!="undefined"){if(f==="throw")return typeof g.expected!="undefined"?a.assert(c).throwsException(h):a.assert(c).throwsException();e=a.assert(g.expr(c,h));if(b){if(e!==null)return e.isTrue()}else if(e!==null)return e.isFalse()}else{if(b)return a.assert(c).isEqualTo(h);if(!b)return a.assert(c).isNotEqualTo(h)}}}}(expectThat||(expectThat={})),function(a){return a.util={extend:function(a,b){var c,d;for(d in b)c=b[d],a[d]=c;return a}},this}(expectThat||(expectThat={})),function(a){return a.api={be:function(a){return a},to:function(a){return a},equal:function(a){return a},throwException:function(a){return{assertionType:"throw",expected:a}},extendApi:function(a,b){var c;return Object.prototype.should||this.init(b),c=a.toString().match(/^[^\{]*\{((.*\s*)*)\}/m)[1],c.replace(/(\^\s+|\s)+$/g,"").replace(/[(\^(?)]/g," ").replace(/.should/g," should").replace(/return/g," ").replace(/shouldnt/g,"shouldn't").replace(/void 0/g,"null").replace(/!= null/g,"").replace(/typeof null !== "undefined" && null !== null/g,"undefined")}},this}(expectThat||(expectThat={})),function(a){return a.api.customMatchers={greaterThan:function(a){return{assertionType:"greaterThan",expected:a,expr:function(a,b){return a>b}}},lessThan:function(a){return{assertionType:"lessThan",expected:a,expr:function(a,b){return a<b}}}},a.util.extend(a.api,a.customMatchers),this}(expectThat||(expectThat={})),function(a,b){return a.api.pavlov={expectThat:function(a){var c;return c=this.extendApi(a,b.api),b.api.it(c,a)}},b.util.extend(a,a.api),b.util.extend(a,a.api.customMatchers),b.util.extend(a,a.api.pavlov),b.util.extend(b.api,a)}(expectThat||(expectThat={}),window.pavlov)
View
2 src/api.coffee
@@ -4,8 +4,6 @@
to: (expected) -> expected
equal: (expected) -> expected
throwException: (expected) -> "assertionType": "throw", "expected": expected
- greaterThan: (expected) -> "assertionType": "greaterThan", "expected": expected, "expr": (actual, expected) -> actual > expected
- lessThan: (expected) -> "assertionType": "lessThan", "expected": expected, "expr": (actual, expected) -> actual < expected
extendApi: (fn, assertProvder) ->
@.init assertProvder if not Object.prototype.should
description = fn.toString().match(/^[^\{]*\{((.*\s*)*)\}/m)[1]
View
8 src/api.customMatchers.coffee
@@ -0,0 +1,8 @@
+((expectThat) ->
+ expectThat.api.customMatchers =
+ greaterThan: (expected) -> "assertionType": "greaterThan", "expected": expected, "expr": (actual, expected) -> actual > expected
+ lessThan: (expected) -> "assertionType": "lessThan", "expected": expected, "expr": (actual, expected) -> actual < expected
+
+ expectThat.util.extend expectThat.api, expectThat.customMatchers
+ @
+) expectThat or= {}
View
1 src/api.pavlov.coffee
@@ -7,6 +7,7 @@
pavlov.api.it testDescription, fn
pavlov.util.extend expectThat, expectThat.api
+ pavlov.util.extend expectThat, expectThat.api.customMatchers
pavlov.util.extend expectThat, expectThat.api.pavlov
pavlov.util.extend pavlov.api, expectThat
) expectThat or= {}, window.pavlov
View
35 src/core.coffee
@@ -0,0 +1,35 @@
+expectThat = ((expectThat) ->
+ init: (assertProvider) ->
+ _this = @
+ Object.prototype.should = (expected) ->
+ _this.evaluateAssertion assertProvider, true, @, expected
+ Object.prototype.shouldnt = (expected) ->
+ _this.evaluateAssertion assertProvider, false, @, expected
+ @
+ evaluateAssertion: (assertProvider, isShould, actual, expectedValueProvider) ->
+ expected = expectedValueProvider
+ customAssertion = expectedValueProvider
+ if typeof expectedValueProvider is "function" then customAssertion = expectedValueProvider()
+ if typeof customAssertion isnt "undefined" and customAssertion isnt null
+ assertionType = customAssertion.assertionType
+ expected = customAssertion.expected if typeof customAssertion.expected isnt "undefined"
+
+ # TODO: This is a mess. Replace with an array that can be extended?
+ if typeof assertionType isnt "undefined"
+ if assertionType is "throw"
+ if typeof customAssertion.expected isnt "undefined"
+ assertProvider.assert(actual).throwsException expected
+ else
+ assertProvider.assert(actual).throwsException()
+ else
+ # TODO: Add a nice error message if expectedVal is null/undefined or not a function
+ assertionToEvaluate = assertProvider.assert(customAssertion.expr actual, expected)
+ if isShould
+ if assertionToEvaluate isnt null then assertionToEvaluate.isTrue()
+ else
+ if assertionToEvaluate isnt null then assertionToEvaluate.isFalse()
+ else if isShould
+ assertProvider.assert(actual).isEqualTo expected
+ else if not isShould
+ assertProvider.assert(actual).isNotEqualTo expected
+) expectThat or= {}
View
40 src/expectThat.coffee
@@ -1,39 +1,5 @@
-expectThat = ((expectThat) ->
- #executeAssertion: (
- init: (assertProvider) ->
- _this = @
- Object.prototype.should = (expected) ->
- _this.evaluateAssertion assertProvider, true, @, expected
- Object.prototype.shouldnt = (expected) ->
- _this.evaluateAssertion assertProvider, false, @, expected
- @
- evaluateAssertion: (assertProvider, isShould, actual, expectedValueProvider) ->
- expected = expectedValueProvider
- customAssertion = expectedValueProvider
- if typeof expectedValueProvider is "function" then customAssertion = expectedValueProvider()
- if typeof customAssertion isnt "undefined" and customAssertion isnt null
- assertionType = customAssertion.assertionType
- expected = customAssertion.expected if typeof customAssertion.expected isnt "undefined"
-
- # TODO: This is a mess. Replace with an array that can be extended?
- if typeof assertionType isnt "undefined"
- if assertionType is "throw"
- if typeof customAssertion.expected isnt "undefined"
- assertProvider.assert(actual).throwsException expected
- else
- assertProvider.assert(actual).throwsException()
- else
- # TODO: Add a nice error message if expectedVal is null/undefined or not a function
- assertionToEvaluate = assertProvider.assert(customAssertion.expr actual, expected)
- if isShould
- if assertionToEvaluate isnt null then assertionToEvaluate.isTrue()
- else
- if assertionToEvaluate isnt null then assertionToEvaluate.isFalse()
- else if isShould
- assertProvider.assert(actual).isEqualTo expected
- else if not isShould
- assertProvider.assert(actual).isNotEqualTo expected
-) expectThat or= {}
-
+###import "core.coffee" ###
+###import "util.coffee" ###
###import "api.coffee" ###
+###import "api.customMatchers.coffee" ###
###import "api.pavlov.coffee" ###
View
7 src/util.coffee
@@ -0,0 +1,7 @@
+((expectThat) ->
+ expectThat.util =
+ extend: (destintation, source) ->
+ destintation[name] = method for name, method of source
+ destintation
+ @
+) expectThat or= {}

0 comments on commit 81a403e

Please sign in to comment.