diff --git a/app/src/main/java/org/oppia/android/app/parser/StringToFractionParser.kt b/app/src/main/java/org/oppia/android/app/parser/StringToFractionParser.kt index 62fc2e9a282..8a11120cacb 100644 --- a/app/src/main/java/org/oppia/android/app/parser/StringToFractionParser.kt +++ b/app/src/main/java/org/oppia/android/app/parser/StringToFractionParser.kt @@ -27,8 +27,12 @@ class StringToFractionParser { * detection should be done using [getRealTimeAnswerError], instead. */ fun getSubmitTimeError(text: String): FractionParsingError { - if (invalidCharsLengthRegex.find(text) != null) + if (invalidCharsLengthRegex.find(text) != null) { return FractionParsingError.NUMBER_TOO_LONG + } + if (text.endsWith("/")) { + return FractionParsingError.INVALID_FORMAT + } val fraction = parseFraction(text) return when { fraction == null -> FractionParsingError.INVALID_FORMAT diff --git a/app/src/test/java/org/oppia/android/app/parser/StringToFractionParserTest.kt b/app/src/test/java/org/oppia/android/app/parser/StringToFractionParserTest.kt index 1d71c8c8afd..4e3f9141027 100644 --- a/app/src/test/java/org/oppia/android/app/parser/StringToFractionParserTest.kt +++ b/app/src/test/java/org/oppia/android/app/parser/StringToFractionParserTest.kt @@ -217,6 +217,22 @@ class StringToFractionParserTest { } } + @Test + fun testSubmitTimeError_noDenominator_returnsInvalidFormat() { + val error = stringToFractionParser.getSubmitTimeError("3/") + assertThat(error).isEqualTo(StringToFractionParser.FractionParsingError.INVALID_FORMAT) + } + + @Test + fun testSubmitTimeError_noDenominator_invalidFormat_hasRelevantErrorMessage() { + activityRule.scenario.onActivity { activity -> + val errorMessage = stringToFractionParser.getSubmitTimeError("3/") + .getErrorMessageFromStringRes(activity.appLanguageResourceHandler) + assertThat(errorMessage) + .isEqualTo("Please enter a valid fraction (e.g., 5/3 or 1 2/3)") + } + } + @Test fun testRealTimeError_regularFraction_returnsValid() { val error = stringToFractionParser.getRealTimeAnswerError("2/3")