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

Fixes #1896: Thoroughly test ItemSelectionInputContainsAtLeastOneOfRuleClassifier #2073

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ internal class ItemSelectionInputContainsAtLeastOneOfRuleClassifierProvider @Inj
)
}

// TODO(#210): Add tests for this classifier.
override fun matches(answer: StringList, input: StringList): Boolean {
return answer.htmlList.toSet().intersect(input.htmlList).isNotEmpty()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.oppia.android.domain.classify.rules.itemselectioninput

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.oppia.android.app.model.StringList
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import javax.inject.Inject
import javax.inject.Singleton

/** Tests for [ItemSelectionInputContainsAtLeastOneOfRuleClassifierProvider]. */
@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
@Config(manifest = Config.NONE)

anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
class ItemSelectionInputContainsAtLeastOneOfRuleClassifierProviderTest {
private val ITEM_SELECTION_SET_5 = createStringList(
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
StringList.newBuilder()
.addHtml("test1")
.addHtml("test2")
.addHtml("test3")
.addHtml("test4")
.addHtml("test5")
.build()
)

private val ITEM_SELECTION_SET_SUBSET = createStringList(
anandwana001 marked this conversation as resolved.
Show resolved Hide resolved
StringList.newBuilder().addHtml("test1")
.build()
)

private val ITEM_SELECTION_SET_ONE_ELEMENT_PRESENT = createStringList(
StringList.newBuilder().addHtml("test1").addHtml("test6")
.build()
)

private val ITEM_SELECTION_SET_EXCLUSIVE = createStringList(
StringList.newBuilder().addHtml("test6")
.build()
)

navneetsaluja marked this conversation as resolved.
Show resolved Hide resolved
@Inject
internal lateinit var itemSelectionInputContainsAtLeastOneOfRuleClassifierProvider:
ItemSelectionInputContainsAtLeastOneOfRuleClassifierProvider

private val inputContainsAtLeastOneOfRuleClassifier by lazy {
itemSelectionInputContainsAtLeastOneOfRuleClassifierProvider.createRuleClassifier()
}

@Before
fun setUp() {
setUpTestApplicationComponent()
}

navneetsaluja marked this conversation as resolved.
Show resolved Hide resolved
@Test
fun testItemSet_setAnswer_inputIsASubset_answerContainsInput() {
val inputs = mapOf("x" to ITEM_SELECTION_SET_SUBSET)

val matches = inputContainsAtLeastOneOfRuleClassifier.matches(
answer = ITEM_SELECTION_SET_5,
inputs = inputs
)

assertThat(matches).isTrue()
}

@Test
fun testItemSet_setAnswer_inputHasOneElementInSet_answerContainsInput() {
val inputs = mapOf("x" to ITEM_SELECTION_SET_ONE_ELEMENT_PRESENT)

val matches = inputContainsAtLeastOneOfRuleClassifier.matches(
answer = ITEM_SELECTION_SET_5,
inputs = inputs
)

assertThat(matches).isTrue()
}

@Test
fun testItemSet_setAnswer_inputIsExclusiveOfSet_answerDoesNotContainInput() {
val inputs = mapOf("x" to ITEM_SELECTION_SET_EXCLUSIVE)

val matches = inputContainsAtLeastOneOfRuleClassifier.matches(
answer = ITEM_SELECTION_SET_5,
inputs = inputs
)

assertThat(matches).isFalse()
}

private fun createStringList(value: StringList): InteractionObject {
return InteractionObject.newBuilder().setSetOfHtmlString(value).build()
}

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

@Singleton
@Component(modules = [])
interface TestApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun setApplication(application: Application): Builder

fun build(): TestApplicationComponent
}

fun inject(test: ItemSelectionInputContainsAtLeastOneOfRuleClassifierProviderTest)
}
}