Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added support for "teardown" blocks. Tweaked documentation.
  • Loading branch information
Norman Clarke committed Apr 16, 2009
1 parent 89c7441 commit 0780942
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
9 changes: 6 additions & 3 deletions README.markdown
Expand Up @@ -6,8 +6,7 @@ Contexts for Test::Unit.
Description
-----------

Write declarative tests with nested contexts without performance
penalties. Contest is less than 100 lines of code and gets the job done.
Write declarative tests using nested contexts without performance penalties. Contest is less than 100 lines of code and gets the job done.

Usage
-----
Expand All @@ -20,6 +19,10 @@ Declare your tests as you would in RSpec or Shoulda:
setup do
@value = 1
end
teardown do
@value = nil
end

test "sample test" do
assert_equal 1, @value
Expand Down Expand Up @@ -64,7 +67,7 @@ For your convenience, `context` is aliased as `describe` and `test` is aliased a
end
end

You can run it normaly, it's Test::Unit after all. If you want to run a particular test, say "yet more tests", try this:
You can run it normally, it's Test::Unit after all. If you want to run a particular test, say "yet more tests", try this:

$ testrb my_test.rb -n test_yet_more_tests

Expand Down
2 changes: 1 addition & 1 deletion contest.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'contest'
s.version = '0.0.7'
s.version = '0.0.8'
s.summary = %{Write more readable tests in Test::Unit with this tiny script.}
s.date = %q{2009-03-16}
s.authors = ["Damian Janowski", "Michel Martens"]
Expand Down
2 changes: 1 addition & 1 deletion contest.gemspec.erb
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'contest'
s.version = '0.0.7'
s.version = '0.0.8'
s.summary = %{Write more readable tests in Test::Unit with this tiny script.}
s.date = %q{2009-03-16}
s.authors = ["Damian Janowski", "Michel Martens"]
Expand Down
33 changes: 24 additions & 9 deletions lib/contest.rb
@@ -1,34 +1,45 @@
require "test/unit"

# Test::Unit loads a default test if the suite is empty, and the only
# purpose of that test is to fail. As having empty contexts is a common
# practice, we decided to overwrite TestSuite#empty? in order to
# allow them. Having a failure when no tests have been defined seems
# counter-intuitive.
# Test::Unit loads a default test if the suite is empty, whose purpose is to
# fail. Since having empty contexts is a common practice, we decided to
# overwrite TestSuite#empty? in order to allow them. Having a failure when no
# tests have been defined seems counter-intuitive.
class Test::Unit::TestSuite
def empty?
false
end
end

# We added setup, test and context as class methods, and the instance
# method setup now iterates on the setup blocks. Note that all setup
# blocks must be defined with the block syntax. Adding a setup instance
# method defeats the purpose of this library.
# Contest adds +teardown+, +test+ and +context+ as class methods, and the
# instance methods +setup+ and +teardown+ now iterate on the corresponding
# blocks. Note that all setup and teardown blocks must be defined with the
# block syntax. Adding setup or teardown instance methods defeats the purpose
# of this library.
class Test::Unit::TestCase
def self.setup(&block)
setup_blocks << block
end

def self.teardown(&block)
teardown_blocks << block
end

def setup
self.class.setup_blocks.each do |block|
instance_eval(&block)
end
end

def teardown
self.class.teardown_blocks.each do |block|
instance_eval(&block)
end
end

def self.context(name, &block)
subclass = Class.new(self.superclass)
subclass.setup_blocks.unshift(*setup_blocks)
subclass.teardown_blocks.unshift(*teardown_blocks)
subclass.class_eval(&block)
const_set(context_name(name), subclass)
end
Expand All @@ -48,6 +59,10 @@ def self.setup_blocks
@setup_blocks ||= []
end

def self.teardown_blocks
@teardown_blocks ||= []
end

def self.context_name(name)
"Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
end
Expand Down
4 changes: 4 additions & 0 deletions test/all_test.rb
Expand Up @@ -4,6 +4,10 @@ class FooTest < Test::Unit::TestCase
setup do
@value = 1
end

teardown do
@value = nil
end

test "truth" do
assert_equal 1, @value
Expand Down

0 comments on commit 0780942

Please sign in to comment.