Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package co.kyash.vtl.validators

import android.content.Context
import co.kyash.vtl.VtlValidationFailureException
import io.reactivex.Completable
import io.reactivex.schedulers.Schedulers

/**
* Validation error when the text length is shorter
*/
class MinLengthValidator(
private val errorMessage: String,
private val minLength: Int,
private val trim: Boolean = true
) : VtlValidator {

/**
* Validate and return completable
*
* @param context
* @param text
* @return Completable
* @throws Exception which contains the error message
*/
override fun validateAsCompletable(context: Context, text: String?): Completable {
return Completable.fromRunnable {
if (!validate(text)) {
throw VtlValidationFailureException(errorMessage)
}
}.subscribeOn(Schedulers.computation())
}

/**
* Validate immediately
*
* @param text
* @return result
*/
override fun validate(text: String?): Boolean {
return text?.let {
if (trim) it.trim() else it
}?.length ?: 0 >= minLength
}

/**
* @return error message
*/
override fun getErrorMessage(): String {
return errorMessage
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import io.reactivex.schedulers.Schedulers
* Validation error when the text is empty.
*/
class RequiredValidator(
private val errorMessage: String
private val errorMessage: String,
private val trim: Boolean = true
) : VtlValidator {

/**
Expand All @@ -36,7 +37,7 @@ class RequiredValidator(
* @return result
*/
override fun validate(text: String?): Boolean {
return !TextUtils.isEmpty(text)
return !TextUtils.isEmpty(text?.let { if (trim) it.trim() else it })
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package co.kyash.vtl.validators

import android.content.Context
import co.kyash.vtl.testing.RxImmediateSchedulerRule
import junit.framework.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.ParameterizedRobolectricTestRunner
import org.robolectric.RuntimeEnvironment

@Suppress("unused")
@RunWith(ParameterizedRobolectricTestRunner::class)
class MinLengthValidatorTest(
private val text: String?,
private val trim: Boolean,
private val result: Boolean,
private val errorMessage: String?
) {

companion object {
private val MIN_LENGTH = 5
private val ERROR_MESSAGE = "This field has error"

@JvmStatic
@ParameterizedRobolectricTestRunner.Parameters
fun data(): List<Array<out Any?>> {
return listOf(
// Failure
arrayOf(null, true, false, ERROR_MESSAGE),
arrayOf("", true, false, ERROR_MESSAGE),
arrayOf(" ", true, false, ERROR_MESSAGE),
arrayOf("abcd", true, false, ERROR_MESSAGE),

// Success
arrayOf(" ", false, true, null),
arrayOf("abcde", true, true, null),
arrayOf("abcdef", true, true, null)
)
}
}

@get:Rule
val rxImmediateSchedulerRule = RxImmediateSchedulerRule()

private lateinit var subject: VtlValidator

private val context: Context = RuntimeEnvironment.application

@Before
@Throws(Exception::class)
fun setUp() {
subject = MinLengthValidator(ERROR_MESSAGE, MIN_LENGTH, trim)
}

@Test
fun validate() {
assertEquals(result, subject.validate(text))
}

@Test
fun validateAsCompletable() {
if (errorMessage == null) {
subject.validateAsCompletable(context, text).test().assertNoErrors().assertComplete()
} else {
subject.validateAsCompletable(context, text).test().assertErrorMessage(errorMessage)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.robolectric.RuntimeEnvironment
@RunWith(ParameterizedRobolectricTestRunner::class)
class RequiredValidatorTest(
private val text: String?,
private val trim: Boolean,
private val result: Boolean,
private val errorMessage: String?
) {
Expand All @@ -25,9 +26,16 @@ class RequiredValidatorTest(
@ParameterizedRobolectricTestRunner.Parameters
fun data(): List<Array<out Any?>> {
return listOf(
arrayOf(null, false, ERROR_MESSAGE),
arrayOf("", false, ERROR_MESSAGE),
arrayOf("konifar", true, null)
// Failure
arrayOf(null, true, false, ERROR_MESSAGE),
arrayOf("", true, false, ERROR_MESSAGE),
arrayOf(" ", true, false, ERROR_MESSAGE),
arrayOf(" ", true, false, ERROR_MESSAGE),

// Success
arrayOf(" ", false, true, null),
arrayOf(" ", false, true, null),
arrayOf("konifar", true, true, null)
)
}
}
Expand All @@ -42,7 +50,7 @@ class RequiredValidatorTest(
@Before
@Throws(Exception::class)
fun setUp() {
subject = RequiredValidator(ERROR_MESSAGE)
subject = RequiredValidator(ERROR_MESSAGE, trim)
}

@Test
Expand Down