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:
parent
a2802923e2
commit
9f2d3e0540
Binary file not shown.
After Width: | Height: | Size: 781 B |
Binary file not shown.
After Width: | Height: | Size: 701 B |
|
@ -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) {
|
|
||||||
var message = 'Passed.';
|
|
||||||
result = (that.actual === expected);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
message = 'Expected ' + expected + ' but got ' + that.actual + '.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Matchers.method('report', function (result, failing_message) {
|
||||||
|
|
||||||
Jasmine.results.push({
|
Jasmine.results.push({
|
||||||
passed: result,
|
passed: result,
|
||||||
message: message
|
message: result ? this.passing_message : failing_message
|
||||||
});
|
});
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
return that;
|
/*
|
||||||
}
|
* Jasmine spec constructor
|
||||||
|
*/
|
||||||
var spec = function (description, func) {
|
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) {
|
||||||
//
|
//
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
|
|
@ -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>
|
|
@ -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>
|
||||||
|
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
|
@ -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;
|
||||||
|
}
|
|
@ -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);">
|
||||||
|
|
53
test/test.js
53
test/test.js
|
@ -44,6 +44,24 @@ 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')){
|
||||||
|
@ -68,22 +86,29 @@ 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')) {
|
||||||
//
|
//
|
||||||
|
|
Loading…
Reference in New Issue