Permalink
Browse files

refactoring a bit, still not happy with the context expanding

  • Loading branch information...
1 parent 5f0b105 commit 58b84c81cfb3e1eb9408bcd3c6da56f3ad83cdaf Sven Fuchs committed Dec 3, 2008
Showing with 66 additions and 51 deletions.
  1. +1 −1 README.textile
  2. +4 −7 demo.rb
  3. +3 −3 lib/with/context.rb
  4. +12 −7 lib/with/group.rb
  5. +2 −2 test/compile_test.rb
  6. +20 −17 test/context_test.rb
  7. +7 −10 test/group_test.rb
  8. +17 −4 test/test_helper.rb
View
@@ -9,7 +9,7 @@ by "Jeremy McAnally":http://github.com/jeremymcanally and
(as well as "rspec_on_rails_on_crack":http://github.com/technoweenie/rspec_on_rails_on_crack)
by "Rick Olson":http://github.com/technoweenie.
-For some demo code see: runnable "demo controller test":http://github.com/svenfuchs/with/tree/master/test/demo.rb.
+For some demo code see: runnable "demo controller test":http://github.com/svenfuchs/with/tree/master/demo.rb.
Style and initial motivation: "http://gist.github.com/30436":http://gist.github.com/30436.
In short the goal of this library is:
View
11 demo.rb
@@ -109,6 +109,10 @@ class ArticlesControllerTest < ActionController::TestCase
action { post :create, @params }
+ it "passes a common assertion (before block was called)" do
+ assert @artificial_precondition
+ end
+
with :login_as_admin do
it_assigns :article
@@ -124,13 +128,6 @@ class ArticlesControllerTest < ActionController::TestCase
with :login_as_user, :no_login do
it_redirects_to { '/login' }
end
-
- it "has called the before block" do
- # TODO remove this extra block
- assertion do
- assert @artificial_precondition
- end
- end
end
puts "tests defined: \n " + instance_methods.grep(/^test_/).join(", \n ")
View
@@ -1,7 +1,7 @@
module With
class Context
attr_accessor :parent
- attr_reader :name, :children
+ attr_reader :name, :children, :action, :assertions, :preconditions
def initialize(name, action, preconditions, assertions, &block)
@name, @action, @preconditions, @assertions = name, action, [], []
@@ -60,9 +60,9 @@ def define_test_method(target, context)
method_name = generate_test_method_name(context)
target.send :define_method, method_name, &lambda {
- preconditions.map { |precondition| instance_eval &precondition }
+ preconditions.map { |precondition| puts precondition.name; instance_eval &precondition }
instance_eval &action if action
- assertions.map { |assertion| instance_eval &assertion }
+ assertions.map { |assertion| puts assertion.name; instance_eval &assertion }
}
end
View
@@ -11,15 +11,15 @@ def initialize(*names, &block)
end
def with(*names, &block)
- options = names.last.is_a?(Hash) ? names.pop : {}
- group = options[:with] ? with(*options[:with]) : self
- group.add_child(*names, &block)
+ add_child(*names, &block)
end
- alias :it :with
def assertion(name = nil, options = {}, &block)
- assertions << NamedBlock.new(name, &block)
+ # options = names.last.is_a?(Hash) ? names.pop : {}
+ group = options[:with] ? with(*options[:with]) : self
+ group.assertions << NamedBlock.new(name, &block)
end
+ alias :it :assertion
def before(name = nil, &block)
name ||= "before #{block.inspect}"
@@ -58,8 +58,13 @@ def expand
end
def contexts_for(name)
- if shared_contexts = find_shared(name)
- shared_contexts.map{|c| c.expand }.flatten
+ # TODO refactor this mess
+ if shared_groups = find_shared(name)
+ shared_groups.map do |g|
+ context = Context.new g.names.first, nil,
+ g.preconditions + preconditions.dup,
+ g.assertions += assertions.dup
+ end
else
[Context.new(name, @action, preconditions.dup, assertions.dup)]
end
View
@@ -28,8 +28,8 @@ class TestUnitWithTest < Test::Unit::TestCase
@@tests_defined = instance_methods.grep(/^test_/).map{|name| name.gsub(/test_[\d]*/, 'test')}.sort
def test_with_defined_two_tests
- names = [ "test_foo_with_context_1_and_with_context_3_and_with_does_something",
- "test_foo_with_context_2_and_with_context_3_and_with_does_something" ]
+ names = [ "test_foo_with_context_1_and_with_context_3",
+ "test_foo_with_context_2_and_with_context_3" ]
assert_equal names, @@tests_defined
end
end
View
@@ -25,39 +25,42 @@ def test_expanded_structure
@nested_context_1 = @context.children[1]
@nested_context_2 = @context.children[1]
- assert_equal 'does something', @does_something.name
+ assert_equal 'something', @does_something.name
assert_equal :nested_context, @nested_context_1.name
assert_equal :nested_context, @nested_context_2.name
# does_something has one child: does_something_nested
assert_equal 1, @does_something.children.size
@does_something_nested = @does_something.children[0]
- assert_equal 'does something nested', @does_something_nested.name
+ assert_equal 'something nested', @does_something_nested.name
- # nested_context_1 and nested_context_2 have each one child: does_something_in_nested_context
- assert_equal 1, @nested_context_1.children.size
- assert_equal 1, @nested_context_2.children.size
- @does_something_in_nested_context = @nested_context_1.children[0]
-
- assert_equal 'does something in nested_context', @does_something_in_nested_context.name
-
- # it's actually the same object instance. not sure if that could cause any trouble:
- assert_equal @nested_context_1.children[0], @nested_context_2.children[0]
+ # nested_context_1 and nested_context_2 have no children
+ assert_equal 0, @nested_context_1.children.size
+ assert_equal 0, @nested_context_2.children.size
end
def test_expanded_leafs
leafs = @expanded.first.leafs
- expected = [ "does something nested",
- "does something in nested_context",
- "does something in nested_context" ]
+ expected = [ "something nested", :nested_context, :nested_context ]
assert_equal expected, leafs.map(&:name)
- expected = [ ["root", :context, "does something", "does something nested"],
- ["root", :context, :nested_context, "does something in nested_context"],
- ["root", :context, :nested_context, "does something in nested_context"] ]
+ expected = [ ["root", :context, "something", "something nested"],
+ ["root", :context, :nested_context],
+ ["root", :context, :nested_context] ]
result = leafs.map {|leaf| leaf.parents.map(&:name) << leaf.name }
assert_equal expected, result
end
+
+ def test_collected_assertions
+ leafs = @expanded.first.leafs
+ expected = [[:defined_assertion, "asserts something", :defined_assertion_in_context, "asserts something nested"],
+ [:defined_assertion, "asserts something", "asserts something in nested_context"],
+ [:defined_assertion, "asserts something", "asserts something in nested_context"]]
+ result = leafs.map { |leaf| leaf.send(:collect_assertions).map(&:name) }
+ assert_equal expected, result
+ end
+
+
end
View
@@ -22,20 +22,17 @@ def test_with_creates_a_child_group
assert something.is_a?(Group)
assert nested_context.is_a?(Group)
- assert_equal ['does something'], something.names
+ assert_equal ['something'], something.names
assert_equal [:nested_context], nested_context.names
# something has one child: something_nested
assert_equal 1, something.children.size
something_nested = something.children[0]
assert something_nested.is_a?(Group)
- assert_equal ['does something nested'], something_nested.names
+ assert_equal ['something nested'], something_nested.names
- # nested_context has one child: something_nested_innested_context
- assert_equal 1, nested_context.children.size
- something_nested_innested_context = nested_context.children[0]
- assert something_nested_innested_context.is_a?(Group)
- assert_equal ['does something in nested_context'], something_nested_innested_context.names
+ # nested_context has no children: something_in_nested_context
+ assert_equal 0, nested_context.children.size
end
def test_sets_action_on_current_group
@@ -54,7 +51,7 @@ def test_records_preconditions_on_current_group
assert_equal [:unique_precondition], something_nested.preconditions.map(&:name)
end
- # def test_records_calls_to_unknown_methods_as_assertions
- # assert_equal :unknown_assertion, @group.assertions[0].name.to_sym
- # end
+ def test_records_calls_to_unknown_methods_as_assertions
+ assert_equal :defined_assertion, @group.assertions[0].name.to_sym
+ end
end
View
@@ -6,23 +6,36 @@ module GroupSetup
def setup_example_group
Group.new 'root' do
- # unknown_assertion :foo, :bar
action { :action_on_group }
before :common_precondition do end
+ defined_assertion
+
+ it "asserts something" do
+ assert :something
+ end
+
with :context do
action { :action_on_context }
before :shared_precondition do end
- it 'does something' do
- it 'does something nested' do
+ with 'something' do
+ defined_assertion_in_context
+
+ with 'something nested' do
before :unique_precondition do end
+
+ it "asserts something nested" do
+ assert :something_nested
+ end
end
end
- it 'does something in nested_context', :with => :nested_context do end
+ it 'asserts something in nested_context', :with => :nested_context do
+ defined_assertion_nested_context
+ end
end
share :nested_context,

0 comments on commit 58b84c8

Please sign in to comment.