Skip to content

Commit

Permalink
Fix oppia#1884: Thoroughly test TextInputCaseSensitiveEqualsRuleClass…
Browse files Browse the repository at this point in the history
…ifierProvider (oppia#1961)

* Added text cases for TextInputCaseSensitiveEqualsRuleClassifierProvider

* Resolved failed lint tests due to naming convention, file end line rule and wrong file name

* Modified TextInputCaseSensitiveEqualsRuleClassifierProviderTest

- Removed TODO statements
- Replaced Truth import with Truth.assertThat
- Replaced STRING_VALUE with STRING_VALUE_RANDOM
- Removed ktlint-disabling comments

* Removed TODO from TextInputCaseSensitiveEqualsRuleClassifierProvider class
  • Loading branch information
alokbharti authored Oct 9, 2020
1 parent a9cf445 commit cb27a1c
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ internal class TextInputCaseSensitiveEqualsRuleClassifierProvider @Inject constr
)
}

// TODO(#210): Add tests for this classifier.
override fun matches(answer: String, input: String): Boolean {
return answer.normalizeWhitespace() == input.normalizeWhitespace()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package org.oppia.android.domain.classify.rules.textinput

import android.app.Application
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import dagger.BindsInstance
import dagger.Component
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.oppia.android.app.model.InteractionObject
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.reflect.KClass
import kotlin.reflect.full.cast
import kotlin.test.fail

/** Tests for [TextInputCaseSensitiveEqualsRuleClassifierProvider]. */
@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
@Config(manifest = Config.NONE)
class TextInputCaseSensitiveEqualsRuleClassifierProviderTest {
private val STRING_VALUE_TEST_UPPERCASE = createString(value = "TEST")
private val STRING_VALUE_TEST_LOWERCASE = createString(value = "test")
private val STRING_VALUE_RANDOM = createString(value = "random")

@Inject
internal lateinit var textInputCaseSensitiveEqualsRuleClassifierProvider:
TextInputCaseSensitiveEqualsRuleClassifierProvider

private val inputCaseSensitiveEqualsRuleClassifier by lazy {
textInputCaseSensitiveEqualsRuleClassifierProvider.createRuleClassifier()
}

@Before
fun setUp() {
setUpTestApplicationComponent()
}

@Test
fun testUppercaseStringAnswer_uppercaseStringInput_sameExactString_bothValuesMatch() {
val inputs = mapOf("x" to STRING_VALUE_TEST_UPPERCASE)

val matches =
inputCaseSensitiveEqualsRuleClassifier.matches(
answer = STRING_VALUE_TEST_UPPERCASE,
inputs = inputs
)

assertThat(matches).isTrue()
}

@Test
fun testUppercaseStringAnswer_lowercaseStringInput_sameExactString_bothValuesDoNotMatch() {
val inputs = mapOf("x" to STRING_VALUE_TEST_LOWERCASE)

val matches =
inputCaseSensitiveEqualsRuleClassifier.matches(
answer = STRING_VALUE_TEST_UPPERCASE,
inputs = inputs
)

assertThat(matches).isFalse()
}

@Test
fun testUppercaseStringAnswer_stringRandomInput_differentString_bothValuesDoNotMatch() {
val inputs = mapOf("x" to STRING_VALUE_RANDOM)

val matches =
inputCaseSensitiveEqualsRuleClassifier.matches(
answer = STRING_VALUE_TEST_UPPERCASE,
inputs = inputs
)

assertThat(matches).isFalse()
}

@Test
fun testStringAnswer_missingInput_throwsException() {
val inputs = mapOf("y" to STRING_VALUE_TEST_LOWERCASE)

val exception = assertThrows(IllegalStateException::class) {
inputCaseSensitiveEqualsRuleClassifier.matches(
answer = STRING_VALUE_TEST_LOWERCASE,
inputs = inputs
)
}

assertThat(exception)
.hasMessageThat()
.contains("Expected classifier inputs to contain parameter with name 'x'")
}

private fun setUpTestApplicationComponent() {
DaggerTextInputCaseSensitiveEqualsRuleClassifierProviderTest_TestApplicationComponent.builder()
.setApplication(ApplicationProvider.getApplicationContext())
.build()
.inject(this)
}

private fun createString(value: String): InteractionObject {
return InteractionObject.newBuilder().setNormalizedString(value).build()
}

// TODO(#89): Move to a common test library.
private fun <T : Throwable> assertThrows(type: KClass<T>, operation: () -> Unit): T {
try {
operation()
fail("Expected to encounter exception of $type")
} catch (t: Throwable) {
if (type.isInstance(t)) {
return type.cast(t)
}
// Unexpected exception; throw it.
throw t
}
}

// TODO(#89): Move this to a common test application component.
@Singleton
@Component(modules = [])
interface TestApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun setApplication(application: Application): Builder

fun build(): TestApplicationComponent
}

fun inject(test: TextInputCaseSensitiveEqualsRuleClassifierProviderTest)
}
}

0 comments on commit cb27a1c

Please sign in to comment.