Skip to content

Commit

Permalink
Proposed Solution Day 22.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanncourtel committed Dec 23, 2023
1 parent 6bc0615 commit 2b6a5a3
Show file tree
Hide file tree
Showing 16 changed files with 1,160 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ A solution proposal will be published here every day during the `Advent Of Craft
- [Day 19: Loosing up dead weight.](solution/day19/docs/step-by-step.md)
- [Day 20: No more exceptions in our domain.](solution/day20/docs/step-by-step.md)
- [Day 21: Refactor the tests and production code to Output-Based tests.](solution/day21/docs/step-by-step.md)
- [Day 22: Design a diamond program using T.D.D and Property-Based Testing.](solution/day22/docs/step-by-step.md)

## Contributors

Expand Down
9 changes: 9 additions & 0 deletions solution/day22/.tcr/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
config:
git:
auto-push: false
polling-period: 2s
mob-timer:
duration: 5m0s
tcr:
language: java
toolchain: maven
9 changes: 9 additions & 0 deletions solution/day22/.tcr/language/java.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
toolchains:
default: maven
compatible-with: [ maven ]
source-files:
directories: [ src/main ]
patterns: [ '(?i)^.*\.java$' ]
test-files:
directories: [ src/test ]
patterns: [ '(?i)^.*\.java$' ]
11 changes: 11 additions & 0 deletions solution/day22/.tcr/toolchain/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
build:
- os: [ darwin, linux, windows ]
arch: [ "386", amd64, arm64 ]
command: mvn
arguments: [ test-compile ]
test:
- os: [ darwin, linux, windows ]
arch: [ "386", amd64, arm64 ]
command: mvn
arguments: [ test ]
test-result-dir: target/surefire-reports
120 changes: 120 additions & 0 deletions solution/day22/docs/challenge-done.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
## Day 22: Design a diamond program using T.D.D and Property-Based Testing.

We think about a possible contract for our class:
- It may contain a `print` method taking the `endCharacter` as parameter
- It could look like `Character` -> `Option<String>`
- We could use an `Option` because there are some cases that are not supported (ex: '^', '1', ...)

Let's design and implement it starting from less to more complex properties.

### None for invalid characters

```text
for all (invalidEndCharacter)
such that print(invalidEndCharacter) fail
```

🔴 We start by creating this first property

- We can generate production code from the `property`
- We work on how to generate `invalidEndCharacters`
- We check the generation of random characters
- Here is an example of values

🟢 We make it pass by `hardcoding` the result

🔵 We clean a little bit the test

### Horizontally symmetric

```text
for all (validEndCharacter)
such that diamond(validEndCharacter) == reverse(diamond(validEndCharacter))
```

🔴 We add a first version of the property

🟢 We make it pass by `hardcoding` the return value

🔵 We extract the guard and use ternary operator

### Is a square (height = width)

```text
for all (validEndCharacter)
such that diamond(validEndCharacter) is a square
```

The result `String` should a square meaning that each line contains the same number of characters than the number of lines.

🔴 Let's identify if it is a square

🟢 We can make it pass by simply returning 'A'

🔵 We have plenty of duplication in our tests

### Contain 2 identical letters per line

```text
for all (validEndCharacter)
such that each line in diamond(validEndCharacter) contains 2 identical letters except first and last
```

😬 It is already green...

It is maybe a signal that we need to iterate on the implementation

- We design from the implementation

🔴 Our properties are now failing, we can triangulate the algorithm

- We experiment and learn from the properties
- We fix the property `be_horizontally_symmetric` by iterating on the code
- We fix `contains_2_letters_per_line` by fixing the `toLine` method
- We fix the property `be_a_square` by fixing the `generateEmptyCharacters`

🟢 All our properties are green again 🤩

🔵 Let's refactor our `Diamond` to extract some method and give business names

### Lines have a decreasing number of left spaces

```text
for all (validEndCharacter)
such that Lines have a decreasing number of left white spaces until end character
```

🟢 Not that easy to create...

🔵 We refactor the test to make it more clear what we do in it

### Lines have a decreasing number of right spaces

🟢 As you may expect the property is green

🔵 We can remove duplications in the test

- We create a new `method` to centralize this logic

> All our properties are green 🤩. Are we confident enough?
### Add an `Approval` test

To increase our confidence we secure our implementation with a `Unit Test`.
We choose to use an `Approval` one.

🔴 It fails because we need to approve the result

🟢 It seems pretty good

We approve the file, and we're done, for now 😉.

Our `Diamond` is complete!

>**Tip of the day: Property based testing with TDD can help you design a more robust implementation.**
### Share your experience

How does your code look like?

Please let everyone know in the discord.
Binary file added solution/day22/docs/img/approve-content.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added solution/day22/docs/img/generate-code-impl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added solution/day22/docs/snippet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2b6a5a3

Please sign in to comment.