Permalink
Browse files

more readable unit test

  • Loading branch information...
1 parent 9f48382 commit 85a5c028073a4b3a12d4bed433fea2801ee2f450 Sven Fuchs committed Dec 1, 2008
Showing with 38 additions and 38 deletions.
  1. +21 −19 lib/with/context.rb
  2. +2 −2 test/demo.rb
  3. +1 −2 test/testunit_with_test.rb
  4. +14 −15 test/with_test.rb
View
@@ -13,37 +13,39 @@ def initialize(name, action, preconditions, assertions, &block)
@preconditions += preconditions
@assertions += assertions
end
+
+ def leafs
+ return self if children.empty?
+ children.map { |child| child.leafs }.flatten
+ end
+
+ def calls
+ [find_action, collect_preconditions, collect_assertions]
+ end
def add_children(*children)
children.each do |child|
child.parent = self
@children << child
end
end
-
- def find_action
- @action || parent && parent.find_action || raise("could not find action")
- end
-
- def collect_preconditions
- (parent ? parent.collect_preconditions : []) + @preconditions
- end
-
- def collect_assertions
- (parent ? parent.collect_assertions : []) + @assertions
- end
def self_and_parents
(parent ? parent.self_and_parents : []) + [self]
end
- def leafs
- return self if children.empty?
- children.map { |child| child.leafs }.flatten
- end
+ protected
- def calls
- [find_action, collect_preconditions, collect_assertions]
- end
+ def find_action
+ @action || parent && parent.find_action || raise("could not find action")
+ end
+
+ def collect_preconditions
+ (parent ? parent.collect_preconditions : []) + @preconditions
+ end
+
+ def collect_assertions
+ (parent ? parent.collect_assertions : []) + @assertions
+ end
end
end
View
@@ -1,10 +1,10 @@
-$:.unshift File.dirname(__FILE__) + '/../lib/'
+require File.dirname(__FILE__) + '/test_helper'
+
require 'rubygems'
require 'actionpack'
require 'action_controller'
require 'action_controller/test_process'
require 'active_support'
-require 'with'
# setup some fakes so the demo can run
@@ -1,5 +1,4 @@
-$:.unshift File.dirname(__FILE__) + '/../lib/'
-require 'with'
+require File.dirname(__FILE__) + '/test_helper'
class TestUnitWithTest < Test::Unit::TestCase
extend With
View
@@ -1,5 +1,4 @@
-$:.unshift File.dirname(__FILE__) + '/../lib/'
-require 'with'
+require File.dirname(__FILE__) + '/test_helper'
# when expanded it sets up the following context tree:
#
@@ -153,62 +152,62 @@ def test_context_group_exapand
root = @group.send(:expand).first
expected = [:'context 1.1', :'context 1.2']
- assert_equal expected, root.children.map {|child| child.name }
+ assert_equal expected, root.children.map(&:name)
expected = [:'context 2']
- assert_equal expected, root.children[0].children.map {|child| child.name }
+ assert_equal expected, root.children[0].children.map(&:name)
expected = [:"context 3.1", :"context 3.2", :"context 4", :"context 4"]
- assert_equal expected, root.children[0].children[0].children.map {|child| child.name }
+ assert_equal expected, root.children[0].children[0].children.map(&:name)
expected = [:'context 2']
- assert_equal expected, root.children[1].children.map {|child| child.name }
+ assert_equal expected, root.children[1].children.map(&:name)
expected = [:"context 3.1", :"context 3.2", :"context 4", :"context 4"]
- assert_equal expected, root.children[1].children[0].children.map {|child| child.name }
+ assert_equal expected, root.children[1].children[0].children.map(&:name)
end
def test_context_group_exapanded_collect_assertations
root = @group.send(:expand).first
expected = [:'shared assertion 1.1.1', :'shared assertion 1.1.2']
- assert_equal expected, root.children[0].collect_assertions.map {|a| a.name }
+ assert_assertions expected, root.children[0]
expected = [:"shared assertion 1.1.1", :"shared assertion 1.1.2",
:"shared assertion 2", :"assertion 2.3"]
- assert_equal expected, root.children[0].children[0].collect_assertions.map {|a| a.name }
+ assert_assertions expected, root.children[0].children[0]
expected = [:"shared assertion 1.1.1", :"shared assertion 1.1.2",
:"shared assertion 2", :"assertion 2.3",
:"shared assertion 3.1", :"assertion 2.1"]
- assert_equal expected, root.children[0].children[0].children[0].collect_assertions.map {|a| a.name }
+ assert_assertions expected, root.children[0].children[0].children[0]
expected = [:"shared assertion 1.1.1", :"shared assertion 1.1.2",
:"shared assertion 2", :"assertion 2.3",
:"shared assertion 3.2", :"assertion 2.1"]
- assert_equal expected, root.children[0].children[0].children[1].collect_assertions.map {|a| a.name }
+ assert_assertions expected, root.children[0].children[0].children[1]
expected = [:"shared assertion 1.1.1", :"shared assertion 1.1.2",
:"shared assertion 2", :"assertion 2.3",
:"shared assertion 4.1", :"assertion 2.2"]
- assert_equal expected, root.children[0].children[0].children[2].collect_assertions.map {|a| a.name }
+ assert_assertions expected, root.children[0].children[0].children[2]
expected = [:"shared assertion 1.2", :"shared assertion 2",
:"assertion 2.3", :"shared assertion 4.1",
:"assertion 2.2"]
- assert_equal expected, root.children[1].children[0].children[2].collect_assertions.map {|a| a.name }
+ assert_assertions expected, root.children[1].children[0].children[2]
expected = [:"shared assertion 1.2", :"shared assertion 2",
:"assertion 2.3", :"shared assertion 4.2",
:"assertion 2.2"]
- assert_equal expected, root.children[1].children[0].children[3].collect_assertions.map {|a| a.name }
+ assert_assertions expected, root.children[1].children[0].children[3]
end
def test_leafs
root = @group.send(:expand).first
expected = [ :"context 3.1", :"context 3.2", :"context 4", :"context 4",
:"context 3.1", :"context 3.2", :"context 4", :"context 4"]
- assert_equal expected, root.leafs.map {|c| c.name }
+ assert_equal expected, root.leafs.map(&:name)
end
def test_calls

0 comments on commit 85a5c02

Please sign in to comment.