spec/lib/percentable/percent_spec.rb in percentable-0.0.1 vs spec/lib/percentable/percent_spec.rb in percentable-0.1.0

- old
+ new

@@ -1,6 +1,8 @@ -require 'percentable/percent' +require 'spec_helper' +require 'bigdecimal' +require 'percentable' describe Percentable::Percent do subject { Percentable::Percent.new(value) } let(:value) { nil } @@ -66,10 +68,52 @@ expect(subject.zero?).to be false end end end + describe '#to_i' do + [0.1, 0.5, 1, 1.8, 2, 2.2].each do |value| + context "value of #{value}" do + let(:value) { value } + + it "should return #{value.floor} for #{value}" do + expect(subject.to_i).to eq value.floor + end + end + end + end + + describe '#coerce' do + context 'when other is non numeric' do + let(:other) { 'string' } + + it 'should raise a TypeError' do + expect { subject.coerce(other) }.to raise_error TypeError + end + end + + context 'math' do + let(:value) { 50 } + + it 'should add the value of percent*number' do + expect(10 + subject).to eq 15 + end + + it 'should subtract the value of percent*number' do + expect(10 - subject).to eq 5 + end + + it 'should multiply by the float value of the percent' do + expect(10 * subject).to eq 5 + end + + it 'should divide by the float value of the percent' do + expect(10 / subject).to eq 20 + end + end + end + shared_examples 'it is equal' do |equal_method| it 'should consider itself equal to other percents with the same value' do percent1 = subject.class.new(50) percent2 = subject.class.new(50) @@ -151,15 +195,15 @@ end end describe '#*' do context 'multiplying percents' do - let(:percent1) { subject.class.new(9) } - let(:percent2) { subject.class.new(9) } + let(:percent1) { subject.class.new(10) } + let(:percent2) { subject.class.new(10) } it 'should return the result of multiplying the percents' do - expect(percent1 * percent2).to eq subject.class.new(81) + expect(percent1 * percent2).to eq subject.class.new(1) end end context 'multiplying other numerics' do let(:numeric) { 2 } @@ -227,9 +271,29 @@ let(:percent) { subject.class.new(101) } it 'should say the percent is larger' do expect(percent > integer).to be true end + end + end + end + + describe '.from_numeric' do + let(:n) { 0.5 } + + it 'should multiply the input by 100 and return a percent for that value' do + expect(subject.class.from_numeric(n)).to eq Percent.new(50) + end + + it 'should be considered equal to the numeric it was passed' do + expect(subject.class.from_numeric(n)).to eq n + end + + context 'when passed a string' do + let(:n) { 'string' } + + it 'should raise an argument error' do + expect { subject.class.from_numeric(n) }.to raise_error TypeError end end end end