Skip to content
Browse files

support activesupport's syntax of setup :some_method, which is meant …

…to invoke :some_method at setup time (also teardown)

Signed-off-by: Jeremy McAnally <jeremymcanally@gmail.com>
  • Loading branch information...
1 parent b94a0ed commit f219801583fadd686a593aafa8bb606b37d2452e @jamesgolick jamesgolick committed with Jan 20, 2009
Showing with 33 additions and 2 deletions.
  1. +13 −1 lib/context/lifecycle.rb
  2. +20 −1 test/test_lifecycle.rb
View
14 lib/context/lifecycle.rb
@@ -9,6 +9,11 @@ class << self
# end
#
def before(period = :each, &block)
+ unless block_given?
+ block = period
+ period = :each
+ end
+
send("before_#{period}_callbacks") << block
end
@@ -21,6 +26,11 @@ def before(period = :each, &block)
# end
#
def after(period = :each, &block)
+ unless block_given?
+ block = period
+ period = :each
+ end
+
send("after_#{period}_callbacks") << block
end
@@ -66,7 +76,9 @@ def teardown
end
def run_each_callbacks(callback_type) # :nodoc:
- self.class.gather_callbacks(callback_type, :each).each { |c| instance_eval(&c) if c }
+ self.class.gather_callbacks(callback_type, :each).each do |c|
+ c.is_a?(Proc) ? instance_eval(&c) : send(c)
+ end
end
def run_all_callbacks(callback_type) # :nodoc:
View
21 test/test_lifecycle.rb
@@ -24,7 +24,7 @@ class TestLifecycle < Test::Unit::TestCase
sample_test = context "lifecycle" do
attr_reader :inherited_before_each_var, :inherited_before_each_var_2, :inherited_after_each_var,
- :after_each_var, :inherited_before_all_var, :inherited_after_all_var, :before_all_var, :after_all_var,
+ :after_each_var, :inherited_before_all_var, :inherited_after_all_var, :before_all_var, :after_all_var, :ivar,
:superclass_before_each_var, :superclass_after_each_var, :superclass_before_all_var, :superclass_after_all_var, :one, :two
before do
@@ -43,6 +43,8 @@ class TestLifecycle < Test::Unit::TestCase
@after_all_var = 1
end
+ after :a_method
+
test "foo" do
end
end
@@ -93,6 +95,10 @@ class TestLifecycle < Test::Unit::TestCase
it "it runs after callbacks" do
assert_equal 1, @test.after_each_var
end
+
+ it "it runs after callbacks specified with method names, instead of blocks" do
+ assert_equal "a method ran", @test.ivar
+ end
end
context "With before/after :all blocks" do
@@ -188,4 +194,17 @@ class TestLifecycle < Test::Unit::TestCase
@ivar = "yup, it's set"
end
end
+
+ context "To be compatible with rails' expectations" do
+ setup :a_method
+
+ it "should accept a symbol for an argument to setup and run that method at setup time" do
+ assert_equal "a method ran", @ivar
+ end
+ end
+
+ protected
+ def a_method
+ @ivar = "a method ran"
+ end
end

0 comments on commit f219801

Please sign in to comment.
Something went wrong with that request. Please try again.