Skip to content

Commit

Permalink
refactor : 변수이름 변경 및 팩토리 사용
Browse files Browse the repository at this point in the history
 - 알파벳 변수 이름 변경 _value -> alphabet
 - 알파벳 팩토리를 사용하도록 변경
 - 함수 위치 변경
 - 깨진 테스트 수정
  • Loading branch information
pci2676 committed Apr 15, 2022
1 parent ea3292f commit 777d6cf
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 21 deletions.
16 changes: 9 additions & 7 deletions src/main/kotlin/edu/nextstep/wordle/application/wordle/Word.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package edu.nextstep.wordle.application.wordle

import edu.nextstep.wordle.application.wordle.window.Alphabet
import edu.nextstep.wordle.application.wordle.window.AlphabetFactory
import edu.nextstep.wordle.application.wordle.window.Match
import edu.nextstep.wordle.application.wordle.window.Window
import edu.nextstep.wordle.application.wordle.window.WindowResult

data class Word(
val windows: Set<Window>,
) {
init {
if (this.windows.size != WORD_SIZE) {
throw IllegalArgumentException("${this.windows.size}: 단어의 사이즈는 ${WORD_SIZE}여야 합니다.")
}
}

fun match(input: Word): List<WindowResult> {
var results = listOf<WindowResult>()

Expand All @@ -30,18 +37,13 @@ data class Word(
return windowResult
}

init {
if (this.windows.size != WORD_SIZE) {
throw IllegalArgumentException("${this.windows.size}: 단어의 사이즈는 ${WORD_SIZE}여야 합니다.")
}
}

companion object {
private const val WORD_SIZE: Int = 5

fun create(input: String): Word {
val alphabetFactory = AlphabetFactory.instance
val windows = input.mapIndexed { index, alphabet ->
Window(alphabet = Alphabet(value = alphabet.toString()), position = index)
Window(alphabet = alphabetFactory.findBy(alphabet.toString()), position = index)
}.toSet()
return Word(windows)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data class Wordle(
private fun WordFinder.notContain(word: Word): Boolean = !this.contain(word)

private fun Word.rawWord(): String {
return windows.sortedBy { it.position }.joinToString(separator = "") { it.alphabet._value }
return windows.sortedBy { it.position }.joinToString(separator = "") { it.alphabet.alphabet }
}

fun isSuccess(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ package edu.nextstep.wordle.application.wordle.window
class Alphabet(
value: String,
) {
val _value: String = value.lowercase()
val alphabet: String = value.lowercase()

init {
if (!alphabets.contains(_value)) {
throw IllegalArgumentException("$_value 알파벳 입력만 허용합니다.")
if (!alphabets.contains(alphabet)) {
throw IllegalArgumentException("$alphabet 알파벳 입력만 허용합니다.")
}
}

companion object {
private val alphabets = ('A'..'Z').map { it.lowercase() }.toSet()
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as Alphabet

if (_value != other._value) return false
if (alphabet != other.alphabet) return false

return true
}

override fun hashCode(): Int {
return _value.hashCode()
return alphabet.hashCode()
}

companion object {
private val alphabets = ('A'..'Z').map { it.lowercase() }.toSet()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class AlphabetFactory(
}

companion object {
fun create(): AlphabetFactory {
val instance = create()

private fun create(): AlphabetFactory {
val alphabets: Set<Alphabet> = ('A'..'Z').map { Alphabet(it.lowercase()) }
.toSet()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal class WordResultsTest {
@Test
fun `결과가 없다면 실패로 간주한다`() {
//given
val result = WordResult(round = 0, windowResults = listOf())
val result = WordResults(emptyList())

//when
val success = result.isSuccess()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class WordleTest {
//then
assertThat(answer).isInstanceOf(WordleAnswer.Retry::class.java)
assertThat(answer.wordle).isEqualTo(wordle)
assertThat((answer as WordleAnswer.Retry).message).isEqualTo("사전에 없는 단어($notExist)입니다.")
assertThat((answer as WordleAnswer.Retry).message).isEqualTo("사전에 없는 단어(asdfa)입니다.")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class AlphabetFactoryTest {
@Test
fun `팩토리를 통해 알파벳을 찾을 수 있다`() {
//given
val alphabetFactory = AlphabetFactory.create()
val alphabetFactory = AlphabetFactory.instance

//when
val alphabet = alphabetFactory.findBy("a")
Expand Down

0 comments on commit 777d6cf

Please sign in to comment.