forked from emilybache/TDDwithMockObjectsAndDesignPrinciples
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lucaminudel#7 from emilybache/master
python version of exercises
- Loading branch information
Showing
9 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ _ReSharper*/ | |
*.suo | ||
*.user | ||
.DS_Store | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import random | ||
|
||
class TelemetryClient(object): | ||
DIAGNOSTIC_MESSAGE = "AT#UD" | ||
|
||
def __init__(self): | ||
self.online_status = False | ||
self.diagnostic_message_result = "" | ||
|
||
def get_online_status(self): | ||
return self.online_status | ||
|
||
def connect(self, telemetry_server_connection_string): | ||
if (telemetry_server_connection_string is None or telemetry_server_connection_string == ""): | ||
raise Exception() | ||
|
||
# simulate the operation on a real modem | ||
success = random.randint(0, 10) <= 8 | ||
|
||
self.online_status = success | ||
|
||
def disconnect(self): | ||
self.online_status = False | ||
|
||
def send(self, message): | ||
if (message is None or message == ""): | ||
raise Exception() | ||
|
||
if (message == TelemetryClient.DIAGNOSTIC_MESSAGE): | ||
# simulate a status report | ||
self.diagnostic_message_result = """\ | ||
LAST TX rate................ 100 MBPS\r\n | ||
HIGHEST TX rate............. 100 MBPS\r\n | ||
LAST RX rate................ 100 MBPS\r\n | ||
HIGHEST RX rate............. 100 MBPS\r\n | ||
BIT RATE.................... 100000000\r\n | ||
WORD LEN.................... 16\r\n | ||
WORD/FRAME.................. 511\r\n | ||
BITS/FRAME.................. 8192\r\n | ||
MODULATION TYPE............. PCM/FM\r\n | ||
TX Digital Los.............. 0.75\r\n | ||
RX Digital Los.............. 0.10\r\n | ||
BEP Test.................... -5\r\n | ||
Local Rtrn Count............ 00\r\n | ||
Remote Rtrn Count........... 00""" | ||
|
||
return | ||
# here should go the real Send operation | ||
|
||
def receive(self): | ||
if (self.diagnostic_message_result is None or self.diagnostic_message_result == ""): | ||
# simulate a received message | ||
message = "" | ||
messageLength = random.randint(0, 50) + 60 | ||
i = messageLength | ||
while(i >= 0): | ||
message += chr((random.randint(0, 40) + 86)) | ||
i -= 1 | ||
else: | ||
message = self.diagnostic_message_result | ||
self.diagnostic_message_result = "" | ||
|
||
return message | ||
|
||
class TelemetryDiagnosticControls: | ||
DiagnosticChannelConnectionString = "*111#" | ||
|
||
def __init__(self): | ||
self.telemetry_client = TelemetryClient() | ||
self.diagnostic_info = "" | ||
|
||
def check_transmission(self): | ||
self.diagnostic_info = "" | ||
|
||
self.telemetry_client.disconnect() | ||
|
||
retryLeft = 3 | ||
while (self.telemetry_client.get_online_status() == False and retryLeft > 0): | ||
self.telemetry_client.connect(TelemetryDiagnosticControls.DiagnosticChannelConnectionString) | ||
retryLeft -= 1 | ||
|
||
if telemetry_client.get_online_status() == False: | ||
raise Exception("Unable to connect.") | ||
|
||
self.telemetry_client.send(TelemetryClient.DIAGNOSTIC_MESSAGE) | ||
self.diagnostic_info = self.telemetry_client.receive() | ||
|
||
|
Binary file not shown.
10 changes: 10 additions & 0 deletions
10
TDDMicroExercises/python/TelemetrySystem/test_telemetry.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import unittest | ||
|
||
from telemetry import * | ||
|
||
class TelemetryDiagnosticControlsTest(unittest.TestCase): | ||
def test_check_transmission_should_send_a_diagnostic_message_and_receive_a_status_message_response(self): | ||
pass | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
15 changes: 15 additions & 0 deletions
15
TDDMicroExercises/python/TextConverter/unicode_to_html_converter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import cgi | ||
|
||
class UnicodeFileToHtmlTextConverter(object): | ||
|
||
def __init__(self, full_filename_with_path): | ||
self.full_filename_with_path = full_filename_with_path | ||
|
||
def convertToHtml(self): | ||
f = open(self.full_filename_with_path, "r") | ||
html = "" | ||
for line in f: | ||
html += cgi.escape(line) | ||
html += "<br />" | ||
|
||
return html |
Binary file added
BIN
+800 Bytes
TDDMicroExercises/python/TextConverter/unicode_to_html_converter.pyc
Binary file not shown.
49 changes: 49 additions & 0 deletions
49
TDDMicroExercises/python/TirePressureMonitoringSystem/test_tire_pressure_monitoring.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
class StubAlarm(object): | ||
|
||
def __init__(self, alarm_onReturnValue): | ||
self.alarm_onReturnValue = alarm_onReturnValue | ||
|
||
def set_alarm_on(self, returnValue): | ||
self.alarm_onReturnValue = returnValue | ||
|
||
def check(self): | ||
pass | ||
|
||
def is_alarm_on(self): | ||
return self.alarm_onReturnValue | ||
|
||
|
||
class MockAlarm(object): | ||
|
||
def __init__(self): | ||
self.alarm_on = False | ||
|
||
self.expectedCheckCallsCount = 0 | ||
self.actualCheckCallsCount = 0 | ||
|
||
self.expectedAlarmOnCallsCount = 0 | ||
self.actualAlarmOnCallsCount = 0 | ||
|
||
def resetExpectedCallsCount(self): | ||
self.expectedCheckCallsCount = 1 | ||
|
||
def resetAlarmToOn(self): | ||
self.expectedAlarmOnCallsCount = 1 | ||
self.alarm_on = True | ||
|
||
def verify(self): | ||
if self.expectedAlarmOnCallsCount != self.actualAlarmOnCallsCount: | ||
#TODO - find a more appropriate exception | ||
raise RuntimeError("AlarmOn calls count: %s expected: %s " % (self.actualAlarmOnCallsCount, self.expectedAlarmOnCallsCount)) | ||
if self.expectedCheckCallsCount != self.actualCheckCallsCount: | ||
raise RuntimeError("Check calls count: %s expected: %s" % (self.actualCheckCallsCount, self.expectedCheckCallsCount)) | ||
|
||
def check(self): | ||
self.actualCheckCallsCount += 1 | ||
|
||
def is_alarm_on(self): | ||
self.actualAlarmOnCallsCount += 1 | ||
return self.alarm_on | ||
|
||
|
||
|
34 changes: 34 additions & 0 deletions
34
TDDMicroExercises/python/TirePressureMonitoringSystem/tire_pressure_monitoring.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
import random | ||
|
||
class Sensor(object): | ||
OFFSET = 16 | ||
|
||
def pop_next_pressure_psi_value(self): | ||
pressure_telemetry_value = self.sample_pressure() | ||
return Sensor.OFFSET + pressure_telemetry_value | ||
|
||
@staticmethod | ||
def sample_pressure(): | ||
# placeholder implementation that simulate a real sensor in a real tire | ||
pressure_telemetry_value = 6 * random.random() * random.random() | ||
return pressure_telemetry_value | ||
|
||
|
||
class Alarm(object): | ||
|
||
def __init__(self): | ||
self.low_pressure_threshold = 17 | ||
self.high_pressure_threshold = 21 | ||
self.sensor = Sensor() | ||
self.alarm_on = False | ||
self.alarm_count = 0 | ||
|
||
def check(self): | ||
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: | ||
self.alarm_on = True | ||
self.alarm_count += 1 | ||
|
||
def is_alarm_on(self): | ||
return self.alarm_on |
18 changes: 18 additions & 0 deletions
18
TDDMicroExercises/python/TurnTicketDispenser/turn_ticket.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class TurnTicket(object): | ||
def __init__(self, turnNumber): | ||
self.turnNumber = turnNumber | ||
|
||
class TurnNumberSequence(object): | ||
_turnNumber = -1 | ||
|
||
@staticmethod | ||
def next_turn_number(): | ||
TurnNumberSequence._turnNumber += 1 | ||
return TurnNumberSequence._turnNumber | ||
|
||
|
||
class TicketDispenser(object): | ||
def getTurnTicket(self): | ||
newTurnNumber = TurnNumberSequence.next_turn_number() | ||
newTurnTicket = TurnTicket(newTurnNumber) | ||
return newTurnTicket |