dojox/testing/DocTest (version 1.10)

Summary

This class executes doctests.

DocTests are tests that are defined inside the comment. A doctest looks as if it was copied from the shell (which it mostly is). A doctest is executed when the following conditions match: 1) all lines are comments 2) the line always starts with spaces/tabs followed by "//" and at least one space 3) the line(s) of the test to execute starts with ">>>" preceded by what is described in 2) 4) the first line after 3) starting without ">>>" is the exptected result. preceded by what is described in 2) 5) the test sequence is terminated by an empty line, or the next test in the following line, or a new line that does not start as described in 2) (simple said: is not a comment) preceded by what is described in 2)

I.e. the following is a simple doctest, that will actually also be run if you run this class against this file here:

1+1 // A simple test case. Terminated by an empty line 2

1==2 false "a"+"b" // Also without the empty line before, this is a new test. "ab"

var anything = "anything" // Multiple commands for one test. "something"==anything false

DocTests are great for inline documenting a class or method, they also are very helpful in understanding what the class/method actually does. They don't make sense everywhere, but sometimes they are really handy.

See the dojox/testing/DocTest reference documentation for more information.

Property Summary

Method Summary

  • _getTestsFromString(data,insideComments) Parse the given string for tests.
  • _run(tests) Each element in the array contains the test in the first element, and the expected result in the second element.
  • getTests(moduleName) Extract the tests from the given module or string.
  • getTestsFromString(data)
  • run(moduleName) Run the doctests in the module given.
  • runTest(commands,expected)

Properties

errors
Defined by: dojox/testing/DocTest

Methods

_getTestsFromString(data,insideComments)

Parse the given string for tests.

Parameter Type Description
data String
insideComments Boolean

if false "data" contains only the pure tests, comments already stripped.

Returns:Array
_run(tests)

Each element in the array contains the test in the first element, and the expected result in the second element.

Parameter Type Description
tests Array

Make sure that the types are compared properly. There used to be the bug that a return value false was compared to "false" which made the test fail. This is fixed and should be verified by the following tests.

false false

"false" "false"

true true

1 1

"s" "s"

dojo.toJson({one:1}) "{"one":1}"

getTests(moduleName)

Extract the tests from the given module or string.

Parameter Type Description
moduleName String
Returns:undefined

Examples

Example 1

dojo.isArray(new dojox.testing.DocTest().getTests("dojox.testing.DocTest")) // Use the module name to extract the tests from. true

getTestsFromString(data)
Parameter Type Description
data String
Returns:undefined

Examples

Example 1

(new dojox.testing.DocTest().getTestsFromString(">>> 1+1\n2\n>>> 2+2\n4")).length // Do tests from strings get detected properly? 2

run(moduleName)

Run the doctests in the module given.

Parameter Type Description
moduleName undefined

Examples

Example 1

doctest = new dojox.testing.DocTest(); doctest.run("dojox.testing.DocTest"); doctest.errors should finally be an empty array. // The above is not a doctest, because it just would // execute itself in a never ending loop.

true==true // Test a new line terminating the test. true

true==true // Test a new test terminating the test. true true==true // Test a "not a comment"-line, especially an empty line terminating the test. true

runTest(commands,expected)
Parameter Type Description
commands undefined
expected undefined
Returns:object
Error in the documentation? Can’t find what you are looking for? Let us know!