Commit 95bad854 authored by Colin DAMON's avatar Colin DAMON
Browse files

Live implementation

parent ec0d2184
......@@ -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)
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