Skip to content
Snippets Groups Projects
Commit c68c8920 authored by Maxime DEZETTE's avatar Maxime DEZETTE Committed by Colin DAMON
Browse files

Mustache replacer implementation

parent 948f74dc
No related branches found
No related tags found
1 merge request!29Resolve "mustach replacer"
...@@ -10,3 +10,4 @@ include: ...@@ -10,3 +10,4 @@ include:
- local: "/ugly-trivia/.gitlab-ci.yml" - local: "/ugly-trivia/.gitlab-ci.yml"
- local: "/borestop/.gitlab-ci.yml" - local: "/borestop/.gitlab-ci.yml"
- local: "/java-pagination-seven/.gitlab-ci.yml" - local: "/java-pagination-seven/.gitlab-ci.yml"
- local: "/mustache-replacer/.gitlab-ci.yml"
...@@ -26,3 +26,4 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré ...@@ -26,3 +26,4 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré
8. [Borestop](/borestop) 8. [Borestop](/borestop)
9. [Java pagination Seven](/java-pagination-seven) 9. [Java pagination Seven](/java-pagination-seven)
10. [PadBowl](https://gitlab.com/cdamon/padbowl) 10. [PadBowl](https://gitlab.com/cdamon/padbowl)
11. [Mustache Replacer](/mustache-replacer)
package-mustache-replacer:
variables:
PROJECT_FOLDER: "mustache-replacer"
extends: .java
only:
refs:
- master
- merge_requests
changes:
- ".gitlab-common-ci.yml"
- "mustache-replacer/**/*"
# Mustache Replacer
Résolution en TDD et avec explications d'un exercice de remplacement d'arguments dans une phrase.
- **Auteurs** : Colin DAMON et Maxime DEZETTE
- **Date** : 13/10/2020
- **Langage** : Java
- **Niveau** : Débutant
- **Replay** : [](#)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<version>1.0.0</version>
<groupId>fr.ippon.kata</groupId>
<artifactId>java-parent</artifactId>
<relativePath>../java-parent</relativePath>
</parent>
<version>1.0.0-SNAPSHOT</version>
<artifactId>string-calculator</artifactId>
<name>MustacheReplacer</name>
<developers>
<developer>
<email>cdamon@ippon.fr</email>
<name>Colin DAMON</name>
</developer>
<developer>
<email>mdezette@ippon.fr</email>
<name>Maxime DEZETTE</name>
</developer>
</developers>
</project>
import java.util.Map;
import java.util.function.BinaryOperator;
public class ArgumentReplacer {
public static final String BEGINING_MUSTACH = "\\{\\{\\s*";
public static final String ENDING_MUSTACH = "\\s*\\}\\}";
public static String replace(String template, Map<String, String> arguments) {
if (needNoReplace(template, arguments)) {
return template;
}
return arguments.entrySet().stream().reduce(template, ArgumentReplacer::replaceEntry, nothing());
}
private static boolean needNoReplace(String template, Map<String, String> arguments) {
return template == null || arguments == null;
}
private static String replaceEntry(String result, Map.Entry<String, String> entry) {
return result.replaceAll(BEGINING_MUSTACH + entry.getKey() + ENDING_MUSTACH, entry.getValue());
}
private static BinaryOperator<String> nothing() {
return (k, v) -> v;
}
}
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
class SampleTest {
@Test
void shouldGetInputStringWithoutArgumentToReplace() {
assertThat(ArgumentReplacer.replace("Bonjour Homer", null)).isEqualTo("Bonjour Homer");
}
@Test
void shouldGetInputStringWithArgumentReplaced() {
assertThat(ArgumentReplacer.replace("Bonjour {{firstname}}", Map.of("firstname", "Homer"))).isEqualTo("Bonjour Homer");
}
@Test
void shouldGetNullResultWithoutTemplate(){
assertThat(ArgumentReplacer.replace(null, Map.of("firstname", "Homer"))).isNull();
}
@Test
void shouldGetInputStringReplacedWithSpace(){
assertThat(ArgumentReplacer.replace("Bonjour {{firstname}} {{ lastname }}", Map.of("firstname","Homer", "lastname", "Simpson"))).isEqualTo("Bonjour Homer Simpson");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment