Skip to content

Commit a217e9f

Browse files
Merge pull request #16 from ResearchCodingClub/fix_indents
Fix indentation errors
2 parents 84345ba + dfbd010 commit a217e9f

File tree

6 files changed

+283
-287
lines changed

6 files changed

+283
-287
lines changed

episodes/01-why-test-my-code.Rmd

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ for the `add` function and call it `test_add`:
7171

7272
```python
7373
def test_add():
74-
# Check that it adds two positive integers
75-
if add(1, 2) != 3:
76-
print("Test failed!")
77-
# Check that it adds zero
78-
if add(5, 0) != 5:
79-
print("Test failed!")
80-
# Check that it adds two negative integers
81-
if add(-1, -2) != -3:
82-
print("Test failed!")
74+
# Check that it adds two positive integers
75+
if add(1, 2) != 3:
76+
print("Test failed!")
77+
# Check that it adds zero
78+
if add(5, 0) != 5:
79+
print("Test failed!")
80+
# Check that it adds two negative integers
81+
if add(-1, -2) != -3:
82+
print("Test failed!")
8383
```
8484

8585
Here we check that the function works for a set of test cases. We ensure that
@@ -98,7 +98,7 @@ functions:
9898

9999
```python
100100
def greet_user(name):
101-
return "Hello" + name + "!"
101+
return "Hello" + name + "!"
102102
```
103103

104104
```python
@@ -116,8 +116,8 @@ working as expected:
116116

117117
```python
118118
def test_greet_user():
119-
if greet_user("Alice") != "Hello Alice!":
120-
print("Test failed!")
119+
if greet_user("Alice") != "Hello Alice!":
120+
print("Test failed!")
121121
```
122122

123123
The second function will crash if `x2 - x1` is zero.
@@ -127,21 +127,21 @@ unexpected behaviour:
127127

128128
```python
129129
def test_gradient():
130-
if gradient(1, 1, 2, 2) != 1:
131-
print("Test failed!")
132-
if gradient(1, 1, 2, 3) != 2:
133-
print("Test failed!")
134-
if gradient(1, 1, 1, 2) != "Undefined":
135-
print("Test failed!")
130+
if gradient(1, 1, 2, 2) != 1:
131+
print("Test failed!")
132+
if gradient(1, 1, 2, 3) != 2:
133+
print("Test failed!")
134+
if gradient(1, 1, 1, 2) != "Undefined":
135+
print("Test failed!")
136136
```
137137

138138
And we could have amended the function:
139139

140140
```python
141141
def gradient(x1, y1, x2, y2):
142-
if x2 - x1 == 0:
143-
return "Undefined"
144-
return (y2 - y1) / (x2 - x1)
142+
if x2 - x1 == 0:
143+
return "Undefined"
144+
return (y2 - y1) / (x2 - x1)
145145
```
146146

147147
:::::::::::::::::::::::::::::::::
@@ -156,13 +156,13 @@ consider the following function:
156156
```python
157157

158158
def multiply(a, b):
159-
return a * a
159+
return a * a
160160

161161
def divide(a, b):
162-
return a / b
162+
return a / b
163163

164164
def triangle_area(base, height):
165-
return divide(multiply(base, height), 2)
165+
return divide(multiply(base, height), 2)
166166
```
167167

168168
There is a bug in this code too, but since we have several functions calling
@@ -205,7 +205,7 @@ Consider a function that controls a driverless car.
205205
```python
206206
def drive_car(speed, direction):
207207

208-
... # complex car driving code
208+
... # complex car driving code
209209

210210
return speed, direction, brake_status
211211
```

episodes/02-simple-tests.Rmd

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ project_directory/
4949

5050
```python
5151
def add(a, b):
52-
return a + b
52+
return a + b
5353
```
5454

5555
- And in `test_calculator.py`, write the test for the add function that we
@@ -61,14 +61,14 @@ def add(a, b):
6161
from calculator import add
6262

6363
def test_add():
64-
# Check that it adds two positive integers
65-
assert add(1, 2) == 3
64+
# Check that it adds two positive integers
65+
assert add(1, 2) == 3
6666

67-
# Check that it adds zero
68-
assert add(5, 0) == 5
67+
# Check that it adds zero
68+
assert add(5, 0) == 5
6969

70-
# Check that it adds two negative integers
71-
assert add(-1, -2) == -3
70+
# Check that it adds two negative integers
71+
assert add(-1, -2) == -3
7272
```
7373

7474
The `assert` statement will crash the test by raising an `AssertionError` if
@@ -88,7 +88,7 @@ Now, let's run the test. We can do this by running the following command in the
8888
(make sure you're in the `my_project` directory before running this command)
8989

9090
```bash
91-
❯ pytest ./
91+
❯ pytest
9292
```
9393

9494
This command tells Pytest to run all the tests in the current directory.
@@ -118,7 +118,7 @@ multiplies two numbers together.
118118

119119
```python
120120
def multiply(a, b):
121-
return a * b
121+
return a * b
122122
```
123123

124124
- Then write a test for this function in `test_calculator.py`. Remember to
@@ -137,14 +137,14 @@ could look like this:
137137

138138
```python
139139
def test_multiply():
140-
# Check that positive numbers work
141-
assert multiply(5, 5) == 25
142-
# Check that multiplying by 1 works
143-
assert multiply(1, 5) == 5
144-
# Check that multiplying by 0 works
145-
assert multiply(0, 3) == 0
146-
# Check that negative numbers work
147-
assert multiply(-5, 2) == -10
140+
# Check that positive numbers work
141+
assert multiply(5, 5) == 25
142+
# Check that multiplying by 1 works
143+
assert multiply(1, 5) == 5
144+
# Check that multiplying by 0 works
145+
assert multiply(0, 3) == 0
146+
# Check that negative numbers work
147+
assert multiply(-5, 2) == -10
148148
```
149149

150150
:::::::::::::::::::::::::::::::::

episodes/04-unit-tests-best-practices.Rmd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,11 @@ def randomly_sample_and_filter_participants(
273273
min_height: int,
274274
max_height: int
275275
):
276-
"""Participants is a list of tuples, containing the age and height of each participant
276+
"""Participants is a list of dicts, containing the age and height of each participant
277277
participants = [
278-
{age: 25, height: 180},
279-
{age: 30, height: 170},
280-
{age: 35, height: 160},
278+
{age: 25, height: 180},
279+
{age: 30, height: 170},
280+
{age: 35, height: 160},
281281
]
282282
"""
283283

episodes/05-testing-exceptions.Rmd

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ Take this example of the `square_root` function. We don't have time to implement
2525
```python
2626

2727
def square_root(x):
28-
if x < 0:
29-
raise ValueError("Cannot compute square root of negative number yet!")
30-
return x ** 0.5
31-
28+
if x < 0:
29+
raise ValueError("Cannot compute square root of negative number yet!")
30+
return x ** 0.5
3231
```
3332

3433
We can test that the function raises an exception using `pytest.raises` as follows:
@@ -52,7 +51,6 @@ def test_square_root():
5251
with pytest.raises(ValueError) as e:
5352
square_root(-1)
5453
assert str(e.value) == "Cannot compute square root of negative number yet!"
55-
5654
```
5755

5856
::::::::::::::::::::::::::::::::::::: challenge

0 commit comments

Comments
 (0)