Skip to content

Commit

Permalink
first commit. added files
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Aug 5, 2008
0 parents commit da10ff6
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README
@@ -0,0 +1,3 @@
= Test-JS =

For testing JavaScript.
7 changes: 7 additions & 0 deletions js/assertions.js
@@ -0,0 +1,7 @@
testJS.Assertions = {
assert: function(cond) {
if (!cond) { this.__passed = false; }
}
}

Object.extend(testJS.testCase.prototype, testJS.Assertions);
15 changes: 15 additions & 0 deletions js/base.js
@@ -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 added js/string_ext.js
Empty file.
32 changes: 32 additions & 0 deletions js/test_case.js
@@ -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'; }
}
});
33 changes: 33 additions & 0 deletions js/test_suite.js
@@ -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;
}
});
3 changes: 3 additions & 0 deletions lib/test_js.rb
@@ -0,0 +1,3 @@
module TestJS

end
1 change: 1 addition & 0 deletions spec/spec.opts
@@ -0,0 +1 @@
--colour
17 changes: 17 additions & 0 deletions spec/spec_helper.rb
@@ -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
82 changes: 82 additions & 0 deletions spec/test_case_spec.rb
@@ -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
24 changes: 24 additions & 0 deletions spec/test_suite_spec.rb
@@ -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

0 comments on commit da10ff6

Please sign in to comment.