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 #4132: Added tests for MathParsingErrorSubject. #5684

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
4 changes: 0 additions & 4 deletions scripts/assets/test_file_exemptions.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -4102,10 +4102,6 @@ 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
}
test_file_exemption {
exempted_file_path: "testing/src/main/java/org/oppia/android/testing/math/TokenSubject.kt"
test_file_not_required: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ import org.oppia.android.util.math.MathParsingError.UnbalancedParenthesesError
import org.oppia.android.util.math.MathParsingError.UnnecessarySymbolsError
import org.oppia.android.util.math.MathParsingError.VariableInNumericExpressionError

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

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

oppia_android_test(
name = "MathParsingErrorSubjectTest",
srcs = ["MathParsingErrorSubjectTest.kt"],
custom_package = "org.oppia.android.testing.math",
test_class = "org.oppia.android.testing.math.MathParsingErrorSubjectTest",
test_manifest = "//testing:test_manifest",
deps = [
"//model/src/main/proto:math_java_proto_lite",
"//testing/src/main/java/org/oppia/android/testing/math:math_parsing_error_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",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
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.MathBinaryOperation
import org.oppia.android.app.model.MathExpression
import org.oppia.android.app.model.Real
import org.oppia.android.util.math.MathParsingError.DisabledVariablesInUseError
import org.oppia.android.util.math.MathParsingError.EquationHasTooManyEqualsError
import org.oppia.android.util.math.MathParsingError.EquationIsMissingEqualsError
import org.oppia.android.util.math.MathParsingError.EquationMissingLhsOrRhsError
import org.oppia.android.util.math.MathParsingError.ExponentIsVariableExpressionError
import org.oppia.android.util.math.MathParsingError.ExponentTooLargeError
import org.oppia.android.util.math.MathParsingError.FunctionNameIncompleteError
import org.oppia.android.util.math.MathParsingError.GenericError
import org.oppia.android.util.math.MathParsingError.HangingSquareRootError
import org.oppia.android.util.math.MathParsingError.InvalidFunctionInUseError
import org.oppia.android.util.math.MathParsingError.MultipleRedundantParenthesesError
import org.oppia.android.util.math.MathParsingError.NestedExponentsError
import org.oppia.android.util.math.MathParsingError.NoVariableOrNumberAfterBinaryOperatorError
import org.oppia.android.util.math.MathParsingError.NoVariableOrNumberBeforeBinaryOperatorError
import org.oppia.android.util.math.MathParsingError.NumberAfterVariableError
import org.oppia.android.util.math.MathParsingError.SingleRedundantParenthesesError
import org.oppia.android.util.math.MathParsingError.SpacesBetweenNumbersError
import org.oppia.android.util.math.MathParsingError.SubsequentBinaryOperatorsError
import org.oppia.android.util.math.MathParsingError.SubsequentUnaryOperatorsError
import org.oppia.android.util.math.MathParsingError.TermDividedByZeroError
import org.oppia.android.util.math.MathParsingError.UnbalancedParenthesesError
import org.oppia.android.util.math.MathParsingError.UnnecessarySymbolsError
import org.oppia.android.util.math.MathParsingError.VariableInNumericExpressionError

/** Tests for [MathParsingErrorSubject]. */
@RunWith(JUnit4::class)
class MathParsingErrorSubjectTest {

@Test
fun testMathParsingErrorSubject_hasSpaceBetweenNumbersError() {
val error = SpacesBetweenNumbersError
MathParsingErrorSubject.assertThat(error).isSpacesBetweenNumbers()
}

@Test
fun testMathParsingErrorSubject_hasSpaceBetweenNumbersError_fails() {
val error = UnbalancedParenthesesError
assertThrows(AssertionError::class.java) {
MathParsingErrorSubject.assertThat(error).isSpacesBetweenNumbers()
}
}

@Test
fun testMathParsingErrorSubject_hasUnbalancedParenthesesError() {
val error = UnbalancedParenthesesError
MathParsingErrorSubject.assertThat(error).isUnbalancedParentheses()
}

@Test
fun testMathParsingErrorSubject_hasUnbalancedParenthesesError_fails() {
val error = SpacesBetweenNumbersError
assertThrows(AssertionError::class.java) {
MathParsingErrorSubject.assertThat(error).isUnbalancedParentheses()
}
}

@Test
fun testMathParsingErrorSubject_hasSingleRedundantParentheses() {
val constant = Real.newBuilder()
.setInteger(5)
.build()
val expression = MathExpression.newBuilder()
.setConstant(constant)
.build()
val group = MathExpression.newBuilder()
.setGroup(expression)
.build()
val error = SingleRedundantParenthesesError("(5)", group)
val subject = MathParsingErrorSubject.assertThat(error).isSingleRedundantParenthesesThat()
subject.hasExpressionThat().evaluatesToIntegerThat().isEqualTo(5)
subject.hasRawExpressionThat().isEqualTo("(5)")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the test below can be better structure following the example usage set in

fun testParseNumExp_outerExpressionGrouped_returnsRedundantParensErrorWithDetails() {


@Test
fun testMathParsingErrorSubject_hasMultipleRedundantParentheses() {
val constant = Real.newBuilder()
.setInteger(5)
.build()
val expression = MathExpression.newBuilder()
.setConstant(constant)
.build()
val groupOne = MathExpression.newBuilder()
.setGroup(expression)
.build()
val groupTwo = MathExpression.newBuilder()
.setGroup(groupOne)
.build()
val error = MultipleRedundantParenthesesError("((5))", groupTwo)
val subject = MathParsingErrorSubject.assertThat(error).isMultipleRedundantParenthesesThat()
subject.hasExpressionThat().evaluatesToIntegerThat().isEqualTo(5)
subject.hasRawExpressionThat().isEqualTo("((5))")
}

@Test
fun testMathParsingErrorSubject_matchesUnnecessarySymbol() {
val error = UnnecessarySymbolsError("@")
MathParsingErrorSubject.assertThat(error).isUnnecessarySymbolWithSymbolThat().isEqualTo("@")
}

@Test
fun testMathParsingErrorSubject_matchesUnnecessarySymbol_fails() {
val error = UnnecessarySymbolsError("@")
assertThrows(AssertionError::class.java) {
MathParsingErrorSubject.assertThat(error).isUnnecessarySymbolWithSymbolThat().isEqualTo("#")
}
}

@Test
fun testMathParsingErrorSubject_isNumberAfterVariableError() {
val number = Real.newBuilder().setInteger(5).build()
val error = NumberAfterVariableError(number, "x")
val subject = MathParsingErrorSubject.assertThat(error).isNumberAfterVariableThat()
subject.hasNumberThat().isIntegerThat().isEqualTo(5)
subject.hasVariableThat().isEqualTo("x")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and all tests below: It is not clear why you are creating the subject variable since it is not being used. Assertions are independent expressions and should not be assigned to a variable.

Suggested change
val subject = MathParsingErrorSubject.assertThat(error).isNumberAfterVariableThat()
subject.hasNumberThat().isIntegerThat().isEqualTo(5)
subject.hasVariableThat().isEqualTo("x")
}
val subject = MathParsingErrorSubject.assertThat(error).isNumberAfterVariableThat().apply {
hasNumberThat().isIntegerThat().isEqualTo(5)
hasVariableThat().isEqualTo("x")
}
}


@Test
fun testMathParsingErrorSubject_isNumberAfterVariableError_fails() {
val number = Real.newBuilder().setInteger(5).build()
val error = NumberAfterVariableError(number, "x")
assertThrows(AssertionError::class.java) {
val subject = MathParsingErrorSubject.assertThat(error).isNumberAfterVariableThat()
subject.hasNumberThat().isIntegerThat().isEqualTo(6)
subject.hasVariableThat().isEqualTo("y")
}
}

@Test
fun testMathParsingErrorSubject_isSubsequentBinaryOperatorsError() {
val error = SubsequentBinaryOperatorsError("x", "+")
val subject = MathParsingErrorSubject.assertThat(error).isSubsequentBinaryOperatorsThat()
subject.hasFirstOperatorThat().isEqualTo("x")
subject.hasSecondOperatorThat().isEqualTo("+")
}

@Test
fun testMathParsingErrorSubject_isSubsequentBinaryOperatorsError_fails() {
val error = SubsequentBinaryOperatorsError("x", "+")
assertThrows(AssertionError::class.java) {
val subject = MathParsingErrorSubject.assertThat(error).isSubsequentBinaryOperatorsThat()
subject.hasFirstOperatorThat().isEqualTo("y")
subject.hasSecondOperatorThat().isEqualTo("-")
}
}

@Test
fun testMathParsingErrorSubject_isSubsequentUnaryOperatorsError() {
val error = SubsequentUnaryOperatorsError
MathParsingErrorSubject.assertThat(error).isSubsequentUnaryOperators()
}

@Test
fun testMathParsingErrorSubject_isNoVarOrNumBeforeBinaryOperator() {
val operator = MathBinaryOperation.Operator.ADD
val error = NoVariableOrNumberBeforeBinaryOperatorError(operator, "+")
val subject = MathParsingErrorSubject.assertThat(error).isNoVarOrNumBeforeBinaryOperatorThat()
subject.hasOperatorThat().isEqualTo(operator)
subject.hasOperatorSymbolThat().isEqualTo("+")
}

@Test
fun testMathParsingErrorSubject_isNoVarOrNumBeforeBinaryOperator_fails() {
val operator = MathBinaryOperation.Operator.ADD
val error = NoVariableOrNumberBeforeBinaryOperatorError(operator, "+")
assertThrows(AssertionError::class.java) {
val subject = MathParsingErrorSubject.assertThat(error).isNoVarOrNumBeforeBinaryOperatorThat()
subject.hasOperatorThat().isEqualTo(MathBinaryOperation.Operator.SUBTRACT)
subject.hasOperatorSymbolThat().isEqualTo("-")
}
}

@Test
fun testMathParsingErrorSubject_isNoVariableOrNumberAfterBinaryOperator() {
val operator = MathBinaryOperation.Operator.ADD
val error = NoVariableOrNumberAfterBinaryOperatorError(operator, "+")
val subject = MathParsingErrorSubject.assertThat(error)
.isNoVariableOrNumberAfterBinaryOperatorThat()
subject.hasOperatorThat().isEqualTo(operator)
subject.hasOperatorSymbolThat().isEqualTo("+")
}

@Test
fun testMathParsingErrorSubject_isNoVariableOrNumberAfterBinaryOperator_fails() {
val operator = MathBinaryOperation.Operator.ADD
val error = NoVariableOrNumberAfterBinaryOperatorError(operator, "+")
assertThrows(AssertionError::class.java) {
val subject = MathParsingErrorSubject.assertThat(error)
.isNoVariableOrNumberAfterBinaryOperatorThat()
subject.hasOperatorThat().isEqualTo(MathBinaryOperation.Operator.SUBTRACT)
subject.hasOperatorSymbolThat().isEqualTo("-")
}
}

@Test
fun testMathParsingErrorSubject_isExponentIsVariableExpressionError() {
val error = ExponentIsVariableExpressionError
MathParsingErrorSubject.assertThat(error).isExponentIsVariableExpression()
}

@Test
fun testMathParsingErrorSubject_isExponentTooLargeError() {
val error = ExponentTooLargeError
MathParsingErrorSubject.assertThat(error).isExponentTooLarge()
}

@Test
fun testMathParsingErrorSubject_isNestedExponentsError() {
val error = NestedExponentsError
MathParsingErrorSubject.assertThat(error).isNestedExponents()
}

@Test
fun testMathParsingErrorSubject_isHangingSquareRootError() {
val error = HangingSquareRootError
MathParsingErrorSubject.assertThat(error).isHangingSquareRoot()
}

@Test
fun testMathParsingErrorSubject_isTermDividedByZeroError() {
val error = TermDividedByZeroError
MathParsingErrorSubject.assertThat(error).isTermDividedByZero()
}

@Test
fun testMathParsingErrorSubject_isVariableInNumericExpressionError() {
val error = VariableInNumericExpressionError
MathParsingErrorSubject.assertThat(error).isVariableInNumericExpression()
}

@Test
fun testMathParsingErrorSubject_isDisabledVariablesInUseWithVariablesError() {
val error = DisabledVariablesInUseError(listOf("x", "y"))
val subject = MathParsingErrorSubject.assertThat(error)
.isDisabledVariablesInUseWithVariablesThat()
subject.containsExactly("x", "y")
}

@Test
fun testMathParsingErrorSubject_isDisabledVariablesInUseWithVariablesError_fails() {
val error = DisabledVariablesInUseError(listOf("x", "y"))
assertThrows(AssertionError::class.java) {
val subject = MathParsingErrorSubject.assertThat(error)
.isDisabledVariablesInUseWithVariablesThat()
subject.containsExactly("x", "z")
}
}

@Test
fun testMathParsingErrorSubject_isEquationIsMissingEqualsError() {
val error = EquationIsMissingEqualsError
MathParsingErrorSubject.assertThat(error).isEquationIsMissingEquals()
}

@Test
fun testMathParsingErrorSubject_isEquationHasTooManyEqualsError() {
val error = EquationHasTooManyEqualsError
MathParsingErrorSubject.assertThat(error).isEquationHasTooManyEquals()
}

@Test
fun testMathParsingErrorSubject_isEquationMissingLhsOrRhsError() {
val error = EquationMissingLhsOrRhsError
MathParsingErrorSubject.assertThat(error).isEquationMissingLhsOrRhs()
}

@Test
fun testMathParsingErrorSubject_isInvalidFunctionInUseWithNameError() {
val error = InvalidFunctionInUseError("sin")
val subject = MathParsingErrorSubject.assertThat(error).isInvalidFunctionInUseWithNameThat()
subject.isEqualTo("sin")
}

@Test
fun testMathParsingErrorSubject_isFunctionNameIncompleteError() {
val error = FunctionNameIncompleteError
MathParsingErrorSubject.assertThat(error).isFunctionNameIncomplete()
}

@Test
fun testMathParsingErrorSubject_isGenericError() {
val error = GenericError
MathParsingErrorSubject.assertThat(error).isGenericError()
}
}
Loading