Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix part of #4044: Add protos & testing libraries for math expression…
…s/equations (#4047) ## Explanation Fix part of #4044 Originally copied from #2173 when it was in proof-of-concept form This PR introduces several new math protos: - Real - MathExpression (& its constituent pieces: MathBinaryOperation, MathUnaryOperation, and MathFunctionCall) - MathEquation The expression & equation structures are necessary for downstream numeric expression, algebraic expression, and algebraic equation parsing, as well as for implementing the MatchesExactlyWith classifiers for each interaction. For specific details on this classifier, see [the PRD](https://docs.google.com/document/d/1x2vcSjocJUXkwwlce5Gjjq_Z83ykVIn2Fp0BcnrOrTg/edit#heading=h.9mdskev1k1ax). None of that code is introduced in this PR, but will be in future PRs. The ``Real`` structure being introduced is to help with full and partial expression evaluation that will be introduced in a future PR to ensure infinite precision can be kept for as long as possible (i.e. by keeping values in the form of integers or fractions). Some caveats regarding the expression structure: - Parsing information is included for token & subexpression extraction which is needed for certain user errors that will be implemented as part of the interaction UI implementations in a future PR - A single generic structure is used to represent both numeric and algebraic expressions to simplify a lot of code (this will be more evident in future PRs) Beyond the protos, new Truth test subject classes are being introduced to make verifying these values easier. Part of this includes a custom Kotlin DSL for verifying expressions & equations in a clean way, e.g.: ```kotlin val equation = parseAlgebraicEquationSuccessfully("y = (x+1)(x-1)") assertThat(equation).hasLeftHandSideThat().hasStructureThatMatches { variable { withNameThat().isEqualTo("y") } } assertThat(equation).hasRightHandSideThat().hasStructureThatMatches { multiplication { leftOperand { group { addition { leftOperand { variable { withNameThat().isEqualTo("x") } } rightOperand { constant { withValueThat().isIntegerThat().isEqualTo(1) } } } } } rightOperand { group { subtraction { leftOperand { variable { withNameThat().isEqualTo("x") } } rightOperand { constant { withValueThat().isIntegerThat().isEqualTo(1) } } } } } } } ``` The main benefit to the DSL is to avoid cascading checks using subsequent assertion lines that can become quite difficult to read, e.g.: ```kotlin val rootExpression = result.getExpectedSuccessfulExpression() val mulOp = rootExpression.getExpectedBinaryOperationWithOperator(MULTIPLY) val leftConstant = mulOp.leftOperand.getExpectedRationalConstant() val rightAddOp = mulOp.rightOperand.getExpectedBinaryOperationWithOperator(ADD) val rightAddLeftVar = rightAddOp.leftOperand.getExpectedVariable() val rightAddRightConstant = rightAddOp.rightOperand.getExpectedRationalConstant() assertThat(leftConstant).isEqualTo(createWholeNumberFraction(2)) assertThat(rightAddLeftVar).isEqualTo("x") assertThat(rightAddRightConstant).isEqualTo(createWholeNumberFraction(1)) ``` The above is for the expression "2(x+1)" and was a previous attempt. It leverages helpers and to me seems much harder to read as compared with the DSL. The DSL does result in more verbosity, but I think that actually helps readability in this case. ### Test exemptions Note that I did not add tests for these. Given that they're test utilities & the importance of getting the downstream work completed, I decided to file #4097 to track adding tests for each. I've already manually verified the subjects work in actual tests downstream, and the code is simple enough that I'm confident in its correctness by inspection. Nevertheless, we should eventually add automated tests for this. Reviewers: please let me know if you have any concerns with this approach. ## Essential Checklist - [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)). ## For UI-specific PRs only N/A -- proto & testing library only change, and the proto changes are only additions. No UI functionality is yet affected by these changes. Commit history: * Copy proto-based changes from #2173. * Introduce math.proto & refactor math extensions. Much of this is copied from #2173. * Migrate tests & remove unneeded prefix. * Add needed newline. * Some needed Fraction changes. * Introduce math expression + equation protos. Also adds testing libraries for both + fractions & reals (new structure). Most of this is copied from #2173. * Lint fix. * Fix broken test post-refactor. * Post-merge fix. * Add regex check, docs, and resolve TODOs. This also changes regex handling in the check to be more generic for better flexibility when matching files. * Lint fix. * Fix failing static checks. * Fix broken CI checks. Adds missing KDocs, test file exemptions, and fixes the Gradle build. * Lint fixes. * Address reviewer comments + other stuff. This also fixes a typo and incorrectly ordered exemptions list I noticed during development of downstream PRs. * Move StringExtensions & fraction parsing. This splits fraction parsing between UI & utility components. * Add missing KDocs. * Fix broken build. * Fix broken build post-merge. * Post-merge fix. * More post-merge fixes.
- Loading branch information