Skip to content

Commit 0ff5a30

Browse files
committed
Add doctest example
1 parent 8818f0a commit 0ff5a30

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

source-code/testing/DocTest/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# DocTest
2+
3+
Illustration of how to use doctest to test classes.
4+
5+
## What is it?
6+
7+
1. `point.py`: Python module containing a class definition.
8+
9+
## How to use?
10+
11+
Test with:
12+
```bash
13+
$ python -m doctest point.py
14+
```

source-code/testing/DocTest/point.py

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
'''Module for working with points in 2D
2+
'''
3+
4+
import math
5+
6+
7+
class Point:
8+
'''Class that represents points in a 2D space
9+
'''
10+
11+
def __init__(self, x_value, y_value):
12+
'''Create a new point at the given position
13+
14+
Parameters
15+
----------
16+
x: float
17+
x-coordinate of the point
18+
y: float
19+
y-coordinate of the point
20+
21+
Examples
22+
--------
23+
>>> Point(3.1, 5.6)
24+
(3.1, 5.6)
25+
'''
26+
self._x = float(x_value)
27+
self._y = float(y_value)
28+
29+
@property
30+
def x(self):
31+
'''Get the x-coordinate of the point
32+
33+
Returns
34+
-------
35+
float
36+
x-coordinate of the point
37+
38+
Examples
39+
--------
40+
>>> p = Point(3.1, 5.6)
41+
>>> p.x
42+
3.1
43+
'''
44+
return self._x
45+
46+
@x.setter
47+
def x(self, value):
48+
'''Set the x-coordinate of the point
49+
50+
Parameters
51+
----------
52+
x: float
53+
x-coordinate to set
54+
55+
Examples
56+
--------
57+
>>> p = Point(3.1, 5.6)
58+
>>> p.x = 1.0
59+
>>> p.x
60+
1.0
61+
'''
62+
self._x = float(value)
63+
64+
@property
65+
def y(self):
66+
'''Get the y-coordinate of the point
67+
68+
Returns
69+
-------
70+
float
71+
y-coordinate of the point
72+
73+
Examples
74+
--------
75+
>>> p = Point(3.1, 5.6)
76+
>>> p.y
77+
5.6
78+
'''
79+
return self._y
80+
81+
@y.setter
82+
def y(self, value):
83+
'''Set the y-coordinate of the point
84+
85+
Parameters
86+
----------
87+
y: float
88+
y-coordinate to set
89+
90+
Examples
91+
--------
92+
>>> p = Point(3.1, 5.6)
93+
>>> p.y = 1.0
94+
>>> p.y
95+
1.0
96+
'''
97+
self._y = float(value)
98+
99+
@property
100+
def coords(self):
101+
'''Get the coordinates of the point as a tuple
102+
103+
Returns
104+
-------
105+
tuple[float, float]
106+
coordinate of the point
107+
108+
Examples
109+
--------
110+
>>> p = Point(3.1, 5.6)
111+
>>> p.coords
112+
(3.1, 5.6)
113+
'''
114+
return (self.x, self.y)
115+
116+
@coords.setter
117+
def coords(self, value):
118+
'''Modify to coordinates of the point
119+
120+
Parameters
121+
----------
122+
value: tuple[float, float]
123+
tuple with the new coordinates for the point
124+
125+
Examples
126+
--------
127+
>>> p = Point(3.1, 5.6)
128+
>>> p.coords = (1.0, -1.0)
129+
>>> p.coords
130+
(1.01, -1.0)
131+
'''
132+
self._x, self._y = value
133+
134+
def distance(self, other):
135+
'''Compute the distance between the point and another point
136+
137+
Parameters
138+
----------
139+
p: Point
140+
point to compute the Euclidean distance to
141+
142+
Returns
143+
-------
144+
float
145+
distance between the two points
146+
147+
Examples
148+
--------
149+
>>> p, q = Point(3.0, 0.0), Point(0.0, 4.0)
150+
>>> import math
151+
>>> math.isclose(5.0, p.distance(q))
152+
True
153+
'''
154+
return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
155+
156+
def __repr__(self):
157+
'''Generate a non-ambiguous string representation of the
158+
point
159+
160+
Returns
161+
-------
162+
str
163+
string representation of the point
164+
165+
Examples
166+
--------
167+
>>> p = Point(3.1, 5.6)
168+
>>> repr(p)
169+
(3.1, 5.6)
170+
'''
171+
return f'({self.x}, {self.y})'
172+
173+
174+
if __name__ == '__main__':
175+
import doctest
176+
doctest.testmod()

source-code/testing/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ versatile, but more modern packages are available.
1515
testing per se. It is mainly intended for contract-based programming
1616
and one can prevent code to be emitted for asserts by using the
1717
`-O` optimizatino flag when running the code.
18+
1. `DocTest`: illustration of using doctest (part of the Python standard
19+
library.

0 commit comments

Comments
 (0)