Permalink
Browse files

changed to make Riot.context able to defer contexts until Riot.run() …

…is called, and work the way test.js does it
  • Loading branch information...
1 parent 84a375d commit 4f1f719902b13054f161359834438f0b7aeea163 Alex Young committed Oct 21, 2009
Showing with 85 additions and 39 deletions.
  1. +10 −4 README.textile
  2. +75 −35 riot.js
View
14 README.textile
@@ -29,21 +29,27 @@ Riot.run(function() {
});
</pre>
+There are a few aliases to make tests (and output) read more naturally:
+
+* <code>context</code>: <code>given</code> -- Given will add "Given..." to the title in the test output
+* <code>asserts</code>: <code>should</code>
+
h3. Assertions
* <code>equals</code> - for example, <code>asserts('description').equals('value')</code>
+* <code>matches</code> - matches a regex
+* <code>typeOf</code> - aliased to isTypeOf, kindOf
* <code>isNull</code>
* <code>isTrue</code>
-* <code>typeOf</code>
* <code>raises</code>
h3. Riot.run
<code>Riot.run(function() { // your tests });</code> just adds your tests to <code>window.onload</code>. If there's already an <code>onload</code> handler it'll prepend it. If there's no <code>window</code> it will just run the tests.
+It can also be called with no parameters to run tests defined with <code>Riot.context</code>. This can be used to create a set of files with tests inside a <code>Riot.context</code> for each file
+
h3. Todo
-* Add formatter for xpcomcore
-* Ask g-man about actual() -- I defer execution to transparently match functions/values, but does this make sense?
* Extract the assertions into their own property list so adding user-defined ones is cleaner
-* If you don't give a damn then we don't give a fuck
+* Package as rubygem (-xpcc)
View
110 riot.js
@@ -1,13 +1,18 @@
/*jslint white: false plusplus: false onevar: false browser: true evil: true*/
/*global window: true*/
var Riot = {
- results: [],
+ results: [],
+ contexts: [],
run: function(tests) {
- if (typeof window === 'undefined') {
+ if (typeof XPCOMCore !== 'undefined') {
+ Riot.formatter = new Riot.Formatters.XPComCore();
+ Riot.runAndReport(tests);
+ Sys.exit(Riot.exitCode);
+ } else if (typeof window === 'undefined') {
Riot.formatter = new Riot.Formatters.Text();
Riot.runAndReport(tests);
- java.lang.System.exit(Riot.exit_code);
+ java.lang.System.exit(Riot.exitCode);
} else {
Riot.formatter = new Riot.Formatters.HTML();
var onload = window.onload;
@@ -19,12 +24,22 @@ var Riot = {
},
runAndReport: function(tests) {
- var benchmark = Riot.Benchmark.run(1, this.withThis(Riot, tests));
+ var benchmark = Riot.Benchmark.run(1, function() { Riot.runAllContexts(tests); });
Riot.formatter.separator();
Riot.summariseAllResults();
Riot.formatter.line(benchmark);
},
+ runAllContexts: function(tests) {
+ if (typeof tests !== 'undefined') {
+ this.withThis(Riot, tests)();
+ }
+
+ for (var i = 0; i < this.contexts.length; i++) {
+ this.contexts[i].run();
+ }
+ },
+
functionBody: function(fn) {
return fn.toString().match(/^[^\{]*{((.*\n*)*)}/m)[1];
},
@@ -34,25 +49,16 @@ var Riot = {
},
context: function(title, callback) {
- var context = new Riot.Context(title, callback, this.setupFunction, this.teardownFunction);
- this.setupFunction = undefined;
- this.teardownFunction = undefined;
- return context.run();
+ var context = new Riot.Context(title, callback);
+ Riot.contexts.push(context);
+ return context;
},
given: function(title, callback) {
title = 'Given ' + title;
return Riot.context(title, callback);
},
- setup: function(setupFunction) {
- this.setupFunction = setupFunction;
- },
-
- teardown: function(teardownFunction) {
- this.teardownFunction = teardownFunction;
- },
-
summariseAllResults: function() { return this.summarise(this.results); },
summarise: function(results) {
@@ -61,7 +67,7 @@ var Riot = {
if (!results[i].pass) { failures++; }
}
this.formatter.line(results.length + ' assertions: ' + failures + ' failures');
- this.exit_code = failures > 0 ? 1 : 0;
+ this.exitCode = failures > 0 ? 1 : 0;
},
addResult: function(context, assertion, pass) {
@@ -167,14 +173,39 @@ Riot.Formatters = {
this.separator = function() {
this.line('');
};
+ },
+
+ XPComCore: function() {
+ this.line = function(text) {
+ puts(text);
+ };
+
+ this.pass = function(message) {
+ this.line(' + ' + message + '');
+ };
+
+ this.fail = function(message) {
+ this.line(' - ' + message + '');
+ };
+
+ this.error = function(message, exception) {
+ this.fail(message);
+ this.line(' Exception: ' + exception);
+ };
+
+ this.context = function(name) {
+ this.line(name);
+ };
+
+ this.separator = function() {
+ this.line('');
+ };
}
};
-Riot.Context = function(name, callback, setup, teardown) {
+Riot.Context = function(name, callback) {
this.name = name;
this.callback = callback;
- this.setupFunction = setup;
- this.teardownFunction = teardown;
this.assertions = [];
this.should = this.asserts;
this.given = Riot.given;
@@ -187,13 +218,21 @@ Riot.Context.prototype = {
return assertion;
},
- setup: function() {
+ setup: function(setupFunction) {
+ this.setupFunction = setupFunction;
+ },
+
+ teardown: function(teardownFunction) {
+ this.teardownFunction = teardownFunction;
+ },
+
+ runSetup: function() {
if (typeof this.setupFunction !== 'undefined') {
return this.setupFunction();
}
},
- teardown: function() {
+ runTeardown: function() {
if (typeof this.teardownFunction !== 'undefined') {
return this.teardownFunction();
}
@@ -202,7 +241,7 @@ Riot.Context.prototype = {
run: function() {
Riot.formatter.context(this.name);
Riot.withThis(this, this.callback)();
-
+ this.runSetup();
for (var i = 0; i < this.assertions.length; i++) {
var pass = false,
assertion = this.assertions[i];
@@ -220,6 +259,7 @@ Riot.Context.prototype = {
Riot.addResult(this.name, assertion.name, pass);
}
+ this.runTeardown();
}
};
@@ -229,11 +269,12 @@ Riot.AssertionFailure = function(message) {
return error;
};
-Riot.Assertion = function(context_name, name, expected) {
- this.name = name;
- this.expected_value = expected;
- this.context_name = context_name;
- this.kindOf = this.typeOf;
+Riot.Assertion = function(contextName, name, expected) {
+ this.name = name;
+ this.expectedValue = expected;
+ this.contextName = contextName;
+ this.kindOf = this.typeOf;
+ this.isTypeOf = this.typeOf;
this.setAssertion(function(actual) {
if ((actual() === null) || (actual() === undefined)) {
@@ -257,19 +298,18 @@ Riot.Assertion.prototype = {
},
expected: function() {
- if (typeof this.expected_memo === 'undefined') {
- if (typeof this.expected_value === 'function') {
+ if (typeof this.expectedMemo === 'undefined') {
+ if (typeof this.expectedValue === 'function') {
try {
- this.expected_memo = this.expected_value();
+ this.expectedMemo = this.expectedValue();
} catch (exception) {
- this.expected_value = exception;
+ this.expectedValue = exception;
}
} else {
- this.expected_memo = this.expected_value;
+ this.expectedMemo = this.expectedValue;
}
}
-
- return this.expected_memo;
+ return this.expectedMemo;
},
/* Assertions */

0 comments on commit 4f1f719

Please sign in to comment.