mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-15 07:23:19 -04:00
Internalized assertions.
This makes us independent of test/unit and minitest.
This commit is contained in:
@@ -122,7 +122,7 @@ class AboutMessagePassing < Neo::Koan
|
|||||||
def test_catching_messages_makes_respond_to_lie
|
def test_catching_messages_makes_respond_to_lie
|
||||||
catcher = AllMessageCatcher.new
|
catcher = AllMessageCatcher.new
|
||||||
|
|
||||||
assert_nothing_raised(NoMethodError) do # __
|
assert_nothing_raised do # __
|
||||||
catcher.any_method
|
catcher.any_method
|
||||||
end
|
end
|
||||||
assert_equal __(false), catcher.respond_to?(:any_method)
|
assert_equal __(false), catcher.respond_to?(:any_method)
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ class AboutModules < Neo::Koan
|
|||||||
|
|
||||||
def test_module_methods_are_also_available_in_the_object
|
def test_module_methods_are_also_available_in_the_object
|
||||||
fido = Dog.new
|
fido = Dog.new
|
||||||
assert_nothing_raised(Exception) do # __
|
assert_nothing_raised do # __
|
||||||
fido.set_name("Rover")
|
fido.set_name("Rover")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
79
src/neo.rb
79
src/neo.rb
@@ -1,7 +1,6 @@
|
|||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
# -*- ruby -*-
|
# -*- ruby -*-
|
||||||
|
|
||||||
require 'test/unit/assertions'
|
|
||||||
begin
|
begin
|
||||||
require 'win32console'
|
require 'win32console'
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
@@ -139,20 +138,68 @@ module Neo
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module Assertions
|
||||||
|
FailedAssertionError = Class.new(StandardError)
|
||||||
|
|
||||||
|
def flunk(msg)
|
||||||
|
raise FailedAssertionError, msg
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert(condition, msg=nil)
|
||||||
|
msg ||= "Failed assertion."
|
||||||
|
flunk(msg) unless condition
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_equal(expected, actual, msg=nil)
|
||||||
|
msg ||= "Expected #{expected.inspect} to equal #{actual.inspect}"
|
||||||
|
assert(expected == actual, msg)
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_not_equal(expected, actual, msg=nil)
|
||||||
|
msg ||= "Expected #{expected.inspect} to not equal #{actual.inspect}"
|
||||||
|
assert(expected != actual, msg)
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_nil(actual, msg=nil)
|
||||||
|
msg ||= "Expected #{actual.inspect} to be nil"
|
||||||
|
assert(nil == actual, msg)
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_not_nil(actual, msg=nil)
|
||||||
|
msg ||= "Expected #{actual.inspect} to not be nil"
|
||||||
|
assert(nil != actual, msg)
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_match(pattern, actual, msg=nil)
|
||||||
|
msg ||= "Expected #{actual.inspect} to match #{pattern.inspect}"
|
||||||
|
assert pattern =~ actual, msg
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_raise(exception)
|
||||||
|
begin
|
||||||
|
yield
|
||||||
|
rescue Exception => ex
|
||||||
|
expected = ex.is_a?(exception)
|
||||||
|
assert(expected, "Exception #{exception.inspect} expected, but #{ex.inspect} was raised")
|
||||||
|
return ex
|
||||||
|
end
|
||||||
|
flunk "Exception #{exception.inspect} expected, but nothing raised"
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_nothing_raised
|
||||||
|
begin
|
||||||
|
yield
|
||||||
|
rescue Exception => ex
|
||||||
|
flunk "Expected nothing to be raised, but exception #{exception.inspect} was raised"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class Sensei
|
class Sensei
|
||||||
attr_reader :failure, :failed_test, :pass_count
|
attr_reader :failure, :failed_test, :pass_count
|
||||||
|
|
||||||
in_ruby_version("1.8") do
|
FailedAssertionError = Assertions::FailedAssertionError
|
||||||
AssertionError = Test::Unit::AssertionFailedError
|
|
||||||
end
|
|
||||||
|
|
||||||
in_ruby_version("1.9", "2.0") do
|
|
||||||
if defined?(MiniTest)
|
|
||||||
AssertionError = MiniTest::Assertion
|
|
||||||
else
|
|
||||||
AssertionError = Test::Unit::AssertionFailedError
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@pass_count = 0
|
@pass_count = 0
|
||||||
@@ -204,7 +251,7 @@ module Neo
|
|||||||
end
|
end
|
||||||
|
|
||||||
def assert_failed?
|
def assert_failed?
|
||||||
failure.is_a?(AssertionError)
|
failure.is_a?(FailedAssertionError)
|
||||||
end
|
end
|
||||||
|
|
||||||
def instruct
|
def instruct
|
||||||
@@ -366,7 +413,7 @@ ENDTEXT
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Koan
|
class Koan
|
||||||
include Test::Unit::Assertions
|
include Assertions
|
||||||
|
|
||||||
attr_reader :name, :failure, :koan_count, :step_count, :koan_file
|
attr_reader :name, :failure, :koan_count, :step_count, :koan_file
|
||||||
|
|
||||||
@@ -396,12 +443,12 @@ ENDTEXT
|
|||||||
setup
|
setup
|
||||||
begin
|
begin
|
||||||
send(name)
|
send(name)
|
||||||
rescue StandardError, Neo::Sensei::AssertionError => ex
|
rescue StandardError, Neo::Sensei::FailedAssertionError => ex
|
||||||
failed(ex)
|
failed(ex)
|
||||||
ensure
|
ensure
|
||||||
begin
|
begin
|
||||||
teardown
|
teardown
|
||||||
rescue StandardError, Neo::Sensei::AssertionError => ex
|
rescue StandardError, Neo::Sensei::FailedAssertionError => ex
|
||||||
failed(ex) if passed?
|
failed(ex) if passed?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user