Skip to content

Commit 5068b32

Browse files
committed
Add more examples
1 parent 641a06d commit 5068b32

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
>>> import calculations
2+
3+
>>> calculations.add(2, 2)
4+
4.0
5+
6+
>>> calculations.subtract(2, 2)
7+
0.0
8+
9+
>>> calculations.multiply(2, 2)
10+
4.0
11+
12+
>>> calculations.divide(2, 2)
13+
1.0
14+
15+
>>> def test_add():
16+
... import calculations
17+
... return calculations.add(2, 4)
18+
>>> test_add()
19+
6.0

python-doctest/context.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# context.py
2+
3+
total = 100
4+
5+
6+
def decrement_by(number):
7+
"""Decrement the global total variable by a given number.
8+
9+
>>> local_total = decrement_by(50)
10+
>>> local_total
11+
50
12+
13+
Changes to total don't affect the code's global scope
14+
>>> total
15+
100
16+
"""
17+
global total
18+
total -= number
19+
return total
20+
21+
22+
def increment_by(number):
23+
"""Increment the global total variable by a given number.
24+
25+
The initial value of total's shallow copy is 50
26+
>>> increment_by(10)
27+
60
28+
29+
The local_total variable is not defined in this test
30+
>>> local_total
31+
Traceback (most recent call last):
32+
NameError: name 'local_total' is not defined
33+
"""
34+
global total
35+
total += number
36+
return total

python-doctest/failing_tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Sample failing tests.
2+
3+
The output must be an integer
4+
>>> 5 + 7
5+
12.0
6+
7+
The output must not contain quotes
8+
>>> print("Hello, World!")
9+
'Hello, World!'
10+
11+
The output must not use double quotes
12+
>>> "Hello," + "World!"
13+
"Hello, World!"
14+
15+
The output must not contain leading or trailing spaces
16+
>>> print("Hello, World!")
17+
Hello, World!
18+
19+
The traceback doesn't include the correct exception message
20+
>>> raise ValueError("incorrect value")
21+
Traceback (most recent call last):
22+
ValueError: invalid value
23+
"""

python-doctest/parametrization.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# even_numbers.py
2+
3+
def get_even_numbers(numbers):
4+
"""Return the even numbers in a list.
5+
6+
>>> args = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
7+
>>> expected = [[2, 4], [6, 8], [10, 12]]
8+
9+
>>> for arg, expected in zip(args, expected):
10+
... get_even_numbers(arg) == expected
11+
True
12+
True
13+
True
14+
"""
15+
return [number for number in numbers if number % 2 == 0]

python-doctest/user.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# class User:
2+
# def __init__(self, name, favorite_colors):
3+
# self.name = name
4+
# self._favorite_colors = set(favorite_colors)
5+
6+
# @property
7+
# def favorite_colors(self):
8+
# """Return the user's favorite colors.
9+
10+
# Usage examples:
11+
# >>> john = User("John", {"#797EF6", "#4ADEDE", "#1AA7EC"})
12+
# >>> john.favorite_colors
13+
# {'#1AA7EC', '#4ADEDE', '#797EF6'}
14+
# """
15+
# return self._favorite_colors
16+
17+
18+
# class User:
19+
# def __init__(self, name, favorite_colors):
20+
# self.name = name
21+
# self._favorite_colors = set(favorite_colors)
22+
23+
# @property
24+
# def favorite_colors(self):
25+
# """Return the user's favorite colors.
26+
27+
# Usage examples:
28+
# >>> john = User("John", {"#797EF6", "#4ADEDE", "#1AA7EC"})
29+
# >>> sorted(john.favorite_colors)
30+
# ['#1AA7EC', '#4ADEDE', '#797EF6']
31+
# """
32+
# return self._favorite_colors
33+
34+
35+
class User:
36+
def __init__(self, name, favorite_colors):
37+
"""Initialize instances of User.
38+
39+
Usage examples:
40+
>>> User(
41+
... "John", {"#797EF6", "#4ADEDE", "#1AA7EC"}
42+
... ) # doctest: +ELLIPSIS
43+
<sets.User object at 0x...>
44+
"""
45+
self.name = name
46+
self._favorite_colors = set(favorite_colors)

0 commit comments

Comments
 (0)