Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

닉네임 유효성 검사 #117

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/android-pull-request-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
KEYSTORE_PROPERTIES: ${{ secrets.KEYSTORE_PROPERTIES }}
LOCAL_PROPERTIES: ${{ secrets.LOCAL_PROPERTIES }}
run: |
echo "$DEBUG_KEYSTORE" | base64 -d > keystore.properties
echo "$DEBUG_KEYSTORE" | base64 -d > debug.keystore
echo "$KEYSTORE_PROPERTIES" > keystore.properties
echo "$LOCAL_PROPERTIES" > local.properties
./gradlew testDebugUnitTest --stacktrace
Expand Down
9 changes: 9 additions & 0 deletions android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ plugins {
alias(libs.plugins.kotlinx.serialization) apply false
alias(libs.plugins.navigation.safe.args) apply false
}

tasks.register<Test>("test") {
useJUnitPlatform()
reports {
junitXml.required.set(false)
}
systemProperty("gradle.build.dir", project.buildDir)
}

true // Needed to make the Suppress annotation work for the plugins block
8 changes: 8 additions & 0 deletions android/core/domain/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ plugins {
alias(libs.plugins.org.jetbrains.kotlin.jvm)
}

tasks.withType<Test>().configureEach {
useJUnitPlatform()
}

dependencies {
testImplementation(libs.junit)
testImplementation(libs.kotest.runner)
testImplementation (libs.kotest.property)
testImplementation (libs.kotest.extentions.junitxml)
api(libs.coroutines)
implementation(libs.inject)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ohdodok.catchytape.core.domain.signup

import javax.inject.Inject

enum class NicknameValidationResult {
VALID,
EMPTY,
INVALID_LENGTH,
INVALID_CHARACTER,
}

class NicknameValidationUseCase @Inject constructor() {
operator fun invoke(nickname: String): NicknameValidationResult {
val regex = "(^[ㄱ-ㅎ가-힣\\w_.]{2,10}$)".toRegex()

return when {
regex.matches(nickname) -> NicknameValidationResult.VALID
nickname.isBlank() -> NicknameValidationResult.EMPTY
nickname.length !in 2..10 -> NicknameValidationResult.INVALID_LENGTH
else -> NicknameValidationResult.INVALID_CHARACTER
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.ohdodok.catchytape.core.domain.signup

import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe

class NicknameValidationUseCaseTest : BehaviorSpec() {
private val nicknameValidationUseCase = NicknameValidationUseCase()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ViewModel을 테스트하는 거라면 viewModel 대신 sut라는 변수명을 사용하는 게 좋을 것 같아.
nicknameValidationUseCase도 원래는 sut라는 이름으로 지었지만, sut("닉네임")이라는 코드의 가독성이 떨어지는 것 같아서 usecase 이름을 그대로 사용했어


init {
given("유효한 닉네임이 주어지고") {
`when`("유효성을 검사하면") {
then("Valid를 반환한다") {
listOf("아이유", "iu", "20", "가a1_.", "특수문자_.").forEach {
nicknameValidationUseCase(nickname = it) shouldBe NicknameValidationResult.VALID
}
}
}
}

given("비어 있는 닉네임이 주어지고") {
`when`("유효성을 검사하면") {
then("Empty를 반환한다") {
nicknameValidationUseCase(nickname = "") shouldBe NicknameValidationResult.EMPTY
}
}
}

given("짧거나 긴 닉네임이 주어지고") {
`when`("유효성을 검사하면") {
then("Invalid length를 반환한다") {
listOf("한", "닉네임을이렇게길게지으면어떡해", "a").forEach {
nicknameValidationUseCase(nickname = it) shouldBe NicknameValidationResult.INVALID_LENGTH
}
}
}
}

given("사용할 수 없는 문자가 포함된 닉네임이 주어지고") {
`when`("유효성을 검사하면") {
then("Invalid length를 반환한다") {
listOf("안 돼", "특수문자^", "특수문자*").forEach {
nicknameValidationUseCase(nickname = it) shouldBe NicknameValidationResult.INVALID_CHARACTER
}
}
}
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestConfig Class가 하는 일에 대해서 설명 부탁드립니다~ ( comment, slack, 또는 대면으로 하셔도 됩니다. )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://kotest.io/docs/extensions/junit_xml.html
이 부분을 적용하기 위한 코드인데, 실제로 적용에 성공하진 못 했어요 ㅠㅠ

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ohdodok.catchytape.core.domain.signup

import io.kotest.core.config.AbstractProjectConfig
import io.kotest.core.extensions.Extension
import io.kotest.extensions.junitxml.JunitXmlReporter

class TestConfig : AbstractProjectConfig() {

override fun extensions(): List<Extension> = listOf(
JunitXmlReporter(
includeContainers = false, // don't write out status for all tests
useTestPathAsName = true, // use the full test path (ie, includes parent test names)
outputDir = "test-results/excludeContainers"
)
)
}
4 changes: 4 additions & 0 deletions android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ javaInject = "1"
gms = "20.7.0"

junit = "4.13.2"
kotest = "5.8.0"

retrofit = "2.9.0"
okhttp = "4.11.0"
Expand Down Expand Up @@ -41,6 +42,9 @@ inject = { group = "javax.inject", name = "javax.inject", version.ref = "javaInj

google-play-services = { group = "com.google.android.gms", name = "play-services-auth", version.ref = "gms" }

kotest-runner = { group = "io.kotest", name = "kotest-runner-junit5", version.ref = "kotest"}
kotest-property = { group = "io.kotest", name = "kotest-property", version.ref = "kotest"}
kotest-extentions-junitxml = { group = "io.kotest", name = "kotest-extensions-junitxml", version.ref = "kotest"}
junit = { group = "junit", name = "junit", version.ref = "junit" }

retrofit = { module = "com.squareup.retrofit2:retrofit", name = "retrofit", version.ref = "retrofit" }
Expand Down