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 all 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/mockito/MockitoKotlinHelper.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 @@ -96,3 +96,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,300 @@
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.testing.math.MathParsingErrorSubject.Companion.assertThat
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
assertThat(error).isSpacesBetweenNumbers()
}

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

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

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

@Test
fun testMathParsingErrorSubject_hasSingleRedundantParenthesesWitDetails() {
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)

assertThat(error).isSingleRedundantParenthesesThat().apply {
hasRawExpressionThat().isEqualTo("(5)")
hasExpressionThat().hasStructureThatMatches {
group {
constant {
hasExpressionThat().evaluatesToIntegerThat().isEqualTo(5)
}
}
}
}
}

@Test
fun testMathParsingErrorSubject_hasMultipleRedundantParenthesesWithDetails() {
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)

assertThat(error).isMultipleRedundantParenthesesThat().apply {
hasRawExpressionThat().isEqualTo("((5))")
hasExpressionThat().hasStructureThatMatches {
group {
group {
constant {
hasExpressionThat().evaluatesToIntegerThat().isEqualTo(5)
}
}
}
}
}
}

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

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

@Test
fun testMathParsingErrorSubject_isNumberAfterVariableError() {
val number = Real.newBuilder().setInteger(5).build()
val error = NumberAfterVariableError(number, "x")

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) {
assertThat(error).isNumberAfterVariableThat().apply {
hasNumberThat().isIntegerThat().isEqualTo(5)
hasVariableThat().isEqualTo("y")
}
}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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