Skip to content

Commit

Permalink
Fix oppia#4121: Added tests for TokenSubject (oppia#5669)
Browse files Browse the repository at this point in the history
Fixes oppia#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
<!-- Please tick the relevant boxes by putting an "x" in them. -->
- [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 <your.email@example.com>
  • Loading branch information
theayushyadav11 and theayushyadav11 authored Feb 13, 2025
1 parent 1586f27 commit 893c7a2
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 6 deletions.
4 changes: 0 additions & 4 deletions scripts/assets/test_file_exemptions.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions testing/src/test/java/org/oppia/android/testing/math/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
Original file line number Diff line number Diff line change
@@ -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()
}
}
}

0 comments on commit 893c7a2

Please sign in to comment.