-
Notifications
You must be signed in to change notification settings - Fork 183
Testing (Deprecated)
There are a number of testing methods that can be used to validate the user's code.
All test assertions will be contained in one of the major testing methods: test()
, testIO()
, testAnswer()
. All of these testing methods are mutually exclusive: Only use test()
on programs that don't have IO, only use testIO()
on programs that have IO, and only use testAnswer()
on programs that aren't meant to be edited or run by the user.
All non-IO assertions should be within a test()
callback. Each test()
method will re-run the user's code for analysis, after which the assertions within the test()
callback will be run.
test( ["Test Name",] function() {
// assert, isEqual, etc. go in here.
});
// For example:
test( "Test with 5", function() {
isEqual( makeNumber(), 5, "makeNumber() returns 5." );
});
If you're going to test the program's IO you'll need to have a testIO()
callback. It works similarly to test()
but will run while the user's code is executing, evaluating the code as it runs. This evaluation step will be done before the user's code is able to be interacted with.
test( ["Test Name",] function() {
// testPrint, testInput go in here
});
// For example:
test( "Add 5 to a number", function() {
testInput( 5 );
testPrint( "10" );
});
Finally, if you wish to prevent evaluation of the code and instead have the student evaluate the code themselves, answering a question about that evaluation, you can use testAnswer()
. A statement like the following:
testAnswer( "What will blah() return?", "3" );
Will require the student to answer 3 in order to progress to the next problem. The UI for this will look something like the following:
Hints are still available in this mode, however no evaluation of the program will be done (and thus, no errors, test methods, or IO will occur).
These are the methods that can be used to evaluate the results of a user's program. They should only be used within a test()
or testIO()
callback function.
The testPrint
method evaluates the text of a printed statement and asserts if it matches the expected result. There are a few ways in which you can use a testPrint
call, all of which are shown below.
testIO(function() {
testPrint( "Strict equals." );
testPrint( /RegExp equals/i );
testPrint( true ); // Any print statement will do
testPrint(function( text ) {
return text.indexOf( "Hello" ) < 0;
});
});
testPrint
returns true
or false
, depending upon if the test passed, or not.
testInput
inputs the specified text into the next available input (either input
or inputNumber
).
testIO(function() {
testInput( "Input String." );
testInput(function( text ) {
return text === "Y" ? "N" : "Y";
});
});
The default is to take a string and feed that into the input. testInput
can also take a function (which receives the text of the input prompt as an argument) - the return result is fed into the input prompt.
testInput
returns true
if a matching input was found.
isEqual
is the default comparison test that you'll want to make (as it provides the greatest amount of context for the user). It takes two values and performs a ===
comparison between them, passing if the comparison is true.
// Example method signature
isEqual( valueA, valueB, msg )
// For example:
isEqual( typeof name, "string", "Name contains a string." );
assert
takes a single value and passes if the value is truth-y (e.g. the value of !!value
).
// Example method signature
assert( value, msg )
// For example:
assert( typeof name === "string", "Name contains a string." );
Tasks are a way of grouping assertions into a logical unit that will make it easier for the user to understand. For example you could create a task "Print out the first name." which actually groups the testPrint
assertion.
testIO(function() {
task( "Ask for the first name." );
testInput( "John" );
task( "Ask for the last name." );
testInput( "Resig" );
task( "Print out the full name." );
testPrint( /John Resig/ );
});