Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Started to implement jack.matchers. So far: is(), isNot(), matches(),…

… hasProperty(), hasProperties()

git-svn-id: http://boss.bekk.no/repos/projects/jack/trunk@4987 43881228-ca37-0410-962c-81b306aa3a78
  • Loading branch information...
commit 96ade0c929a2b73ee9d0d443b222f6e5da58f4e9 1 parent 25b463d
@keronsen keronsen authored
View
11 .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>
View
8 docs/todo.txt
@@ -15,9 +15,15 @@ REFACTORING:
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
* 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
* Text reports for argument constraints:
- isType()
View
93 src/jack.js
@@ -11,6 +11,8 @@ function jack() {} // This needs to be here to make error reporting work correct
(function (){ // START HIDING FROM GLOBAL SCOPE
/** EXPORT JACK **/
window.jack = new Jack();
+ window.jack.matchers = new Matchers();
+ window.jack.FunctionInvocation = FunctionInvocation;
return;
@@ -32,6 +34,7 @@ function jack() {} // This needs to be here to make error reporting work correct
api.create = create;
api.inspect = inspect;
api.expect = expect;
+ api.verify = verify;
api.report = report;
api.reportAll = reportAll;
api.env = environment;
@@ -172,6 +175,13 @@ function jack() {} // This needs to be here to make error reporting work correct
currentExpectation = findGrab(name).expect().once();
return currentExpectation;
}
+ function verify(name) {
+ if(findGrab(name) == null) {
+ grab(name);
+ }
+ currentExpectation = findGrab(name).expect().once();
+ return currentExpectation;
+ }
function report(name, expectation) {
return findGrab(name).report(expectation, name);
}
@@ -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
View
54 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);
+ }
+ */
+});
+
+*/
View
172 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();
+ }
+});
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
View
117 test/test_13_new_syntax_using_verify.js
@@ -0,0 +1,117 @@
+
+/*
+
+describe('Using verify() instead of expect() for objects created with jack.create()', {
+ before_all: function() {
+ jack.env.disableReporting();
+ }
+ ,
+ after_all: function() {
+ jack.env.enableReporting();
+ }
+ ,
+ 'Should verify correct number of calls': function() {
+ var someData = "Not important for the test";
+
+ jack(function(){
+ var myStack = jack.create("myStack", ["push","pop"]);
+ myStack.push(someData);
+ myStack.push(someData);
+ myStack.push(someData);
+ jack.verify("myStack.push").atLeast("3 times");
+ });
+
+ var report = jack.report("myStack.push");
+ value_of(report.expected).should_be(3);
+ value_of(report.success).should_be_true();
+ }
+
+
+ /*
+ ,
+ 'Should be able to specify a minimum number of expected calls (Example 2)': function() {
+ window.globalFunction = function() {}
+
+ jack(function(){
+ jack.expect("globalFunction").atLeast("3 times");
+ window.globalFunction();
+ window.globalFunction();
+ window.globalFunction();
+ });
+
+ var report = jack.report("globalFunction");
+ value_of(report.expected).should_be(3);
+ value_of(report.success).should_be_true();
+
+ window.globalFunction = null;
+ }
+ ,
+ 'Should be able to specify a minimum number of expected calls (Example 3)': function() {
+ window.globalFunction = function() {}
+
+ jack(function(){
+ jack.expect("globalFunction").atLeast("3 times");
+ window.globalFunction();
+ });
+
+ var report = jack.report("globalFunction");
+ value_of(report.expected).should_be(3);
+ value_of(report.success).should_be_false();
+
+ window.globalFunction = null;
+ }
+ ,
+ 'Should be able to specify a maximum number of expected calls (Example 1)': function() {
+ window.globalFunction = function() {}
+
+ jack(function(){
+ jack.expect("globalFunction").atMost("3 times");
+ window.globalFunction();
+ window.globalFunction();
+ window.globalFunction();
+ window.globalFunction();
+ });
+
+ var report = jack.report("globalFunction");
+ value_of(report.expected).should_be(3);
+ value_of(report.success).should_be_false();
+
+ window.globalFunction = null;
+ }
+ ,
+ 'Should be able to specify a maximum number of expected calls (Example 2)': function() {
+ window.globalFunction = function() {}
+
+ jack(function(){
+ jack.expect("globalFunction").atMost("3 times");
+ window.globalFunction();
+ window.globalFunction();
+ window.globalFunction();
+ });
+
+ var report = jack.report("globalFunction");
+ value_of(report.expected).should_be(3);
+ value_of(report.success).should_be_true();
+
+ window.globalFunction = null;
+ }
+ ,
+ 'Should be able to specify a maximum number of expected calls (Example 3)': function() {
+ window.globalFunction = function() {}
+
+ jack(function(){
+ jack.expect("globalFunction").atMost("3 times");
+ window.globalFunction();
+ window.globalFunction();
+ });
+
+ var report = jack.report("globalFunction");
+ value_of(report.expected).should_be(3);
+ value_of(report.success).should_be_true();
+
+ window.globalFunction = null;
+ }
+ */
+});
+
+*/
View
6 test/test_all.html
@@ -18,8 +18,12 @@
<script type="text/javascript" src="test_07_multiple_expectations_for_same_function.js"></script>
<script type="text/javascript" src="test_08_functions_with_properties.js"></script>
<script type="text/javascript" src="test_09_text_reports.js"></script>
-<script type="text/javascript" src="test_80_integration_jsspec.js"></script>
<script type="text/javascript" src="test_10_expectation_details.js"></script>
+<script type="text/javascript" src="test_11_FunctionInvocation_class.js"></script>
+<script type="text/javascript" src="test_12_matchers.js"></script>
+<script type="text/javascript" src="test_13_new_syntax_using_verify.js"></script>
+<script type="text/javascript" src="test_80_integration_jsspec.js"></script>
+
<script type="text/javascript" src="test_81_integration_scriptaculous.js"></script>
<script type="text/javascript" src="test_99_bugfix.js"></script>
Please sign in to comment.
Something went wrong with that request. Please try again.