This repository has been archived by the owner on Jul 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_with_unittest.py
68 lines (57 loc) · 2.08 KB
/
test_with_unittest.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
from example_module import (file_iterator, variadic_addition,
variadic_multiplication)
import random
import unittest
class TestExample(unittest.TestCase):
def test_file_iterator_strips_newlines(self):
"""
Assert the file iterator strips \n from
yielded lines.
"""
for line in file_iterator('example_module.py'):
self.assertFalse(line.endswith('\n'))
def test_file_iterator_removes_leading_whitespace(self):
"""
Assert the file iterator strips whitespace
from the start of yielded lines.
"""
for line in file_iterator('example_module.py'):
self.assertFalse(line.startswith(' '))
def test_file_iterator_removes_all_whitespace(self):
"""
Assert the file iterator strips whitespace
from both ends of yielded lines.
"""
for line in file_iterator('example_module.py'):
self.assertEqual(line, line.strip())
def assertIdentity(self, func):
"""
Assert that ``func(x) == x``.
"""
x = random.randint(0, 100)
self.assertEqual(func(x), x)
def test_variadic_addition_identity(self):
"""
Assert that variadic_addition will
calculate the sum of one argument.
"""
self.assertIdentity(variadic_addition)
def test_variadic_addition_takes_arbitrary_arguments(self):
"""
Assert an arbitrary number of arguments
is acceptable.
"""
self.assertEqual(
variadic_addition(1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15), 90
)
def test_variadic_multiplication_identity(self):
"""
Assert variadic_multiplication will accept
and multiply one argument.
"""
self.assertIdentity(variadic_multiplication)
def test_variadic_multiplication_takes_arbitrary_arguments(self):
"""Assert an arbitrary number of arguments is acceptable."""
self.assertEqual(variadic_multiplication(1, 2, 3, 4, 5), 120)
if __name__ == '__main__':
unittest.main()