Skip to content

Commit

Permalink
refactor(Lotto): Lotto 입력 형식이 틀렸을 때, 틀린 이유를 에러 메세지로 출력하도록 함
Browse files Browse the repository at this point in the history
- 예외 던지는 것을 LottoSetting에서 하도록 함
어느 부분이 잘못되었는지 판단을 LottoSetting에서 하기 때문에 옮김
- 역할 수정에 따른 테스트 코드 수정
  • Loading branch information
leegwichan committed Nov 16, 2022
1 parent fe2c7a6 commit 201cfeb
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

- Lotto
- [x] Lotto 입력 형식이 올바른지 체크
- 입력 형식이 올바르지 않을 경우 `IllegalArgumentException`을 발생
- LottoSetting을 통해 입력 형식 확인
- [x] 현재 가지고 있는 로또 정보를 String 형식으로 전달함
- 숫자 오름차순 정리 및 양 옆에 대괄호 추가
- ex) "[1, 2, 3, 4, 5, 6]"
Expand All @@ -34,6 +34,7 @@
- [x] 해당 정보에 따라 객체(LottoRewardCoordinator)를 생성
- LottoSetting
- [x] 변경 가능한 정보들을 enum 값으로 담고 있음
- [x] 형식이 올바른지 확인하고 올바르지 않으면 `IllegalArgumentException`을 발생
- WinningNumberSetting
- [x] 당첨 번호에 대한 정보를 저장하고 있음 (enum)
- LottoRewardSetting
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/lotto/lotto/Lotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ public Lotto(List<Integer> numbers) {
}

public Lotto(List<Integer> numbers, LottoSetting setting) {
validate(numbers, setting);
setting.validate(numbers);
this.numbers = numbers;
}

private void validate(List<Integer> numbers, LottoSetting setting) {
if (!setting.isValidNumbers(numbers)) {
throw new IllegalArgumentException(ExceptionMessage.LOTTO_FORM_NOT_MATCHED.getMessage());
}
}

public String getNumbers() {
return numbers.stream().sorted()
.map(number -> String.valueOf(number))
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/lotto/message/ExceptionMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import device.message.Message;

public enum ExceptionMessage implements Message {
LOTTO_FORM_NOT_MATCHED("[ERROR] 로또 형식이 올바르지 않습니다."),
LOTTO_SIZE_NOT_MATCHED("[ERROR] 입력한 로또 숫자 개수가 올바르지 않습니다. "),
LOTTO_NUMBER_RANGE_OUT("[ERROR] 입력한 로또 숫자가 범위를 벗어납니다."),
LOTTO_NUMBER_OVERLAPPED("[ERROR] 로또 형식이 올바르지 않습니다."),
WINNING_NUMBER_FORM_NOT_MATCHED("[ERROR] 당첨 번호 형식이 일치하지 않습니다."),
NOT_DIVIDED_PRISE("[ERROR] 구매 금액은 로또 한 장 금액으로 나누어 떨어져야 합니다.");

Expand Down
18 changes: 15 additions & 3 deletions src/main/java/lotto/setting/LottoSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import camp.nextstep.edu.missionutils.Randoms;
import lotto.lotto.Lotto;
import lotto.message.ExceptionMessage;
import java.util.List;

public enum LottoSetting {
Expand All @@ -16,8 +17,16 @@ public enum LottoSetting {
this.NUMBER_OF_DRAWS = numberOfDraws;
}

public boolean isValidNumbers(List<Integer> numbers) {
return isSizeMatched(numbers) && isNumberInRange(numbers) && isNotOverlapped(numbers);
public void validate(List<Integer> numbers) {
if (!isSizeMatched(numbers)) {
throwException(ExceptionMessage.LOTTO_SIZE_NOT_MATCHED);
}
if (!isNumberInRange(numbers)) {
throwException(ExceptionMessage.LOTTO_NUMBER_RANGE_OUT);
}
if (!isNotOverlapped(numbers)) {
throwException(ExceptionMessage.LOTTO_NUMBER_OVERLAPPED);
}
}

private boolean isSizeMatched(List<Integer> numbers) {
Expand All @@ -34,9 +43,12 @@ private boolean isNotOverlapped(List<Integer> numbers) {
return numbers.stream().distinct().count() == NUMBER_OF_DRAWS;
}

private void throwException(ExceptionMessage exceptionMessage) {
throw new IllegalArgumentException(exceptionMessage.getMessage());
}

public Lotto createAutoLotto() {
List<Integer> randomNumbers = Randoms.pickUniqueNumbersInRange(MIN_NUMBER, MAX_NUMBER, NUMBER_OF_DRAWS);
return new Lotto(randomNumbers,this);
}

}
9 changes: 4 additions & 5 deletions src/test/java/lotto/lotto/LottoUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -17,7 +19,7 @@ public class LottoUnitTest {
@Test
void throwExceptionTest_whenFormNotMatched() {
LottoSetting lottoSetting = mock(LottoSetting.class);
when(lottoSetting.isValidNumbers(anyList())).thenReturn(false);
doThrow(IllegalArgumentException.class).when(lottoSetting).validate(anyList());

assertThrows(IllegalArgumentException.class, () -> {
new Lotto(List.of(1,2,3,4,5,6), lottoSetting);
Expand All @@ -27,7 +29,7 @@ void throwExceptionTest_whenFormNotMatched() {
@Test
void notThrowExceptionTest_whenFormMatched() {
LottoSetting lottoSetting = mock(LottoSetting.class);
when(lottoSetting.isValidNumbers(anyList())).thenReturn(true);
doNothing().when(lottoSetting).validate(anyList());

assertThatCode(() -> {
new Lotto(List.of(1,2,3,4,5,6), lottoSetting);
Expand All @@ -37,7 +39,6 @@ void notThrowExceptionTest_whenFormMatched() {
@Test
void getNumbersTest() {
LottoSetting lottoSetting = mock(LottoSetting.class);
when(lottoSetting.isValidNumbers(anyList())).thenReturn(true);
Lotto lotto = new Lotto(List.of(6,5,4,3,2,1), lottoSetting);
String excepted = "[1, 2, 3, 4, 5, 6]";

Expand All @@ -49,7 +50,6 @@ void getNumbersTest() {
@Test
void isInNumberTest_expectedTrue() {
LottoSetting lottoSetting = mock(LottoSetting.class);
when(lottoSetting.isValidNumbers(anyList())).thenReturn(true);
Lotto lotto = new Lotto(List.of(6,5,4,3,2,1), lottoSetting);
boolean excepted = true;

Expand All @@ -61,7 +61,6 @@ void isInNumberTest_expectedTrue() {
@Test
void isInNumberTest_expectedFalse() {
LottoSetting lottoSetting = mock(LottoSetting.class);
when(lottoSetting.isValidNumbers(anyList())).thenReturn(true);
Lotto lotto = new Lotto(List.of(6,5,4,3,2,1), lottoSetting);
boolean excepted = false;

Expand Down
43 changes: 32 additions & 11 deletions src/test/java/lotto/setting/LottoSettingTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lotto.setting;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -12,26 +14,45 @@

public class LottoSettingTest {

@ParameterizedTest(name = "{2}")
@ArgumentsSource(ValidTestData.class)
void validTest(List<Integer> numbers, boolean expected, String testMessage) {
@ParameterizedTest(name = "validateTest: Case {index}")
@ArgumentsSource(ValidateTestNormalData.class)
void validateTest_NormalCase(List<Integer> numbers) {
LottoSetting lottoSetting = LottoSetting.NORMAL;

boolean result = lottoSetting.isValidNumbers(numbers);
assertThatCode(() -> {
lottoSetting.validate(numbers);
}).doesNotThrowAnyException();
}

static class ValidateTestNormalData implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
return Stream.of(
Arguments.of(List.of(4,8,12,16,20,24)),
Arguments.of(List.of(5,9,12,13,44,45))
);
}
}

@ParameterizedTest(name = "validateTest: Abnormal Case {1}")
@ArgumentsSource(ValidateTestAbnormalData.class)
void validateTest_AbnormalCase(List<Integer> numbers, String testMessage) {
LottoSetting lottoSetting = LottoSetting.NORMAL;

assertThat(result).isEqualTo(expected);
assertThrows(IllegalArgumentException.class, () -> {
lottoSetting.validate(numbers);
});
}

static class ValidTestData implements ArgumentsProvider {
static class ValidateTestAbnormalData implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
return Stream.of(
Arguments.of(List.of(4,8,12,16,20,24), true, "true case 1"),
Arguments.of(List.of(5,9,12,13,44,45), true, "true case 1"),
Arguments.of(List.of(5,5,12,13,14,15), false, "false case, Overlapped"),
Arguments.of(List.of(1,2,3), false, "false case, list size not matched"),
Arguments.of(List.of(1,2,3,4,5,55), false, "false case, over max number")
Arguments.of(List.of(5,5,12,13,14,15), "Overlapped"),
Arguments.of(List.of(1,2,3), "list size not matched"),
Arguments.of(List.of(1,2,3,4,5,55), "over max number")
);
}
}
Expand Down

0 comments on commit 201cfeb

Please sign in to comment.