-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package racingcar.feedback | ||
|
||
// 상수를 최상위 수준과 동반 객체 중 무엇으로 정의하느냐는 상수를 사용하는 클래스의 범위에 따라 다르다! | ||
// 만약에 상수를 최상위 수준에서 선언하면 코틀린은 CarKt 이라는 클래스 파일을 생성하여 모아둔다. | ||
// private const val ... | ||
|
||
object Numbers { | ||
// 최상위 수준으로 선언했을 때의 단점은 개발자가 직접 상수 이름을 알아야 한다는 것이다. | ||
// 그래서 object 안에 상수를 선언해서 사용하기도 한다. | ||
const val MAXIMUM_NAME_LENGTH: Int = 5 | ||
} | ||
|
||
class Car(val name: String, position: Int = DEFAULT_POSITION) { | ||
var position: Int = position | ||
private set // 커스텀 게터와 세터는 생성자에서 바로 사용할 수 없다! | ||
/* | ||
private var _position: Int = position | ||
val position: Int | ||
get() = _position | ||
*/ | ||
|
||
init { | ||
require(name.length <= Numbers.MAXIMUM_NAME_LENGTH) { "자동차 이름은 5글자를 넘길 수 없습니다." } | ||
} | ||
|
||
fun move() { | ||
position++ | ||
} | ||
|
||
companion object { | ||
private const val DEFAULT_POSITION: Int = 0 | ||
|
||
@JvmField | ||
val DEFAULT_CAR: Car = Car("") | ||
fun of(name: String): Car = Car(name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@file:JvmName("NumberUtils") | ||
|
||
package racingcar.feedback | ||
|
||
const val FORWARD_NUMBER: Int = 4 | ||
|
||
fun calculate(text: String?): Int { | ||
require(!text.isNullOrBlank()) { "입력값이 null 또는 빈 문자열일 수 없습니다." } | ||
run(!text.isNullOrBlank()) { "예외가 발생했습니다." } // 커스텀 예외에 대해서 직접 만들어 써보자! | ||
// ... | ||
return 0 | ||
} | ||
|
||
fun run(value: Boolean, lazyMessage: () -> Any) { | ||
if (!value) { | ||
val message = lazyMessage() | ||
throw RuntimeException(message.toString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package racingcar.feedback | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.assertThatExceptionOfType | ||
import org.junit.jupiter.api.Test | ||
|
||
class CarTest { | ||
@Test | ||
fun `자동차를 생성한다`() { | ||
val car = Car("jason", 0) | ||
assertThat(car.name).isEqualTo("jason") | ||
assertThat(car.position).isEqualTo(0) | ||
} | ||
|
||
@Test | ||
fun `기본 인자를 활용하여 자동차를 생성한다`() { | ||
val car = Car("jason") | ||
assertThat(car.name).isEqualTo("jason") | ||
assertThat(car.position).isEqualTo(0) | ||
} | ||
|
||
@Test | ||
fun `자동차의 이름이 5글자를 초과하면 오류를 발생시킨다`() { | ||
assertThatExceptionOfType(IllegalArgumentException::class.java).isThrownBy { | ||
Car("가나다라마바사") | ||
} | ||
} | ||
|
||
@Test | ||
fun `팩토리 함수를 이용하여 자동차를 생성한다`() { | ||
val car = Car.of("jason") | ||
assertThat(car.name).isEqualTo("jason") | ||
assertThat(car.position).isEqualTo(0) | ||
} | ||
|
||
@Test | ||
fun `정적 변수를 사용한다`() { | ||
val car = Car.DEFAULT_CAR | ||
assertThat(car.name).isEqualTo("") | ||
assertThat(car.position).isEqualTo(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package racingcar.feedback; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class NumberUtilsTest { | ||
@Test | ||
void 코틀린상수() { | ||
assertThat(NumberUtils.FORWARD_NUMBER).isEqualTo(4); | ||
Car defaultCar = Car.DEFAULT_CAR; | ||
} | ||
} |