mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
fix(curriculum): add test for falsy value in lab travel weather planner (#65986)
This commit is contained in:
+59
@@ -146,6 +146,65 @@ You should use the `print()` function to display the result.
|
||||
({ test: () => runPython(`assert _Node(_code).block_has_call("print")`) })
|
||||
```
|
||||
|
||||
When `distance_mi` is a falsy value, the program should print `False`.
|
||||
|
||||
```js
|
||||
({ test: () => runPython(`
|
||||
import ast, io, contextlib
|
||||
|
||||
VARIABLES = {
|
||||
"distance_mi",
|
||||
"is_raining",
|
||||
"has_bike",
|
||||
"has_car",
|
||||
"has_ride_share_app"
|
||||
}
|
||||
|
||||
def run_case(env, expected):
|
||||
tree = ast.parse(_code)
|
||||
|
||||
tree.body = [
|
||||
node for node in tree.body
|
||||
if not (
|
||||
isinstance(node, ast.Assign)
|
||||
and isinstance(node.targets[0], ast.Name)
|
||||
and node.targets[0].id in VARIABLES
|
||||
)
|
||||
]
|
||||
|
||||
clean_code = compile(tree, "<ast>", "exec")
|
||||
|
||||
buffer = io.StringIO()
|
||||
with contextlib.redirect_stdout(buffer):
|
||||
exec(clean_code, env)
|
||||
|
||||
assert buffer.getvalue().strip() == expected
|
||||
|
||||
|
||||
run_case(
|
||||
{
|
||||
"distance_mi": 0,
|
||||
"is_raining": False,
|
||||
"has_bike": True,
|
||||
"has_car": True,
|
||||
"has_ride_share_app": True
|
||||
},
|
||||
"False"
|
||||
)
|
||||
|
||||
run_case(
|
||||
{
|
||||
"distance_mi": 0.0,
|
||||
"is_raining": False,
|
||||
"has_bike": True,
|
||||
"has_car": True,
|
||||
"has_ride_share_app": True
|
||||
},
|
||||
"False"
|
||||
)
|
||||
`) })
|
||||
```
|
||||
|
||||
When the distance is `1` mile or less and it is not raining, the program should print `True`.
|
||||
|
||||
```js
|
||||
|
||||
Reference in New Issue
Block a user