Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
p.129-130 6.1 Extract Method
Before Refactoring
  • Loading branch information
kono committed May 8, 2010
1 parent 1b39830 commit f62c285
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
38 changes: 38 additions & 0 deletions chapter6/Extract_Method.rb
@@ -0,0 +1,38 @@

class Extract_Method_Order
attr_reader :amount
def initialize(amount)
@amount = amount
end
end

class Extract_Method
def initialize(name)
@name=name
@orders=Array.new
end

def order_entry(amount)
@orders << Extract_Method_Order.new(amount)
end

def print_owing
outstanding = 0.0

#バナーを出力(print banner)
puts "*************************"
puts "***** Customer Owes *****"
puts "*************************"

#勘定を計算(calculate outstanding)
@orders.each do |order|
outstanding += order.amount
end

#詳細を表示(print details)
puts "name: #{@name}"
puts "amount: #{outstanding}"
end

end

37 changes: 37 additions & 0 deletions chapter6/TC_Extract_Method.rb
@@ -0,0 +1,37 @@
require 'rubygems'
require 'test/unit'
require 'must'
require 'kconv'
require 'tempfile'
require 'Extract_Method'

$KCODE="UTF8"
class TC_S6_1_with_local_val < 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
@obj = Extract_Method.new("kono")
@obj.order_entry(10)
@obj.order_entry(20)
@obj.order_entry(30)
end

must "test1" do
temp=Tempfile::new("foobar")
$stdout=temp
@obj.print_owing
$stdout=STDOUT
temp.close
temp.open
result= temp.read
expectation = "*************************\n***** Customer Owes *****\n*************************\nname: kono\namount: 60.0\n"
temp.close
assert_equal(result, expectation)
end
end
22 changes: 22 additions & 0 deletions chapter6/must.rb
@@ -0,0 +1,22 @@
module Test::Unit
# Used to fix a minor minitest/unit incompatibility in flexmock
#AssertionFailedError = Class.new(StandardError)

class TestCase

def self.must(name, &block)
test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
defined = instance_method(test_name) rescue false
raise "#{test_name} is already defined in #{self}" if defined
if block_given?
define_method(test_name, &block)
else
define_method(test_name) do
flunk "No implementation provided for #{name}"
end
end
end

end
end

0 comments on commit f62c285

Please sign in to comment.