-
Notifications
You must be signed in to change notification settings - Fork 21
/
tests.py
192 lines (148 loc) · 5.59 KB
/
tests.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import unittest
class TestAssignmentTwo_None(unittest.TestCase):
def setUp(self):
pass
def test_none_is_universal(self):
"""
Think about what 'is' does here.
"""
truth = ____
self.assertEqual(truth, None is None)
def test_none_is_unique(self):
"""
This is a great place to launch an interpreter and check
what the tight hand side of the equalities evaluate to.
"""
truth = ____
self.assertEqual(truth, None is not 0)
truth = ____ # This resets the value of truth
self.assertEqual(truth, None is not False)
def test_none_existence(self):
x = 0
checker = None
if checker:
x = 1
self.assertEqual(1,x)
def test_truthiness(self):
"""
Compare this test to the above.
"""
x = [] # An empty list
y = 0
if not x:
y = 1
self.assertEqual(0, y)
self.assertTrue(x == None) # Not PEP8 compliant, but commonly used.
"""
The take away here is that truthiness is not the same as None
None evaluates to False in these if statements, as does an
empty list, but None != (is not equal to) False.
"""
def tearDown(self):
pass
class TestAssignmentTwo_Strings(unittest.TestCase):
def setUp(self):
pass
def test_quotations(self):
"""
Replace the blanks with a boolean and think about
why you might want to utilize different quotation
mechanisms.
"""
string = 'Hello World'
self.assertEqual(____, isinstance(string, str))
string = "Hello World"
self.assertEqual(____, isinstance(string, str))
string = '''Hello World'''
self.assertEqual(____, isinstance(string, str))
string = """Hello World"""
self.assertEqual(____, isinstance(string, str))
def test_escaping_quotes(self):
"""
Another replacement. This tests mirrors common
data munging techniques. For example if needing
to modify a string column of a shapefile and maintain
some quotation scheme.
"""
a = "He said, \"Don't forget to relax a little\""
b = 'He said, "Don\'t" forget to relax a little'
self.assertEqual(____, a == b)
def test_string_concatenation(self):
"""
Concatenation is either explicit or implicit.
See the Zen of Python for which is preferable
"""
string = "Go" + " Sun Devils"
self.assertEqual(____, string)
string = "Go" " Sun Devils"
self.assertEqual(____, string)
def test_in(self):
"""
The in operator can be helpful in finding
substrings.
Once the complexity gets high, I would suggest switching
to the 're' module (for Regex).
For these, fix the assertion methods
"""
cities = '"New York", "Boston", "Chicago", "Dallas", "St. Louis", "Phoenix" '
self.assert____('Dallas' in cities)
self.assert____('"Dallas"' in cities)
self.assert____('ton' in cities)
self.assert____("\", \"" in cities)
def test_format(self):
"""
This is presented in a style that I frequently use
to debug something simple (when I do not want to
use a debugger)
"""
s = 'The current index is {}'
self.assertEqual(____,isinstance(s, str))
i = 0
truth = 'The current index is _____' # Replace the ____
self.assertEqual(truth, s.format(i))
def test_string_cases(self):
"""
Another common string manipulation set borrowed from
Python Koans.
"""
self.assertEqual(__, 'guido'.capitalize())
self.assertEqual(__, 'guido'.upper())
self.assertEqual(__, 'TimBot'.lower())
self.assertEqual(__, 'guido van rossum'.title())
self.assertEqual(__, 'ToTaLlY aWeSoMe'.swapcase())
def test_format_rounding(self):
"""
This test mirrors real world code that I use frequently.
Assume that you have a bunch of data in a cvs file. You
have read in the data and now want to print some of it
to screen or pipe it into another file. The only thing is,
the new output can be much less precise.
"""
pi = 3.14159265 # Please use math.pi if you ever really need to use pi
s = '{}'
# Check to see what the heck 's' is
self.assertEqual(____, isinstance(s, str))
rounded_pi = _________
self.assertEqual(rounded_pi, s.format(round(pi, 4)))
def test_translation(self):
"""
Take a look at the string module to see how this is working.
The example is from PyMoTW (see the syllabus for a link).
In Python 3 (the version we are using) `maketrans` has moved
from the string module to a method on the `str` type. This
is why we do not need to `import string`
Fill in the truth value
"""
leet = str.maketrans('abegiloprstz', '463611092572')
s = 'The quick brown fox jumped over the lazy dog.'
s.translate(leet) # Translate the string here
truth = ____ # Truth is the newly translated string
self.assertEqual(truth, s)
def test_write_your_own(self):
"""
Write your own test here demonstrating either string
or None usage that has not been demonstrated above.
"""
self.assertTrue(False) # You can either fix this line or remove it once the test is in.
def tearDown(self):
pass