Permalink
Browse files

refactored context expanding, a bit happier with that now

  • Loading branch information...
Sven Fuchs
Sven Fuchs committed Dec 3, 2008
1 parent 58b84c8 commit 249068bdae15f01734d7ee9f3802334f84147b9a
Showing with 52 additions and 29 deletions.
  1. +8 −0 demo.rb
  2. +1 −1 lib/with.rb
  3. +2 −2 lib/with/context.rb
  4. +23 −18 lib/with/group.rb
  5. +11 −5 test/compile_test.rb
  6. +2 −2 test/context_test.rb
  7. +1 −1 test/group_test.rb
  8. +4 −0 test/test_helper.rb
View
@@ -68,6 +68,14 @@ class ActionController::TestCase
before { @controller.current_user = User.new(true) }
end
+ share :login_as_user do
+ before { @controller.current_user = User.new(false) }
+ end
+
+ share :no_login do
+ before { @controller.current_user = nil }
+ end
+
share :valid_article_params do
before { @params = valid_article_params }
end
View
@@ -17,7 +17,7 @@ def inherited(base)
def describe(name, &block)
group = Group.new name, &block
- shared.each {|name, contexts| group.share(name, *contexts) }
+ shared.each {|name, groups| group.share(name, *groups) }
group.compile(self)
group
end
View
@@ -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| puts precondition.name; instance_eval &precondition }
+ preconditions.map { |precondition| instance_eval &precondition }
instance_eval &action if action
- assertions.map { |assertion| puts assertion.name; instance_eval &assertion }
+ assertions.map { |assertion| instance_eval &assertion }
}
end
View
@@ -48,30 +48,35 @@ def compile(target)
end
protected
-
+
def expand
+ contexts = use_shared? ? shared_contexts : [to_context]
+ contexts.each do |context|
+ context.append_children children.map{|c| c.expand }.flatten
+ end
+ end
+
+ def use_shared?
+ names.first.is_a?(Symbol)
+ end
+
+ def shared_group(name)
+ shared[name] || parent && parent.shared_group(name) or raise "could not find shared context #{name.inspect}"
+ end
+
+ def shared_contexts
names.map do |name|
- contexts_for(name).each do |context|
- context.append_children children.map{|c| c.expand }.flatten
+ shared_group(name).map do |group|
+ group.to_context(@action, preconditions, assertions)
end
end.flatten
end
- def contexts_for(name)
- # 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
- end
-
- def find_shared(name)
- shared[name] || parent && parent.find_shared(name)
+ def to_context(action = nil, preconditions = [], assertions = [])
+ action ||= @action
+ # raise if there's more than one name?
+ # or maybe better have separate attributes for name and shared_names?
+ Context.new(names.first, action, self.preconditions + preconditions, self.assertions + assertions)
end
def add_child(*names, &block)
View
@@ -2,12 +2,18 @@
class TestUnitWithTest < Test::Unit::TestCase
include With
+
+ share :'context 2' do
+ before :'precondition 2' do
+ :'called precondition 2'
+ end
+ end
describe 'foo' do
action { :'called action!' }
with :'context 1', :'context 2' do
- it :'does something', :with => :'context 3' do
+ it 'does something', :with => :'context 3' do
:'called assertion 1'
end
end
@@ -17,11 +23,11 @@ class TestUnitWithTest < Test::Unit::TestCase
:'called precondition 1'
end
end
- end
- share :'context 2' do
- before :'precondition 2' do
- :'called precondition 2'
+ share :'context 3' do
+ before :'precondition 3' do
+ :'called precondition 3'
+ end
end
end
View
@@ -41,7 +41,7 @@ def test_expanded_structure
def test_expanded_leafs
leafs = @expanded.first.leafs
-
+
expected = [ "something nested", :nested_context, :nested_context ]
assert_equal expected, leafs.map(&:name)
@@ -51,7 +51,7 @@ def test_expanded_leafs
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"],
View
@@ -52,6 +52,6 @@ def test_records_preconditions_on_current_group
end
def test_records_calls_to_unknown_methods_as_assertions
- assert_equal :defined_assertion, @group.assertions[0].name.to_sym
+ assert_equal :defined_assertion, @group.assertions[0].name
end
end
View
@@ -38,6 +38,10 @@ def setup_example_group
end
end
+ share :context do
+ # whatever
+ end
+
share :nested_context,
lambda { before :precondition_in_nested_context_1 do end },
lambda { before :precondition_in_nested_context_2 do end }

0 comments on commit 249068b

Please sign in to comment.