Skip to content

Commit

Permalink
Day 17: One and thousand tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanncourtel committed Dec 17, 2023
1 parent b87e70f commit 70ed7c1
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Here are the different challenges :
- [Day 14: Do not use exceptions anymore.](exercise/day14/docs/challenge.md)
- [Day 15: Put a code under tests.](exercise/day15/docs/challenge.md)
- [Day 16: Make this code immutable.](exercise/day16/docs/challenge.md)
- [Day 17: Design one test that has the impact of thousands.](exercise/day17/docs/challenge.md)

### Solutions
A solution proposal will be published here every day during the `Advent Of Craft` containing `the code` and a `step by step` guide.
Expand Down
23 changes: 23 additions & 0 deletions exercise/day17/docs/challenge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Day 17: One and thousand tests.

Today, you are recovering at the nearby harbor to
replenish your ship before the last week on the journey.

Challenges are not going to lack and in order to face
them, new skills are required.

Today's exercise is about transforming your test in
a way you've not done before.

However, nothing is simple. You've got to think
of your tests and your code differently.

_What characteristic does my code have?_

> **Day 17: Design one test that has the impact of thousands.**
May your crafting journey bring you to new heights!

- <u>💡HINT:</u> What PBT stands for.

![snippet of the day](snippet.png)
Binary file added exercise/day17/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.
33 changes: 33 additions & 0 deletions exercise/day17/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.advent-of-craft</groupId>
<artifactId>advent-of-craft2023</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>fizzbuzz-part5</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<vavr.version>0.10.4</vavr.version>
</properties>

<dependencies>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>${vavr.version}</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr-test</artifactId>
<version>${vavr.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
39 changes: 39 additions & 0 deletions exercise/day17/src/main/java/games/FizzBuzz.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package games;

import io.vavr.collection.LinkedHashMap;
import io.vavr.collection.Map;
import io.vavr.control.Option;

import static io.vavr.control.Option.none;
import static io.vavr.control.Option.some;

public class FizzBuzz {
public static final int MIN = 1;
public static final int MAX = 100;
private static final Map<Integer, String> mapping = LinkedHashMap.of(
15, "FizzBuzz",
3, "Fizz",
5, "Buzz"
);

public static Option<String> convert(int input) {
return isOutOfRange(input)
? none()
: some(convertSafely(input));
}

private static String convertSafely(Integer input) {
return mapping
.find(p -> is(p._1, input))
.map(p -> p._2)
.getOrElse(input.toString());
}

private static boolean is(Integer divisor, Integer input) {
return input % divisor == 0;
}

private static boolean isOutOfRange(Integer input) {
return input < MIN || input > MAX;
}
}
51 changes: 51 additions & 0 deletions exercise/day17/src/test/java/games/FizzBuzzTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package games;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static io.vavr.API.Some;
import static org.assertj.core.api.Assertions.assertThat;

class FizzBuzzTests {
private static Stream<Arguments> validInputs() {
return Stream.of(
Arguments.of(1, "1"),
Arguments.of(67, "67"),
Arguments.of(82, "82"),
Arguments.of(3, "Fizz"),
Arguments.of(66, "Fizz"),
Arguments.of(99, "Fizz"),
Arguments.of(5, "Buzz"),
Arguments.of(50, "Buzz"),
Arguments.of(85, "Buzz"),
Arguments.of(15, "FizzBuzz"),
Arguments.of(30, "FizzBuzz"),
Arguments.of(45, "FizzBuzz")
);
}

private static Stream<Arguments> invalidInputs() {
return Stream.of(
Arguments.of(0),
Arguments.of(-1),
Arguments.of(101)
);
}

@ParameterizedTest
@MethodSource("validInputs")
void parse_successfully_numbers_between_1_and_100(int input, String expectedResult) {
assertThat(FizzBuzz.convert(input))
.isEqualTo(Some(expectedResult));
}

@ParameterizedTest
@MethodSource("invalidInputs")
void parse_fail_for_numbers_out_of_range(int input) {
assertThat(FizzBuzz.convert(input).isEmpty())
.isTrue();
}
}
1 change: 1 addition & 0 deletions exercise/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<module>day14</module>
<module>day15</module>
<module>day16</module>
<module>day17</module>
</modules>

<properties>
Expand Down

0 comments on commit 70ed7c1

Please sign in to comment.