Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: old
Fetching contributors…

Cannot retrieve contributors at this time

113 lines (105 sloc) 2.411 kB
# TestCase represents an automated test case containing many tests to be run.
#
# It contains a couple of assertion methods (+assert+ and +assert equal+) to
# express expectations about objects.
#
# @example
#
# UserTest = TestCase.clone({
# setup: ->
# @user = Object.clone({ name: 'John', admin: true})
# end
#
# it has a name: ->
# @assert equal('John', @user.name())
# end
#
# it is an admin: ->
# @assert(@user.admin())
# end
#
# teardown: ->
# # Cleanup
# end
# })
#
# UserTest.run()
#
# ---------------
# Output:
#
# ..
#
# 2 tests, 2 assertions, 0 failures
#
TestCase = Object.clone({
initialize: ->
@errors = Array.clone()
@tests = 0
@assertions = 0
@failures = 0
end,
# Override #setup with a custom setup task that will be ran before each test.
#
# @return [Function]
# The setup function.
setup: ->; end,
# Override #teardown with a custom teardown task that will be ran after each
# test.
#
# @return [Function]
# The teardown function.
teardown: ->; end,
# Asserts that a given value is truthy.
#
# @param [Object] value
# The object to assert the truthiness about.
assert: -> value
if value != false
print('\e[32m.\e[0m')
else
print('\e[31mF\e[0m')
@errors.push('Expected #{value} to be truthy.')
@failures.increment()
end
@assertions.increment()
end,
# Asserts equality between two objects.
#
# @param [Object] expected
# The expected value.
#
# @param [Object] actual
# The actual value.
assert equal: -> expected, actual
if expected == actual
print('\e[32m.\e[0m')
else
print('\e[31mF\e[0m')
@errors.push('Expected #{expected}, got #{actual}.')
@failures.increment()
end
@assertions.increment()
end,
# Prints the report with the number of +tests+, +assertions+ and +failures+
#
# It also prints a failure report.
print report: ->
puts('\n\n\e[0m#{@tests} tests, #{@assertions} assertions, #{@failures} failures')
@errors.each(-> error
puts(' * #{error}')
end)
end,
# Runs the test case and prints the report afterwards.
run: ->
@each slot(-> name, test
if name.starts with('it')
@setup()
test()
@teardown()
@tests.increment()
end
end)
@print report()
end
})
Jump to Line
Something went wrong with that request. Please try again.