From 893c7a25f613102682495f216c8d0254a8c87375 Mon Sep 17 00:00:00 2001 From: Ayush Yadav <143514610+theayushyadav11@users.noreply.github.com> Date: Thu, 13 Feb 2025 16:59:01 +0530 Subject: [PATCH] Fix #4121: Added tests for TokenSubject (#5669) Fixes #4121 This pull request introduces a new test class `TokenSubjectTest` to the `oppia_android_test` suite, focusing on validating various aspects of the `Token` class from the `MathTokenizer` utility. The primary changes include the addition of the test class itself and the corresponding Bazel build rule. ### Key Changes: #### Addition of Test Class: * [`testing/src/test/java/org/oppia/android/testing/math/TokenSubjectTest.kt`](diffhunk://#diff-aeecd58bdeac384c551e2a73a6debca08e2bdfb0492eb84007773ed78af057d0R1-R180): Added a new test class `TokenSubjectTest` with multiple test methods to verify the correctness of different `Token` types and their properties. #### Bazel Build Rule: * [`testing/src/test/java/org/oppia/android/testing/math/BUILD.bazel`](diffhunk://#diff-8a86bceb0e654069d42dcb4e76a52f65aeea3bc7d76a9cdff53f5e4bdde93f08R70-R85): Added a new `oppia_android_test` target for `TokenSubjectTest`, specifying its dependencies and configuration. #### Screenshot of the passing tests. ![image](https://github.com/user-attachments/assets/49628814-1616-45be-ae64-2f95aa5ce298) ## Essential Checklist - [x] The PR title and explanation each start with "Fix #bugnum: " (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".) - [x] Any changes to [scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets) files have their rationale included in the PR explanation. - [x] The PR follows the [style guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide). - [x] The PR does not contain any unnecessary code changes from Android Studio ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)). - [x] The PR is made from a branch that's **not** called "develop" and is up-to-date with "develop". - [x] The PR is **assigned** to the appropriate reviewers ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)) --------- Co-authored-by: theayushyadav11 --- scripts/assets/test_file_exemptions.textproto | 4 - .../android/testing/math/TokenSubject.kt | 2 - .../oppia/android/testing/math/BUILD.bazel | 14 ++ .../android/testing/math/TokenSubjectTest.kt | 217 ++++++++++++++++++ 4 files changed, 231 insertions(+), 6 deletions(-) create mode 100644 testing/src/test/java/org/oppia/android/testing/math/TokenSubjectTest.kt diff --git a/scripts/assets/test_file_exemptions.textproto b/scripts/assets/test_file_exemptions.textproto index 94ebe621a53..02d22431745 100644 --- a/scripts/assets/test_file_exemptions.textproto +++ b/scripts/assets/test_file_exemptions.textproto @@ -4106,10 +4106,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/TokenSubject.kt" - test_file_not_required: true -} test_file_exemption { exempted_file_path: "testing/src/main/java/org/oppia/android/testing/mockito/MockitoKotlinHelper.kt" test_file_not_required: true diff --git a/testing/src/main/java/org/oppia/android/testing/math/TokenSubject.kt b/testing/src/main/java/org/oppia/android/testing/math/TokenSubject.kt index 737de61a7b9..49cce0f2030 100644 --- a/testing/src/main/java/org/oppia/android/testing/math/TokenSubject.kt +++ b/testing/src/main/java/org/oppia/android/testing/math/TokenSubject.kt @@ -26,8 +26,6 @@ import org.oppia.android.util.math.MathTokenizer.Companion.Token.RightParenthesi import org.oppia.android.util.math.MathTokenizer.Companion.Token.SquareRootSymbol import org.oppia.android.util.math.MathTokenizer.Companion.Token.VariableName -// TODO(#4121): Add tests for this class. - /** * Truth subject for verifying properties of [Token]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 52d56ecfcfd..63cb143e73e 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 @@ -82,3 +82,17 @@ oppia_android_test( "//third_party:robolectric_android-all", ], ) + +oppia_android_test( + name = "TokenSubjectTest", + srcs = ["TokenSubjectTest.kt"], + custom_package = "org.oppia.android.testing.math", + test_class = "org.oppia.android.testing.math.TokenSubjectTest", + test_manifest = "//testing:test_manifest", + deps = [ + "//testing/src/main/java/org/oppia/android/testing/math:token_subject", + "//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/TokenSubjectTest.kt b/testing/src/test/java/org/oppia/android/testing/math/TokenSubjectTest.kt new file mode 100644 index 00000000000..a50967aa57d --- /dev/null +++ b/testing/src/test/java/org/oppia/android/testing/math/TokenSubjectTest.kt @@ -0,0 +1,217 @@ +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.util.math.MathTokenizer.Companion.Token + +/** Tests for [TokenSubject]. */ +@RunWith(JUnit4::class) +class TokenSubjectTest { + + @Test + fun testTokenSubject_passesWithCorrectStartIndex() { + val token = Token.PositiveInteger(42, 10, 15) + TokenSubject.assertThat(token).hasStartIndexThat().isEqualTo(10) + } + + @Test + fun testTokenSubject_failsWithIncorrectStartIndex() { + val token = Token.PositiveInteger(42, 10, 15) + assertThrows(AssertionError::class.java) { + TokenSubject.assertThat(token).hasStartIndexThat().isEqualTo(11) + } + } + + @Test + fun testTokenSubject_passesWithCorrectEndIndex() { + val token = Token.PositiveInteger(42, 10, 15) + TokenSubject.assertThat(token).hasEndIndexThat().isEqualTo(15) + } + + @Test + fun testTokenSubject_failsWithIncorrectEndIndex() { + val token = Token.PositiveInteger(42, 10, 15) + assertThrows(AssertionError::class.java) { + TokenSubject.assertThat(token).hasEndIndexThat().isEqualTo(14) + } + } + + @Test + fun testTokenSubject_passesWithCorrectPositiveIntegerValue() { + val token = Token.PositiveInteger(42, 10, 15) + TokenSubject.assertThat(token).isPositiveIntegerWhoseValue().isEqualTo(42) + } + + @Test + fun testTokenSubject_failsWithIncorrectPositiveIntegerValue() { + val token = Token.PositiveInteger(42, 10, 15) + assertThrows(AssertionError::class.java) { + TokenSubject.assertThat(token).isPositiveIntegerWhoseValue().isEqualTo(45) + } + } + + @Test + fun testTokenSubject_passesWithCoreectPositiveRealNumberValue() { + val token = Token.PositiveRealNumber(3.14, 10, 15) + TokenSubject.assertThat(token).isPositiveRealNumberWhoseValue().isEqualTo(3.14) + } + + fun testTokenSubject_failsWithIncorrectPositiveRealNumberValue() { + val token = Token.PositiveRealNumber(3.14, 10, 15) + assertThrows(AssertionError::class.java) { + TokenSubject.assertThat(token).isPositiveRealNumberWhoseValue().isEqualTo(25) + } + } + + @Test + fun testTokenSubject_passesWithCorrectVariableName() { + val token = Token.VariableName("x", 10, 15) + TokenSubject.assertThat(token).isVariableWhoseName().isEqualTo("x") + } + + @Test + fun testTokenSubject_isVariableWhoseName_incorrectName_fails() { + val token = Token.VariableName("x", 10, 15) + assertThrows(AssertionError::class.java) { + TokenSubject.assertThat(token).isVariableWhoseName().isEqualTo("y") + } + } + + @Test + fun testTokenSubject_passesWithCorrectFunctionNameAndAllowedStatus() { + val token = Token.FunctionName("sqrt", true, 10, 15) + TokenSubject.assertThat(token) + .isFunctionNameThat() + .hasNameThat().isEqualTo("sqrt") + } + + @Test + fun testTokenSubject_failsWithIncorrectFunctionNameAndAllowedStatus() { + val token = Token.FunctionName("sqrt", true, 10, 15) + assertThrows(AssertionError::class.java) { + TokenSubject.assertThat(token) + .isFunctionNameThat() + .hasNameThat().isEqualTo("sine") + } + } + + @Test + fun testTokenSubject_symbolIsMinusSymbol() { + val token = Token.MinusSymbol(10, 11) + TokenSubject.assertThat(token).isMinusSymbol() + } + + @Test + fun testTokenSubject_symbolIsSquareRootSymbol() { + val token = Token.SquareRootSymbol(10, 11) + TokenSubject.assertThat(token).isSquareRootSymbol() + } + + @Test + fun testTokenSubject_symbolIsPlusSymbol() { + val token = Token.PlusSymbol(10, 11) + TokenSubject.assertThat(token).isPlusSymbol() + } + + @Test + fun testTokenSubject_symbolIsMultiplySymbol() { + val token = Token.MultiplySymbol(10, 11) + TokenSubject.assertThat(token).isMultiplySymbol() + } + + @Test + fun testTokenSubject_symbolIsDivideSymbol() { + val token = Token.DivideSymbol(10, 11) + TokenSubject.assertThat(token).isDivideSymbol() + } + + @Test + fun testTokenSubject_symbolIsExponentiationSymbol() { + val token = Token.ExponentiationSymbol(10, 11) + TokenSubject.assertThat(token).isExponentiationSymbol() + } + + @Test + fun testTokenSubject_symbolIsEqualsSymbol() { + val token = Token.EqualsSymbol(10, 11) + TokenSubject.assertThat(token).isEqualsSymbol() + } + + @Test + fun testTokenSubject_symbolIsLeftParenthesisSymbol() { + val token = Token.LeftParenthesisSymbol(10, 11) + TokenSubject.assertThat(token).isLeftParenthesisSymbol() + } + + @Test + fun testTokenSubject_symbolIsRightParenthesisSymbol() { + val token = Token.RightParenthesisSymbol(10, 11) + TokenSubject.assertThat(token).isRightParenthesisSymbol() + } + + @Test + fun testTokenSubject_failsWithIncorrectSymbol() { + val token = Token.RightParenthesisSymbol(10, 11) + assertThrows(AssertionError::class.java) { + TokenSubject.assertThat(token).isMinusSymbol() + } + } + + @Test + fun testTokenSubject_checkIsInvalidToken_passes() { + val token = Token.InvalidToken(10, 11) + TokenSubject.assertThat(token).isInvalidToken() + } + + @Test + fun testTokenSubject_checkIsInvalidToken_fails() { + val token = Token.PositiveInteger(10, 11, 42) + assertThrows(AssertionError::class.java) { + TokenSubject.assertThat(token).isInvalidToken() + } + } + + @Test + fun testTokenSubject_checkIsIncompleteFunctionName() { + val token = Token.IncompleteFunctionName(10, 11) + TokenSubject.assertThat(token).isIncompleteFunctionName() + } + + @Test + fun testTokenSubject_functionNameSubject_nameCheck_passes() { + val token = Token.FunctionName("sqrt", true, 10, 15) + TokenSubject.assertThat(token) + .isFunctionNameThat() + .hasNameThat().isEqualTo("sqrt") + } + + @Test + fun testTokenSubject_functionNameSubject_nameCheck_fails() { + val token = Token.FunctionName("sqrt", true, 10, 15) + assertThrows(AssertionError::class.java) { + TokenSubject.assertThat(token) + .isFunctionNameThat() + .hasNameThat().isEqualTo("sin") + } + } + + @Test + fun testTokenSubject_functionNameSubject_allowedPropertyCheck_passes() { + val token = Token.FunctionName("sqrt", true, 10, 15) + TokenSubject.assertThat(token) + .isFunctionNameThat() + .hasIsAllowedPropertyThat().isTrue() + } + + @Test + fun testTokenSubject_functionNameSubject_allowedPropertyCheck_fails() { + val token = Token.FunctionName("sqrt", false, 10, 15) + assertThrows(AssertionError::class.java) { + TokenSubject.assertThat(token) + .isFunctionNameThat() + .hasIsAllowedPropertyThat().isTrue() + } + } +}