Permalink
Browse files

Make sure that normal tests run in canvas and IO mode. Fixes #96.

  • Loading branch information...
1 parent 5c1310a commit 1188e05ff155d2bce6c0fdfc22fe6e50c979a3e3 @jeresig jeresig committed Apr 5, 2012
Showing with 51 additions and 21 deletions.
  1. +51 −21 js/output.js
View
@@ -246,15 +246,26 @@ var Output = {
runTest: function( userCode, test, i ) {
Output.clear();
- if ( Output.output && Output.output.runTest ) {
- Output.output.runTest( userCode, test, i );
+ if ( Output.output && Output.output.preTest ) {
+ Output.output.preTest();
+ }
+
+ if ( typeof test.type === "object" ) {
+ if ( test.type.runTest ) {
+ test.type.runTest( userCode, test, i );
+ }
} else {
// We need to maintain the closure so we have to re-initialize the tests
// and then run the current one. Definitely not ideal.
Output.exec( userCode +
"\n(function(){ Output.tests = [];\n" +
- curProblem.validate + "\n})(); Output.tests[" + i + "].fn();" );
+ curProblem.validate + "\n})(); Output.tests[" + i + "].fn();",
+ Output.context, Output.testContext );
+ }
+
+ if ( Output.output && Output.output.postTest ) {
+ return Output.output.postTest();
}
},
@@ -563,15 +574,22 @@ var TextOutput = {
}
},
+ preTest: function() {
+ TextOutput.$elem = $( "#" + this.id + "-test" );
+ },
+
+ postTest: function() {
+ var oldElem = TextOutput.$elem[0];
+
+ TextOutput.$elem = $( "#" + this.id );
+
+ return oldElem;
+ },
+
runTest: function( userCode, test, i ) {
// TODO: Have all tests run after user's code has been defined
// Will need to force input/print statements to block during testMode
- // TODO: Output to temporary location instead
- // testOutput = [];
-
- TextOutput.$elem = $( "#" + this.id + "-test" );
-
Output.clear();
// Load up the IO tests
@@ -588,8 +606,6 @@ var TextOutput = {
// Then run the user's code
Output.exec( userCode, Output.context );
- TextOutput.$elem = $( "#" + this.id );
-
// Make sure the remaining IO tests are printed out so that the
// user knows what's expected of them
var checkIO;
@@ -653,13 +669,7 @@ var CanvasOutput = {
CanvasOutput.lastGrab = null;
- CanvasOutput.canvas = Output.context = new Processing( this.id, function( instance ) {
- instance.draw = CanvasOutput.DUMMY;
- });
-
- CanvasOutput.canvas.size( 400, 360 );
- CanvasOutput.canvas.frameRate( 30 );
- CanvasOutput.clear();
+ CanvasOutput.build( this.id );
if ( !CanvasOutput.props ) {
var props = CanvasOutput.props = {};
@@ -678,8 +688,31 @@ var CanvasOutput = {
return this;
},
+ build: function( canvas ) {
+ CanvasOutput.canvas = Output.context = new Processing( canvas, function( instance ) {
+ instance.draw = CanvasOutput.DUMMY;
+ });
+
+ CanvasOutput.canvas.size( 400, 360 );
+ CanvasOutput.canvas.frameRate( 30 );
+ CanvasOutput.clear();
+ },
+
DUMMY: function(){},
+ preTest: function() {
+ CanvasOutput.oldContext = Output.context;
+
+ CanvasOutput.testCanvas = document.createElement( "canvas" );
+ CanvasOutput.build( CanvasOutput.testCanvas );
+ },
+
+ postTest: function() {
+ CanvasOutput.canvas = Output.context = CanvasOutput.oldContext;
+
+ return CanvasOutput.testCanvas;
+ },
+
runTest: function( userCode, test, i ) {
// TODO: Add in Canvas testing
// Create a temporary canvas and a new processing instance
@@ -776,10 +809,7 @@ var CanvasOutput = {
},
clear: function() {
- // TODO: Remove when testing is implemented
- if ( !Output.testing ) {
- CanvasOutput.canvas.background( 255 );
- }
+ CanvasOutput.canvas.background( 255 );
},
kill: function() {

0 comments on commit 1188e05

Please sign in to comment.