Skip to content

Commit cd33d60

Browse files
committed
Removed placeholder exercise and added first real one
1 parent 89f181a commit cd33d60

12 files changed

Lines changed: 183 additions & 69 deletions

File tree

‎conftest.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,84 @@ def _expect(stdout, line_number, expected):
9494
)
9595

9696
return _expect
97+
98+
99+
@pytest.fixture
100+
def expect_variables():
101+
"""Assert that several variables in a script's namespace all match
102+
expected values at once, with a single friendly summary of everything
103+
that's missing or wrong — instead of stopping at the first problem
104+
like expect_variable does.
105+
106+
Usage:
107+
def test_conversions_are_correct(run_script, expect_variables):
108+
_, namespace = run_script("fahrenheit.py")
109+
expect_variables(namespace, {"fahrenheit": 68.0, "kelvin": 293.15})
110+
"""
111+
112+
def _expect(namespace, expected):
113+
missing = [name for name in expected if name not in namespace]
114+
wrong = {
115+
name: (namespace[name], expected_value)
116+
for name, expected_value in expected.items()
117+
if name in namespace and namespace[name] != expected_value
118+
}
119+
120+
if not missing and not wrong:
121+
return
122+
123+
lines = []
124+
if missing:
125+
names = ", ".join(f"'{name}'" for name in missing)
126+
lines.append(f"Missing variable(s): {names}. Have you created them yet?")
127+
for name, (actual, expected_value) in wrong.items():
128+
lines.append(
129+
f"'{name}' should be {expected_value!r}, but it was {actual!r}."
130+
)
131+
132+
pytest.fail("\n".join(lines), pytrace=False)
133+
134+
return _expect
135+
136+
137+
@pytest.fixture
138+
def expect_variable_type():
139+
"""Assert that a variable exists and is of a given type, without caring
140+
about its exact value — e.g. "is this a string" rather than "is this
141+
exactly 'hello'".
142+
143+
Usage:
144+
def test_name_is_a_string(run_script, expect_variable_type):
145+
_, namespace = run_script("about_you.py")
146+
expect_variable_type(namespace, "name", str)
147+
"""
148+
149+
friendly_names = {
150+
str: "text (str)",
151+
int: "a whole number (int)",
152+
float: "a decimal number (float)",
153+
bool: "True or False (bool)",
154+
list: "a list",
155+
dict: "a dictionary",
156+
tuple: "a tuple",
157+
}
158+
159+
def _friendly(t):
160+
return friendly_names.get(t, t.__name__)
161+
162+
def _expect(namespace, name, expected_type):
163+
if name not in namespace:
164+
pytest.fail(
165+
f"Expected a variable named '{name}', but it doesn't exist.\n"
166+
f"Have you created a variable called '{name}' yet?",
167+
pytrace=False,
168+
)
169+
actual = namespace[name]
170+
if not isinstance(actual, expected_type):
171+
pytest.fail(
172+
f"Expected '{name}' to be {_friendly(expected_type)}, "
173+
f"but it was {_friendly(type(actual))} ({actual!r}).",
174+
pytrace=False,
175+
)
176+
177+
return _expect

‎foundations/02_add_numbers/README.md‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎foundations/02_add_numbers/add_numbers.py‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎foundations/02_add_numbers/solution/add_numbers_solution.py‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎foundations/02_add_numbers/solution/test_add_numbers_solution.py‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎foundations/02_add_numbers/test_add_numbers.py‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎foundations/02_datatypes/README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Exercise 02 - Datatypes
2+
3+
## Description
4+
Welcome to your first real excercise. In the last one, we checked if everything is working correctly, now its time to put your knowlege to test. This and feature excercises contain multiple tests which are skipped by default (only first one is enabled). Your job is to finish one thing at a time and run the test. To run the next test, go to `test_<lesson_name>.py` and remove the `@pytest.mark.skip` line from the next test. Your goal is to make all tests pass.
5+
6+
In this excercise you will define datatypes we talked about in lesson: **String**, **Integer**, **Float**, **Boolean**, **None**.
7+
8+
You need to define the following variables:
9+
* `mystring` - set it to some string
10+
* `myinteger` - set it to some integer
11+
* `myfloat` - set it to some float
12+
* `myboolean` - set it to some boolean
13+
* `mynonevalue` - set it to some None
14+
15+
You need to make sure the variables are named **exactly** like written here, otherwise the tests will fail, values can be anything you want.
16+
17+
## Notes
18+
To finish this excercise, visit our first lesson in TPL - [Datatypes](https://thepythonledger.github.io/Docusaurus-engine/lessons/python-foundations/core-datatypes)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 1. Define variable with a name "mystring" and assign it to some string
2+
3+
4+
# 2. Define variable with a name "myinteger" and assign it to some integer
5+
6+
7+
# 3. Define variable with a name "myfloat" and assign it to some float
8+
9+
10+
# 4. Define variable with a name "myboolean" and assign it to some bool
11+
12+
13+
# 5. Define a variable with a name "mynonevalue" and assign it to None
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 1. Define variable with a name "mystring" and assign it to some string
2+
mystring = "some_string"
3+
4+
# 2. Define variable with a name "myinteger" and assign it to some integer
5+
myinteger = 5
6+
7+
# 3. Define variable with a name "myfloat" and assign it to some float
8+
myfloat = 14.5
9+
10+
# 4. Define variable with a name "myboolean" and assign it to some bool
11+
myboolean = True
12+
13+
# 5. Define a variable with a name "mynonevalue" and assign it to None
14+
mynonevalue = None
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def test_string_definition(run_script, expect_variable_type):
2+
_, namespace = run_script("datatypes_solution.py")
3+
expect_variable_type(namespace, "mystring", str)
4+
5+
6+
def test_integer_definition(run_script, expect_variable_type):
7+
_, namespace = run_script("datatypes_solution.py")
8+
expect_variable_type(namespace, "myinteger", int)
9+
10+
11+
def test_float_definition(run_script, expect_variable_type):
12+
_, namespace = run_script("datatypes_solution.py")
13+
expect_variable_type(namespace, "myfloat", float)
14+
15+
16+
def test_boolean_definition(run_script, expect_variable_type):
17+
_, namespace = run_script("datatypes_solution.py")
18+
expect_variable_type(namespace, "myboolean", bool)
19+
20+
21+
def test_none_definition(run_script, expect_variable_type):
22+
_, namespace = run_script("datatypes_solution.py")
23+
expect_variable_type(namespace, "mynonevalue", type(None))

0 commit comments

Comments
 (0)