From 643a5f56881c2030696a4c1192c6c106291bf561 Mon Sep 17 00:00:00 2001 From: theayushyadav11 Date: Mon, 27 Jan 2025 19:34:57 +0530 Subject: [PATCH 1/3] Added tests for ComparableOperationSubjectTest --- .../math/ComparableOperationSubject.kt | 2 - .../oppia/android/testing/math/BUILD.bazel | 16 + .../math/ComparableOperationSubjectTest.kt | 419 ++++++++++++++++++ 3 files changed, 435 insertions(+), 2 deletions(-) create mode 100644 testing/src/test/java/org/oppia/android/testing/math/ComparableOperationSubjectTest.kt diff --git a/testing/src/main/java/org/oppia/android/testing/math/ComparableOperationSubject.kt b/testing/src/main/java/org/oppia/android/testing/math/ComparableOperationSubject.kt index 06dd8bae7ba..cddce924cdb 100644 --- a/testing/src/main/java/org/oppia/android/testing/math/ComparableOperationSubject.kt +++ b/testing/src/main/java/org/oppia/android/testing/math/ComparableOperationSubject.kt @@ -12,8 +12,6 @@ import org.oppia.android.app.model.ComparableOperation.ComparisonTypeCase import org.oppia.android.app.model.Real import org.oppia.android.testing.math.RealSubject.Companion.assertThat -// TODO(#4098): Add tests for this class. - /** * Truth subject for verifying properties of [ComparableOperation]s. * 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..924dcd2ff9b 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 = "ComparableOperationSubjectTest", + srcs = ["ComparableOperationSubjectTest.kt"], + custom_package = "org.oppia.android.testing.math", + test_class = "org.oppia.android.testing.math.ComparableOperationSubjectTest", + test_manifest = "//testing:test_manifest", + deps = [ + "//model/src/main/proto:math_java_proto_lite", + "//testing/src/main/java/org/oppia/android/testing/math:comparable_operation_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/ComparableOperationSubjectTest.kt b/testing/src/test/java/org/oppia/android/testing/math/ComparableOperationSubjectTest.kt new file mode 100644 index 00000000000..5303244b9a6 --- /dev/null +++ b/testing/src/test/java/org/oppia/android/testing/math/ComparableOperationSubjectTest.kt @@ -0,0 +1,419 @@ +package org.oppia.android.testing.math + +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.oppia.android.app.model.ComparableOperation +import org.oppia.android.app.model.Real + +/** Tests for [ComparableOperationSubject]. */ +@RunWith(JUnit4::class) +class ComparableOperationSubjectTest { + + private fun createConstantOperation(value: Int): ComparableOperation { + return ComparableOperation.newBuilder() + .setConstantTerm(Real.newBuilder().setInteger(value)) + .build() + } + + private fun createVariableOperation(name: String): ComparableOperation { + return ComparableOperation.newBuilder() + .setVariableTerm(name) + .build() + } + + private fun createCommutativeAccumulation( + type: ComparableOperation.CommutativeAccumulation.AccumulationType, + vararg operations: ComparableOperation + ): ComparableOperation { + val accumulation = ComparableOperation.CommutativeAccumulation.newBuilder() + .setAccumulationType(type) + operations.forEach { accumulation.addCombinedOperations(it) } + return ComparableOperation.newBuilder() + .setCommutativeAccumulation(accumulation) + .build() + } + + @Test + fun testHasStructureThatMatches_success() { + val operation = createConstantOperation(42) + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(42) + } + } + } + + @Test(expected = AssertionError::class) + fun testHasStructureThatMatches_withInvalidStructure_fails() { + val operation = createConstantOperation(42) + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + variableTerm { + withNameThat().isEqualTo("x") + } + } + } + + @Test + fun testHasNegatedPropertyThat_withFalseDefault_matchesSuccessfully() { + val operation = createConstantOperation(42) + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + hasNegatedPropertyThat().isFalse() + } + } + + @Test + fun testHasNegatedPropertyThat_withExplicitTrue_matchesSuccessfully() { + val operation = ComparableOperation.newBuilder() + .setConstantTerm(Real.newBuilder().setInteger(42)) + .setIsNegated(true) + .build() + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + hasNegatedPropertyThat().isTrue() + } + } + + @Test + fun testHasInvertedPropertyThat_withFalseDefault_matchesSuccessfully() { + val operation = createConstantOperation(42) + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + hasInvertedPropertyThat().isFalse() + } + } + + @Test + fun testHasInvertedPropertyThat_withExplicitTrue_matchesSuccessfully() { + val operation = ComparableOperation.newBuilder() + .setConstantTerm(Real.newBuilder().setInteger(42)) + .setIsInverted(true) + .build() + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + hasInvertedPropertyThat().isTrue() + } + } + + @Test + fun testCommutativeAccumulation_withValidSummation_matchesSuccessfully() { + val operation = createCommutativeAccumulation( + ComparableOperation.CommutativeAccumulation.AccumulationType.SUMMATION, + createConstantOperation(1), + createConstantOperation(2) + ) + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + commutativeAccumulationWithType( + ComparableOperation.CommutativeAccumulation + .AccumulationType.SUMMATION + ) { + hasOperandCountThat().isEqualTo(2) + index(0) { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(1) + } + } + index(1) { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(2) + } + } + } + } + } + + @Test + fun testCommutativeAccumulation_withEmptyAccumulation_matchesSuccessfully() { + val operation = createCommutativeAccumulation( + ComparableOperation.CommutativeAccumulation.AccumulationType.SUMMATION + ) + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + commutativeAccumulationWithType( + ComparableOperation.CommutativeAccumulation + .AccumulationType.SUMMATION + ) { + hasOperandCountThat().isEqualTo(0) + } + } + } + + @Test(expected = AssertionError::class) + fun testCommutativeAccumulation_withInvalidType_fails() { + val operation = createConstantOperation(42) + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + commutativeAccumulationWithType( + ComparableOperation.CommutativeAccumulation + .AccumulationType.SUMMATION + ) { + hasOperandCountThat().isEqualTo(0) + } + } + } + + @Test(expected = IndexOutOfBoundsException::class) + fun testCommutativeAccumulation_withInvalidIndex_fails() { + val operation = createCommutativeAccumulation( + ComparableOperation.CommutativeAccumulation.AccumulationType.SUMMATION, + createConstantOperation(1) + ) + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + commutativeAccumulationWithType( + ComparableOperation.CommutativeAccumulation + .AccumulationType.SUMMATION + ) { + index(1) { } + } + } + } + + @Test + fun testExponentiation_withValidOperation_matchesSuccessfully() { + val operation = ComparableOperation.newBuilder() + .setNonCommutativeOperation( + ComparableOperation.NonCommutativeOperation.newBuilder() + .setExponentiation( + ComparableOperation.NonCommutativeOperation.BinaryOperation.newBuilder() + .setLeftOperand(createConstantOperation(2)) + .setRightOperand(createConstantOperation(3)) + ) + ) + .build() + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + nonCommutativeOperation { + exponentiation { + leftOperand { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(2) + } + } + rightOperand { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(3) + } + } + } + } + } + } + + @Test(expected = AssertionError::class) + fun testExponentiation_withInvalidOperation_fails() { + val operation = ComparableOperation.newBuilder() + .setNonCommutativeOperation( + ComparableOperation.NonCommutativeOperation.newBuilder() + .setSquareRoot(createConstantOperation(4)) + ) + .build() + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + nonCommutativeOperation { + exponentiation { } + } + } + } + + @Test + fun testSquareRoot_withValidOperation_matchesSuccessfully() { + val operation = ComparableOperation.newBuilder() + .setNonCommutativeOperation( + ComparableOperation.NonCommutativeOperation.newBuilder() + .setSquareRoot(createConstantOperation(4)) + ) + .build() + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + nonCommutativeOperation { + squareRootWithArgument { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(4) + } + } + } + } + } + + @Test(expected = AssertionError::class) + fun testSquareRoot_withInvalidOperation_fails() { + val operation = ComparableOperation.newBuilder() + .setNonCommutativeOperation( + ComparableOperation.NonCommutativeOperation.newBuilder() + .setExponentiation( + ComparableOperation.NonCommutativeOperation.BinaryOperation.newBuilder() + .setLeftOperand(createConstantOperation(2)) + .setRightOperand(createConstantOperation(3)) + ) + ) + .build() + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + nonCommutativeOperation { + squareRootWithArgument { } + } + } + } + + @Test(expected = AssertionError::class) + fun testBinaryOperation_withInvalidLeftOperand_fails() { + val operation = ComparableOperation.newBuilder() + .setNonCommutativeOperation( + ComparableOperation.NonCommutativeOperation.newBuilder() + .setExponentiation( + ComparableOperation.NonCommutativeOperation.BinaryOperation.newBuilder() + .setRightOperand(createConstantOperation(3)) + ) + ) + .build() + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + nonCommutativeOperation { + exponentiation { + leftOperand { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(2) + } + } + } + } + } + } + + @Test(expected = AssertionError::class) + fun testBinaryOperation_withInvalidRightOperand_fails() { + val operation = ComparableOperation.newBuilder() + .setNonCommutativeOperation( + ComparableOperation.NonCommutativeOperation.newBuilder() + .setExponentiation( + ComparableOperation.NonCommutativeOperation.BinaryOperation.newBuilder() + .setLeftOperand(createConstantOperation(2)) + ) + ) + .build() + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + nonCommutativeOperation { + exponentiation { + rightOperand { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(3) + } + } + } + } + } + } + + @Test + fun testConstantTerm_withValidValue_matchesSuccessfully() { + val operation = createConstantOperation(42) + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(42) + } + } + } + + @Test(expected = AssertionError::class) + fun testConstantTerm_withInvalidType_fails() { + val operation = createVariableOperation("x") + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(42) + } + } + } + + @Test + fun testVariableTerm_withValidName_matchesSuccessfully() { + val operation = createVariableOperation("x") + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + variableTerm { + withNameThat().isEqualTo("x") + } + } + } + + @Test(expected = AssertionError::class) + fun testVariableTerm_withInvalidType_fails() { + val operation = createConstantOperation(42) + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + variableTerm { + withNameThat().isEqualTo("x") + } + } + } + + @Test + fun testComplexExpression_withNestedOperations_matchesSuccessfully() { + val operation = createCommutativeAccumulation( + ComparableOperation.CommutativeAccumulation.AccumulationType.PRODUCT, + ComparableOperation.newBuilder() + .setNonCommutativeOperation( + ComparableOperation.NonCommutativeOperation.newBuilder() + .setExponentiation( + ComparableOperation.NonCommutativeOperation.BinaryOperation.newBuilder() + .setLeftOperand(createConstantOperation(2)) + .setRightOperand(createConstantOperation(3)) + ) + ) + .build(), + ComparableOperation.newBuilder() + .setNonCommutativeOperation( + ComparableOperation.NonCommutativeOperation.newBuilder() + .setSquareRoot(createConstantOperation(4)) + ) + .build(), + createVariableOperation("x") + ) + + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + commutativeAccumulationWithType( + ComparableOperation.CommutativeAccumulation + .AccumulationType.PRODUCT + ) { + hasOperandCountThat().isEqualTo(3) + index(0) { + nonCommutativeOperation { + exponentiation { + leftOperand { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(2) + } + } + rightOperand { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(3) + } + } + } + } + } + index(1) { + nonCommutativeOperation { + squareRootWithArgument { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(4) + } + } + } + } + index(2) { + variableTerm { + withNameThat().isEqualTo("x") + } + } + } + } + } +} From 3d07473f7fda601721510475681a4ae4a1e0c000 Mon Sep 17 00:00:00 2001 From: theayushyadav11 Date: Sat, 1 Feb 2025 00:07:31 +0530 Subject: [PATCH 2/3] renamed functions --- scripts/assets/test_file_exemptions.textproto | 4 - .../oppia/android/testing/math/BUILD.bazel | 1 - .../math/ComparableOperationSubjectTest.kt | 155 +++++++++--------- 3 files changed, 82 insertions(+), 78 deletions(-) diff --git a/scripts/assets/test_file_exemptions.textproto b/scripts/assets/test_file_exemptions.textproto index 8529317da0b..edcfea18f81 100644 --- a/scripts/assets/test_file_exemptions.textproto +++ b/scripts/assets/test_file_exemptions.textproto @@ -4098,10 +4098,6 @@ test_file_exemption { exempted_file_path: "testing/src/main/java/org/oppia/android/testing/logging/SyncStatusTestModule.kt" override_min_coverage_percent_required: 0 } -test_file_exemption { - exempted_file_path: "testing/src/main/java/org/oppia/android/testing/math/ComparableOperationSubject.kt" - test_file_not_required: true -} test_file_exemption { exempted_file_path: "testing/src/main/java/org/oppia/android/testing/math/MathParsingErrorSubject.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 924dcd2ff9b..1e8c7ab230a 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 @@ -75,7 +75,6 @@ oppia_android_test( test_class = "org.oppia.android.testing.math.ComparableOperationSubjectTest", test_manifest = "//testing:test_manifest", deps = [ - "//model/src/main/proto:math_java_proto_lite", "//testing/src/main/java/org/oppia/android/testing/math:comparable_operation_subject", "//testing/src/main/java/org/oppia/android/testing/robolectric:test_module", "//third_party:com_google_truth_truth", diff --git a/testing/src/test/java/org/oppia/android/testing/math/ComparableOperationSubjectTest.kt b/testing/src/test/java/org/oppia/android/testing/math/ComparableOperationSubjectTest.kt index 5303244b9a6..66555cc64d0 100644 --- a/testing/src/test/java/org/oppia/android/testing/math/ComparableOperationSubjectTest.kt +++ b/testing/src/test/java/org/oppia/android/testing/math/ComparableOperationSubjectTest.kt @@ -3,6 +3,7 @@ package org.oppia.android.testing.math import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 +import org.junit.Assert.assertThrows import org.oppia.android.app.model.ComparableOperation import org.oppia.android.app.model.Real @@ -35,7 +36,7 @@ class ComparableOperationSubjectTest { } @Test - fun testHasStructureThatMatches_success() { + fun testComparableOperationSubject_hasStructureThatMatches() { val operation = createConstantOperation(42) ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { @@ -45,19 +46,20 @@ class ComparableOperationSubjectTest { } } - @Test(expected = AssertionError::class) - fun testHasStructureThatMatches_withInvalidStructure_fails() { + @Test + fun testComparableOperationSubject_failsWithInvalidStructure() { val operation = createConstantOperation(42) - - ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { - variableTerm { - withNameThat().isEqualTo("x") + assertThrows(AssertionError::class.java) { + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + variableTerm { + withNameThat().isEqualTo("x") + } } } } @Test - fun testHasNegatedPropertyThat_withFalseDefault_matchesSuccessfully() { + fun testComparableOperationSubject_hasNegatedProperty_matchesFalse() { val operation = createConstantOperation(42) ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { @@ -66,7 +68,7 @@ class ComparableOperationSubjectTest { } @Test - fun testHasNegatedPropertyThat_withExplicitTrue_matchesSuccessfully() { + fun testComparableOperationSubject_hasNegatedPropertyThat_matchesTrue() { val operation = ComparableOperation.newBuilder() .setConstantTerm(Real.newBuilder().setInteger(42)) .setIsNegated(true) @@ -78,7 +80,7 @@ class ComparableOperationSubjectTest { } @Test - fun testHasInvertedPropertyThat_withFalseDefault_matchesSuccessfully() { + fun testComparableOperationSubject_hasInvertedProperty_matchesFalse() { val operation = createConstantOperation(42) ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { @@ -87,7 +89,7 @@ class ComparableOperationSubjectTest { } @Test - fun testHasInvertedPropertyThat_withExplicitTrue_matchesSuccessfully() { + fun testComparableOperationSubject_hasInvertedProperty() { val operation = ComparableOperation.newBuilder() .setConstantTerm(Real.newBuilder().setInteger(42)) .setIsInverted(true) @@ -99,7 +101,7 @@ class ComparableOperationSubjectTest { } @Test - fun testCommutativeAccumulation_withValidSummation_matchesSuccessfully() { + fun testComparableOperationSubject_commutativeAccumulation_withValidSummation() { val operation = createCommutativeAccumulation( ComparableOperation.CommutativeAccumulation.AccumulationType.SUMMATION, createConstantOperation(1), @@ -127,7 +129,7 @@ class ComparableOperationSubjectTest { } @Test - fun testCommutativeAccumulation_withEmptyAccumulation_matchesSuccessfully() { + fun testComparableOperationSubject_commutativeAccumulation_withEmptyAccumulation() { val operation = createCommutativeAccumulation( ComparableOperation.CommutativeAccumulation.AccumulationType.SUMMATION ) @@ -142,39 +144,41 @@ class ComparableOperationSubjectTest { } } - @Test(expected = AssertionError::class) - fun testCommutativeAccumulation_withInvalidType_fails() { + @Test + fun testComparableOperationSubject_commutativeAccumulationFailsWithInvalidType() { val operation = createConstantOperation(42) - - ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { - commutativeAccumulationWithType( - ComparableOperation.CommutativeAccumulation - .AccumulationType.SUMMATION - ) { - hasOperandCountThat().isEqualTo(0) + assertThrows(AssertionError::class.java) { + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + commutativeAccumulationWithType( + ComparableOperation.CommutativeAccumulation + .AccumulationType.SUMMATION + ) { + hasOperandCountThat().isEqualTo(0) + } } } } - @Test(expected = IndexOutOfBoundsException::class) - fun testCommutativeAccumulation_withInvalidIndex_fails() { + @Test + fun testComparableOperationSubject_commutativeAccumulation_failsWithInvalidIndex() { val operation = createCommutativeAccumulation( ComparableOperation.CommutativeAccumulation.AccumulationType.SUMMATION, createConstantOperation(1) ) - - ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { - commutativeAccumulationWithType( - ComparableOperation.CommutativeAccumulation - .AccumulationType.SUMMATION - ) { - index(1) { } + assertThrows(IndexOutOfBoundsException::class.java) { + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + commutativeAccumulationWithType( + ComparableOperation.CommutativeAccumulation + .AccumulationType.SUMMATION + ) { + index(1) { } + } } } } @Test - fun testExponentiation_withValidOperation_matchesSuccessfully() { + fun testComparableOperationSubject_matchesWithValidOperation() { val operation = ComparableOperation.newBuilder() .setNonCommutativeOperation( ComparableOperation.NonCommutativeOperation.newBuilder() @@ -204,24 +208,25 @@ class ComparableOperationSubjectTest { } } - @Test(expected = AssertionError::class) - fun testExponentiation_withInvalidOperation_fails() { + @Test + fun testComparableOperationSubject_exponentiationFailsWithInvalidOperation() { val operation = ComparableOperation.newBuilder() .setNonCommutativeOperation( ComparableOperation.NonCommutativeOperation.newBuilder() .setSquareRoot(createConstantOperation(4)) ) .build() - - ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { - nonCommutativeOperation { - exponentiation { } + assertThrows(AssertionError::class.java) { + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + nonCommutativeOperation { + exponentiation { } + } } } } @Test - fun testSquareRoot_withValidOperation_matchesSuccessfully() { + fun testComparableOperationSubject_squareRoot_withValidOperation() { val operation = ComparableOperation.newBuilder() .setNonCommutativeOperation( ComparableOperation.NonCommutativeOperation.newBuilder() @@ -240,8 +245,8 @@ class ComparableOperationSubjectTest { } } - @Test(expected = AssertionError::class) - fun testSquareRoot_withInvalidOperation_fails() { + @Test + fun testComparableOperationSubject_squareRoot_failsWithInvalidOperation() { val operation = ComparableOperation.newBuilder() .setNonCommutativeOperation( ComparableOperation.NonCommutativeOperation.newBuilder() @@ -252,16 +257,17 @@ class ComparableOperationSubjectTest { ) ) .build() - - ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { - nonCommutativeOperation { - squareRootWithArgument { } + assertThrows(AssertionError::class.java) { + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + nonCommutativeOperation { + squareRootWithArgument { } + } } } } - @Test(expected = AssertionError::class) - fun testBinaryOperation_withInvalidLeftOperand_fails() { + @Test + fun testComparableOperationSubject_binaryOperation_failsWithInvalidLeftOperand() { val operation = ComparableOperation.newBuilder() .setNonCommutativeOperation( ComparableOperation.NonCommutativeOperation.newBuilder() @@ -271,13 +277,14 @@ class ComparableOperationSubjectTest { ) ) .build() - - ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { - nonCommutativeOperation { - exponentiation { - leftOperand { - constantTerm { - withValueThat().isIntegerThat().isEqualTo(2) + assertThrows(AssertionError::class.java) { + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + nonCommutativeOperation { + exponentiation { + leftOperand { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(2) + } } } } @@ -285,8 +292,8 @@ class ComparableOperationSubjectTest { } } - @Test(expected = AssertionError::class) - fun testBinaryOperation_withInvalidRightOperand_fails() { + @Test + fun testComparableOperationSubject_binaryOperation_failsWithInvalidRightOperand() { val operation = ComparableOperation.newBuilder() .setNonCommutativeOperation( ComparableOperation.NonCommutativeOperation.newBuilder() @@ -296,13 +303,14 @@ class ComparableOperationSubjectTest { ) ) .build() - - ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { - nonCommutativeOperation { - exponentiation { - rightOperand { - constantTerm { - withValueThat().isIntegerThat().isEqualTo(3) + assertThrows(AssertionError::class.java) { + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + nonCommutativeOperation { + exponentiation { + rightOperand { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(3) + } } } } @@ -311,7 +319,7 @@ class ComparableOperationSubjectTest { } @Test - fun testConstantTerm_withValidValue_matchesSuccessfully() { + fun testComparableOperationSubject_checksConstantTerm_withValidValue() { val operation = createConstantOperation(42) ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { @@ -321,19 +329,20 @@ class ComparableOperationSubjectTest { } } - @Test(expected = AssertionError::class) - fun testConstantTerm_withInvalidType_fails() { + @Test + fun testComparableOperationSubject_constantTerm_failsWithInvalidType() { val operation = createVariableOperation("x") - - ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { - constantTerm { - withValueThat().isIntegerThat().isEqualTo(42) + assertThrows(AssertionError::class.java) { + ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { + constantTerm { + withValueThat().isIntegerThat().isEqualTo(42) + } } } } @Test - fun testVariableTerm_withValidName_matchesSuccessfully() { + fun testComparableOperationSubject_validatesVariableTerm() { val operation = createVariableOperation("x") ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { @@ -344,7 +353,7 @@ class ComparableOperationSubjectTest { } @Test(expected = AssertionError::class) - fun testVariableTerm_withInvalidType_fails() { + fun testComparableOperationSubject_variableTerm_failWithInvalidType() { val operation = createConstantOperation(42) ComparableOperationSubject.assertThat(operation).hasStructureThatMatches { @@ -355,7 +364,7 @@ class ComparableOperationSubjectTest { } @Test - fun testComplexExpression_withNestedOperations_matchesSuccessfully() { + fun testComparableOperationSubject_complexExpression_withNestedOperations() { val operation = createCommutativeAccumulation( ComparableOperation.CommutativeAccumulation.AccumulationType.PRODUCT, ComparableOperation.newBuilder() From bc06ff354cfef61becda327a4b7dc0eb9a0e1521 Mon Sep 17 00:00:00 2001 From: theayushyadav11 Date: Sat, 1 Feb 2025 00:09:48 +0530 Subject: [PATCH 3/3] formatting --- .../android/testing/math/ComparableOperationSubjectTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/src/test/java/org/oppia/android/testing/math/ComparableOperationSubjectTest.kt b/testing/src/test/java/org/oppia/android/testing/math/ComparableOperationSubjectTest.kt index 66555cc64d0..0304c8c1ac0 100644 --- a/testing/src/test/java/org/oppia/android/testing/math/ComparableOperationSubjectTest.kt +++ b/testing/src/test/java/org/oppia/android/testing/math/ComparableOperationSubjectTest.kt @@ -1,9 +1,9 @@ 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.junit.Assert.assertThrows import org.oppia.android.app.model.ComparableOperation import org.oppia.android.app.model.Real