Added specs
This commit is contained in:
parent
dd493a09d5
commit
b890665966
@ -98,9 +98,9 @@ module UnitConversion
|
||||
def convert(value_unit)
|
||||
if value_unit.unit && value_unit.unit.metric?
|
||||
if value_unit.mass?
|
||||
value_unit = value_unit.convert('oz').auto_unit
|
||||
value_unit = value_unit.convert('oz').auto_unit.change_formatter(RationalFormatter.new)
|
||||
elsif value_unit.volume?
|
||||
value_unit = value_unit.convert('cup').auto_unit
|
||||
value_unit = value_unit.convert('cup').auto_unit.change_formatter(RationalFormatter.new)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -74,6 +74,11 @@ module UnitConversion
|
||||
unit.unitwise(value.value)
|
||||
end
|
||||
|
||||
def change_formatter(formatter)
|
||||
@formatter = formatter
|
||||
self
|
||||
end
|
||||
|
||||
# Returns a new ValueUnit scaled by the given factor
|
||||
def scale(factor)
|
||||
if factor.present?
|
||||
|
@ -36,6 +36,16 @@ RSpec.describe UnitConversion::Conversion do
|
||||
end
|
||||
end
|
||||
|
||||
describe UnitConversion::StandardUnitConversion do
|
||||
it 'converts standard units' do
|
||||
expect(UnitConversion::StandardUnitConversion.new.convert(get_value_unit(200, 'ml')).unit.to_s).to eq 'cup'
|
||||
expect(UnitConversion::StandardUnitConversion.new.convert(get_value_unit(200, 'ml')).pretty_value).to eq '7/8'
|
||||
|
||||
expect(UnitConversion::StandardUnitConversion.new.convert(get_value_unit(5, 'liter')).unit.to_s).to eq 'gallon'
|
||||
expect(UnitConversion::StandardUnitConversion.new.convert(get_value_unit(5, 'liter')).pretty_value).to eq '1 5/16'
|
||||
end
|
||||
end
|
||||
|
||||
describe UnitConversion::ConvertConversion do
|
||||
it 'converts standard units' do
|
||||
expect(UnitConversion::ConvertConversion.new(get_unit('tbsp')).convert(get_value_unit(1, 'cups')).raw_value).to eq 16
|
||||
|
Loading…
Reference in New Issue
Block a user