Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
twitch
live-coding-fr
Commits
c68c8920
Commit
c68c8920
authored
Oct 13, 2020
by
Maxime DEZETTE
Committed by
Colin DAMON
Oct 18, 2020
Browse files
Mustache replacer implementation
parent
948f74dc
Changes
7
Hide whitespace changes
Inline
Side-by-side
.gitlab-ci.yml
View file @
c68c8920
...
...
@@ -10,3 +10,4 @@ include:
-
local
:
"
/ugly-trivia/.gitlab-ci.yml"
-
local
:
"
/borestop/.gitlab-ci.yml"
-
local
:
"
/java-pagination-seven/.gitlab-ci.yml"
-
local
:
"
/mustache-replacer/.gitlab-ci.yml"
README.md
View file @
c68c8920
...
...
@@ -26,3 +26,4 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré
8.
[
Borestop
](
/borestop
)
9.
[
Java pagination Seven
](
/java-pagination-seven
)
10.
[
PadBowl
](
https://gitlab.com/cdamon/padbowl
)
11.
[
Mustache Replacer
](
/mustache-replacer
)
mustache-replacer/.gitlab-ci.yml
0 → 100644
View file @
c68c8920
package-mustache-replacer
:
variables
:
PROJECT_FOLDER
:
"
mustache-replacer"
extends
:
.java
only
:
refs
:
-
master
-
merge_requests
changes
:
-
"
.gitlab-common-ci.yml"
-
"
mustache-replacer/**/*"
mustache-replacer/README.md
0 → 100644
View file @
c68c8920
# 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**
:
[](
#
)
mustache-replacer/pom.xml
0 → 100644
View file @
c68c8920
<?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>
mustache-replacer/src/main/java/ArgumentReplacer.java
0 → 100644
View file @
c68c8920
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
;
}
}
mustache-replacer/src/test/java/ArgumentReplacerTest.java
0 → 100644
View file @
c68c8920
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"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment