Skip to content

Commit

Permalink
p.162 Extract Surrounding Method;before refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kono committed Jun 12, 2010
1 parent b9336f1 commit 1d9f2a7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
32 changes: 32 additions & 0 deletions chapter6/Extract_Surrounding_Method.rb
@@ -0,0 +1,32 @@
class Person
attr_reader :mother, :children, :name

def initialize(name, date_of_birth, date_of_death=nil, mother=nil)
@name, @mother = name, mother
@date_of_birth, @date_of_death = date_of_birth, date_of_death
@children = []
@mother.add_child(self) if @mother
end

def add_child(child)
@children << child
end

def number_of_living_descendants
children.inject(0) do |count, child|
count += 1 if child.alive?
count + child.number_of_living_descendants
end
end

def number_of_descendants_named(name)
children.inject(0) do |count, child|
count += 1 if child.name == name
count+ child.number_of_descendants_named(name)
end
end

def alive?
@date_of_death.nil?
end
end
32 changes: 32 additions & 0 deletions chapter6/TC_Extract_Surrounding_Method.rb
@@ -0,0 +1,32 @@
require 'rubygems'
require 'test/unit'
require 'must'
require 'kconv'
require 'tempfile'
require 'Extract_Surrounding_Method'

$KCODE="UTF8"
class TC_Extract_Surrounding_Method < Test::Unit::TestCase

def assert(status,msg)
if(RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|cygwin|bccwin/)
msg=Kconv.tosjis(msg)
end
super(status,msg)
end

def setup
@kokeka = Person.new('Kokeka', '2007-09-05',nil, nil)
@kokeko1 = Person.new('Kokeko', '2007-10-05', nil, @kokeka)
@kokekakiikii1 = Person.new('Kokekakiikii', '2007-10-05', nil, @kokeka)
@kokeko2 = Person.new('Kokeko', '2007-11-05', nil, @kokeko1)
@kokekakiikii2 = Person.new('Kokeikakiikii', '2007-11-05', nil, @kokeko1)
@kokekokko = Person.new('Kokekokko', '2007-12-05', nil, @kokeko2)
@kokkokkokko = Person.new('Kokkokkokko','2007-12-05', nil, @kokeko2)
end

must "test1" do
assert_equal(6, @kokeka.number_of_living_descendants)
assert_equal(2, @kokeka.number_of_descendants_named('Kokeko'))
end
end

0 comments on commit 1d9f2a7

Please sign in to comment.