Skip to content
Browse files

Merge pull request #8 from Sam-Serpoosh/master

Added the Ruby version of TirePressureMonitorySystem with tests and refactorings
  • Loading branch information...
2 parents 45097f9 + b1b5c10 commit 2e297ad00cc2b401ead8a8029ceead65bfb953a1 @lucaminudel committed Sep 6, 2012
View
1 TDDMicroExercises.YoursSolutions/Sam-Serpoosh/.gitignore
@@ -0,0 +1 @@
+*.swp
View
41 TDDMicroExercises.YoursSolutions/Sam-Serpoosh/code/tire_pressure_monitoring_system/alarm.rb
@@ -0,0 +1,41 @@
+require_relative "./sensor"
+
+class Alarm
+ LOWPRESSURETHRESHOLD = 17
+ HIGHPRESSURETHRESHOLD = 21
+
+ attr_reader :alarm_count
+
+ def initialize(sensor=Sensor.new)
+ @sensor = sensor
+ turn_off_alarm
+ @alarm_count = 0
+ end
+
+ def check
+ psi_pressure = @sensor.pop_next_pressure_psi_value
+
+ if dangerous_pressure?(psi_pressure)
+ turn_on_alarm
+ @alarm_count += 1
+ end
+ end
+
+ def on?
+ @alarm_on
+ end
+
+ private
+ def dangerous_pressure?(pressure)
+ pressure < LOWPRESSURETHRESHOLD ||
+ pressure > HIGHPRESSURETHRESHOLD
+ end
+
+ def turn_on_alarm
+ @alarm_on = true
+ end
+
+ def turn_off_alarm
+ @alarm_on = false
+ end
+end
View
13 TDDMicroExercises.YoursSolutions/Sam-Serpoosh/code/tire_pressure_monitoring_system/sensor.rb
@@ -0,0 +1,13 @@
+class Sensor
+ OFFSET = 16;
+
+ def pop_next_pressure_psi_value
+ OFFSET + generate_sample_pressure
+ end
+
+ def generate_sample_pressure
+ #placeholder implementation that simulate
+ #a real sensor in a real tire
+ rand(6) * rand(6)
+ end
+end
View
46 ...cises.YoursSolutions/Sam-Serpoosh/spec/tire_pressure_monitoring_system_spec/alarm_spec.rb
@@ -0,0 +1,46 @@
+require_relative "../../code/tire_pressure_monitoring_system/alarm"
+
+describe Alarm do
+ context "#alarm_status" do
+ it "turns on alarm when pressure is less than minimum threshold" do
+ check_pressure(Alarm::LOWPRESSURETHRESHOLD - 1, true)
+ end
+
+ it "turns on alarm when pressure is greater than maximum threshold" do
+ check_pressure(Alarm::HIGHPRESSURETHRESHOLD + 1, true)
+ end
+
+ it "turns off alarm when pressure is at the threshold" do
+ check_pressure(Alarm::HIGHPRESSURETHRESHOLD, false)
+ end
+ end
+
+ context "#alarm_count" do
+ it "increases nubmer of alarms after turning on the alarm" do
+ alarm = create_alarm_with_stub_sensor(
+ Alarm::HIGHPRESSURETHRESHOLD + 1)
+ expect do
+ alarm.check
+ end.to change { alarm.alarm_count }.by 1
+ end
+
+ it "doesn't increase alarm counts when doesn't turn on alarm" do
+ alarm = create_alarm_with_stub_sensor(
+ Alarm::LOWPRESSURETHRESHOLD)
+ expect do
+ alarm.check
+ end.not_to change { alarm.alarm_count }
+ end
+ end
+
+ def check_pressure(stub_pressure, expected_alarm_status)
+ alarm = create_alarm_with_stub_sensor(stub_pressure)
+ alarm.check
+ alarm.on?.should == expected_alarm_status
+ end
+
+ def create_alarm_with_stub_sensor(stub_pressure)
+ sensor = stub(:pop_next_pressure_psi_value => stub_pressure)
+ Alarm.new(sensor)
+ end
+end

0 comments on commit 2e297ad

Please sign in to comment.
Something went wrong with that request. Please try again.