From d1619f38fa770208699ef652064527870dcb88af Mon Sep 17 00:00:00 2001 From: kimbacksul Date: Tue, 6 Jun 2023 23:05:11 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20=EC=BD=94=ED=8B=80?= =?UTF-8?q?=EB=A6=B0=20=EA=B8=B0=EC=B4=88,=20=EB=8B=A8=EC=9C=84=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EA=B8=B0=EC=B4=88=20=EC=8B=A4?= =?UTF-8?q?=EC=8A=B5=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/Person.kt | 29 +++++++++++++++++++++++++++++ src/test/kotlin/PersonTest.kt | 33 +++++++++++++++++++++++++++++++++ src/test/kotlin/StringTest.kt | 14 ++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/test/kotlin/Person.kt create mode 100644 src/test/kotlin/PersonTest.kt create mode 100644 src/test/kotlin/StringTest.kt diff --git a/src/test/kotlin/Person.kt b/src/test/kotlin/Person.kt new file mode 100644 index 0000000000..227b2976c7 --- /dev/null +++ b/src/test/kotlin/Person.kt @@ -0,0 +1,29 @@ +package study + +data class Person( + val name: String, + val age: Int, + var nickname: String? = "Guest" +) { +// constructor(name: String, age: Int): this(name, age, "guest") + +// override fun equals(other: Any?): Boolean { +// if (this === other) return true +// if (javaClass != other?.javaClass) return false +// +// other as Person +// +// if (name != other.name) return false +// if (age != other.age) return false +// if (nickname != other.nickname) return false +// +// return true +// } +// +// override fun hashCode(): Int { +// var result = name.hashCode() +// result = 31 * result + age +// result = 31 * result + (nickname?.hashCode() ?: 0) +// return result +// } +} diff --git a/src/test/kotlin/PersonTest.kt b/src/test/kotlin/PersonTest.kt new file mode 100644 index 0000000000..f4b6af6b6f --- /dev/null +++ b/src/test/kotlin/PersonTest.kt @@ -0,0 +1,33 @@ +package study + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class PersonTest { + @Test + internal fun `이름 붙인 인자`() { + val actual = Person(name = "오한결", nickname = "kimbacksul", age = 20) + assertThat(actual.name).isEqualTo("오한결") + assertThat(actual.nickname).isEqualTo("kimbacksul") + assertThat(actual.age).isEqualTo(20) + } + + @Test + internal fun `널 타입`() { + val actual = Person("홍길동", 20, null) + assertThat(actual.nickname).isNull() + } + + @Test + internal fun `기본 인자`() { + val actual = Person(name = "홍길동", age = 20) + assertThat(actual.nickname).isEqualTo("Guest") + } + + @Test + internal fun `데이터 클래스`() { + val Person1 = Person("홍길동", 20, "홍") + val Person2 = Person("홍길동", 20, "홍") + assertThat(Person1).isEqualTo(Person2) + } +} diff --git a/src/test/kotlin/StringTest.kt b/src/test/kotlin/StringTest.kt new file mode 100644 index 0000000000..4efe604441 --- /dev/null +++ b/src/test/kotlin/StringTest.kt @@ -0,0 +1,14 @@ +package study + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class StringTest { + + @Test + fun isEmpty() { + assertThat("".isEmpty()).isTrue() + assertThat(" ".isEmpty()).isFalse() + assertThat("a".isEmpty()).isFalse() + } +} From f1c70dc9bba8a48afc96e63620a448de89896733 Mon Sep 17 00:00:00 2001 From: kimbacksul Date: Mon, 12 Jun 2023 00:40:17 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=EC=88=98=EC=A0=95=20:=20setp=201=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=9C=84=EC=B9=98=20=EB=B3=80=EA=B2=BD=20?= =?UTF-8?q?=EB=B0=8F=20=ED=95=84=EC=9A=94=20=EC=97=86=EB=8A=94=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=A3=BC=EC=84=9D=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/Person.kt | 29 ----------------------------- src/test/kotlin/PersonTest.kt | 1 + 2 files changed, 1 insertion(+), 29 deletions(-) delete mode 100644 src/test/kotlin/Person.kt diff --git a/src/test/kotlin/Person.kt b/src/test/kotlin/Person.kt deleted file mode 100644 index 227b2976c7..0000000000 --- a/src/test/kotlin/Person.kt +++ /dev/null @@ -1,29 +0,0 @@ -package study - -data class Person( - val name: String, - val age: Int, - var nickname: String? = "Guest" -) { -// constructor(name: String, age: Int): this(name, age, "guest") - -// override fun equals(other: Any?): Boolean { -// if (this === other) return true -// if (javaClass != other?.javaClass) return false -// -// other as Person -// -// if (name != other.name) return false -// if (age != other.age) return false -// if (nickname != other.nickname) return false -// -// return true -// } -// -// override fun hashCode(): Int { -// var result = name.hashCode() -// result = 31 * result + age -// result = 31 * result + (nickname?.hashCode() ?: 0) -// return result -// } -} diff --git a/src/test/kotlin/PersonTest.kt b/src/test/kotlin/PersonTest.kt index f4b6af6b6f..bc029ed101 100644 --- a/src/test/kotlin/PersonTest.kt +++ b/src/test/kotlin/PersonTest.kt @@ -1,5 +1,6 @@ package study +import Person import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test From c264520c443b5c1ad843127878c0d90363a329d6 Mon Sep 17 00:00:00 2001 From: kimbacksul Date: Mon, 12 Jun 2023 00:41:32 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=EC=88=98=EC=A0=95=20:=20setp=201=20?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9C=84=EC=B9=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/Person.kt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/kotlin/Person.kt diff --git a/src/main/kotlin/Person.kt b/src/main/kotlin/Person.kt new file mode 100644 index 0000000000..dc028dffc2 --- /dev/null +++ b/src/main/kotlin/Person.kt @@ -0,0 +1,5 @@ +data class Person( + val name: String, + val age: Int, + var nickname: String? = "Guest" +) From 89b030b800f906ba6f7ec35641ce0acf32739475 Mon Sep 17 00:00:00 2001 From: kimbacksul Date: Mon, 12 Jun 2023 00:41:58 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=EC=B6=94=EA=B0=80=20:=20step=202=20?= =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=20=EA=B3=84=EC=82=B0=EA=B8=B0=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/Calculation.kt | 40 +++++++++++++++++++++++++++ src/test/kotlin/CalcutationTest.kt | 44 ++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/main/kotlin/Calculation.kt create mode 100644 src/test/kotlin/CalcutationTest.kt diff --git a/src/main/kotlin/Calculation.kt b/src/main/kotlin/Calculation.kt new file mode 100644 index 0000000000..e59bd50ce9 --- /dev/null +++ b/src/main/kotlin/Calculation.kt @@ -0,0 +1,40 @@ +class Calculation { + + fun calcutateByString(input: String?): Int { + if(input.isNullOrBlank()) { + throw IllegalArgumentException() + } + val splitedInput = input.split(" ") + var result: Int = splitedInput.first().toInt() + splitedInput.drop(1).windowed(2, 2).forEach { + result = calculate(result, it[1].toInt(), it[0]) + } + return result + } + + private fun calculate(first: Int, second: Int, operator: String): Int { + return when(operator) { + "+" -> add(first, second) + "-" -> subtract(first, second) + "*" -> multiply(first, second) + "/" -> divide(first, second) + else -> throw IllegalArgumentException("Using unsupported operators") + } + } + + private fun add(first: Int, second: Int): Int { + return first + second + } + + private fun subtract(first: Int, second: Int): Int { + return first - second + } + + private fun multiply(first: Int, second: Int): Int { + return first * second + } + + private fun divide(first: Int, second: Int): Int { + return first / second + } +} \ No newline at end of file diff --git a/src/test/kotlin/CalcutationTest.kt b/src/test/kotlin/CalcutationTest.kt new file mode 100644 index 0000000000..c9d007d3ea --- /dev/null +++ b/src/test/kotlin/CalcutationTest.kt @@ -0,0 +1,44 @@ +package study + +import Calculation +import org.assertj.core.api.Assertions +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test + +class CalcutationTest { + + @Test + fun `예제 확인`() { + assertThat(Calculation().calcutateByString("2 + 3 * 4 / 2")) + .isEqualTo(10) + } + + @Test + fun `입력값이 Null`() { + assertThatThrownBy { + Calculation().calcutateByString(null) + }.isInstanceOf(IllegalArgumentException::class.java) + } + + @Test + fun `연산자 다를때`() { + assertThatThrownBy { + Calculation().calcutateByString("2 + 3 * 4 % 2") + }.isInstanceOf(IllegalArgumentException::class.java) + } + + @Test + fun `문자열 처음 슷자가 아닐 때`() { + assertThatThrownBy { + Calculation().calcutateByString("# + 3 * 4 / 2") + }.isInstanceOf(IllegalArgumentException::class.java) + } + + @Test + fun `문자열 마지막이 숫자가 아닐 때`() { + assertThatThrownBy { + Calculation().calcutateByString("1 + 3 * 4 / #") + }.isInstanceOf(IllegalArgumentException::class.java) + } +} \ No newline at end of file