|
| 1 | +## Day 22: Design a diamond program using T.D.D and Property-Based Testing. |
| 2 | + |
| 3 | +We think about a possible contract for our class: |
| 4 | +- It may contain a `print` method taking the `endCharacter` as parameter |
| 5 | + - It could look like `Character` -> `Option<String>` |
| 6 | + - We could use an `Option` because there are some cases that are not supported (ex: '^', '1', ...) |
| 7 | + |
| 8 | +Let's design and implement it starting from less to more complex properties. |
| 9 | + |
| 10 | +### None for invalid characters |
| 11 | + |
| 12 | +```text |
| 13 | +for all (invalidEndCharacter) |
| 14 | +such that print(invalidEndCharacter) fail |
| 15 | +``` |
| 16 | + |
| 17 | +🔴 We start by creating this first property |
| 18 | + |
| 19 | +- We can generate production code from the `property` |
| 20 | +- We work on how to generate `invalidEndCharacters` |
| 21 | +- We check the generation of random characters |
| 22 | + - Here is an example of values |
| 23 | + |
| 24 | +🟢 We make it pass by `hardcoding` the result |
| 25 | + |
| 26 | +🔵 We clean a little bit the test |
| 27 | + |
| 28 | +### Horizontally symmetric |
| 29 | + |
| 30 | +```text |
| 31 | +for all (validEndCharacter) |
| 32 | +such that diamond(validEndCharacter) == reverse(diamond(validEndCharacter)) |
| 33 | +``` |
| 34 | + |
| 35 | +🔴 We add a first version of the property |
| 36 | + |
| 37 | +🟢 We make it pass by `hardcoding` the return value |
| 38 | + |
| 39 | +🔵 We extract the guard and use ternary operator |
| 40 | + |
| 41 | +### Is a square (height = width) |
| 42 | + |
| 43 | +```text |
| 44 | +for all (validEndCharacter) |
| 45 | +such that diamond(validEndCharacter) is a square |
| 46 | +``` |
| 47 | + |
| 48 | +The result `String` should a square meaning that each line contains the same number of characters than the number of lines. |
| 49 | + |
| 50 | +🔴 Let's identify if it is a square |
| 51 | + |
| 52 | +🟢 We can make it pass by simply returning 'A' |
| 53 | + |
| 54 | +🔵 We have plenty of duplication in our tests |
| 55 | + |
| 56 | +### Contain 2 identical letters per line |
| 57 | + |
| 58 | +```text |
| 59 | +for all (validEndCharacter) |
| 60 | +such that each line in diamond(validEndCharacter) contains 2 identical letters except first and last |
| 61 | +``` |
| 62 | + |
| 63 | +😬 It is already green... |
| 64 | + |
| 65 | +It is maybe a signal that we need to iterate on the implementation |
| 66 | + |
| 67 | +- We design from the implementation |
| 68 | + |
| 69 | +🔴 Our properties are now failing, we can triangulate the algorithm |
| 70 | + |
| 71 | +- We experiment and learn from the properties |
| 72 | +- We fix the property `be_horizontally_symmetric` by iterating on the code |
| 73 | +- We fix `contains_2_letters_per_line` by fixing the `toLine` method |
| 74 | +- We fix the property `be_a_square` by fixing the `generateEmptyCharacters` |
| 75 | + |
| 76 | +🟢 All our properties are green again 🤩 |
| 77 | + |
| 78 | +🔵 Let's refactor our `Diamond` to extract some method and give business names |
| 79 | + |
| 80 | +### Lines have a decreasing number of left spaces |
| 81 | + |
| 82 | +```text |
| 83 | +for all (validEndCharacter) |
| 84 | +such that Lines have a decreasing number of left white spaces until end character |
| 85 | +``` |
| 86 | + |
| 87 | +🟢 Not that easy to create... |
| 88 | + |
| 89 | +🔵 We refactor the test to make it more clear what we do in it |
| 90 | + |
| 91 | +### Lines have a decreasing number of right spaces |
| 92 | + |
| 93 | +🟢 As you may expect the property is green |
| 94 | + |
| 95 | +🔵 We can remove duplications in the test |
| 96 | + |
| 97 | +- We create a new `method` to centralize this logic |
| 98 | + |
| 99 | +> All our properties are green 🤩. Are we confident enough? |
| 100 | +
|
| 101 | +### Add an `Approval` test |
| 102 | + |
| 103 | +To increase our confidence we secure our implementation with a `Unit Test`. |
| 104 | +We choose to use an `Approval` one. |
| 105 | + |
| 106 | +🔴 It fails because we need to approve the result |
| 107 | + |
| 108 | +🟢 It seems pretty good |
| 109 | + |
| 110 | +We approve the file, and we're done, for now 😉. |
| 111 | + |
| 112 | +Our `Diamond` is complete! |
| 113 | + |
| 114 | +>**Tip of the day: Property based testing with TDD can help you design a more robust implementation.** |
| 115 | +
|
| 116 | +### Share your experience |
| 117 | + |
| 118 | +How does your code look like? |
| 119 | + |
| 120 | +Please let everyone know in the discord. |
0 commit comments