Skip to content

Commit

Permalink
T-48 Temperature#boil_water? and Temperature#freeze_water? (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Apr 15, 2020
1 parent 1311b7d commit db1916b
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ Also [Ruby coersion mechanism](https://ruby-doc.org/core/Numeric.html#method-i-c
# => -20 °C
```

### Queries
```ruby
Temperature[0, :celsius].boil_water?
# => false

Temperature[0, :celsius].freeze_water?
# => true
```

## Versioning
Basic Temperature follows the [Semantic Versioning](https://semver.org/) standard.

Expand Down
16 changes: 16 additions & 0 deletions lib/basic_temperature/temperature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,22 @@ def -@
Temperature.new(-self.degrees, self.scale)
end

##
# Returns true when temperature boils water (is greater than or equal to 100 °C),
# false otherwise.
#
def boil_water?
self.to_celsius.degrees >= 100
end

##
# Returns true when temperature freezes water (is less than or equal to 0 °C),
# false otherwise.
#
def freeze_water?
self.to_celsius.degrees <= 0
end

# Is used by {+}[rdoc-ref:Temperature#+] and {-}[rdoc-ref:Temperature#-]
# for {Ruby coersion mechanism}[https://ruby-doc.org/core/Numeric.html#method-i-coerce].
def coerce(numeric) #:nodoc:
Expand Down
2 changes: 1 addition & 1 deletion lib/basic_temperature/temperature/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def assert_numeric_or_temperature!(numeric_or_temperature)
end

def assert_numeric!(numeric)
raise_invalid_numeric unless numeric.is_a?(Numeric)
raise_invalid_numeric(numeric) unless numeric.is_a?(Numeric)
end

def assert_temperature(temperature)
Expand Down
9 changes: 8 additions & 1 deletion lib/basic_temperature/temperature/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ class InvalidDegrees < StandardError; end
# See {SCALES}[rdoc-ref:Temperature::SCALES].
class InvalidScale < StandardError; end

# Raised when <tt>other</tt> is not a Numeric or Temperature in math operations.
# Raised when <tt>other</tt> is neither Numeric nor Temperature in math operations.
class InvalidNumericOrTemperature < StandardError; end

# Raised when <tt>other</tt> is not a Numeric in math operations.
class InvalidNumeric < StandardError; end

private

def raise_initialization_arguments_error
Expand All @@ -42,6 +45,10 @@ def raise_invalid_scale_error
def raise_invalid_numeric_or_temperature_error(numeric_or_temperature)
raise InvalidNumericOrTemperature, "`#{numeric_or_temperature}` is neither Numeric nor Temperature."
end

def raise_invalid_numeric(numeric)
raise InvalidNumeric, "`#{numeric}` is not a Numeric."
end
end
end
end
80 changes: 79 additions & 1 deletion spec/basic_temperature/temperature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,86 @@
end
end

describe '#boil_water?' do
context 'when temperature is greater than 100 °C' do
it 'returns true' do
expect(Temperature.new(101, 'celsius').boil_water?).to eq(true)
end
end

context 'when temperature is equal to 100 °C' do
it 'returns true' do
expect(Temperature.new(100, 'celsius').boil_water?).to eq(true)
end
end

context 'when tempreture is less than 100 °C' do
it 'returns false' do
expect(Temperature.new(99, 'celsius').boil_water?).to eq(false)
end
end

it 'converts temperature to celsius before comparison' do
expect(Temperature.new(0, 'kelvin').boil_water?).to eq(false)
end
end

describe '#freeze_water?' do
context 'when temperature is greater than 0 °C' do
it 'returns false' do
expect(Temperature.new(1, 'celsius').freeze_water?).to eq(false)
end
end

context 'when temperature is equal to 0 °C' do
it 'returns true' do
expect(Temperature.new(0, 'celsius').freeze_water?).to eq(true)
end
end

context 'when tempreture is less than 0 °C' do
it 'returns true' do
expect(Temperature.new(-1, 'celsius').freeze_water?).to eq(true)
end
end

it 'converts temperature to celsius before comparison' do
expect(Temperature.new(0, 'kelvin').freeze_water?).to eq(true)
end
end

describe '#coerce' do
context 'when other is a Numeric' do
it (
<<~DESCRIPTION
returns two elements array,
where first element is other converted to Temperature
and has the same scale as temperature,
second - is temperature itself
DESCRIPTION
) do
temperature = Temperature.new(0, 'celsius')

coerce = temperature.coerce(10)

expect(coerce.first.degrees).to eq(10)
expect(coerce.first.scale).to eq('celsius')

expect(coerce.last).to eq(temperature)
end
end

context 'when other is NOT a Numeric' do
it 'raises Errors::InvalidNumeric' do
expect { Temperature.new(0, 'celsius').coerce('abc') }
.to raise_error(Temperature::Errors::InvalidNumeric)
.with_message('`abc` is not a Numeric.')
end
end
end

describe '#inspect' do
context 'when scale is celcius' do
context 'when scale is celsius' do
it 'returns tempeture as string in special format' do
temperature = Temperature.new(0, 'celsius')

Expand Down

0 comments on commit db1916b

Please sign in to comment.