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 #4100 : Added tests for PolynomialSubject #5667

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion scripts/assets/test_file_exemptions.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -4107,7 +4107,7 @@ test_file_exemption {
test_file_not_required: true
}
test_file_exemption {
exempted_file_path: "testing/src/main/java/org/oppia/android/testing/math/PolynomialSubject.kt"
exempted_file_path: "testing/src/main/java/org/oppia/android/testing/math/RealSubject.kt"
test_file_not_required: true
}
test_file_exemption {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import org.oppia.android.util.math.getConstant
import org.oppia.android.util.math.isConstant
import org.oppia.android.util.math.toPlainText

// TODO(#4100): Add tests for this class.

/**
* Truth subject for verifying properties of [Polynomial]s.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,18 @@ oppia_android_test(
"//third_party:robolectric_android-all",
],
)

oppia_android_test(
name = "PolynomialSubjectTest",
srcs = ["PolynomialSubjectTest.kt"],
custom_package = "org.oppia.android.testing.math",
test_class = "org.oppia.android.testing.math.PolynomialSubjectTest",
test_manifest = "//testing:test_manifest",
deps = [
"//model/src/main/proto:math_java_proto_lite",
"//testing/src/main/java/org/oppia/android/testing/math:polynomial_subject",
"//third_party:com_google_truth_truth",
"//third_party:junit_junit",
"//third_party:robolectric_android-all",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package org.oppia.android.testing.math

import org.junit.Assert.assertThrows
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.oppia.android.app.model.Polynomial
import org.oppia.android.app.model.Real

/** Tests for [PolynomialSubject]. */
@RunWith(JUnit4::class)
class PolynomialSubjectTest {

@Test
fun testPolynomialSubject_withNullPolynomial_isNotValidPolynomial() {
PolynomialSubject.assertThat(null).isNotValidPolynomial()
}

@Test
fun testPolynomialSubject_withNonNullPolynomial_isNotValidPolynomial_fails() {
val polynomial = Polynomial.newBuilder()
.addTerm(Polynomial.Term.newBuilder().setCoefficient(Real.newBuilder().setInteger(1)))
.build()
assertThrows(AssertionError::class.java) {
PolynomialSubject.assertThat(polynomial).isNotValidPolynomial()
}
}

@Test
fun testPolynomialSubject_withConstantPolynomial_isConstantThat() {
val constantPolynomial = Polynomial.newBuilder()
.addTerm(Polynomial.Term.newBuilder().setCoefficient(Real.newBuilder().setInteger(5)))
.build()
PolynomialSubject.assertThat(constantPolynomial)
.isConstantThat()
.isIntegerThat()
.isEqualTo(5)
}

@Test
fun testPolynomialSubject_withNonConstantPolynomial_isConstantThat_fails() {
val nonConstantPolynomial = Polynomial.newBuilder()
.addTerm(
Polynomial.Term.newBuilder()
.setCoefficient(Real.newBuilder().setInteger(5))
.addVariable(Polynomial.Term.Variable.newBuilder().setName("x").setPower(1))
)
.build()
assertThrows(AssertionError::class.java) {
PolynomialSubject.assertThat(nonConstantPolynomial).isConstantThat()
}
}

@Test
fun testPolynomialSubject_withZeroTerms_hasTermCountThat() {
val emptyPolynomial = Polynomial.newBuilder().build()
PolynomialSubject.assertThat(emptyPolynomial)
.hasTermCountThat()
.isEqualTo(0)
}

@Test
fun testPolynomialSubject_withMultipleTerms_hasTermCountThat() {
val multiTermPolynomial = Polynomial.newBuilder()
.addTerm(Polynomial.Term.newBuilder().setCoefficient(Real.newBuilder().setInteger(1)))
.addTerm(Polynomial.Term.newBuilder().setCoefficient(Real.newBuilder().setInteger(2)))
.build()
PolynomialSubject.assertThat(multiTermPolynomial)
.hasTermCountThat()
.isEqualTo(2)
}

@Test
fun testPolynomialSubject_withValidIndex_termHasCoefficient() {
val polynomial = Polynomial.newBuilder()
.addTerm(
Polynomial.Term.newBuilder()
.setCoefficient(Real.newBuilder().setInteger(5))
.addVariable(Polynomial.Term.Variable.newBuilder().setName("x").setPower(2))
)
.addTerm(
Polynomial.Term.newBuilder()
.setCoefficient(Real.newBuilder().setInteger(3))
.addVariable(Polynomial.Term.Variable.newBuilder().setName("y").setPower(1))
)
.build()

PolynomialSubject.assertThat(polynomial)
.term(0)
.hasCoefficientThat()
.isIntegerThat()
.isEqualTo(5)

PolynomialSubject.assertThat(polynomial)
.term(0)
.variable(0)
.hasNameThat()
.isEqualTo("x")

PolynomialSubject.assertThat(polynomial)
.term(1)
.hasCoefficientThat()
.isIntegerThat()
.isEqualTo(3)
}

@Test
fun testPolynomialSubject_failsWithInvalidIndex() {
val polynomial = Polynomial.newBuilder().build()
assertThrows(IndexOutOfBoundsException::class.java) {
PolynomialSubject.assertThat(polynomial).term(0)
}
}

@Test
fun testPolynomialSubject_withConstantPolynomial_evaluatesToPlainTextThat() {
val constantPolynomial = Polynomial.newBuilder()
.addTerm(Polynomial.Term.newBuilder().setCoefficient(Real.newBuilder().setInteger(5)))
.build()
PolynomialSubject.assertThat(constantPolynomial)
.evaluatesToPlainTextThat()
.isEqualTo("5")
}

@Test
fun testPolynomialSubject_withComplexPolynomial_evaluatesToPlainTextThat() {
val polynomial = Polynomial.newBuilder()
.addTerm(
Polynomial.Term.newBuilder()
.setCoefficient(Real.newBuilder().setInteger(2))
.addVariable(Polynomial.Term.Variable.newBuilder().setName("x").setPower(2))
)
.addTerm(
Polynomial.Term.newBuilder()
.setCoefficient(Real.newBuilder().setInteger(3))
.addVariable(Polynomial.Term.Variable.newBuilder().setName("x").setPower(1))
)
.addTerm(Polynomial.Term.newBuilder().setCoefficient(Real.newBuilder().setInteger(1)))
.build()
PolynomialSubject.assertThat(polynomial)
.evaluatesToPlainTextThat()
.isEqualTo("2x^2 + 3x + 1")
}

@Test
fun testPolynomialSubject_withTermHasVariableCount_that() {
val term = Polynomial.Term.newBuilder()
.setCoefficient(Real.newBuilder().setInteger(5))
.addVariable(Polynomial.Term.Variable.newBuilder().setName("x").setPower(2))
.addVariable(Polynomial.Term.Variable.newBuilder().setName("y").setPower(1))
.build()

PolynomialSubject.assertThat(Polynomial.newBuilder().addTerm(term).build())
.term(0)
.hasVariableCountThat()
.isEqualTo(2)
}

@Test
fun testPolynomialSubject_withTermVariableHasDetails_that() {
val term = Polynomial.Term.newBuilder()
.addVariable(Polynomial.Term.Variable.newBuilder().setName("x").setPower(3))
.build()

PolynomialSubject.assertThat(Polynomial.newBuilder().addTerm(term).build())
.term(0)
.variable(0)
.apply {
hasNameThat().isEqualTo("x")
hasPowerThat().isEqualTo(3)
}
}
}
Loading