Skip to content

Instantly share code, notes, and snippets.

@biwakonbu
Created December 6, 2012 14:50
Show Gist options
  • Save biwakonbu/4224966 to your computer and use it in GitHub Desktop.
Save biwakonbu/4224966 to your computer and use it in GitHub Desktop.
プログラマのためのサバイバルマニュアル(Ruby file)
require 'test/unit'
require 'mocha/setup'
require 'rexml/document'
class CustomerStats
def initialize
@customers = []
end
def self.load
xml = File.read('customer_database.xml')
stats = CustomerStats.new
stats.append parse_xml(xml)
stats
end
def self.parse_xml(xml)
entries = []
doc = REXML::Document.new(xml)
doc.elements.each('//customer') do |customer|
entries.push({
:name => customer.elements['name'].text,
:age => customer.elements['age'].text.to_i })
end
entries
end
def append(data)
@customers += data
end
def mean_age
sum = @customers.inject(0) {|s, c| s += c[:age]}
sum / @customers.length
end
def length
@customers.length
end
end
class TestCustomerStats < Test::Unit::TestCase
def test_mean_age
data =
[{:name => 'A', :age => 33},
{:name => 'B', :age => 25}]
CustomerStats.expects(:parse_xml).returns(data)
File.expects(:read).returns(nil)
stats = CustomerStats.load
assert_equal 29, stats.mean_age
end
def test_parse_xml
stats = CustomerStats.parse_xml(canned_data_from 'customers.xml')
assert_equal 2, stats.length
assert_equal 'Alice', stats.first[:name]
end
def canned_data_from(file)
dir = File.dirname(__FILE__)
File.read(File.join(dir, 'data', file))
end
end
require 'test/unit'
class TestFactorial < Test::Unit::TestCase
def test_valid_input
assert_equal 1, 0.factorial
assert_equal 1, 1.factorial
assert_equal 2, 2.factorial
assert_equal 6, 3.factorial
end
def test_raises_onneative_input
assert_raise(ArgumentError) { -1.factorial }
end
def test_factorial_does_not_work_on_floats
assert_raise(NoMethodError) { 1.0.factorial }
end
end
class Integer
def factorial
return 1 if self == 0
raise ArgumentError if self < 0
(1..self).inject(:*)
end
end
def add(customer_id)
begin
@mutex.lock
old_head = @head
@head = Customer.new
@head.name =
@database.query(customer_id, :name)
@head.address =
@database.query(customer_id, :address)
@head.next = old_head
ensure
@mutex.unlock
end
end
# -*- coding: utf-8 -*-
require 'test/unit'
require 'mocha/setup'
require 'thread'
class Customer
attr_accessor :name, :address, :next
def initialize
@name = nil
@address = nil
@next = nil
end
end
class ShippingQueue
attr_reader :head
def initialize(database)
@database = database
@head = nil
@mutex = Mutex.new
end
def add(customer_id)
@mutex.lock
old_head = @head
@head = Customer.new
@head.name = @database.query(customer_id, :name)
@head.address = @database.query(customer_id, :address)
@head.next = old_head
rescue
@head = old_head
raise "RuntimeError: customer_id is not match."
ensure
@mutex.unlock
end
end
class ListUpdateTest < Test::Unit::TestCase
def test_database_failure
database = mock()
database.expects(:query).with(1, :name).returns('Anand')
database.expects(:query).with(1, :address).returns('')
database.expects(:query).with(2, :name).raises
q = ShippingQueue.new(database)
q.add(1)
assert_raise(RuntimeError) do
q.add(2) #<callout id="co.trapped_exception"/>
end
# リストはまだ正しい
assert_equal 'Anand', q.head.name #<callout id="co.list_verification"/>
assert_equal nil, q.head.next
end
end
# -*- coding: utf-8 -*-
t = Transaction.new(savings, checking)
t.start
# エラーの注入
checking.exports(:deposit).with(100).raises
begin
savings.deduct(100)
checking.deposit(100)
t.commit
rescue
t.rollback
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment