dwf: refactored Jasmine for less coupling; adding Crockford's helper

methods; crafted a new 'bootstrap' test in order to allow name
collisions with JSpec; better formatting of results in bootstrap test.
This commit is contained in:
Davis W. Frank 2008-11-29 18:12:55 -08:00
parent a2802923e2
commit 9f2d3e0540
10 changed files with 4611 additions and 45 deletions

BIN
images/accept.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 781 B

BIN
images/exclamation.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 701 B

View File

@ -20,44 +20,63 @@ Function.prototype.method = function (name, func) {
******************************************************************************/ ******************************************************************************/
/* /*
* Jasmine expectation constructor * Matchers methods; add your own with Matchers.method()
*/ */
var expects = function (actual) { Matchers = function (actual) {
var that = {}; this.actual = actual;
that.actual = actual; this.passing_message = 'Passed.'
}
that.should_equal = function(expected) { Matchers.method('report', function (result, failing_message) {
var message = 'Passed.';
result = (that.actual === expected);
if (!result) {
message = 'Expected ' + expected + ' but got ' + that.actual + '.';
}
Jasmine.results.push({ Jasmine.results.push({
passed: result, passed: result,
message: message message: result ? this.passing_message : failing_message
}); });
return result; return result;
} });
return that; Matchers.method('should_equal', function (expected) {
return this.report((this.actual === expected),
'Expected ' + expected + ' but got ' + this.actual + '.');
});
Matchers.method('should_not_equal', function (expected) {
return this.report((this.actual !== expected),
'Expected ' + expected + ' to not equal ' + this.actual + ', but it does.');
});
/*
* expects helper method that allows for chaining Matcher
*/
var expects_that = function (actual) {
return new Matchers(actual);
} }
var spec = function (description, func) { /*
* Jasmine spec constructor
*/
var it = function (description, func) {
return { return {
description: description, description: description,
execute: func execute: func
} }
} }
/*
* Jasmine constructor
*/
var jasmine_init = function () { var jasmine_init = function () {
return { return {
results: [] results: []
} }
} }
/*
* Jasmine instance
*/
var Jasmine = jasmine_init(); var Jasmine = jasmine_init();
// spec: { // spec: {
@ -78,4 +97,3 @@ var Jasmine = jasmine_init();
//var JasmineSpec = function(description, func) { //var JasmineSpec = function(description, func) {
// //
//} //}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
</project-private>

30
test/bootstrap.html Normal file
View File

@ -0,0 +1,30 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jasmine Tests</title>
<script type="text/javascript" src="prototype-1.6.0.3.js"></script>
<script type="text/javascript" src="../lib/jasmine.js"></script>
<link type="text/css" rel="stylesheet" href="test.css"/>
<script type="text/javascript" src="bootstrap.js"></script>
</head>
<body onLoad="runTests();">
<h1>
Running all Jasmine Test Suites
</h1>
<div id="icons">
<img id="spinner" src="spinner.gif" alt="" />
</div>
<div id="report">
<div id="results_summary" style="display:none;">
<h2>Summary</h2>
</div>
<div id="fails">
<h2 id="fails_header" style="display:none;">Failure Messages</h2>
<div id="fail_messages"></div>
</div>
</div>
</body>
</html>

140
test/bootstrap.js vendored Normal file
View File

@ -0,0 +1,140 @@
// Bootstrap Test Reporter function
var reporter = function () {
var total = 0;
var passes = 0;
var fails = 0;
var that = {
test: function (result, message) {
total++;
if (result) {
passes++;
iconElement = $('icons');
iconElement.appendChild(new Element('img', {src: '../images/accept.png'}));
}
else {
fails++;
var failsHeader = $('fails_header');
failsHeader.show();
iconElement = $('icons');
iconElement.appendChild(new Element('img', {src: '../images/exclamation.png'}));
var failMessages = $('fail_messages');
var newFail = new Element('p', {class: 'fail'});
newFail.innerHTML = message;
failMessages.appendChild(newFail);
}
},
summary: function () {
summary = new Element('p', {class: ((fails > 0) ? 'fail_in_summary' : '') });
summary.innerHTML = total + ' tests, ' + passes + ' passing, ' + fails + ' failed.';
var summaryElement = $('results_summary');
summaryElement.appendChild(summary);
summaryElement.show();
}
}
return that;
}();
var testMatchersComparisons = function () {
Jasmine = jasmine_init();
reporter.test(expects_that(true).should_equal(true),
'expects_that(true).should_equal(true) returned false');
reporter.test(!(expects_that(false).should_equal(true)),
'expects_that(true).should_equal(true) returned true');
reporter.test(expects_that(true).should_not_equal(false),
'expects_that(true).should_not_equal(false) retruned false');
reporter.test(!(expects_that(true).should_not_equal(true)),
'expects_that(true).should_not_equal(false) retruned true');
}
var testMatchersReporting = function () {
Jasmine = jasmine_init();
expects_that(true).should_equal(true);
expects_that(false).should_equal(true);
reporter.test((Jasmine.results.length == 2),
"Jasmine results array doesn't have 2 results");
reporter.test((Jasmine.results[0].passed == true),
"First spec didn't pass");
reporter.test((Jasmine.results[1].passed == false),
"Second spec did pass");
Jasmine = jasmine_init();
expects_that(false).should_equal(true);
reporter.test((Jasmine.results[0].message == 'Expected true but got false.'),
"Failed expectation didn't test the failure message");
Jasmine = jasmine_init();
expects_that(true).should_equal(true);
reporter.test((Jasmine.results[0].message == 'Passed.'),
"Passing expectation didn't test the passing message");
}
var testSpecs = function () {
Jasmine = jasmine_init();
var spec = it('new spec');
reporter.test((spec.description == 'new spec'),
"Spec did not have a description");
Jasmine = jasmine_init();
var another_spec = it('another spec', function () {
var foo = 'bar';
expects_that(foo).should_equal('bar');
});
another_spec.execute();
reporter.test((Jasmine.results.length == 1),
"Results aren't there after a spec was executed");
reporter.test((Jasmine.results[0].passed == true),
"Results has a result, but it's true");
Jasmine = jasmine_init();
var yet_another_spec = it('spec with failing expectation', function () {
var foo = 'bar';
expects_that(foo).should_equal('baz');
});
yet_another_spec.execute();
reporter.test((Jasmine.results[0].passed == false),
"Expectation that failed, passed");
Jasmine = jasmine_init();
var yet_yet_another_spec = it('spec with multiple assertions', function () {
var foo = 'bar';
var baz = 'quux';
expects_that(foo).should_equal('bar');
expects_that(baz).should_equal('quux');
});
yet_yet_another_spec.execute();
reporter.test((Jasmine.results.length == 2),
"Spec doesn't support multiple expectations");
}
var runTests = function () {
$('spinner').show();
testMatchersComparisons();
testMatchersReporting();
testSpecs();
$('spinner').hide();
reporter.summary();
}

4320
test/prototype-1.6.0.3.js vendored Normal file

File diff suppressed because it is too large Load Diff

28
test/test.css Normal file
View File

@ -0,0 +1,28 @@
body {
font: 14px "Helvetica Neue Light", "Lucida Grande", "Calibri", "Arial", sans-serif;
padding-left: 40px;
}
img#spinner {
display: block;
}
h1 {
padding-top: 20px;
font-weight: bold;
font: 24px; /* "Helvetica Neue Light", "Lucida Grande", "Calibri", "Arial", sans-serif; */
}
p {
margin-top: 5px;
padding-left: 20px;
}
p.fail {
background: url(../images/exclamation.png) no-repeat;
color: red;
}
p.fail_in_summary {
color: red;
}

View File

@ -7,6 +7,7 @@
<script type="text/javascript" src="../jspec/lib/jspec.js"></script> <script type="text/javascript" src="../jspec/lib/jspec.js"></script>
<script type="text/javascript" src="../lib/jasmine.js"></script> <script type="text/javascript" src="../lib/jasmine.js"></script>
<link type="text/css" rel="stylesheet" href="test.css"/>
<script type="text/javascript" src="test.js"></script> <script type="text/javascript" src="test.js"></script>
</head> </head>
<body onLoad="JSpecManager.run(htmlReporter);"> <body onLoad="JSpecManager.run(htmlReporter);">

View File

@ -3,19 +3,19 @@
with(JSpec('Jasmine expectation results')) { with(JSpec('Jasmine expectation results')) {
it('should compare actual and expected values that are equal', function(){ it('should compare actual and expected values that are equal', function () {
var jasmine_result = expects(true).should_equal(true); var jasmine_result = expects(true).should_equal(true);
expects_that(jasmine_result).should_equal(true).and_finish(); expects_that(jasmine_result).should_equal(true).and_finish();
}); });
it('should compare actual and expected values that are NOT equal', function(){ it('should compare actual and expected values that are NOT equal', function () {
var jasmine_result = expects(false).should_equal(true); var jasmine_result = expects(false).should_equal(true);
expects_that(jasmine_result).should_equal(false).and_finish(); expects_that(jasmine_result).should_equal(false).and_finish();
}); });
it('should be able to store the results of assertions', function(){ it('should be able to store the results of assertions', function () {
Jasmine = jasmine_init(); // re-clears out Jasmine Jasmine = jasmine_init(); // re-clears out Jasmine
expects(true).should_equal(true); expects(true).should_equal(true);
@ -26,7 +26,7 @@ with(JSpec('Jasmine expectation results')) {
expects_that(Jasmine.results[1].passed).should_equal(false).and_finish(); expects_that(Jasmine.results[1].passed).should_equal(false).and_finish();
}); });
it('should store a message with a failed expectation', function(){ it('should store a message with a failed expectation', function () {
Jasmine = jasmine_init(); // re-clears out Jasmine Jasmine = jasmine_init(); // re-clears out Jasmine
expects(false).should_equal(true); expects(false).should_equal(true);
@ -35,7 +35,7 @@ with(JSpec('Jasmine expectation results')) {
expects_that(Jasmine.results[0].message).should_equal(expected_message).and_finish(); expects_that(Jasmine.results[0].message).should_equal(expected_message).and_finish();
}); });
it('should store a default message with a passed expectation', function(){ it('should store a default message with a passed expectation', function () {
Jasmine = jasmine_init(); Jasmine = jasmine_init();
expects(true).should_equal(true); expects(true).should_equal(true);
@ -44,21 +44,39 @@ with(JSpec('Jasmine expectation results')) {
expects_that(Jasmine.results[0].message).should_equal(expected_message).and_finish(); expects_that(Jasmine.results[0].message).should_equal(expected_message).and_finish();
}); });
it('should support should_not_equal() passing', function () {
Jasmine = jasmine_init();
expects(true).should_not_equal(false);
var expected_message = 'Passed.';
expects_that(Jasmine.results[0].message).should_equal(expected_message).and_finish();
});
it('should support should_not_equal() message', function () {
Jasmine = jasmine_init();
expects(true).should_not_equal(true);
var expected_message = 'Expected true to not equal true, but it does.';
expects_that(Jasmine.results[0].message).should_equal(expected_message).and_finish();
});
} }
with(JSpec('Jasmine specs')){ with(JSpec('Jasmine specs')){
it('can have a description', function(){ it('can have a description', function () {
Jasmine = jasmine_init(); Jasmine = jasmine_init();
var a_spec = spec('new spec'); var a_spec = spec('new spec');
expects_that(a_spec.description).should_equal('new spec').and_finish(); expects_that(a_spec.description).should_equal('new spec').and_finish();
}); });
it('can execute some statements & expectations', function(){ it('can execute some statements & expectations', function () {
Jasmine = jasmine_init(); Jasmine = jasmine_init();
var a_spec = spec('new spec', function() { var a_spec = spec('new spec', function () {
var foo = 'bar'; var foo = 'bar';
expects(foo).should_equal('bar'); expects(foo).should_equal('bar');
}); });
@ -68,26 +86,33 @@ with(JSpec('Jasmine specs')){
expects_that(Jasmine.results.length).should_equal(1) expects_that(Jasmine.results.length).should_equal(1)
expects_that(Jasmine.results[0].passed).should_equal(true).and_finish(); expects_that(Jasmine.results[0].passed).should_equal(true).and_finish();
}); });
it('can have multiple assertions', function () {
Jasmine = jasmine_init();
var a_spec = spec('new spec', function () {
var foo = 'bar';
var baz = 'quux'
expects(foo).should_equal('bar');
expects(baz).should_equal('quux');
});
a_spec.execute();
expects_that(Jasmine.results.length).should_equal(2).and_finish();
});
// it('can evaluate expectations after an asynchronous set of execution steps', function () {
// });
} }
// it('should return true if all of its results are true', function(){
// });
// it('can have multiple assertions', function(){
//
// var jasmine = Jasmine();
// jasmine.expects_that(true).should_equal(true);
//
// var expected_message = 'Passed.';
// expects_that(jasmine.results[0].message).should_equal(expected_message).and_finish();
//
// });
//}
// should be able to run multiple specs in a suite in order
//with(JSpec('Test runner')) { //with(JSpec('Test runner')) {
// //
// it('should run a test and collect a result', function(){ // it('should run a test and collect a result', function () {
// //
// //
// //