Skip to content

Commit

Permalink
Merge pull request #15 from emilybache/master
Browse files Browse the repository at this point in the history
Small updates to python code, updated problem description
  • Loading branch information
lucaminudel committed Sep 22, 2012
2 parents 428078b + 2d1de05 commit 7408b5d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
20 changes: 15 additions & 5 deletions TDDMicroExercises/README.md
Expand Up @@ -2,14 +2,24 @@


## Instructions ## Instructions


Here follow these 4 distinct projects. They could be code you inherited from a legacy code-base. Now you want to write unit tests for these projects: Here follow 4 distinct problems. They could be code you inherited from a legacy code-base. Now you want to write unit tests for them, and to do so you will need to do some refactoring. Take care when refactoring not to change interfaces which client code may rely on. Apply the unit testing style and framework you are most comfortable with. You can choose to use stubs or mocks or none at all. If you do, you are free to use the mocking tool that you prefer.


1. **TirePressureMonitoringSystem exercise**: write the unit tests for the Alarm class, refactor the code as much as you need to make the class testable 1. **TirePressureMonitoringSystem exercise**: write the unit tests for the Alarm class, refactor the code as much as you need to make the class testable
1. **UnicodeFileToHtmTextConverter exercise**: write the unit tests for the UnicodeFileToHtmTextConverter class, refactor the code as much as you need to make the class testable
1. **TelemetrySystem exercise**: write the unit tests for the TelemetryDiagnosticControls class, refactor the code as much as you need to make the class testable
1. **TicketDispenser exercise**: write the unit tests for the TicketDispenser, refactor the code as much as you need to make the class testable


Apply the unit testing style you are more comfortable with. You can choose to use stub or mock or not at all. If you do, you are free to use the mocking tool that you prefer. Do the same for the unit testing framework. The Alarm class is designed to monitor tire pressure and set an alarm if the pressure falls outside of the expected range. The Sensor class provided for the exercise fakes the behaviour of a real tire sensor, providing random but realistic values.

2. **UnicodeFileToHtmTextConverter exercise**: write the unit tests for the UnicodeFileToHtmTextConverter class, refactor the code as much as you need to make the class testable.

The UnicodeFileToHtmTextConverter class is designed to reformat a plain text file for display in a browser.

3. **TelemetrySystem exercise**: write the unit tests for the TelemetryDiagnosticControls class, refactor the code as much as you need to make the class testable.

The responsibility of the TelemetryDiagnosticControls class is to establish a connection to the telemetry server (through the TelemetryClient), send a diagnostic request and successfully receive the response that contains the diagnostic info. The TelemetryClient class provided for the exercise fakes the behavior of the real TelemetryClient class, and can respond with either the diagnostic information or a random sequence. The real TelemetryClient class would connect and communicate with the telemetry server via tcp/ip.

4. **TicketDispenser exercise**: write the unit tests for the TicketDispenser, refactor the code as much as you need to make the class testable

The TicketDispenser class is designed to be used to manage a queuing system in a shop. There may be more than one ticket dispenser but the same ticket should not be issued to two different customers.



## A possible solution ## A possible solution


Expand Down
Expand Up @@ -5,11 +5,12 @@ class UnicodeFileToHtmlTextConverter(object):
def __init__(self, full_filename_with_path): def __init__(self, full_filename_with_path):
self.full_filename_with_path = full_filename_with_path self.full_filename_with_path = full_filename_with_path


def convertToHtml(self): def convert_to_html(self):
f = open(self.full_filename_with_path, "r") f = open(self.full_filename_with_path, "r")
html = "" html = ""
for line in f: for line in f:
html += cgi.escape(line) line = line.rstrip()
html += "<br />" html += cgi.escape(line, quote=True)

html += "<br />"
return html
return html
Expand Up @@ -18,17 +18,14 @@ def sample_pressure():
class Alarm(object): class Alarm(object):


def __init__(self): def __init__(self):
self.low_pressure_threshold = 17 self._low_pressure_threshold = 17
self.high_pressure_threshold = 21 self._high_pressure_threshold = 21
self.sensor = Sensor() self._sensor = Sensor()
self.alarm_on = False self.is_alarm_on = False
self.alarm_count = 0 self._alarm_count = 0


def check(self): def check(self):
psi_pressure_value = self.sensor.pop_next_pressure_psi_value() psi_pressure_value = self._sensor.pop_next_pressure_psi_value()
if psi_pressure_value < self.low_pressure_threshold or self.high_pressure_threshold < psi_pressure_value: if psi_pressure_value < self._low_pressure_threshold or self._high_pressure_threshold < psi_pressure_value:
self.alarm_on = True self.is_alarm_on = True
self.alarm_count += 1 self._alarm_count += 1

def is_alarm_on(self):
return self.alarm_on

0 comments on commit 7408b5d

Please sign in to comment.