Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 9ae40c2ab0
Fetching contributors…

Cannot retrieve contributors at this time

144 lines (119 sloc) 3.721 kB
import reflection
var _totalTests = 0
var _totalFailures = 0
var _currentTestSuite = nothing
var _startTimeMs = 0
// TODO(bob): Better way to make an empty list.
var _specs = [] toList
def runTests()
_totalTests = 0
_totalFailures = 0
_currentTestSuite = nothing
print("Executing specification...")
_startTimeMs = currentTime()
for description, block in _specs do
_currentTestSuite = _TestSuite new(_name: description)
block call(_currentTestSuite)
end
var endTimeMs = currentTime()
print()
print("Passed " + (_totalTests - _totalFailures) + " out of " +
_totalTests + " tests in " + (endTimeMs - _startTimeMs) + "ms.")
end
def specify(spec (is String, is Function))
_specs add(spec)
end
def specifyMethod(method is String, body is Function)
specify("The method " + method, body)
end
defclass _TestSuite
var _name is String
var _currentTest = ""
var _failedTest = false
end
// TODO(bob): Need function types.
def (this is _TestSuite) should(description is String, block)
this _currentTest = description
this _failedTest = false
val testsBefore = _totalTests
do
block call()
catch error then
fail("Unexpected error: " + error class + "\n" + error)
end
if _totalTests == testsBefore then
print("NONE: " + this _name + " should " + this _currentTest + ".")
end
end
def (this is _TestSuite) _pass()
this _advance()
end
def (this is _TestSuite) _advance()
_totalTests = _totalTests + 1
end
def (this is _TestSuite) _fail(message is String)
if not(this _failedTest) then
print("FAIL: " + this _name + " should " + this _currentTest + ".")
end
this _advance()
_totalFailures = _totalFailures + 1
print(" " + message)
this _failedTest = true
end
def fail(message is String)
_currentTestSuite _fail(message)
end
def (this) shouldEqual(expected, message is String)
if this != expected then
_currentTestSuite _fail(message)
else
_currentTestSuite _pass()
end
end
def escape(string is String)
string replace("\n", with: "\\n") \
replace("\r", with: "\\r") \
replace("\t", with: "\\t")
end
def (this) shouldEqual(expected)
this shouldEqual(expected, "Expected " + escape(expected toString) +
" but was " + escape(this toString))
end
def (this) shouldBe(expected is Class, message is String)
if this isa(expected) then
_currentTestSuite _pass()
else
_currentTestSuite _fail(message)
end
end
def (this) shouldBe(expected is Class)
/// Verifies that the left argument is an instance of the given class.
this shouldBe(expected, "Expected class " + expected + " but was " +
this class)
end
def (this is Function) shouldThrow(errorClass is Class)
/// Verifies that a function throws an error of the given class. Will
/// execute the function.
var caught = false
do
this call()
catch err is Error then
err shouldBe(errorClass, "Should have thrown an error of class " +
errorClass + " but got an error of class " + err class + ".")
caught = true
end
caught shouldEqual(true, "Should have thrown an error of class " +
errorClass + " but no error was thrown.")
end
def (this is Function) shouldNotThrow()
/// Verifies that a function does not throw an error. Will execute the
/// function.
var caught = nothing
do
this call()
catch err is Error then
caught = err
end
caught shouldEqual(nothing, "Should not have thrown an error but threw " +
"an error of class " + caught class + ".")
end
Jump to Line
Something went wrong with that request. Please try again.