From 19faa4f37469775a19f6f3887ddc06de1c1d8e14 Mon Sep 17 00:00:00 2001 From: Akshay Nandwana Date: Mon, 19 Oct 2020 14:11:44 +0530 Subject: [PATCH] Fix #2019: Introduce InteractionObjectBuilder (#2020) * introduce interaction object helper class * nit change * format fix --- .../classify/InteractionObjectTestBuilder.kt | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 domain/src/test/java/org/oppia/android/domain/classify/InteractionObjectTestBuilder.kt diff --git a/domain/src/test/java/org/oppia/android/domain/classify/InteractionObjectTestBuilder.kt b/domain/src/test/java/org/oppia/android/domain/classify/InteractionObjectTestBuilder.kt new file mode 100644 index 00000000000..6e0ad14f952 --- /dev/null +++ b/domain/src/test/java/org/oppia/android/domain/classify/InteractionObjectTestBuilder.kt @@ -0,0 +1,91 @@ +package org.oppia.android.domain.classify + +import org.oppia.android.app.model.Fraction +import org.oppia.android.app.model.InteractionObject +import org.oppia.android.app.model.ListOfSetsOfHtmlStrings +import org.oppia.android.app.model.StringList +import javax.inject.Inject + +/** + * Helper class for test cases which can provide the [InteractionObject] + * of a particular ObjectTypeCase. + */ +class InteractionObjectTestBuilder @Inject constructor() { + + fun createNonNegativeInt(value: Int): InteractionObject { + return InteractionObject.newBuilder().setNonNegativeInt(value).build() + } + + fun createString(value: String): InteractionObject { + return InteractionObject.newBuilder().setNormalizedString(value).build() + } + + fun createListOfSetsOfHtmlStrings(listOfStringList: List): InteractionObject { + val listOfSetsOfHtmlStrings = ListOfSetsOfHtmlStrings.newBuilder() + .addAllSetOfHtmlStrings( + listOfStringList + ) + .build() + + return InteractionObject.newBuilder().setListOfSetsOfHtmlString(listOfSetsOfHtmlStrings).build() + } + + fun createHtmlStringList(vararg items: String): StringList { + return StringList.newBuilder().addAllHtml(items.toList()).build() + } + + fun createWholeNumber(isNegative: Boolean, value: Int): InteractionObject { + // Whole number fractions imply '0/1' fractional parts. + return InteractionObject.newBuilder().setFraction( + Fraction.newBuilder() + .setIsNegative(isNegative) + .setWholeNumber(value) + .setNumerator(0) + .setDenominator(1) + .build() + ).build() + } + + fun createFraction( + isNegative: Boolean, + numerator: Int, + denominator: Int + ): InteractionObject { + // Fraction-only numbers imply no whole number. + return InteractionObject.newBuilder().setFraction( + Fraction.newBuilder() + .setIsNegative(isNegative) + .setNumerator(numerator) + .setDenominator(denominator) + .build() + ).build() + } + + fun createMixedNumber( + isNegative: Boolean, + wholeNumber: Int, + numerator: Int, + denominator: Int + ): InteractionObject { + return InteractionObject.newBuilder().setFraction( + Fraction.newBuilder() + .setIsNegative(isNegative) + .setWholeNumber(wholeNumber) + .setNumerator(numerator) + .setDenominator(denominator) + .build() + ).build() + } + + fun createSignedInt(value: Int): InteractionObject { + return InteractionObject.newBuilder().setSignedInt(value).build() + } + + fun createReal(value: Double): InteractionObject { + return InteractionObject.newBuilder().setReal(value).build() + } + + fun createInt(value: Int): InteractionObject { + return InteractionObject.newBuilder().setReal(value.toDouble()).build() + } +}