-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluglead.py
83 lines (59 loc) · 2.21 KB
/
pluglead.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from encoder import TranslationTableEncoder
from validation import Validator, TypeValidator, AlphaValidator, UniqueValuesValidator
class PlugLead(TranslationTableEncoder):
"""
Plug Lead class
This class simulates a lead that connect two plugs in the Enigma machine
:param chars: A string that represents the two different characters the lead should connect
:type chars: str
:raises: :class:`TypeError, ValueError`: If the provided chars string is invalid
Example::
lead = PlugLead("AG")
lead.encode("A")
"""
def __init__(self, chars):
self.chars = chars
super().__init__(self.chars, self.chars[::-1])
@property
def chars(self):
"""
The two characters the plug lead connects
:getter: Returns the plug lead's characters
:setter: Sets the plug lead's characters
:type: str
"""
return self.__chars
@chars.setter
def chars(self, chars):
Validator(TypeValidator(str), AlphaValidator(), UniqueValuesValidator(2)).validate(chars)
self.__chars = chars.upper()
def __eq__(self, other):
if isinstance(other, type(self)):
return len(set(self._trans_table.keys()).intersection(set(other._trans_table.keys()))) > 0
return False
if __name__ == "__main__":
# tests from workbook
lead = PlugLead("AG")
assert (lead.encode("A") == "G")
assert (lead.encode("D") == "D")
lead = PlugLead("DA")
assert (lead.encode("A") == "D")
assert (lead.encode("D") == "A")
# custom tests
good_inputs = [("AB", "A"), ("AB", "B"), ("AB", "C")]
expected_outputs = ["B", "A", "C"]
bad_inputs = ["", "A", "AA"]
expected_error = [ValueError] * len(bad_inputs)
# meta-tests
assert (len(good_inputs) == len(expected_outputs))
assert (len(bad_inputs) == len(expected_error))
# good tests (normal and boundary)
for i in range(len(good_inputs)):
assert (PlugLead(good_inputs[i][0]).encode(good_inputs[i][1]) == expected_outputs[i])
# bad tests (errors)
for i in range(len(bad_inputs)):
try:
PlugLead(bad_inputs[i])
assert False
except expected_error[i]:
pass