Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
110 lines (102 sloc) 2.3 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({
errors: [],
tests: 0,
assertions: 0,
failures: 0,
# 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
'\e[32m.\e[0m'.print()
else
'\e[31mF\e[0m'.print()
@errors << 'Expected %s to be truthy.' % value
end
@assertions = @assertions + 1
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
'\e[32m.\e[0m'.print()
else
'\e[31mF\e[0m'.print()
@errors << 'Expected %s, got %s.' % [expected, actual]
end
@assertions = @assertions + 1
end,
# Prints the report with the number of +tests+, +assertions+ and failures
#
# It also prints a failure report.
print report: ->
report = '\n\n\e[0m%i tests, %i assertions, %i failures' % [@tests, @assertions, @errors.length()]
report.puts()
@errors.each(-> error
(' * %s' % error).puts()
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 = @tests + 1
end
end)
@print report()
end
})
Something went wrong with that request. Please try again.