Created
August 2, 2017 01:07
-
-
Save tjinjin/14bbd558bb555fdfeb98e1a0756f764c to your computer and use it in GitHub Desktop.
Tdd work shop
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
class ClosedRange | |
attr_reader :lower_endpoint, :upper_endpoint | |
def initialize(lower_endpoint, upper_endpoint) | |
raise IllegalRangeError if lower_endpoint > upper_endpoint | |
@lower_endpoint = lower_endpoint | |
@upper_endpoint = upper_endpoint | |
end | |
def to_s | |
"[#{lower_endpoint},#{upper_endpoint}]" | |
end | |
def include?(num) | |
(@lower_endpoint..@upper_endpoint).include?(num) | |
end | |
def ==(other_range) | |
@lower_endpoint == other_range.lower_endpoint && @upper_endpoint == other_range.upper_endpoint | |
end | |
def contain?(range) | |
self.lower_endpoint <= range.lower_endpoint && range.lower_endpoint <= self.upper_endpoint | |
end | |
end | |
class IllegalRangeError < ArgumentError ;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
require 'spec_helper' | |
describe '整数閉区間を示す' do | |
context '整数閉区間3-8' do | |
let(:range) { ClosedRange.new(3, 8) } | |
it '下端点3を持つ' do | |
expect(range.lower_endpoint).to eq(3) | |
end | |
it '上端点8を持つ' do | |
expect(range.upper_endpoint).to eq(8) | |
end | |
it '文字列表現は"[3,8]"' do | |
expect(range.to_s).to eq("[3,8]") | |
end | |
describe '数値を含む・含まない' do | |
it '2を含まない' do | |
expect(range.include?(3)).to be_truthy | |
end | |
it '3を含む' do | |
expect(range.include?(3)).to be_truthy | |
end | |
it '6を含む' do | |
expect(range.include?(6)).to be_truthy | |
end | |
it '8を含む' do | |
expect(range.include?(6)).to be_truthy | |
end | |
it '9を含まない' do | |
expect(range.include?(9)).to be_falsey | |
end | |
end | |
describe '等価判定' do | |
it '3-8と等価である' do | |
other_range = ClosedRange.new(3, 8) | |
expect(range == other_range).to be_truthy | |
end | |
it '3-9と等価である' do | |
other_range = ClosedRange.new(3, 9) | |
expect(range == other_range).to be_falsey | |
end | |
end | |
describe '包含判定' do | |
it '4-7を包含する' do | |
other_range = ClosedRange.new(4, 7) | |
expect(range.contain?(other_range)).to be_truthy | |
end | |
it '2-7を包含しない' do | |
other_range = ClosedRange.new(2, 7) | |
expect(range.contain?(other_range)).to be_falsey | |
end | |
end | |
end | |
context '整数閉区間4-9' do | |
let(:range) { ClosedRange.new(4, 9) } | |
it '下端点4を持つ' do | |
expect(range.lower_endpoint).to eq(4) | |
end | |
it '上端点9を持つ' do | |
expect(range.upper_endpoint).to eq(9) | |
end | |
it '文字列表現は"[4,9]"' do | |
expect(range.to_s).to eq("[4,9]") | |
end | |
describe '数値を含む・含まない' do | |
it '3を含まない' do | |
expect(range.include?(3)).to be_falsey | |
end | |
it '4を含む' do | |
expect(range.include?(4)).to be_truthy | |
end | |
it '9を含む' do | |
expect(range.include?(9)).to be_truthy | |
end | |
it '10を含まない' do | |
expect(range.include?(10)).to be_falsey | |
end | |
end | |
end | |
context '整数閉区間8-3' do | |
let(:range) { ClosedRange.new(8, 3) } | |
it '上端点より下端点が大きい閉区間を作ることはできない' do | |
expect{range}.to raise_exception(IllegalRangeError) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment