Skip to content

Commit 189d0eb

Browse files
committed
Added alphabet only validator
1 parent 0d4029e commit 189d0eb

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package co.kyash.vtl.validators
2+
3+
import android.content.Context
4+
import android.text.TextUtils
5+
import co.kyash.vtl.VtlValidationFailureException
6+
import io.reactivex.Completable
7+
import io.reactivex.schedulers.Schedulers
8+
import java.util.regex.Pattern
9+
10+
/**
11+
* Validation error when the text is not written by Alphabet
12+
*/
13+
class AlphabetOnlyValidator(
14+
private val errorMessage: String
15+
) : VtlValidator {
16+
17+
companion object {
18+
private val PATTERN = Pattern.compile("^[a-zA-Z]+\$")
19+
}
20+
21+
override fun validateAsCompletable(context: Context, text: String?): Completable {
22+
return Completable.fromRunnable {
23+
if (!validate(text)) {
24+
throw VtlValidationFailureException(errorMessage)
25+
}
26+
}.subscribeOn(Schedulers.computation())
27+
}
28+
29+
override fun validate(text: String?): Boolean {
30+
val trimText = text?.replace(" ", "")?.replace(" ", "")?.trim()
31+
return TextUtils.isEmpty(trimText) || PATTERN.matcher(trimText).matches()
32+
}
33+
34+
override fun getErrorMessage(): String {
35+
return errorMessage
36+
}
37+
38+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package co.kyash.vtl.validators
2+
3+
import android.content.Context
4+
import co.kyash.vtl.testing.RxImmediateSchedulerRule
5+
import junit.framework.Assert.assertEquals
6+
import org.junit.Before
7+
import org.junit.Rule
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
import org.robolectric.ParameterizedRobolectricTestRunner
11+
import org.robolectric.RuntimeEnvironment
12+
13+
@Suppress("unused")
14+
@RunWith(ParameterizedRobolectricTestRunner::class)
15+
class AlphabetOnlyValidatorTest(
16+
private val text: String?,
17+
private val result: Boolean,
18+
private val errorMessage: String?
19+
) {
20+
21+
companion object {
22+
private const val ERROR_MESSAGE = "This contains non-hiragana characters"
23+
24+
@JvmStatic
25+
@ParameterizedRobolectricTestRunner.Parameters
26+
fun data(): List<Array<out Any?>> {
27+
return listOf(
28+
arrayOf("", false, ERROR_MESSAGE),
29+
arrayOf("", false, ERROR_MESSAGE),
30+
arrayOf("", false, ERROR_MESSAGE),
31+
arrayOf("", false, ERROR_MESSAGE),
32+
arrayOf("****です", false, ERROR_MESSAGE),
33+
arrayOf("-", false, ERROR_MESSAGE),
34+
arrayOf("@", false, ERROR_MESSAGE),
35+
arrayOf("*", false, ERROR_MESSAGE),
36+
arrayOf("1", false, ERROR_MESSAGE),
37+
arrayOf(null, true, null),
38+
arrayOf("", true, null),
39+
arrayOf(" ", true, null),
40+
arrayOf(" ", true, null),
41+
arrayOf("a", true, null),
42+
arrayOf("A", true, null),
43+
arrayOf("YUSUKE KONISHI", true, null)
44+
)
45+
}
46+
}
47+
48+
@get:Rule
49+
val rxImmediateSchedulerRule = RxImmediateSchedulerRule()
50+
51+
private lateinit var subject: VtlValidator
52+
53+
private val context: Context = RuntimeEnvironment.application
54+
55+
@Before
56+
@Throws(Exception::class)
57+
fun setUp() {
58+
subject = AlphabetOnlyValidator(ERROR_MESSAGE)
59+
}
60+
61+
@Test
62+
fun validate() {
63+
assertEquals(result, subject.validate(text))
64+
}
65+
66+
@Test
67+
fun validateAsCompletable() {
68+
if (errorMessage == null) {
69+
subject.validateAsCompletable(context, text).test().assertNoErrors().assertComplete()
70+
} else {
71+
subject.validateAsCompletable(context, text).test().assertErrorMessage(errorMessage)
72+
}
73+
}
74+
75+
}

library/src/test/java/co/kyash/vtl/validators/AsciiOnlyValidatorTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class AsciiOnlyValidatorTest(
1919
) {
2020

2121
companion object {
22-
private val ERROR_MESSAGE = "This contains non-hiragana characters"
22+
private const val ERROR_MESSAGE = "This contains non-hiragana characters"
2323

2424
@JvmStatic
2525
@ParameterizedRobolectricTestRunner.Parameters
@@ -32,6 +32,7 @@ class AsciiOnlyValidatorTest(
3232
arrayOf("****です", false, ERROR_MESSAGE),
3333
arrayOf(null, true, null),
3434
arrayOf("", true, null),
35+
arrayOf(" ", true, null),
3536
arrayOf("a", true, null),
3637
arrayOf("-", true, null),
3738
arrayOf("@", true, null),

0 commit comments

Comments
 (0)