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
f0985fd7
Commit
f0985fd7
authored
Jul 26, 2020
by
Colin DAMON
Browse files
Full format (missing hook)
parent
3cf36dae
Changes
2
Hide whitespace changes
Inline
Side-by-side
stringCalculator/src/main/java/fr/ippon/stringcalculator/Calculator.java
View file @
f0985fd7
...
...
@@ -5,26 +5,21 @@ import java.util.function.BinaryOperator;
public
final
class
Calculator
{
private
Calculator
()
{
}
private
Calculator
()
{}
public
static
String
sum
(
String
first
,
String
second
)
{
return
operate
(
first
,
second
,
BigDecimal:
:
add
);
}
public
static
String
substract
(
String
first
,
String
second
)
{
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
)
{
private
static
String
operate
(
String
first
,
String
second
,
BinaryOperator
<
BigDecimal
>
operation
)
{
notNull
(
first
);
notNull
(
second
);
return
operation
.
apply
(
toBigDecimal
(
first
),
toBigDecimal
(
second
))
.
toPlainString
();
return
operation
.
apply
(
toBigDecimal
(
first
),
toBigDecimal
(
second
)).
toPlainString
();
}
private
static
BigDecimal
toBigDecimal
(
String
value
)
{
...
...
@@ -40,5 +35,4 @@ public final class Calculator {
throw
new
IllegalArgumentException
();
}
}
}
stringCalculator/src/test/java/fr/ippon/stringcalculator/CalculatorTest.java
View file @
f0985fd7
...
...
@@ -8,16 +8,12 @@ public class CalculatorTest {
@Test
void
shouldNotSumWithNullFirstInput
()
{
assertThatThrownBy
(()
->
Calculator
.
sum
(
null
,
"3"
))
.
isExactlyInstanceOf
(
IllegalArgumentException
.
class
);
assertThatThrownBy
(()
->
Calculator
.
sum
(
null
,
"3"
)).
isExactlyInstanceOf
(
IllegalArgumentException
.
class
);
}
@Test
void
shouldNotSumWithNullSecondInput
()
{
assertThatThrownBy
(()
->
Calculator
.
sum
(
"3"
,
null
))
.
isExactlyInstanceOf
(
IllegalArgumentException
.
class
);
assertThatThrownBy
(()
->
Calculator
.
sum
(
"3"
,
null
)).
isExactlyInstanceOf
(
IllegalArgumentException
.
class
);
}
@Test
...
...
@@ -42,48 +38,36 @@ public class CalculatorTest {
@Test
void
shouldSumFloats
()
{
assertThat
(
Calculator
.
sum
(
"1.01"
,
"2.02"
))
.
isEqualTo
(
"3.03"
);
assertThat
(
Calculator
.
sum
(
"1.01"
,
"2.02"
)).
isEqualTo
(
"3.03"
);
}
@Test
void
shouldSumCommaSeparatedFloats
()
{
assertThat
(
Calculator
.
sum
(
"1,01"
,
"2,02"
))
.
isEqualTo
(
"3.03"
);
assertThat
(
Calculator
.
sum
(
"1,01"
,
"2,02"
)).
isEqualTo
(
"3.03"
);
}
@Test
void
shouldSumNegativeCommaSeparatedFloats
()
{
assertThat
(
Calculator
.
sum
(
"-1,01"
,
"-2,02"
))
.
isEqualTo
(
"-3.03"
);
assertThat
(
Calculator
.
sum
(
"-1,01"
,
"-2,02"
)).
isEqualTo
(
"-3.03"
);
}
@Test
void
shouldNotSubstractNullInputs
()
{
assertThatThrownBy
(
()
->
Calculator
.
substract
(
null
,
"3"
))
.
isExactlyInstanceOf
(
IllegalArgumentException
.
class
);
assertThatThrownBy
(()
->
Calculator
.
substract
(
null
,
"3"
)).
isExactlyInstanceOf
(
IllegalArgumentException
.
class
);
}
@Test
void
shouldNotSubstractWithNullSecondInputs
()
{
assertThatThrownBy
(
()
->
Calculator
.
substract
(
"3"
,
null
))
.
isExactlyInstanceOf
(
IllegalArgumentException
.
class
);
assertThatThrownBy
(()
->
Calculator
.
substract
(
"3"
,
null
)).
isExactlyInstanceOf
(
IllegalArgumentException
.
class
);
}
@Test
void
shouldSubstractCommaSeparatedFloats
()
{
assertThat
(
Calculator
.
substract
(
"1,01"
,
"2,02"
))
.
isEqualTo
(
"-1.01"
);
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"
);
assertThat
(
Calculator
.
sum
(
String
.
valueOf
(
Integer
.
MAX_VALUE
),
"2,02"
)).
isEqualTo
(
"2147483649.02"
);
}
}
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