Skip to content

Commit 3c555c8

Browse files
committed
add chapter3
1 parent 0076163 commit 3c555c8

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

Diff for: chapter3/bare_asserts.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
def assert_flow_control(value):
3+
"""
4+
An example on flow control with bare asserts
5+
"""
6+
assert value
7+
return "got a value of expected type!"

Diff for: chapter3/simplest.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from unittest import TestCase
2+
3+
4+
def test_simplest():
5+
assert True
6+
7+
class TestSimple(TestCase):
8+
9+
def test_simple(self):
10+
self.assertTrue(True)
11+
12+
13+

Diff for: chapter3/test_asserts.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
def test_long_strings():
3+
string = ("This is a very, very, long string that has some differences"
4+
"that are hard to catch")
5+
expected = ("This is a very, very long string that hes some differences"
6+
"that are hard to catch")
7+
assert string == expected
8+
9+
10+
def test_nested_dictionaries():
11+
result = {'first': 12, 'second': 13,
12+
'others': {'success': True, 'msg': 'A success message!'}}
13+
expected = {'first': 12, 'second': 13,
14+
'others': {'success': True, 'msg': 'A sucess message!'}}
15+
assert result == expected
16+
17+
def test_missing_key():
18+
result = {'first': 12, 'second': 13,
19+
'others': {'success': True, 'msg': 'A success message!'}}
20+
expected = {'first': 12, 'second': 13,
21+
'others': {'msg': 'A success message!'}}
22+
assert result == expected
23+

Diff for: chapter3/test_is_empty.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def is_empty(value):
2+
return len(value) == 0
3+
4+
5+
def test_empty_list():
6+
assert is_empty([]) is True
7+
8+
def test_empty_dict():
9+
assert is_empty({}) is True
10+
11+
def test_list_is_not_empty():
12+
assert is_empty([1,2,3]) is False
13+
14+
def test_dict_is_not_empty():
15+
assert is_empty({"item": 1}) is False
16+
17+
def test_it_breaks():
18+
assert is_empty(1) is False

Diff for: chapter3/test_is_empty_robust.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def is_empty(value):
2+
try:
3+
return len(value) == 0
4+
except TypeError:
5+
return False
6+
7+
def test_empty_list():
8+
assert is_empty([]) is True
9+
10+
def test_empty_dict():
11+
assert is_empty({}) is True
12+
13+
def test_list_is_not_empty():
14+
assert is_empty([1,2,3]) is False
15+
16+
def test_dict_is_not_empty():
17+
assert is_empty({"item": 1}) is False
18+
19+
def test_integer_is_false():
20+
assert is_empty(1) is False

0 commit comments

Comments
 (0)