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

Fix part of #4044: Add support for expression evaluation & conversion to LaTeX #4054

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
179 commits
Select commit Hold shift + click to select a range
17472e7
Copy proto-based changes from #2173.
BenHenning Dec 14, 2021
fe73a2f
Introduce math.proto & refactor math extensions.
BenHenning Dec 15, 2021
d17e3dc
Migrate tests & remove unneeded prefix.
BenHenning Dec 15, 2021
fb61e39
Add needed newline.
BenHenning Dec 15, 2021
acab98b
Some needed Fraction changes.
BenHenning Dec 15, 2021
02e930f
Introduce math expression + equation protos.
BenHenning Dec 15, 2021
e45635d
Add protos + testing lib for commutative exprs.
BenHenning Dec 15, 2021
da5d72d
Add protos & test libs for polynomials.
BenHenning Dec 15, 2021
7c36fdf
Lint fix.
BenHenning Dec 15, 2021
ed290a0
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Dec 15, 2021
70e8039
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Dec 15, 2021
b957284
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Dec 15, 2021
d430f8c
Lint fixes.
BenHenning Dec 15, 2021
0099d67
Add math tokenizer + utility & tests.
BenHenning Dec 15, 2021
1d721d5
Add math expression/equation parsing support.
BenHenning Dec 15, 2021
22ae591
Add exp evaluation & LaTeX conversion support.
BenHenning Dec 15, 2021
d9d4963
Remove unneeded comment lines.
BenHenning Dec 15, 2021
ac87d8e
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Dec 15, 2021
d5dd596
Fix broken test post-refactor.
BenHenning Dec 16, 2021
ff813ad
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Dec 16, 2021
a77499c
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Dec 16, 2021
31022f0
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Dec 16, 2021
028e821
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Dec 16, 2021
c2ac696
Merge branch 'add-support-for-math-expressions-pt6-tokenizer' into ad…
BenHenning Dec 16, 2021
b75627c
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Dec 16, 2021
aceddf8
Add reasonable import for abs().
BenHenning Dec 16, 2021
312708c
Fix equals errors for equations.
BenHenning Jan 8, 2022
d08260b
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Jan 8, 2022
9826b11
Fix rational^rational powers.
BenHenning Jan 8, 2022
a1d481d
Merge branch 'develop' into add-support-for-math-expressions-pt1-fix-…
BenHenning Jan 13, 2022
7740c81
Post-merge fix.
BenHenning Jan 13, 2022
929de4f
Add regex check, docs, and resolve TODOs.
BenHenning Jan 14, 2022
e50a50f
Lint fix.
BenHenning Jan 14, 2022
c2726ac
Merge branch 'add-support-for-math-expressions-pt1-fix-proto-imports'…
BenHenning Jan 14, 2022
ec575b7
Fix failing static checks.
BenHenning Jan 14, 2022
5fe790a
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Jan 14, 2022
1268fc5
Fix broken CI checks.
BenHenning Jan 14, 2022
977eb9e
Lint fixes.
BenHenning Jan 14, 2022
a7ab1a5
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Jan 14, 2022
eed21d5
Add docs & exempted tests.
BenHenning Jan 14, 2022
b1102a3
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Jan 14, 2022
080e7dd
Remove blank line.
BenHenning Jan 14, 2022
904aad8
Add docs + tests.
BenHenning Jan 19, 2022
61fbfe5
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Jan 19, 2022
fca0cd9
Add parameterized test runner.
BenHenning Jan 21, 2022
83e800c
Add & update tests.
BenHenning Jan 21, 2022
3fa8dad
Lint fixes.
BenHenning Jan 21, 2022
87a41db
Remove internals that broke things.
BenHenning Jan 21, 2022
c0172ce
Add regex exemptions.
BenHenning Jan 21, 2022
8723ee8
Merge branch 'add-support-for-math-expressions-pt6-tokenizer' into ad…
BenHenning Jan 21, 2022
ff41eeb
Post-merge fix.
BenHenning Jan 21, 2022
f5b6571
Add error tests.
BenHenning Jan 22, 2022
b7ac054
Finish algebraic equation tests.
BenHenning Jan 22, 2022
a122515
Reimplement numeric expression tests.
BenHenning Jan 22, 2022
984e740
Finish algebraic expression tests.
BenHenning Jan 22, 2022
042a1ad
Add missing tests for better coverage.
BenHenning Jan 24, 2022
8ead9bf
Add KDocs & test exemptions.
BenHenning Jan 25, 2022
aeec35c
Lint fixes.
BenHenning Jan 25, 2022
57e6f5b
Remove temporary TODOs.
BenHenning Jan 25, 2022
10eac78
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Jan 25, 2022
07be596
Add tests.
BenHenning Jan 26, 2022
19a6425
Split StringToFractionParser.
BenHenning Jan 26, 2022
444d3d0
Merge branch 'develop' into add-support-for-math-expressions-pt1-fix-…
BenHenning Jan 26, 2022
cb5c5d1
Merge branch 'add-support-for-math-expressions-pt1-fix-proto-imports'…
BenHenning Jan 27, 2022
7a364c3
Address reviewer comments + other stuff.
BenHenning Jan 27, 2022
2c0b9cd
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Jan 27, 2022
4364d7f
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Jan 27, 2022
cfe6cab
Move StringExtensions & fraction parsing.
BenHenning Jan 27, 2022
5e83a33
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Jan 27, 2022
c6cf5f5
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Jan 27, 2022
73dccd8
Address reviewer comments.
BenHenning Jan 27, 2022
b7535fa
Alphabetize test exemptions.
BenHenning Jan 27, 2022
f4cd6cd
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Jan 27, 2022
574ebb8
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Jan 27, 2022
a00164c
Fix typo & add regex check.
BenHenning Jan 27, 2022
0287f19
Add missing KDocs.
BenHenning Jan 27, 2022
75103d5
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Jan 27, 2022
52d2605
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Jan 27, 2022
37dbc85
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Jan 27, 2022
385d2d3
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Jan 27, 2022
4bacc8a
Merge branch 'add-support-for-math-expressions-pt6-tokenizer' into ad…
BenHenning Jan 27, 2022
1e5279d
Post-merge cleanups.
BenHenning Jan 27, 2022
bbf7e2d
Add new test for negation with math symbol.
BenHenning Jan 27, 2022
cf72c8d
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Jan 27, 2022
ba4128c
Post-merge fixes.
BenHenning Jan 27, 2022
fd09758
Add KDocs.
BenHenning Jan 27, 2022
01c2326
Refactor & simplify real ext impl.
BenHenning Jan 27, 2022
98d6939
Lint fixes.
BenHenning Jan 28, 2022
65a9fe1
Remove the ComparableOperationList wrapper.
BenHenning Jan 28, 2022
fa4de34
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Jan 28, 2022
e2d226f
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Jan 28, 2022
fbd935c
Change parameterized method delimiter.
BenHenning Jan 28, 2022
5b6d1f2
Merge branch 'add-support-for-math-expressions-pt6-tokenizer' into ad…
BenHenning Jan 28, 2022
fd11a79
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Jan 28, 2022
8420c56
Use utility directly in test.
BenHenning Jan 28, 2022
d20256c
Use more intentional epsilons for float comparing.
BenHenning Feb 4, 2022
cdaf778
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Feb 4, 2022
7e97d0b
Treat en-dash as a subtraction symbol.
BenHenning Feb 4, 2022
a9c68b1
Add explicit platform selection for paramerized.
BenHenning Feb 5, 2022
a273ee6
Exemption fixes.
BenHenning Feb 5, 2022
924214d
Merge branch 'develop' into add-support-for-math-expressions-pt1-fix-…
BenHenning Feb 5, 2022
686c4b6
Merge branch 'add-support-for-math-expressions-pt1-fix-proto-imports'…
BenHenning Feb 5, 2022
1a3cba5
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Feb 5, 2022
bff824e
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Feb 5, 2022
90b05bf
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Feb 5, 2022
09e2aad
Remove failing test.
BenHenning Feb 5, 2022
c7c2e4b
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Feb 5, 2022
8bdf183
Merge branch 'add-support-for-math-expressions-pt6-tokenizer' into ad…
BenHenning Feb 5, 2022
b48d06a
Fix unary expression precedence.
BenHenning Feb 5, 2022
8ac22cb
Fixes & add more test cases.
BenHenning Feb 5, 2022
cdee008
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Feb 5, 2022
476e604
Post-merge fixes & test changes.
BenHenning Feb 5, 2022
5ed0013
Use utility directly in LaTeX tests.
BenHenning Feb 5, 2022
ecc836e
Merge branch 'develop' into add-support-for-math-expressions-pt1-fix-…
BenHenning Feb 18, 2022
b183d67
Merge branch 'add-support-for-math-expressions-pt1-fix-proto-imports'…
BenHenning Feb 18, 2022
005d9a0
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Feb 18, 2022
ba198fd
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Feb 18, 2022
1f2b90d
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Feb 18, 2022
86e90f7
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Feb 18, 2022
a6dc7d4
Address reviewer comment.
BenHenning Feb 18, 2022
a6d1111
Merge branch 'add-support-for-math-expressions-pt6-tokenizer' into ad…
BenHenning Feb 18, 2022
64a1b55
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Feb 18, 2022
fd9ec1f
Fix broken build.
BenHenning Feb 18, 2022
960287d
Merge branch 'add-support-for-math-expressions-pt1-fix-proto-imports'…
BenHenning Feb 18, 2022
b85567e
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Feb 18, 2022
2ed67c5
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Feb 18, 2022
3744cf2
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Feb 18, 2022
c57e792
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Feb 18, 2022
056f547
Merge branch 'add-support-for-math-expressions-pt6-tokenizer' into ad…
BenHenning Feb 18, 2022
5431e27
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Feb 18, 2022
0fc8a1b
Fix broken build post-merge.
BenHenning Feb 18, 2022
e4b92e5
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Feb 18, 2022
9eb1c2d
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Feb 18, 2022
1f3fb48
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Feb 18, 2022
24ac61c
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Feb 18, 2022
c74aee0
Merge branch 'add-support-for-math-expressions-pt6-tokenizer' into ad…
BenHenning Feb 18, 2022
e814df8
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Feb 18, 2022
c01f939
Merge branch 'develop' into add-support-for-math-expressions-pt1-fix-…
BenHenning Feb 24, 2022
14f7057
Merge branch 'add-support-for-math-expressions-pt1-fix-proto-imports'…
BenHenning Feb 24, 2022
b98e1ec
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Feb 24, 2022
b50c40f
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Feb 24, 2022
6d0cc9e
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Feb 24, 2022
2217f7d
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Feb 24, 2022
a9adda1
Merge branch 'add-support-for-math-expressions-pt6-tokenizer' into ad…
BenHenning Feb 24, 2022
ac63847
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Feb 24, 2022
1d74aed
Merge branch 'develop' into add-support-for-math-expressions-pt1-fix-…
BenHenning Mar 7, 2022
2ae10ec
Merge branch 'add-support-for-math-expressions-pt1-fix-proto-imports'…
BenHenning Mar 7, 2022
60670a5
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Mar 7, 2022
473b562
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Mar 7, 2022
73a052f
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Mar 7, 2022
e35b13f
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Mar 7, 2022
9aa8315
Merge branch 'add-support-for-math-expressions-pt6-tokenizer' into ad…
BenHenning Mar 7, 2022
e8b8f9f
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Mar 7, 2022
8945c3a
Merge branch 'develop' into add-support-for-math-expressions-pt1-fix-…
BenHenning Mar 17, 2022
391cf22
Post-merge fix.
BenHenning Mar 17, 2022
7918001
Merge branch 'add-support-for-math-expressions-pt1-fix-proto-imports'…
BenHenning Mar 17, 2022
c29a47c
More post-merge fixes.
BenHenning Mar 17, 2022
19b3800
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Mar 17, 2022
aa4fd38
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Mar 17, 2022
eb2bcc3
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Mar 17, 2022
8bd314f
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Mar 17, 2022
429ee02
Merge branch 'add-support-for-math-expressions-pt6-tokenizer' into ad…
BenHenning Mar 17, 2022
445664a
Fix TODO comment.
BenHenning Mar 17, 2022
b82b158
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Mar 17, 2022
52862d6
Merge branch 'develop' into add-support-for-math-expressions-pt1-fix-…
BenHenning Mar 25, 2022
cd31247
Merge branch 'develop' into add-support-for-math-expressions-pt1-fix-…
BenHenning Mar 25, 2022
4dbd838
Merge branch 'add-support-for-math-expressions-pt1-fix-proto-imports'…
BenHenning Mar 25, 2022
deabf3a
Merge branch 'develop' into add-support-for-math-expressions-pt2-math…
BenHenning Mar 26, 2022
8bd89e6
Merge branch 'add-support-for-math-expressions-pt2-math-utility-refac…
BenHenning Mar 26, 2022
06f9cfd
Merge branch 'develop' into add-support-for-math-expressions-pt3-math…
BenHenning Mar 26, 2022
a1284a1
Merge branch 'add-support-for-math-expressions-pt3-math-expression-pr…
BenHenning Mar 26, 2022
2c2328b
Merge branch 'develop' into add-support-for-math-expressions-pt4-comm…
BenHenning Mar 26, 2022
feb901b
Merge branch 'add-support-for-math-expressions-pt4-commutative-compar…
BenHenning Mar 26, 2022
783aaf0
Merge branch 'develop' into add-support-for-math-expressions-pt5-poly…
BenHenning Mar 26, 2022
7306faa
Merge branch 'add-support-for-math-expressions-pt5-polynomial-protos'…
BenHenning Mar 26, 2022
d59437c
Merge branch 'develop' into add-support-for-math-expressions-pt6-toke…
BenHenning Mar 26, 2022
be04055
Merge branch 'add-support-for-math-expressions-pt6-tokenizer' into ad…
BenHenning Mar 26, 2022
88ecf9e
Merge branch 'develop' into add-support-for-math-expressions-pt7-math…
BenHenning Mar 26, 2022
7cb991a
Merge branch 'add-support-for-math-expressions-pt7-math-expression-pa…
BenHenning Mar 26, 2022
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
1 change: 1 addition & 0 deletions scripts/assets/file_content_validation_checks.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,6 @@ file_content_checks {
exempted_file_name: "scripts/src/javatests/org/oppia/android/scripts/regex/RegexPatternValidationCheckTest.kt"
exempted_file_name: "utility/src/test/java/org/oppia/android/util/math/MathExpressionParserTest.kt"
exempted_file_name: "utility/src/test/java/org/oppia/android/util/math/MathTokenizerTest.kt"
exempted_file_name: "utility/src/test/java/org/oppia/android/util/math/RealExtensionsTest.kt"
exempted_file_patterns: "testing/src/main/java/org/oppia/android/testing/junit/.+?\\.kt"
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.oppia.android.testing.math

import com.google.common.truth.FailureMetadata
import com.google.common.truth.StringSubject
import com.google.common.truth.Truth.assertAbout
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.extensions.proto.LiteProtoSubject
import org.oppia.android.app.model.MathEquation
import org.oppia.android.testing.math.MathEquationSubject.Companion.assertThat
import org.oppia.android.testing.math.MathExpressionSubject.Companion.assertThat
import org.oppia.android.util.math.toRawLatex

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

Expand Down Expand Up @@ -34,6 +36,26 @@ class MathEquationSubject private constructor(
*/
fun hasRightHandSideThat(): MathExpressionSubject = assertThat(actual.rightSide)

/**
* Returns a [StringSubject] to verify the LaTeX conversion of the tested [MathEquation].
*
* For more details on LaTeX conversion, see [toRawLatex]. Note that this method, in contrast to
* [convertsWithFractionsToLatexStringThat], retains division operations as-is.
*/
fun convertsToLatexStringThat(): StringSubject =
assertThat(convertToLatex(divAsFraction = false))

/**
* Returns a [StringSubject] to verify the LaTeX conversion of the tested [MathEquation].
*
* For more details on LaTeX conversion, see [toRawLatex]. Note that this method, in contrast to
* [convertsToLatexStringThat], treats divisions as fractions.
*/
fun convertsWithFractionsToLatexStringThat(): StringSubject =
assertThat(convertToLatex(divAsFraction = true))

private fun convertToLatex(divAsFraction: Boolean): String = actual.toRawLatex(divAsFraction)

companion object {
/**
* Returns a new [MathEquationSubject] to verify aspects of the specified [MathEquation] value.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.oppia.android.testing.math

import com.google.common.truth.DoubleSubject
import com.google.common.truth.FailureMetadata
import com.google.common.truth.IntegerSubject
import com.google.common.truth.StringSubject
import com.google.common.truth.Truth.assertAbout
import com.google.common.truth.Truth.assertThat
Expand All @@ -18,6 +20,8 @@ import org.oppia.android.app.model.MathUnaryOperation
import org.oppia.android.app.model.Real
import org.oppia.android.testing.math.MathExpressionSubject.Companion.assertThat
import org.oppia.android.testing.math.RealSubject.Companion.assertThat
import org.oppia.android.util.math.evaluateAsNumericExpression
import org.oppia.android.util.math.toRawLatex

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

Expand Down Expand Up @@ -91,6 +95,65 @@ class MathExpressionSubject private constructor(
ExpressionComparator.createFromExpression(actual).also(init)
}

/**
* Assumes that this expression evaluates to a fraction (i.e. [Real.getRational]) and returns a
* [FractionSubject] to verify the computed value.
*
* Note that this should only be used for numeric expressions as variable expressions cannot be
* evaluated. For more context on expression evaluation, see [evaluateAsNumericExpression].
*/
fun evaluatesToRationalThat(): FractionSubject =
FractionSubject.assertThat(evaluateAsReal(expectedType = Real.RealTypeCase.RATIONAL).rational)

/**
* Assumes that this expression evaluates to an irrational (i.e. [Real.getIrrational]) and returns
* a [DoubleSubject] to verify the computed value.
*
* Note that this should only be used for numeric expressions as variable expressions cannot be
* evaluated. For more context on expression evaluation, see [evaluateAsNumericExpression].
*/
fun evaluatesToIrrationalThat(): DoubleSubject =
assertThat(evaluateAsReal(expectedType = Real.RealTypeCase.IRRATIONAL).irrational)

/**
* Assumes that this expression evaluates to an integer (i.e. [Real.getInteger]) and returns an
* [IntegerSubject] to verify the computed value.
*
* Note that this should only be used for numeric expressions as variable expressions cannot be
* evaluated. For more context on expression evaluation, see [evaluateAsNumericExpression].
*/
fun evaluatesToIntegerThat(): IntegerSubject =
assertThat(evaluateAsReal(expectedType = Real.RealTypeCase.INTEGER).integer)

/**
* Returns a [StringSubject] to verify the LaTeX conversion of the tested [MathExpression].
*
* For more details on LaTeX conversion, see [toRawLatex]. Note that this method, in contrast to
* [convertsWithFractionsToLatexStringThat], retains division operations as-is.
*/
fun convertsToLatexStringThat(): StringSubject =
assertThat(convertToLatex(divAsFraction = false))

/**
* Returns a [StringSubject] to verify the LaTeX conversion of the tested [MathExpression].
*
* For more details on LaTeX conversion, see [toRawLatex]. Note that this method, in contrast to
* [convertsToLatexStringThat], treats divisions as fractions.
*/
fun convertsWithFractionsToLatexStringThat(): StringSubject =
assertThat(convertToLatex(divAsFraction = true))

private fun evaluateAsReal(expectedType: Real.RealTypeCase): Real {
val real = actual.evaluateAsNumericExpression()
assertWithMessage("Failed to evaluate numeric expression").that(real).isNotNull()
assertWithMessage("Expected constant to evaluate to $expectedType")
.that(real?.realTypeCase)
.isEqualTo(expectedType)
return checkNotNull(real) // Just to remove the nullable operator; the actual check is above.
}

private fun convertToLatex(divAsFraction: Boolean): String = actual.toRawLatex(divAsFraction)

/**
* DSL syntax provider for verifying the structure of a [MathExpression].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ import org.oppia.android.testing.math.FractionSubject.Companion.assertThat
*/
class RealSubject private constructor(
metadata: FailureMetadata,
private val actual: Real
private val actual: Real?
) : LiteProtoSubject(metadata, actual) {
private val nonNullActual by lazy { checkNotNull(actual) { "Expected real to be non-null" } }

/**
* Returns a [FractionSubject] to test [Real.getRational]. This will fail if the [Real] pertaining
* to this subject is not of type rational.
*/
fun isRationalThat(): FractionSubject {
verifyTypeToBe(Real.RealTypeCase.RATIONAL)
return assertThat(actual.rational)
return assertThat(nonNullActual.rational)
}

/**
Expand All @@ -39,7 +41,7 @@ class RealSubject private constructor(
*/
fun isIrrationalThat(): DoubleSubject {
verifyTypeToBe(Real.RealTypeCase.IRRATIONAL)
return assertThat(actual.irrational)
return assertThat(nonNullActual.irrational)
}

/**
Expand All @@ -48,17 +50,17 @@ class RealSubject private constructor(
*/
fun isIntegerThat(): IntegerSubject {
verifyTypeToBe(Real.RealTypeCase.INTEGER)
return assertThat(actual.integer)
return assertThat(nonNullActual.integer)
}

private fun verifyTypeToBe(expected: Real.RealTypeCase) {
assertWithMessage("Expected real type to be $expected, not: ${actual.realTypeCase}")
.that(actual.realTypeCase)
assertWithMessage("Expected real type to be $expected, not: ${nonNullActual.realTypeCase}")
.that(nonNullActual.realTypeCase)
.isEqualTo(expected)
}

companion object {
/** Returns a new [RealSubject] to verify aspects of the specified [Real] value. */
fun assertThat(actual: Real): RealSubject = assertAbout(::RealSubject).that(actual)
fun assertThat(actual: Real?): RealSubject = assertAbout(::RealSubject).that(actual)
}
}
113 changes: 101 additions & 12 deletions utility/src/main/java/org/oppia/android/util/math/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ General-purpose mathematics utilities, especially for supporting math-based inte

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library")

kt_android_library(
android_library(
name = "extensions",
srcs = [
"FloatExtensions.kt",
"FractionExtensions.kt",
"PolynomialExtensions.kt",
"RatioExtensions.kt",
"RealExtensions.kt",
],
visibility = [
"//:oppia_api_visibility",
],
deps = [
"//model/src/main/proto:math_java_proto_lite",
exports = [
":float_extensions",
":fraction_extensions",
":math_expression_extensions",
":polynomial_extensions",
":ratio_extensions",
":real_extensions",
],
)

Expand All @@ -39,12 +37,12 @@ kt_android_library(
"MathExpressionParser.kt",
],
visibility = [
"//:oppia_testing_visibility",
"//:oppia_api_visibility",
],
deps = [
":extensions",
":math_parsing_error",
":peekable_iterator",
":real_extensions",
":tokenizer",
"//model/src/main/proto:math_java_proto_lite",
],
Expand Down Expand Up @@ -86,3 +84,94 @@ kt_android_library(
"//:oppia_testing_visibility",
],
)

kt_android_library(
name = "float_extensions",
srcs = [
"FloatExtensions.kt",
],
deps = [
"//model/src/main/proto:math_java_proto_lite",
],
)

kt_android_library(
name = "fraction_extensions",
srcs = [
"FractionExtensions.kt",
],
deps = [
"//model/src/main/proto:math_java_proto_lite",
],
)

kt_android_library(
name = "math_expression_extensions",
srcs = [
"MathExpressionExtensions.kt",
],
deps = [
":expression_to_latex_converter",
":numeric_expression_evaluator",
"//model/src/main/proto:math_java_proto_lite",
],
)

kt_android_library(
name = "polynomial_extensions",
srcs = [
"PolynomialExtensions.kt",
],
deps = [
":real_extensions",
"//model/src/main/proto:math_java_proto_lite",
],
)

kt_android_library(
name = "ratio_extensions",
srcs = [
"RatioExtensions.kt",
],
deps = [
":fraction_extensions",
"//model/src/main/proto:math_java_proto_lite",
],
)

kt_android_library(
name = "real_extensions",
srcs = [
"RealExtensions.kt",
],
deps = [
":float_extensions",
":fraction_extensions",
"//model/src/main/proto:math_java_proto_lite",
],
)

kt_android_library(
name = "expression_to_latex_converter",
srcs = [
"ExpressionToLatexConverter.kt",
],
visibility = [
"//:oppia_testing_visibility",
],
deps = [
":real_extensions",
"//model/src/main/proto:math_java_proto_lite",
],
)

kt_android_library(
name = "numeric_expression_evaluator",
srcs = [
"NumericExpressionEvaluator.kt",
],
deps = [
":real_extensions",
"//model/src/main/proto:math_java_proto_lite",
],
)
Loading