Skip to content

Commit

Permalink
Fix oppia#4097: Adding tests for math utils (oppia#5627)
Browse files Browse the repository at this point in the history
<!-- READ ME FIRST: Please fill in the explanation section below and
check off every point from the Essential Checklist! -->
## Explanation
<!--
- Explain what your PR does. If this PR fixes an existing bug, please
include
- "Fixes #bugnum:" in the explanation so that GitHub can auto-close the
issue
  - when this PR is merged.
  -->
Fix oppia#4097
This PR adds test files for the `FractionSubject`, `RealSubject`,
`MathExpressionSubject`, and `MathEquationSubject` classes and includes
a `BUILD.bazel` file to enable building and running these tests.

## 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)).
  • Loading branch information
manas-yu authored Jan 15, 2025
1 parent 164956c commit fc92932
Show file tree
Hide file tree
Showing 5 changed files with 1,244 additions and 0 deletions.
69 changes: 69 additions & 0 deletions testing/src/test/java/org/oppia/android/testing/math/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
Tests for math-related test utilities.
"""

load("//:oppia_android_test.bzl", "oppia_android_test")

oppia_android_test(
name = "FractionSubjectTest",
srcs = ["FractionSubjectTest.kt"],
custom_package = "org.oppia.android.testing.math",
test_class = "org.oppia.android.testing.math.FractionSubjectTest",
test_manifest = "//testing:test_manifest",
deps = [
"//model/src/main/proto:math_java_proto_lite",
"//testing/src/main/java/org/oppia/android/testing/math:fraction_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",
],
)

oppia_android_test(
name = "MathEquationSubjectTest",
srcs = ["MathEquationSubjectTest.kt"],
custom_package = "org.oppia.android.testing.math",
test_class = "org.oppia.android.testing.math.MathEquationSubjectTest",
test_manifest = "//testing:test_manifest",
deps = [
"//model/src/main/proto:math_java_proto_lite",
"//testing/src/main/java/org/oppia/android/testing/math:math_equation_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",
],
)

oppia_android_test(
name = "MathExpressionSubjectTest",
srcs = ["MathExpressionSubjectTest.kt"],
custom_package = "org.oppia.android.testing.math",
test_class = "org.oppia.android.testing.math.MathExpressionSubjectTest",
test_manifest = "//testing:test_manifest",
deps = [
"//model/src/main/proto:math_java_proto_lite",
"//testing/src/main/java/org/oppia/android/testing/math:math_expression_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",
],
)

oppia_android_test(
name = "RealSubjectTest",
srcs = ["RealSubjectTest.kt"],
custom_package = "org.oppia.android.testing.math",
test_class = "org.oppia.android.testing.math.RealSubjectTest",
test_manifest = "//testing:test_manifest",
deps = [
"//model/src/main/proto:math_java_proto_lite",
"//testing/src/main/java/org/oppia/android/testing/math:real_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,206 @@
package org.oppia.android.testing.math

import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.oppia.android.app.model.Fraction

/** Tests for [FractionSubject]. */
@RunWith(JUnit4::class)
class FractionSubjectTest {

@Test
fun testHasNegativeProperty_withNegativeFraction_matchesTrue() {
val fraction = Fraction.newBuilder()
.setIsNegative(true)
.build()

FractionSubject.assertThat(fraction).hasNegativePropertyThat().isTrue()
}

@Test
fun testHasNegativeProperty_withPositiveFraction_matchesFalse() {
val fraction = Fraction.newBuilder()
.setIsNegative(false)
.build()

FractionSubject.assertThat(fraction).hasNegativePropertyThat().isFalse()
}

@Test
fun testHasNegativeProperty_defaultValue_matchesFalse() {
val fraction = Fraction.newBuilder().build()

FractionSubject.assertThat(fraction).hasNegativePropertyThat().isFalse()
}

@Test
fun testHasWholeNumber_withPositiveValue_matchesValue() {
val fraction = Fraction.newBuilder()
.setWholeNumber(5)
.build()

FractionSubject.assertThat(fraction).hasWholeNumberThat().isEqualTo(5)
}

@Test
fun testHasWholeNumber_withMaxUint32_matchesValue() {
val fraction = Fraction.newBuilder()
.setWholeNumber(Int.MAX_VALUE)
.build()

FractionSubject.assertThat(fraction).hasWholeNumberThat().isEqualTo(Int.MAX_VALUE)
}

@Test
fun testHasWholeNumber_defaultValue_matchesZero() {
val fraction = Fraction.newBuilder().build()

FractionSubject.assertThat(fraction).hasWholeNumberThat().isEqualTo(0)
}

@Test
fun testHasNumerator_withPositiveValue_matchesValue() {
val fraction = Fraction.newBuilder()
.setNumerator(3)
.build()

FractionSubject.assertThat(fraction).hasNumeratorThat().isEqualTo(3)
}

@Test
fun testHasNumerator_withMaxUint32_matchesValue() {
val fraction = Fraction.newBuilder()
.setNumerator(Int.MAX_VALUE)
.build()

FractionSubject.assertThat(fraction).hasNumeratorThat().isEqualTo(Int.MAX_VALUE)
}

@Test
fun testHasNumerator_defaultValue_matchesZero() {
val fraction = Fraction.newBuilder().build()

FractionSubject.assertThat(fraction).hasNumeratorThat().isEqualTo(0)
}

@Test
fun testHasDenominator_withPositiveValue_matchesValue() {
val fraction = Fraction.newBuilder()
.setDenominator(4)
.build()

FractionSubject.assertThat(fraction).hasDenominatorThat().isEqualTo(4)
}

@Test
fun testHasDenominator_withMaxUint32_matchesValue() {
val fraction = Fraction.newBuilder()
.setDenominator(Int.MAX_VALUE)
.build()

FractionSubject.assertThat(fraction).hasDenominatorThat().isEqualTo(Int.MAX_VALUE)
}

@Test
fun testHasDenominator_defaultValue_matchesZero() {
val fraction = Fraction.newBuilder().build()

FractionSubject.assertThat(fraction).hasDenominatorThat().isEqualTo(0)
}

@Test
fun testEvaluatesToDouble_withProperFraction_matchesExpectedValue() {
val fraction = Fraction.newBuilder()
.setNumerator(3)
.setDenominator(4)
.build()

FractionSubject.assertThat(fraction).evaluatesToDoubleThat().isEqualTo(0.75)
}

@Test
fun testEvaluatesToDouble_withImproperFraction_matchesExpectedValue() {
val fraction = Fraction.newBuilder()
.setNumerator(5)
.setDenominator(2)
.build()

FractionSubject.assertThat(fraction).evaluatesToDoubleThat().isEqualTo(2.5)
}

@Test
fun testEvaluatesToDouble_withMixedNumber_matchesExpectedValue() {
val fraction = Fraction.newBuilder()
.setWholeNumber(2)
.setNumerator(3)
.setDenominator(4)
.build()

FractionSubject.assertThat(fraction).evaluatesToDoubleThat().isEqualTo(2.75)
}

@Test
fun testEvaluatesToDouble_withNegativeValue_matchesExpectedValue() {
val fraction = Fraction.newBuilder()
.setIsNegative(true)
.setWholeNumber(2)
.setNumerator(1)
.setDenominator(2)
.build()

FractionSubject.assertThat(fraction).evaluatesToDoubleThat().isEqualTo(-2.5)
}

@Test
fun testProtoEquality_withIdenticalValues_areEqual() {
val fraction1 = Fraction.newBuilder()
.setIsNegative(true)
.setWholeNumber(3)
.setNumerator(2)
.setDenominator(5)
.build()

val fraction2 = Fraction.newBuilder()
.setIsNegative(true)
.setWholeNumber(3)
.setNumerator(2)
.setDenominator(5)
.build()

assertThat(fraction1).isEqualTo(fraction2)
}

@Test
fun testProtoSerialization_withComplexFraction_maintainsValues() {
val originalFraction = Fraction.newBuilder()
.setIsNegative(true)
.setWholeNumber(3)
.setNumerator(2)
.setDenominator(5)
.build()

val bytes = originalFraction.toByteArray()
val deserializedFraction = Fraction.parseFrom(bytes)

FractionSubject.assertThat(deserializedFraction).apply {
hasNegativePropertyThat().isTrue()
hasWholeNumberThat().isEqualTo(3)
hasNumeratorThat().isEqualTo(2)
hasDenominatorThat().isEqualTo(5)
}
}

@Test
fun testDefaultInstance_hasDefaultValues() {
val defaultFraction = Fraction.getDefaultInstance()

FractionSubject.assertThat(defaultFraction).apply {
hasNegativePropertyThat().isFalse()
hasWholeNumberThat().isEqualTo(0)
hasNumeratorThat().isEqualTo(0)
hasDenominatorThat().isEqualTo(0)
}
}
}
Loading

0 comments on commit fc92932

Please sign in to comment.