From ce555977f8afacd07340d876855699b37125e5c7 Mon Sep 17 00:00:00 2001 From: theayushyadav11 Date: Sun, 26 Jan 2025 16:04:25 +0530 Subject: [PATCH 1/3] added tests for PolynomialSubject --- .../oppia/android/testing/math/BUILD.bazel | 16 ++ .../testing/math/PolynomialSubjectTest.kt | 166 ++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt diff --git a/testing/src/test/java/org/oppia/android/testing/math/BUILD.bazel b/testing/src/test/java/org/oppia/android/testing/math/BUILD.bazel index 354acf48077..c3f4219491b 100644 --- a/testing/src/test/java/org/oppia/android/testing/math/BUILD.bazel +++ b/testing/src/test/java/org/oppia/android/testing/math/BUILD.bazel @@ -67,3 +67,19 @@ 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", + "//testing/src/main/java/org/oppia/android/testing/robolectric:test_module", + "//third_party:com_google_truth_truth", + "//third_party:junit_junit", + "//third_party:robolectric_android-all", + ], +) diff --git a/testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt b/testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt new file mode 100644 index 00000000000..5d97bcf4589 --- /dev/null +++ b/testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt @@ -0,0 +1,166 @@ +package org.oppia.android.testing.math + +import com.google.common.truth.Truth.assertThat +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 + +@RunWith(JUnit4::class) +class PolynomialSubjectTest { + + @Test + fun testIsNotValidPolynomial_withNullPolynomial_passes() { + PolynomialSubject.assertThat(null).isNotValidPolynomial() + } + + @Test(expected = AssertionError::class) + fun testIsNotValidPolynomial_withNonNullPolynomial_fails() { + val polynomial = Polynomial.newBuilder() + .addTerm(Polynomial.Term.newBuilder().setCoefficient(Real.newBuilder().setInteger(1))) + .build() + PolynomialSubject.assertThat(polynomial).isNotValidPolynomial() + } + + @Test + fun testIsConstantThat_constantPolynomial_passes() { + val constantPolynomial = Polynomial.newBuilder() + .addTerm(Polynomial.Term.newBuilder().setCoefficient(Real.newBuilder().setInteger(5))) + .build() + PolynomialSubject.assertThat(constantPolynomial) + .isConstantThat() + .isIntegerThat() + .isEqualTo(5) + } + + @Test(expected = AssertionError::class) + fun testIsConstantThat_nonConstantPolynomial_fails() { + val nonConstantPolynomial = Polynomial.newBuilder() + .addTerm( + Polynomial.Term.newBuilder() + .setCoefficient(Real.newBuilder().setInteger(5)) + .addVariable(Polynomial.Term.Variable.newBuilder().setName("x").setPower(1)) + ) + .build() + PolynomialSubject.assertThat(nonConstantPolynomial).isConstantThat() + } + + @Test + fun testHasTermCountThat_zeroTerms_passes() { + val emptyPolynomial = Polynomial.newBuilder().build() + PolynomialSubject.assertThat(emptyPolynomial) + .hasTermCountThat() + .isEqualTo(0) + } + + @Test + fun testHasTermCountThat_multipleTerms_passes() { + 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 testTerm_validIndex_passes() { + 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(expected = IndexOutOfBoundsException::class) + fun testTerm_invalidIndex_throws() { + val polynomial = Polynomial.newBuilder().build() + PolynomialSubject.assertThat(polynomial).term(0) + } + + @Test + fun testEvaluatesToPlainTextThat_constantPolynomial_passes() { + val constantPolynomial = Polynomial.newBuilder() + .addTerm(Polynomial.Term.newBuilder().setCoefficient(Real.newBuilder().setInteger(5))) + .build() + PolynomialSubject.assertThat(constantPolynomial) + .evaluatesToPlainTextThat() + .isEqualTo("5") + } + + @Test + fun testEvaluatesToPlainTextThat_complexPolynomial_passes() { + 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 testPolynomialTermSubject_variableCounts() { + 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 testPolynomialTermVariableSubject_details() { + 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) + } + } +} From 467d89117f505453182ba6bb31eca072502ac72a Mon Sep 17 00:00:00 2001 From: theayushyadav11 Date: Mon, 27 Jan 2025 13:04:37 +0530 Subject: [PATCH 2/3] added kDoc and Removed Todo --- .../java/org/oppia/android/testing/math/PolynomialSubject.kt | 2 -- .../org/oppia/android/testing/math/PolynomialSubjectTest.kt | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/testing/src/main/java/org/oppia/android/testing/math/PolynomialSubject.kt b/testing/src/main/java/org/oppia/android/testing/math/PolynomialSubject.kt index 23dc2b479a7..8d2013a8d65 100644 --- a/testing/src/main/java/org/oppia/android/testing/math/PolynomialSubject.kt +++ b/testing/src/main/java/org/oppia/android/testing/math/PolynomialSubject.kt @@ -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. * diff --git a/testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt b/testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt index 5d97bcf4589..ee148d0441b 100644 --- a/testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt +++ b/testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt @@ -7,6 +7,7 @@ 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 { From 42b2e6fbcb4b961177108e4091d8c27feabbd0fc Mon Sep 17 00:00:00 2001 From: theayushyadav11 Date: Thu, 30 Jan 2025 16:59:45 +0530 Subject: [PATCH 3/3] renamed function names --- scripts/assets/test_file_exemptions.textproto | 4 -- .../oppia/android/testing/math/BUILD.bazel | 1 - .../testing/math/PolynomialSubjectTest.kt | 44 +++++++++++-------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/scripts/assets/test_file_exemptions.textproto b/scripts/assets/test_file_exemptions.textproto index 0baccac8151..a577a22f6db 100644 --- a/scripts/assets/test_file_exemptions.textproto +++ b/scripts/assets/test_file_exemptions.textproto @@ -4118,10 +4118,6 @@ test_file_exemption { exempted_file_path: "testing/src/main/java/org/oppia/android/testing/math/MathParsingErrorSubject.kt" test_file_not_required: true } -test_file_exemption { - exempted_file_path: "testing/src/main/java/org/oppia/android/testing/math/PolynomialSubject.kt" - test_file_not_required: true -} test_file_exemption { exempted_file_path: "testing/src/main/java/org/oppia/android/testing/math/RealSubject.kt" test_file_not_required: true diff --git a/testing/src/test/java/org/oppia/android/testing/math/BUILD.bazel b/testing/src/test/java/org/oppia/android/testing/math/BUILD.bazel index c3f4219491b..52d56ecfcfd 100644 --- a/testing/src/test/java/org/oppia/android/testing/math/BUILD.bazel +++ b/testing/src/test/java/org/oppia/android/testing/math/BUILD.bazel @@ -77,7 +77,6 @@ oppia_android_test( deps = [ "//model/src/main/proto:math_java_proto_lite", "//testing/src/main/java/org/oppia/android/testing/math:polynomial_subject", - "//testing/src/main/java/org/oppia/android/testing/robolectric:test_module", "//third_party:com_google_truth_truth", "//third_party:junit_junit", "//third_party:robolectric_android-all", diff --git a/testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt b/testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt index ee148d0441b..d550bd6e90d 100644 --- a/testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt +++ b/testing/src/test/java/org/oppia/android/testing/math/PolynomialSubjectTest.kt @@ -1,6 +1,6 @@ package org.oppia.android.testing.math -import com.google.common.truth.Truth.assertThat +import org.junit.Assert.assertThrows import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 @@ -12,20 +12,22 @@ import org.oppia.android.app.model.Real class PolynomialSubjectTest { @Test - fun testIsNotValidPolynomial_withNullPolynomial_passes() { + fun testPolynomialSubject_withNullPolynomial_isNotValidPolynomial() { PolynomialSubject.assertThat(null).isNotValidPolynomial() } - @Test(expected = AssertionError::class) - fun testIsNotValidPolynomial_withNonNullPolynomial_fails() { + @Test + fun testPolynomialSubject_withNonNullPolynomial_isNotValidPolynomial_fails() { val polynomial = Polynomial.newBuilder() .addTerm(Polynomial.Term.newBuilder().setCoefficient(Real.newBuilder().setInteger(1))) .build() - PolynomialSubject.assertThat(polynomial).isNotValidPolynomial() + assertThrows(AssertionError::class.java) { + PolynomialSubject.assertThat(polynomial).isNotValidPolynomial() + } } @Test - fun testIsConstantThat_constantPolynomial_passes() { + fun testPolynomialSubject_withConstantPolynomial_isConstantThat() { val constantPolynomial = Polynomial.newBuilder() .addTerm(Polynomial.Term.newBuilder().setCoefficient(Real.newBuilder().setInteger(5))) .build() @@ -35,8 +37,8 @@ class PolynomialSubjectTest { .isEqualTo(5) } - @Test(expected = AssertionError::class) - fun testIsConstantThat_nonConstantPolynomial_fails() { + @Test + fun testPolynomialSubject_withNonConstantPolynomial_isConstantThat_fails() { val nonConstantPolynomial = Polynomial.newBuilder() .addTerm( Polynomial.Term.newBuilder() @@ -44,11 +46,13 @@ class PolynomialSubjectTest { .addVariable(Polynomial.Term.Variable.newBuilder().setName("x").setPower(1)) ) .build() - PolynomialSubject.assertThat(nonConstantPolynomial).isConstantThat() + assertThrows(AssertionError::class.java) { + PolynomialSubject.assertThat(nonConstantPolynomial).isConstantThat() + } } @Test - fun testHasTermCountThat_zeroTerms_passes() { + fun testPolynomialSubject_withZeroTerms_hasTermCountThat() { val emptyPolynomial = Polynomial.newBuilder().build() PolynomialSubject.assertThat(emptyPolynomial) .hasTermCountThat() @@ -56,7 +60,7 @@ class PolynomialSubjectTest { } @Test - fun testHasTermCountThat_multipleTerms_passes() { + 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))) @@ -67,7 +71,7 @@ class PolynomialSubjectTest { } @Test - fun testTerm_validIndex_passes() { + fun testPolynomialSubject_withValidIndex_termHasCoefficient() { val polynomial = Polynomial.newBuilder() .addTerm( Polynomial.Term.newBuilder() @@ -100,14 +104,16 @@ class PolynomialSubjectTest { .isEqualTo(3) } - @Test(expected = IndexOutOfBoundsException::class) - fun testTerm_invalidIndex_throws() { + @Test + fun testPolynomialSubject_failsWithInvalidIndex() { val polynomial = Polynomial.newBuilder().build() - PolynomialSubject.assertThat(polynomial).term(0) + assertThrows(IndexOutOfBoundsException::class.java) { + PolynomialSubject.assertThat(polynomial).term(0) + } } @Test - fun testEvaluatesToPlainTextThat_constantPolynomial_passes() { + fun testPolynomialSubject_withConstantPolynomial_evaluatesToPlainTextThat() { val constantPolynomial = Polynomial.newBuilder() .addTerm(Polynomial.Term.newBuilder().setCoefficient(Real.newBuilder().setInteger(5))) .build() @@ -117,7 +123,7 @@ class PolynomialSubjectTest { } @Test - fun testEvaluatesToPlainTextThat_complexPolynomial_passes() { + fun testPolynomialSubject_withComplexPolynomial_evaluatesToPlainTextThat() { val polynomial = Polynomial.newBuilder() .addTerm( Polynomial.Term.newBuilder() @@ -137,7 +143,7 @@ class PolynomialSubjectTest { } @Test - fun testPolynomialTermSubject_variableCounts() { + fun testPolynomialSubject_withTermHasVariableCount_that() { val term = Polynomial.Term.newBuilder() .setCoefficient(Real.newBuilder().setInteger(5)) .addVariable(Polynomial.Term.Variable.newBuilder().setName("x").setPower(2)) @@ -151,7 +157,7 @@ class PolynomialSubjectTest { } @Test - fun testPolynomialTermVariableSubject_details() { + fun testPolynomialSubject_withTermVariableHasDetails_that() { val term = Polynomial.Term.newBuilder() .addVariable(Polynomial.Term.Variable.newBuilder().setName("x").setPower(3)) .build()