Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit da10ff6
Showing
11 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
= Test-JS = | ||
|
||
For testing JavaScript. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
testJS.Assertions = { | ||
assert: function(cond) { | ||
if (!cond) { this.__passed = false; } | ||
} | ||
} | ||
|
||
Object.extend(testJS.testCase.prototype, testJS.Assertions); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Object.extend = function(receiver, features) { | ||
for (key in features) { receiver[key] = features[key]; } | ||
}; | ||
|
||
Object.extend(Function.prototype, { | ||
bind: function(object) { | ||
var __method = this; | ||
var __object = arguments[1]; | ||
return function() { | ||
return __method.apply(__object); | ||
} | ||
} | ||
}); | ||
|
||
var testJS = { }; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
testJS.testCase = function(name, action) { | ||
this.name = name; | ||
this.action = action; | ||
this.errorMessage = false; | ||
this.__passed = true; | ||
} | ||
|
||
Object.extend(testJS.testCase.prototype, { | ||
run: function() { | ||
try { this.action.apply(this); } | ||
catch(e) { this.errorMessage = e.toString(); } | ||
return this; | ||
}, | ||
|
||
passed: function() { | ||
return this.__passed && !this.errored(); | ||
}, | ||
|
||
failed: function() { | ||
return !this.__passed && !this.errored(); | ||
}, | ||
|
||
errored: function() { | ||
return this.errorMessage ? true : false; | ||
}, | ||
|
||
resultBin: function() { | ||
if (this.errored()) { return 'errored'; } | ||
if (this.passed()) { return 'passed'; } | ||
if (this.failed()) { return 'failed'; } | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
testJS.testSuite = function(tests) { | ||
this.setup(tests); | ||
} | ||
|
||
Object.extend(testJS.testSuite.prototype, { | ||
setup: function(tests) { | ||
this.tests = new Array; | ||
this.passed = new Array; | ||
this.failed = new Array; | ||
this.errored = new Array; | ||
for (name in tests) { | ||
this.tests.push(new testJS.testCase(name, tests[name])); | ||
} | ||
}, | ||
|
||
run: function() { | ||
for (name in this.tests) { | ||
var test = this.tests[name]; | ||
test.run(); | ||
this[test.resultBin()].push(name); | ||
} | ||
return this.report(); | ||
}, | ||
|
||
report: function() { | ||
var results = { | ||
Passed: this.passed.length, | ||
Failed: this.failed.length | ||
} | ||
|
||
return results; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module TestJS | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--colour |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
unless defined?(TEST_JS_ROOT) | ||
TEST_JS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) | ||
$:.push(TEST_JS_ROOT) | ||
end | ||
|
||
require 'rubygems' | ||
require 'johnson' | ||
require 'spec' | ||
|
||
def setup_runtime | ||
runtime = Johnson::Runtime.new | ||
runtime.evaluate("Johnson.require('js/base.js');") | ||
runtime.evaluate("Johnson.require('js/test_case.js');") | ||
runtime.evaluate("Johnson.require('js/test_suite.js');") | ||
runtime.evaluate("Johnson.require('js/assertions.js');") | ||
runtime | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require File.join(File.dirname(__FILE__), 'spec_helper') | ||
|
||
describe "testJS.testCase" do | ||
before(:each) do | ||
@runtime = setup_runtime | ||
end | ||
|
||
describe "naming" do | ||
it "should return name" do | ||
tc = @runtime.evaluate("new testJS.testCase('a name', function() { });") | ||
tc.name.should == 'a name' | ||
end | ||
end | ||
|
||
describe "statuses" do | ||
it "should return if passed" do | ||
tc = @runtime.evaluate("new testJS.testCase('test', function() { });") | ||
tc.__passed = true | ||
tc.passed.should be_true | ||
tc.failed.should be_false | ||
tc.errored.should be_false | ||
end | ||
|
||
it "should return if failed" do | ||
tc = @runtime.evaluate("new testJS.testCase('test', function() { });") | ||
tc.__passed = false | ||
tc.passed.should be_false | ||
tc.failed.should be_true | ||
tc.errored.should be_false | ||
end | ||
|
||
it "should return if errored" do | ||
tc = @runtime.evaluate("new testJS.testCase('test', function() { });") | ||
tc.errorMessage = 'Whoops!' | ||
tc.passed.should be_false | ||
tc.failed.should be_false | ||
tc.errored.should be_true | ||
end | ||
|
||
it "should return passed result bin" do | ||
tc = @runtime.evaluate("new testJS.testCase('test', function() { });") | ||
tc.__passed = true | ||
tc.resultBin.should == 'passed' | ||
end | ||
|
||
it "should return failed result bin" do | ||
tc = @runtime.evaluate("new testJS.testCase('test', function() { });") | ||
tc.__passed = false | ||
tc.resultBin.should == 'failed' | ||
end | ||
|
||
it "should return errored result bin" do | ||
tc = @runtime.evaluate("new testJS.testCase('test', function() { });") | ||
tc.errorMessage = 'oops' | ||
tc.resultBin.should == 'errored' | ||
end | ||
end | ||
|
||
describe "run()" do | ||
it "should return self" do | ||
tc = @runtime.evaluate("new testJS.testCase('test', function() { this.assert(true); });") | ||
tc.run.should == tc | ||
end | ||
|
||
it "should pass when asserting true" do | ||
tc = @runtime.evaluate("new testJS.testCase('test', function() { this.assert(true); });") | ||
tc.run | ||
tc.passed.should be_true | ||
end | ||
|
||
it "should fail when asserting true" do | ||
tc = @runtime.evaluate("new testJS.testCase('test', function() { this.assert(false); });") | ||
tc.run | ||
tc.passed.should be_false | ||
end | ||
|
||
it "should handle errors" do | ||
tc = @runtime.evaluate("new testJS.testCase('test', function() { throw('whoops'); });") | ||
lambda { tc.run }.should_not raise_error | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require File.join(File.dirname(__FILE__), 'spec_helper') | ||
|
||
describe "testJS.testSuite" do | ||
before(:each) do | ||
@runtime = setup_runtime | ||
@ts = @runtime.evaluate <<-JS | ||
new testJS.testSuite({ | ||
'test #1': function() { this.assert(true); }, | ||
'test #2': function() { this.assert(false); }, | ||
'test #3': function() { throw('whoops'); } | ||
}); | ||
JS | ||
end | ||
|
||
it "should have tests" do | ||
@ts.should have(3).tests | ||
end | ||
|
||
it "should have result bins" do | ||
@ts.passed.length.should == 0 | ||
@ts.failed.length.should == 0 | ||
@ts.errored.length.should == 0 | ||
end | ||
end |