|
| 1 | +def _fn(name): |
| 2 | + import exercise |
| 3 | + |
| 4 | + assert hasattr(exercise, name), f"Your program needs a function called {name}(). Define it with: def {name}(...):" |
| 5 | + func = getattr(exercise, name) |
| 6 | + assert callable(func), f"{name} should be a function, but it is {func!r}." |
| 7 | + return func |
| 8 | + |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | + |
| 13 | +def test_validate_age_accepts_good_values(): |
| 14 | + validate_age = _fn("validate_age") |
| 15 | + for age in [0, 35, 150]: |
| 16 | + assert validate_age(age) == age, f"validate_age({age}) should return {age}." |
| 17 | + |
| 18 | + |
| 19 | +def test_validate_age_rejects_wrong_type(): |
| 20 | + validate_age = _fn("validate_age") |
| 21 | + with pytest.raises(TypeError) as info: |
| 22 | + validate_age("35") |
| 23 | + assert "whole number" in str(info.value), f"The TypeError message should say the age must be a whole number, but it was {str(info.value)!r}." |
| 24 | + |
| 25 | + |
| 26 | +def test_validate_age_rejects_out_of_range(): |
| 27 | + validate_age = _fn("validate_age") |
| 28 | + for age in [-1, 151]: |
| 29 | + with pytest.raises(ValueError) as info: |
| 30 | + validate_age(age) |
| 31 | + assert "between 0 and 150" in str(info.value), f"validate_age({age}) should raise ValueError('age must be between 0 and 150') but the message was {str(info.value)!r}." |
| 32 | + |
| 33 | + |
| 34 | +def test_out_of_stock_is_an_exception_type(): |
| 35 | + import exercise |
| 36 | + |
| 37 | + assert hasattr(exercise, "OutOfStock"), "Define your own error with: class OutOfStock(Exception):" |
| 38 | + assert isinstance(exercise.OutOfStock, type) and issubclass(exercise.OutOfStock, Exception), "OutOfStock must be a class that inherits from Exception: class OutOfStock(Exception):" |
| 39 | + |
| 40 | + |
| 41 | +def test_take_reduces_stock(): |
| 42 | + take = _fn("take") |
| 43 | + stock = {"apple": 5} |
| 44 | + assert take(stock, "apple", 2) == 3, "take({'apple': 5}, 'apple', 2) should return 3." |
| 45 | + assert stock == {"apple": 3}, f"take should also update the dictionary, which is now {stock!r}." |
| 46 | + assert take(stock, "apple") == 2, "The quantity defaults to 1." |
| 47 | + |
| 48 | + |
| 49 | +def test_take_raises_out_of_stock_with_a_clear_message(): |
| 50 | + import exercise |
| 51 | + |
| 52 | + take = _fn("take") |
| 53 | + with pytest.raises(exercise.OutOfStock) as info: |
| 54 | + take({"apple": 2}, "apple", 5) |
| 55 | + assert str(info.value) == "only 2 apple left, wanted 5", f"The message should be 'only 2 apple left, wanted 5' but it was {str(info.value)!r}." |
| 56 | + with pytest.raises(exercise.OutOfStock): |
| 57 | + take({}, "pear") |
| 58 | + |
| 59 | + |
| 60 | +def test_parse_percent_valid(): |
| 61 | + parse_percent = _fn("parse_percent") |
| 62 | + assert parse_percent("45%") == 45, f"parse_percent('45%') should return 45 but returned {parse_percent('45%')!r}." |
| 63 | + assert parse_percent("0%") == 0 and parse_percent("100%") == 100, "0% and 100% are both allowed." |
| 64 | + |
| 65 | + |
| 66 | +def test_parse_percent_malformed(): |
| 67 | + parse_percent = _fn("parse_percent") |
| 68 | + for text in ["45", "abc%", "%", "4.5%"]: |
| 69 | + with pytest.raises(ValueError) as info: |
| 70 | + parse_percent(text) |
| 71 | + assert str(info.value) == f"not a percentage: {text!r}", f"parse_percent({text!r}) should raise ValueError('not a percentage: {text!r}') but the message was {str(info.value)!r}." |
| 72 | + |
| 73 | + |
| 74 | +def test_parse_percent_out_of_range(): |
| 75 | + parse_percent = _fn("parse_percent") |
| 76 | + with pytest.raises(ValueError) as info: |
| 77 | + parse_percent("150%") |
| 78 | + assert str(info.value).startswith("percentage out of range"), f"parse_percent('150%') should raise a ValueError starting with 'percentage out of range' but the message was {str(info.value)!r}." |
0 commit comments