Skip to content

Commit

Permalink
refactor: 당첨번호 예외 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
SANDY-9 committed Nov 6, 2024
1 parent 67c83ca commit ec43d16
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
12 changes: 12 additions & 0 deletions src/main/kotlin/lotto/constants/ErrorMessage.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lotto.constants

import lotto.utils.InputManager
import lotto.utils.LottoGenerator

object ErrorMessage {
Expand All @@ -17,4 +18,15 @@ object ErrorMessage {
" 사이의 정수만 가능합니다."
const val LOTTO_NUMBER_OVERLAP = "[ERROR] 로또 번호는 중복될 수 없습니다."

// WinningNumbers
const val WINNING_NUMBER_NOT_ONE_SET_SIZE = "[ERROR] 당첨 번호는 ${LottoGenerator.LOTTO_ONE_SET_SIZE}개여야 합니다."
const val WINNING_NUMBER_NOT_IN_RANGE = "[ERROR] 당첨 번호는 " +
"${LottoGenerator.LOTTO_START_INCLUSIVE_NUMBER}~${LottoGenerator.LOTTO_END_INCLUSIVE_NUMBER}" +
" 사이의 정수만 가능합니다."
const val WINNING_NUMBER_OVERLAP = "[ERROR] 당첨 번호는 중복될 수 없습니다."
const val WINNING_NUMBER_INVALID_FORMAT = "[ERROR] 당첨 번호는 공백 없이" +
"${LottoGenerator.LOTTO_START_INCLUSIVE_NUMBER}~${LottoGenerator.LOTTO_END_INCLUSIVE_NUMBER}" +
" 사이의 정수만 ${InputManager.INPUT_WINNING_NUMBERS_SEPARATOR} 구분으로" +
" ${LottoGenerator.LOTTO_ONE_SET_SIZE}개 입력 가능합니다."

}
4 changes: 2 additions & 2 deletions src/main/kotlin/lotto/utils/InputManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ object InputManager {
}
}

private const val INPUT_WINNING_NUMBERS_SEPARATOR = ","
const val INPUT_WINNING_NUMBERS_SEPARATOR = ","
private fun String.toWinningNumbers(): List<Int> {
ValidationUtils.checkValidWinningNumbers(this, INPUT_WINNING_NUMBERS_SEPARATOR)
ValidationUtils.checkValidWinningNumbers(this)
return split(INPUT_WINNING_NUMBERS_SEPARATOR).map {
it.toInt()
}
Expand Down
26 changes: 12 additions & 14 deletions src/main/kotlin/lotto/utils/ValidationUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,18 @@ object ValidationUtils {
}
}

fun checkValidWinningNumbers(numbers: String, separator: String) {
try {
val validNumbers = numbers.split(separator).map { it.toInt() }
require(validNumbers.size == 6 ) {
"[ERROR] 당첨 번호는 6개여야 합니다."
}
require(validNumbers.all { it in 1..45 }) {
"[ERROR] 당첨 번호는 1~45 사이의 정수만 입력 가능합니다."
}
require(validNumbers.size == validNumbers.toSet().size) {
"[ERROR] 당첨 번호는 중복될 수 없습니다."
}
} catch (e: Exception) {
throw IllegalArgumentException("[ERROR] 당첨 번호는 공백없이 1~45 이내의 양의 정수만 공백 없이 $separator 구분으로 6개 입력 가능합니다.")
fun checkValidWinningNumbers(numbers: String) {
val validNumbers = numbers.split(InputManager.INPUT_WINNING_NUMBERS_SEPARATOR).map {
it.toIntOrNull() ?: throw IllegalArgumentException(ErrorMessage.WINNING_NUMBER_INVALID_FORMAT)
}
require(validNumbers.size == LottoGenerator.LOTTO_ONE_SET_SIZE ) {
ErrorMessage.WINNING_NUMBER_NOT_ONE_SET_SIZE
}
require(validNumbers.all { it in LottoGenerator.LOTTO_START_INCLUSIVE_NUMBER..LottoGenerator.LOTTO_END_INCLUSIVE_NUMBER }) {
ErrorMessage.WINNING_NUMBER_NOT_IN_RANGE
}
require(validNumbers.size == validNumbers.toSet().size) {
ErrorMessage.WINNING_NUMBER_OVERLAP
}
}

Expand Down
27 changes: 16 additions & 11 deletions src/test/kotlin/lotto/input/InputWinningNumberValidationTest.kt
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
package lotto.input

import camp.nextstep.edu.missionutils.Console
import camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest
import camp.nextstep.edu.missionutils.test.NsTest
import lotto.constants.ErrorMessage
import lotto.utils.ValidationUtils
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

class InputWinningNumberValidationTest : NsTest() {

@Test
fun `예외 테스트-당첨번호가 "숫자,"가 아닌 다른 문자열이 섞여 있을 때`() {
assertSimpleTest {
assertThrows<IllegalArgumentException> { runException("1,2,3,7,선미,샌디") }
fun `예외 테스트-당첨번호가 '숫자,'가 아닌 다른 문자열이 섞여 있을 때`() {
val exception = assertThrows<IllegalArgumentException> {
runException("1,2,3,7,선미,샌디")
}
assertEquals(ErrorMessage.WINNING_NUMBER_INVALID_FORMAT, exception.message)
}

@Test
fun `예외 테스트-당첨번호의 개수가 6개가 아닐 때`() {
assertSimpleTest {
assertThrows<IllegalArgumentException> { runException("1,2,3,4,5") }
val exception = assertThrows<IllegalArgumentException> {
runException("1,2,3,4,5")
}
assertEquals(ErrorMessage.WINNING_NUMBER_NOT_ONE_SET_SIZE, exception.message)
}

@Test
fun `예외 테스트-당첨번호에 1~45 사이가 아닌 정수가 있을 때`() {
assertSimpleTest {
assertThrows<IllegalArgumentException> { runException("1,2,3,4,5,100") }
val exception = assertThrows<IllegalArgumentException> {
runException("1,2,3,4,5,100")
}
assertEquals(ErrorMessage.WINNING_NUMBER_NOT_IN_RANGE, exception.message)
}

@Test
fun `예외 테스트-당첨번호에 중복된 숫자가 있을 때`() {
assertSimpleTest {
assertThrows<IllegalArgumentException> { runException("20000000") }
val exception = assertThrows<IllegalArgumentException> {
runException("1,5,5,6,7,8")
}
assertEquals(ErrorMessage.WINNING_NUMBER_OVERLAP, exception.message)
}

override fun runMain() {
val number = Console.readLine()
ValidationUtils.checkValidWinningNumbers(number, ",")
ValidationUtils.checkValidWinningNumbers(number)
Console.close()
}
}

0 comments on commit ec43d16

Please sign in to comment.