forked from next-step/java-racingcar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringAddCalculatorTest.java
69 lines (58 loc) · 2.58 KB
/
StringAddCalculatorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package step2;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static step2.StringAddCalculator.calculate;
public class StringAddCalculatorTest {
@ParameterizedTest
@DisplayName("숫자 하나를 문자열로 입력할 경우 해당 숫자를 반환한다.")
@ValueSource(strings = {"1", "2", "3"})
public void calculate_숫자하나(String input) throws Exception {
int result = calculate(input);
assertThat(result).isEqualTo(Integer.parseInt(input));
}
@ParameterizedTest
@NullAndEmptySource
@DisplayName("null 또는 빈문자를 입력할 경우 0을 반환해야 한다.")
public void calculate_null_또는_빈문자(String input) {
assertThat(calculate(input)).isEqualTo(0);
}
@ParameterizedTest
@DisplayName("숫자 두 개를 콤마(,) 구분자로 입력할 경우 두 숫자의 합을 반환한다.")
@ValueSource(strings = {"1,2", "1,2,"})
public void calculate_쉼표구분자(String input) throws Exception {
int result = calculate(input);
assertThat(result).isEqualTo(3);
}
@Test
@DisplayName("구분자는 콤마(,)와 콜론(:)을 사용할 수 있다.")
public void calculate_쉼표_또는_콜론_구분자() throws Exception {
int result = calculate("1,2:3");
assertThat(result).isEqualTo(6);
}
@ParameterizedTest
@DisplayName("구분자는 //와 /n 문자를 이용해 커스텀 구분자를 지정할 수 있다.")
@ValueSource(strings = {";", ":"})
public void calculate_custom_구분자(String delimiter) throws Exception {
List<String> inputNumbers = List.of("1", "2", "3");
String customDelimiter = "//" + delimiter + "\n";
int result = calculate(customDelimiter + String.join(delimiter, inputNumbers));
assertThat(result).isEqualTo(
inputNumbers.stream()
.mapToInt(Integer::parseInt)
.sum()
);
}
@ParameterizedTest
@DisplayName("음수를 전달할 경우 예외가 발생한다.")
@ValueSource(strings = {"-1", "1,-2,3"})
public void calculate_negative(String input) throws Exception {
assertThatThrownBy(() -> calculate(input))
.isInstanceOf(RuntimeException.class);
}
}