Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
p.129-130 6.1 Extract Method
Before Refactoring
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|