Skip to content

Instantly share code, notes, and snippets.

@sinsoku
Last active Dec 29, 2015
Embed
What would you like to do?
class LeapYear
LEAP_MSG = "%d is a leap year"
NOT_LEAP_MSG = "%d is not a leap year"
class << self
def leap?(n)
n % 400 == 0 || (n % 4 == 0 && n % 100 != 0)
end
def msg(n)
leap?(n) ? LEAP_MSG % n : NOT_LEAP_MSG % n
end
def run
t = gets.chomp.to_i
t.times.each do
n = gets.chomp.to_i
puts msg(n)
end
end
end
end
if __FILE__ == $0
LeapYear.run
end
require_relative 'leap_year'
describe LeapYear do
describe ".run" do
describe "example.1" do
let(:input) { ['4', '1000', '1992', '2000', '2001'] }
let(:output) { ['1000 is not a leap year',
'1992 is a leap year',
'2000 is a leap year',
'2001 is not a leap year',] }
before do
LeapYear.stub(:gets).and_return(*input)
output.each { |x| LeapYear.should_receive(:puts).with(x) }
end
it { LeapYear.run }
end
end
describe ".leap?" do
it { expect(LeapYear.leap?(1000)).to be_false }
it { expect(LeapYear.leap?(1992)).to be_true }
it { expect(LeapYear.leap?(2000)).to be_true }
it { expect(LeapYear.leap?(2001)).to be_false }
end
describe ".msg" do
context "n is a leap year" do
before { LeapYear.stub(leap?: true) }
it { expect(LeapYear.msg(0)).to eq '0 is a leap year' }
end
context "n is not a leap year" do
before { LeapYear.stub(leap?: false) }
it { expect(LeapYear.msg(0)).to eq '0 is not a leap year' }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment