spec/ruby-measurement/measurement_spec.rb in ruby-measurement-1.2.3 vs spec/ruby-measurement/measurement_spec.rb in ruby-measurement-1.3.0

- old
+ new

@@ -36,17 +36,17 @@ end end describe 'with invalid quantity' do it 'raises exception' do - expect { subject.new('hi') }.to raise_exception + expect { subject.new('hi') }.to raise_error(ArgumentError, "Invalid quantity: 'hi'") end end describe 'with invalid unit' do it 'raises exception' do - expect { subject.new(3, :finklebaum) }.to raise_exception + expect { subject.new(3, :finklebaum) }.to raise_error(ArgumentError, "Invalid unit: 'finklebaum'") end end end describe '.parse' do @@ -144,11 +144,11 @@ it 'converts when defined' do expect(subject.parse('3 dozen').unit).to eq Measurement::Unit[:dozen] end it 'raises exception when undefined' do - expect { subject.parse('3 finklebaums') }.to raise_error + expect { subject.parse('3 finklebaums') }.to raise_error(ArgumentError, "Invalid unit: 'finklebaums'") end end end describe '.define' do @@ -172,15 +172,15 @@ expect(result.quantity).to eq 0.25 expect(result.unit).to eq Measurement::Unit[:dozen] end it 'raises exception if unit exists and is not convertable' do - expect { measurement.convert_to(:inches) }.to raise_error + expect { measurement.convert_to(:inches) }.to raise_error(ArgumentError, "Invalid conversion: 'count' to 'in.'") end it 'raises exception if unit does not exist' do - expect { measurement.convert_to(:finklebaum) }.to raise_error + expect { measurement.convert_to(:finklebaum) }.to raise_error(ArgumentError, "Invalid unit: 'finklebaum'") end end describe '#convert_to!' do let(:measurement) { subject.new(3) } @@ -220,45 +220,45 @@ end it 'raises exception for incompatible units' do other = subject.new(4, :inches) expect(other.unit).to_not eq measurement.unit - expect { measurement + other }.to raise_error + expect { measurement + other }.to raise_error(ArgumentError, "Invalid conversion: 'in.' to 'count'") end end describe '#-' do let(:measurement) { subject.new(3) } it 'subtracts numeric values' do result = measurement - 4 - expect(result.quantity).to eq -1 + expect(result.quantity).to eq(-1) expect(result.unit).to eq measurement.unit end it 'subtracts units of the same type' do other = subject.new(4) expect(other.unit).to eq measurement.unit result = measurement - other - expect(result.quantity).to eq -1 + expect(result.quantity).to eq(-1) expect(result.unit).to eq measurement.unit end it 'subtracts units of a convertable type' do other = subject.new(2, :dozen) expect(other.unit).to_not eq measurement.unit result = measurement - other - expect(result.quantity).to eq -21 + expect(result.quantity).to eq(-21) expect(result.unit).to eq measurement.unit end it 'raises exception for incompatible units' do other = subject.new(4, :inches) expect(other.unit).to_not eq measurement.unit - expect { measurement - other }.to raise_error + expect { measurement - other }.to raise_error(ArgumentError, "Invalid conversion: 'in.' to 'count'") end end describe '#*' do let(:measurement) { subject.new(3) } @@ -288,11 +288,11 @@ end it 'raises exception for incompatible units' do other = subject.new(4, :inches) expect(other.unit).to_not eq measurement.unit - expect { measurement * other }.to raise_error + expect { measurement * other }.to raise_error(ArgumentError, "Invalid conversion: 'in.' to 'count'") end end describe '#/' do let(:measurement) { subject.new(12) } @@ -322,11 +322,11 @@ end it 'raises exception for incompatible units' do other = subject.new(4, :inches) expect(other.unit).to_not eq measurement.unit - expect { measurement / other }.to raise_error + expect { measurement / other }.to raise_error(ArgumentError, "Invalid conversion: 'in.' to 'count'") end end describe '#**' do let(:measurement) { subject.new(3) } @@ -334,11 +334,11 @@ it 'raises to the power of numeric values' do expect((measurement ** 3).quantity).to eq 27 end it 'raises exception for non-numeric values' do - expect { measurement ** subject.new(3) }.to raise_error + expect { measurement ** subject.new(3) }.to raise_error(ArgumentError, 'Invalid arithmetic: 3 count ** 3 count') end end describe '#==' do let(:measurement) { subject.new(3) } @@ -355,9 +355,49 @@ expect(measurement == subject.new(4)).to be false end it 'returns false for non-measurement objects' do expect(measurement == 3).to be false + end + end + + describe '#>' do + let(:measurement) { subject.new(3) } + + it 'raises ArgumentError for non-measurement objects' do + expect { measurement > 2 }.to raise_error(ArgumentError) + end + + it 'returns false for measurements for different unit' do + expect { measurement > subject.new(2, :dozen) }.to raise_error(ArgumentError) + end + + it 'returns true for measurements with same unit and larger quantity' do + expect(measurement > subject.new(2)).to be true + end + + it 'returns false for measurements with same unit and smaller quantity' do + expect(measurement > subject.new(4)).to be false + end + end + + describe '#<' do + let(:measurement) { subject.new(3) } + + it 'raises ArgumentError for non-measurement objects' do + expect { measurement < 4 }.to raise_error(ArgumentError) + end + + it 'returns false for measurements for different unit' do + expect { measurement < subject.new(4, :dozen) }.to raise_error(ArgumentError) + end + + it 'returns true for measurements with same unit and smaller quantity' do + expect(measurement < subject.new(4)).to be true + end + + it 'returns false for measurements with same unit and larger quantity' do + expect(measurement < subject.new(2)).to be false end end describe '#to_s' do it 'returns the quantity and unit' do