Skip to content

Commit

Permalink
Started to implement jack.matchers. So far: is(), isNot(), matches(),…
Browse files Browse the repository at this point in the history
… hasProperty(), hasProperties()

git-svn-id: http://boss.bekk.no/repos/projects/jack/trunk@4987 43881228-ca37-0410-962c-81b306aa3a78
  • Loading branch information
keronsen committed Feb 28, 2009
1 parent 25b463d commit 96ade0c
Show file tree
Hide file tree
Showing 7 changed files with 459 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .project
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Jack</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>
8 changes: 7 additions & 1 deletion docs/todo.txt
Expand Up @@ -15,9 +15,15 @@ REFACTORING:


FEATURES: FEATURES:


* Implement a new jack.verify() syntax that can be called *after* execution instead of jack.expect() that needs to be called *before*
- For objects created with jack.create();
- For existsing, global objects

* (Details)
- Rename "expectations" to "preparations"

* Adapt Jack to be useable with env.js - http://ejohn.org/projects/bringing-the-browser-to-the-server * Adapt Jack to be useable with env.js - http://ejohn.org/projects/bringing-the-browser-to-the-server
* Integrate Jack with Screw.Unit - http://github.com/nkallen/screw-unit/tree/master * Integrate Jack with Screw.Unit - http://github.com/nkallen/screw-unit/tree/master
* Implement a new jack.verify() syntax that can be called *after* execution instead of jack.expect() that needs to be called *before*
* Find out how to handle calling of original functions * Find out how to handle calling of original functions
* Text reports for argument constraints: * Text reports for argument constraints:
- isType() - isType()
Expand Down
93 changes: 93 additions & 0 deletions src/jack.js
Expand Up @@ -11,6 +11,8 @@ function jack() {} // This needs to be here to make error reporting work correct
(function (){ // START HIDING FROM GLOBAL SCOPE (function (){ // START HIDING FROM GLOBAL SCOPE
/** EXPORT JACK **/ /** EXPORT JACK **/
window.jack = new Jack(); window.jack = new Jack();
window.jack.matchers = new Matchers();
window.jack.FunctionInvocation = FunctionInvocation;
return; return;




Expand All @@ -32,6 +34,7 @@ function jack() {} // This needs to be here to make error reporting work correct
api.create = create; api.create = create;
api.inspect = inspect; api.inspect = inspect;
api.expect = expect; api.expect = expect;
api.verify = verify;
api.report = report; api.report = report;
api.reportAll = reportAll; api.reportAll = reportAll;
api.env = environment; api.env = environment;
Expand Down Expand Up @@ -172,6 +175,13 @@ function jack() {} // This needs to be here to make error reporting work correct
currentExpectation = findGrab(name).expect().once(); currentExpectation = findGrab(name).expect().once();
return currentExpectation; return currentExpectation;
} }
function verify(name) {
if(findGrab(name) == null) {
grab(name);
}
currentExpectation = findGrab(name).expect().once();
return currentExpectation;
}
function report(name, expectation) { function report(name, expectation) {
return findGrab(name).report(expectation, name); return findGrab(name).report(expectation, name);
} }
Expand Down Expand Up @@ -573,6 +583,89 @@ function jack() {} // This needs to be here to make error reporting work correct
} }
} }
} }


/**
*
*/
function FunctionInvocation() {
var argumentConstraints = null;

return api();

function api() {
var api = matchers();
api.test = test;
return api;
}
function matchers() {
var m = {};
m.withNoArguments = function() { argumentConstraints = []; }
m.withArguments = function() { argumentConstraints = arguments; }
m.whereArgument = function(argIndex) {
return {};
}
return m;
}
function test() {
var result = true;
if(argumentConstraints != null) {
if(arguments.length != argumentConstraints.length) {
result = false;
} else {
for(var i=0; i<argumentConstraints.length; i++) {
if(arguments[i] != argumentConstraints[i]) {
result = false;
}
}
}
}
return result;
}

}


/**
*
*/
function Matchers() {
return {
'is':
function(a, b) {
return result(a==b);
},
'isNot':
function(a, b) {
return result(a!=b);
},
'matches':
function(a, b) {
return result(b.test(a))
},
'hasProperty':
function(a, b, c) {
return result(c ? a[b]==c : a[b]!=undefined)
},
'hasProperties':
function(a, b) {
var match = true;
for(var p in b) {
if(a[p] != b[p]) {
match = false;
}
}
return result(match);
}
}

function result(match) {
return {
result: match
}
}
}

})(); // END HIDING FROM GLOBAL SCOPE })(); // END HIDING FROM GLOBAL SCOPE




Expand Down
54 changes: 54 additions & 0 deletions test/test_11_FunctionInvocation_class.js
@@ -0,0 +1,54 @@

/*
describe('FunctionInvocation class', {
'Class should exist': function() {
var inv = new jack.FunctionInvocation();
value_of(inv).should_not_be_undefined();
}
,
'Should have test() method': function() {
var inv = new jack.FunctionInvocation();
var result = inv.test();
value_of(result).should_be_true();
}
,
'Should specify that no arguments are expected': function() {
var inv = new jack.FunctionInvocation();
inv.withNoArguments();
value_of(inv.test()).should_be_true();
value_of(inv.test("foo")).should_be_false();
}
,
'Should check for equal strings': function() {
var inv = new jack.FunctionInvocation();
inv.withArguments("argValue1","argValue2");
value_of(inv.test("foo","bar")).should_be_false();
value_of(inv.test("argValue1","argValue2")).should_be_true();
}
,
'whereArgument(n) should return all available matchers from jack.matchers': function() {
var inv = new jack.FunctionInvocation();
var matchers = inv.whereArgument(0);
value_of(matchers).should_not_be_undefined();
for(var m in jack.matchers) {
value_of(typeof matchers[m]).should_be("function");
}
}
/*
,
'Should check for equal numbers': function() {
jack(function(){
jack.expect("globalFunctionOne").exactly("1 time").withArguments(1234,5678);
jack.expect("globalFunctionTwo").exactly("1 time").withArguments(1234,5678);
window.globalFunctionOne(1111, 5555);
window.globalFunctionTwo(1234, 5678);
});
value_of(jack.report("globalFunctionOne").actual).should_be(0);
value_of(jack.report("globalFunctionTwo").actual).should_be(1);
}
*/
});

*/
172 changes: 172 additions & 0 deletions test/test_12_matchers.js
@@ -0,0 +1,172 @@


describe('Matching objects', {
'Namespace should exist': function() {
value_of(jack.matchers).should_not_be_undefined();
}
,
'is() should match on equal strings': function() {
value_of(jack.matchers.is("foo", "foo").result).should_be_true();
}
,
'is() should not match on unequal strings': function() {
value_of(jack.matchers.is("foo", "bar").result).should_be_false();
}
,
'is() should match on equal numbers': function() {
value_of(jack.matchers.is(1, 1).result).should_be_true();
}
,
'is() should not match on unequal numbers': function() {
value_of(jack.matchers.is(1, 2).result).should_be_false();
}
,
'is() should match on equal booleans': function() {
value_of(jack.matchers.is(true, true).result).should_be_true();
}
,
'is() should not match on unequal booleans': function() {
value_of(jack.matchers.is(true, false).result).should_be_false();
}
,
'is() should match on equal object references': function() {
var obj1 = {};
value_of(jack.matchers.is(obj1, obj1).result).should_be_true();
}
,
'is() should not match on unequal object references': function() {
var obj1 = {};
var obj2 = {};
value_of(jack.matchers.is(obj1, obj2).result).should_be_false();
}
,
'isNot() should not match on equal strings': function() {
value_of(jack.matchers.isNot("foo", "foo").result).should_be_false();
}
,
'isNot() should match on unequal strings': function() {
value_of(jack.matchers.isNot("foo", "bar").result).should_be_true();
}
,
'isNot() should not match on equal numbers': function() {
value_of(jack.matchers.isNot(1, 1).result).should_be_false();
}
,
'isNot() should match on unequal numbers': function() {
value_of(jack.matchers.isNot(1, 2).result).should_be_true();
}
,
'isNot() should not match on equal booleans': function() {
value_of(jack.matchers.isNot(true, true).result).should_be_false();
}
,
'isNot() should match on unequal booleans': function() {
value_of(jack.matchers.isNot(true, false).result).should_be_true();
}
,
'isNot() should not match on equal object references': function() {
var obj1 = {};
value_of(jack.matchers.isNot(obj1, obj1).result).should_be_false();
}
,
'isNot() should match on unequal object references': function() {
var obj1 = {};
var obj2 = {};
value_of(jack.matchers.isNot(obj1, obj2).result).should_be_true();
}
,
'matches() should match with regular expressions': function() {
var testString = "Homer Simpson is 40 years old...";
value_of(jack.matchers.matches(testString, /Homer/).result).should_be_true();
value_of(jack.matchers.matches(testString, /Flanders/).result).should_be_false();
value_of(jack.matchers.matches(testString, /^Homer/).result).should_be_true();
value_of(jack.matchers.matches(testString, /Simpson$/).result).should_be_false();
value_of(jack.matchers.matches(testString, /\d{2}/).result).should_be_true();
value_of(jack.matchers.matches(testString, /\d{3}/).result).should_be_false();
}
,
'hasProperty(a, b) should match if b is a property of a': function() {
value_of(jack.matchers.hasProperty({property1:'value1'}, "property1").result).should_be_true();
}
,
'hasProperty(a, b) should not match if b is not a property of a': function() {
value_of(jack.matchers.hasProperty({property1:'value1'}, "property2").result).should_be_false();
}
,
'hasProperty(a, b, c) should match if b is a property of a with value c': function() {
value_of(jack.matchers.hasProperty({property1:'value1'}, "property1", "value1").result).should_be_true();
}
,
'hasProperty(a, b, c) should not match if b is a property of a with a value other than c': function() {
value_of(jack.matchers.hasProperty({property1:'value1'}, "property1", "value2").result).should_be_false();
}
,
'hasProperty(a, b, c) should not match if b is not a property of a': function() {
value_of(jack.matchers.hasProperty({property1:'value1'}, "property2", "value2").result).should_be_false();
}
,
'hasProperties() should match if all properties have the expected values': function() {
var objectUnderTest = {
property1: 'value1',
property2: 'value2'
}
var propertyCheck = {
property1: 'value1',
property2: 'value2'
}
value_of(jack.matchers.hasProperties(objectUnderTest, propertyCheck).result).should_be_true();
}
,
'hasProperties() should not match if one of the properties have the wrong value': function() {
var objectUnderTest = {
property1: 'value1',
property2: 'value2'
}
var propertyCheck = {
property1: 'value1',
property2: 'somethingElse'
}
value_of(jack.matchers.hasProperties(objectUnderTest, propertyCheck).result).should_be_false();
}
,
'hasProperties() should not match if one of the properties is missing': function() {
var objectUnderTest = {
property1: 'value1'
}
var propertyCheck = {
property1: 'value1',
property2: 'value2'
}
value_of(jack.matchers.hasProperties(objectUnderTest, propertyCheck).result).should_be_false();
}
,
'hasProperties() should match if the object has extra properties': function() {
var objectUnderTest = {
property1: 'value1',
property2: 'value2',
property3: 'value3'
}
var propertyCheck = {
property1: 'value1',
property2: 'value2'
}
value_of(jack.matchers.hasProperties(objectUnderTest, propertyCheck).result).should_be_true();
}
});

















0 comments on commit 96ade0c

Please sign in to comment.