From 58b11ee8dd9d4da8242836a207548bcd485cdc50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20=C5=9Aleszy=C5=84ski?= Date: Tue, 20 Apr 2021 13:35:05 +0200 Subject: [PATCH] [EGD-6563] Don't allow to edit exponent output in calculator Clear the calculator input field before typing if it contains exponent output from previous calculations. --- .../data/CalculatorInputProcessorText.cpp | 9 ++++++-- .../data/CalculatorInputProcessorText.hpp | 4 +++- .../tests/CalculatorInput_tests.cpp | 21 +++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/module-apps/application-calculator/data/CalculatorInputProcessorText.cpp b/module-apps/application-calculator/data/CalculatorInputProcessorText.cpp index c0b12588e65e88299230e3addee27aa70577c54b..d28986c8f3a22565bd600feecbfac76e52726c67 100644 --- a/module-apps/application-calculator/data/CalculatorInputProcessorText.cpp +++ b/module-apps/application-calculator/data/CalculatorInputProcessorText.cpp @@ -13,7 +13,7 @@ calc::InputProcessorText::InputProcessorText(gsl::strict_not_null i bool calc::InputProcessorText::handle(const gui::InputEvent &event) { - if (clearInput) { + if (clearInput || inputContainsExponent()) { clear(); } @@ -157,7 +157,12 @@ bool calc::InputProcessorText::decimalLimitReached() const return false; } -std::uint32_t calc::InputProcessorText::getPenultimate() +bool calc::InputProcessorText::inputContainsExponent() const +{ + return std::string{inputField->getText()}.find('e') != std::string::npos; +} + +std::uint32_t calc::InputProcessorText::getPenultimate() const { const auto &text = inputField->getText(); const auto len = text.length(); diff --git a/module-apps/application-calculator/data/CalculatorInputProcessorText.hpp b/module-apps/application-calculator/data/CalculatorInputProcessorText.hpp index 4ae2605faab200b019c52ec2af35704cba748edf..2597e5f1b3f1f959f13e42ead1ec581bacd4da36 100644 --- a/module-apps/application-calculator/data/CalculatorInputProcessorText.hpp +++ b/module-apps/application-calculator/data/CalculatorInputProcessorText.hpp @@ -28,7 +28,9 @@ namespace calc bool isPreviousNumberDecimal() const; bool decimalLimitReached() const; - std::uint32_t getPenultimate(); + bool inputContainsExponent() const; + + std::uint32_t getPenultimate() const; gui::Text *inputField{nullptr}; bool clearInput{false}; diff --git a/module-apps/application-calculator/tests/CalculatorInput_tests.cpp b/module-apps/application-calculator/tests/CalculatorInput_tests.cpp index d0409c044ef1aa6cfb21e82d1ad5e4e628c3e58e..22dff8fb81d7537798c8b4cbab62db930aff9cc1 100644 --- a/module-apps/application-calculator/tests/CalculatorInput_tests.cpp +++ b/module-apps/application-calculator/tests/CalculatorInput_tests.cpp @@ -351,5 +351,26 @@ SCENARIO("Input Processor tests") } } } + + WHEN("We do BIG math") + { + inputField.setText("99999×99999"); + passShortKeyPress(KeyCodes::JoystickEnter); + + THEN("Output contains exponent") + { + REQUIRE(inputField.getText() == "9.9998e9"); + } + + AND_WHEN("We start typing") + { + passShortKeyPresses({KeyCodes::NumericKey4, MinusKey, KeyCodes::NumericKey5, KeyCodes::NumericKey6}); + + THEN("Input is cleared before typing") + { + REQUIRE(inputField.getText() == "4-56"); + } + } + } } }