Skip to content

Commit

Permalink
Modularized the solution a bit more.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmohl committed Dec 23, 2011
1 parent 3b0aa23 commit 81a403e
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 50 deletions.
20 changes: 20 additions & 0 deletions MIT.License
Original file line number Diff line number Diff line change
@@ -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.
Binary file removed lib/api.min.gz.js
Binary file not shown.
1 change: 0 additions & 1 deletion lib/api.min.js

This file was deleted.

Binary file removed lib/api.pavlov.min.gz.js
Binary file not shown.
1 change: 0 additions & 1 deletion lib/api.pavlov.min.js

This file was deleted.

36 changes: 28 additions & 8 deletions lib/expectThat.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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) {
Expand All @@ -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",
Expand All @@ -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) {
Expand All @@ -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);
Binary file modified lib/expectThat.min.gz.js
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/expectThat.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/api.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 8 additions & 0 deletions src/api.customMatchers.coffee
Original file line number Diff line number Diff line change
@@ -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= {}
1 change: 1 addition & 0 deletions src/api.pavlov.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 35 additions & 0 deletions src/core.coffee
Original file line number Diff line number Diff line change
@@ -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= {}
40 changes: 3 additions & 37 deletions src/expectThat.coffee
Original file line number Diff line number Diff line change
@@ -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" ###
7 changes: 7 additions & 0 deletions src/util.coffee
Original file line number Diff line number Diff line change
@@ -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.