|
| 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 inspect |
| 11 | +import itertools |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | + |
| 16 | +def test_fibonacci_is_an_endless_generator(): |
| 17 | + fibonacci = _fn("fibonacci") |
| 18 | + gen = fibonacci() |
| 19 | + assert inspect.isgenerator(gen), "fibonacci() should return a generator: use yield inside the function" |
| 20 | + first = list(itertools.islice(gen, 10)) |
| 21 | + assert first == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34], f"The first ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, got {first!r}" |
| 22 | + |
| 23 | + |
| 24 | +def test_fibonacci_gives_a_fresh_sequence_each_call(): |
| 25 | + fibonacci = _fn("fibonacci") |
| 26 | + a = fibonacci() |
| 27 | + next(a) |
| 28 | + next(a) |
| 29 | + assert next(fibonacci()) == 0, "each call to fibonacci() should start again from 0" |
| 30 | + |
| 31 | + |
| 32 | +def test_chunks(): |
| 33 | + chunks = _fn("chunks") |
| 34 | + assert list(chunks([1, 2, 3, 4, 5], 2)) == [[1, 2], [3, 4], [5]], f"chunks([1, 2, 3, 4, 5], 2) should be [[1, 2], [3, 4], [5]], got {list(chunks([1, 2, 3, 4, 5], 2))!r}" |
| 35 | + assert list(chunks([1, 2, 3], 3)) == [[1, 2, 3]], "a size equal to the length gives one chunk" |
| 36 | + assert list(chunks([], 4)) == [], "no items means no chunks" |
| 37 | + assert list(chunks("abcd", 3)) == ["abc", "d"], "slices of a string are strings, so chunks should keep the type of the slices" |
| 38 | + |
| 39 | + |
| 40 | +def test_chunks_is_a_generator_and_validates_size(): |
| 41 | + chunks = _fn("chunks") |
| 42 | + assert inspect.isgenerator(chunks([1], 1)), "chunks should be a generator function (use yield)" |
| 43 | + with pytest.raises(ValueError): |
| 44 | + list(chunks([1, 2], 0)) |
| 45 | + |
| 46 | + |
| 47 | +def test_running_total(): |
| 48 | + running_total = _fn("running_total") |
| 49 | + assert list(running_total([1, 2, 3])) == [1, 3, 6], f"running_total([1, 2, 3]) should give 1, 3, 6, got {list(running_total([1, 2, 3]))!r}" |
| 50 | + assert list(running_total([])) == [], "no numbers means no totals" |
| 51 | + assert list(running_total([5, -5, 5])) == [5, 0, 5], "negative numbers should work too" |
| 52 | + |
| 53 | + |
| 54 | +def test_running_total_is_lazy(): |
| 55 | + running_total = _fn("running_total") |
| 56 | + pulled = [] |
| 57 | + |
| 58 | + def source(): |
| 59 | + for n in (1, 2, 3, 4): |
| 60 | + pulled.append(n) |
| 61 | + yield n |
| 62 | + |
| 63 | + gen = running_total(source()) |
| 64 | + assert inspect.isgenerator(gen), "running_total should be a generator function (use yield)" |
| 65 | + assert pulled == [], "creating the generator must not read anything yet" |
| 66 | + assert next(gen) == 1, "the first total should be 1" |
| 67 | + assert pulled == [1], f"after one next() only one item should have been read, but {pulled!r} were read: yield each total as soon as you have it" |
0 commit comments