|
require 'rspec' |
|
require_relative 'fibonacci' |
|
|
|
RSpec.describe Fibonacci do |
|
context 'with default initialization' do |
|
let(:fib) { described_class.new } |
|
|
|
it 'is enumerable' do |
|
expect(fib).to respond_to :each |
|
end |
|
|
|
it 'up to a default limit of 4,000,000' do |
|
expect(fib.to_a.last).to be < 4_000_000 |
|
end |
|
|
|
it 'can calculate even numbers' do |
|
expect(fib.evens.to_a).to eq [2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578] |
|
end |
|
|
|
it 'can calculate odd numbers' do |
|
expect(fib.odds.to_a).to eq [1, 1, 3, 5, 13, 21, 55, 89, 233, 377, 987, 1597, 4181, 6765, 17711, 28657, 75025, 121393, 317811, 514229, 1346269, 2178309] |
|
end |
|
|
|
it 'has nice stringified version' do |
|
expect(fib.to_s).to eq "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578]" |
|
end |
|
end |
|
context 'with custom limit' do |
|
let(:fib) { described_class.new(100) } |
|
|
|
it 'is enumerable' do |
|
expect(fib).to respond_to :each |
|
end |
|
|
|
it 'stops calculating number up to its limit' do |
|
expect(fib.to_a.last).to eq 89 |
|
end |
|
|
|
it 'can calculate even numbers' do |
|
expect(fib.evens.to_a).to eq [2, 8, 34] |
|
end |
|
|
|
it 'can calculate odd numbers' do |
|
expect(fib.odds.to_a).to eq [1, 1, 3, 5, 13, 21, 55, 89] |
|
end |
|
|
|
it 'has nice stringified version' do |
|
expect(fib.to_s).to eq "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]" |
|
end |
|
end |
|
end |