Commit e1b6db0e authored by Colin DAMON's avatar Colin DAMON
Browse files

Merge branch '11-init-string-calculator-project' into 'master'

Resolve "Init String calculator project"

Closes #11

See merge request !10
parents e8c000ea 95bad854
......@@ -5,3 +5,4 @@ include:
- local: "/.gitlab-common-ci.yml"
- local: "/wordWrap/.gitlab-ci.yml"
- local: "/romanNumerals/.gitlab-ci.yml"
- local: "/stringCalculator/.gitlab-ci.yml"
......@@ -23,3 +23,7 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré
* Niveau: Débutant
* Lien: [Roman Numerals avec Julie et Colin](https://www.youtube.com/watch?v=CWJqq_k7hDY)
* Go To: [RomanNumerals](/romanNumerals)
* **[Presque String Calculator](https://codingdojo.org/kata/StringCalculator/)**: Par Marina FILLATRE et Colin DAMON le 23/07/2020
* Niveau: Moyen
* Lien: [Agility && TDD, Marina et Colin](https://www.twitch.tv/videos/688222419)
* Go To: [stringCalculator](/stringCalculator)
stages:
- build
package-string-calculator:
extends: .java
stage: build
tags:
- docker
script:
- cd stringCalculator
- mvn $MAVEN_CLI_OPTS clean package
- cat target/site/jacoco/index.html
artifacts:
reports:
junit: stringCalculator/target/surefire-reports/TEST-*.xml
expire_in: 1 day
only:
refs:
- master
- merge_requests
changes:
- ".gitlab-ci.yml"
- ".gitlab-common-ci.yml"
- "stringCalculator/**/*"
<p align="center">
<a href="https://fr.ippon.tech/" target="_blank">
<img alt="Ippon Technologies Logo" width="100" src="https://fr.ippon.tech/assets/images/common/Logo.svg">
</a>
</p>
# Ippon Kata : String Calculator
<?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>
<version>1.0.0-SNAPSHOT</version>
<groupId>fr.ippon.kata</groupId>
<artifactId>string-calculator</artifactId>
<name>StringCalculator</name>
<properties>
<java.version>14</java.version>
<junit.version>5.7.0-M1</junit.version>
<assertj.version>3.16.1</assertj.version>
<mockito.version>3.1.0</mockito.version>
<surefire-plugin.version>3.0.0-M4</surefire-plugin.version>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<jacoco.version>0.8.5</jacoco.version>
</properties>
<developers>
<developer>
<email>cdamon@ippon.fr</email>
<name>Colin DAMON</name>
</developer>
<developer>
<email>mfillatre@ippon.fr</email>
<name>Marina FILLATRE</name>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package fr.ippon.stringcalculator;
import java.math.BigDecimal;
import java.util.function.BinaryOperator;
public class Calculator {
public static String sum(String first, String second) {
return operate(first, second, BigDecimal::add);
}
public static String substract(String first,
String second) {
return operate(first, second, BigDecimal::subtract);
}
private static String operate(String first,
String second, BinaryOperator<BigDecimal> operation) {
notNull(first);
notNull(second);
return operation
.apply(toBigDecimal(first), toBigDecimal(second))
.toPlainString();
}
private static BigDecimal toBigDecimal(String value) {
if (value.isBlank()) {
return BigDecimal.ZERO;
}
return new BigDecimal(value.replace(",", "."));
}
private static void notNull(String first) {
if (first == null) {
throw new IllegalArgumentException();
}
}
}
package fr.ippon.stringcalculator;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class CalculatorTest {
@Test
void shouldNotSumWithNullFirstInput() {
assertThatThrownBy(() -> Calculator.sum(null, "3"))
.isExactlyInstanceOf(
IllegalArgumentException.class);
}
@Test
void shouldNotSumWithNullSecondInput() {
assertThatThrownBy(() -> Calculator.sum("3", null))
.isExactlyInstanceOf(
IllegalArgumentException.class);
}
@Test
void shouldSumEmptyInputs() {
assertThat(Calculator.sum("", "")).isEqualTo("0");
}
@Test
void shouldSumIntegerWithEmptyFirstInput() {
assertThat(Calculator.sum("", "4")).isEqualTo("4");
}
@Test
void shouldSumIntegerWithEmptySecondInput() {
assertThat(Calculator.sum("4", "")).isEqualTo("4");
}
@Test
void shouldSumIntegers() {
assertThat(Calculator.sum("1", "2")).isEqualTo("3");
}
@Test
void shouldSumFloats() {
assertThat(Calculator.sum("1.01", "2.02"))
.isEqualTo("3.03");
}
@Test
void shouldSumCommaSeparatedFloats() {
assertThat(Calculator.sum("1,01", "2,02"))
.isEqualTo("3.03");
}
@Test
void shouldSumNegativeCommaSeparatedFloats() {
assertThat(Calculator.sum("-1,01", "-2,02"))
.isEqualTo("-3.03");
}
@Test
void shouldNotSubstractNullInputs() {
assertThatThrownBy(
() -> Calculator.substract(null, "3"))
.isExactlyInstanceOf(
IllegalArgumentException.class);
}
@Test
void shouldNotSubstractWithNullSecondInputs() {
assertThatThrownBy(
() -> Calculator.substract("3", null))
.isExactlyInstanceOf(
IllegalArgumentException.class);
}
@Test
void shouldSubstractCommaSeparatedFloats() {
assertThat(Calculator.substract("1,01", "2,02"))
.isEqualTo("-1.01");
}
@Test
void shouldSumTooBigIntegers() {
assertThat(Calculator
.sum(String.valueOf(Integer.MAX_VALUE), "2,02"))
.isEqualTo("2147483649.02");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment