From 7ce0e8901eb39396304bea65e41c3df8c033d252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20=C5=9Aleszy=C5=84ski?= Date: Tue, 20 Apr 2021 13:07:45 +0200 Subject: [PATCH] [EGD-6561] Limit calculator decimals to 6 digits Limit to 6 since our `utils::to_string(double)' function also limits to 6 decimals. --- .../data/CalculatorInputProcessor.hpp | 2 ++ .../data/CalculatorInputProcessorText.cpp | 21 ++++++++++++- .../data/CalculatorInputProcessorText.hpp | 5 +++- .../tests/CalculatorInput_tests.cpp | 30 +++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/module-apps/application-calculator/data/CalculatorInputProcessor.hpp b/module-apps/application-calculator/data/CalculatorInputProcessor.hpp index e1b4ab2cd97ce091e41e060766a021c62a444655..df81cc9a1d3df9eeaf4844a1909b793219c0e356 100644 --- a/module-apps/application-calculator/data/CalculatorInputProcessor.hpp +++ b/module-apps/application-calculator/data/CalculatorInputProcessor.hpp @@ -43,6 +43,8 @@ namespace calc class InputProcessor { public: + static inline constexpr auto DecimalDigitsLimit = 6; + virtual ~InputProcessor() = default; virtual bool handle(const gui::InputEvent &event) = 0; diff --git a/module-apps/application-calculator/data/CalculatorInputProcessorText.cpp b/module-apps/application-calculator/data/CalculatorInputProcessorText.cpp index 342014909cbf99069c72d3b137032faa132e8823..e20b6f4c75c288f8fc92c731ce5a9b109702b395 100644 --- a/module-apps/application-calculator/data/CalculatorInputProcessorText.cpp +++ b/module-apps/application-calculator/data/CalculatorInputProcessorText.cpp @@ -83,6 +83,11 @@ bool calc::InputProcessorText::handle(const gui::InputEvent &event) return true; } + if (decimalLimitReached()) { + // Consume event to don't allow more decimals + return true; + } + return false; } @@ -111,7 +116,7 @@ void calc::InputProcessorText::writeEquation(bool lastCharIsSymbol, const UTF8 & } } -bool calc::InputProcessorText::isPreviousNumberDecimal() +bool calc::InputProcessorText::isPreviousNumberDecimal() const { if (!inputField->getText().empty()) { std::vector symbolsIndexes; @@ -138,6 +143,20 @@ bool calc::InputProcessorText::isPreviousNumberDecimal() return false; } +bool calc::InputProcessorText::decimalLimitReached() const +{ + if (!isPreviousNumberDecimal()) + return false; + + const auto &txt = std::string{inputField->getText()}; + const auto separator_pos = txt.find_last_of(utils::localize.get("app_calculator_decimal_separator")); + + if ((txt.size() - separator_pos) > DecimalDigitsLimit) + return true; + + return false; +} + std::uint32_t calc::InputProcessorText::getPenultimate() { const auto &text = inputField->getText(); diff --git a/module-apps/application-calculator/data/CalculatorInputProcessorText.hpp b/module-apps/application-calculator/data/CalculatorInputProcessorText.hpp index 9fc11aa81410f11afc44deb2539a939567d98040..4ae2605faab200b019c52ec2af35704cba748edf 100644 --- a/module-apps/application-calculator/data/CalculatorInputProcessorText.hpp +++ b/module-apps/application-calculator/data/CalculatorInputProcessorText.hpp @@ -24,7 +24,10 @@ namespace calc private: void writeEquation(bool lastCharIsSymbol, const UTF8 &symbol); - bool isPreviousNumberDecimal(); + + bool isPreviousNumberDecimal() const; + bool decimalLimitReached() const; + std::uint32_t getPenultimate(); gui::Text *inputField{nullptr}; diff --git a/module-apps/application-calculator/tests/CalculatorInput_tests.cpp b/module-apps/application-calculator/tests/CalculatorInput_tests.cpp index cae2ab21deb1bfa78b419a22fa82fcf6f78ed1cc..1863e5b7f38d6b69b66bec5aaf756235214f1fe9 100644 --- a/module-apps/application-calculator/tests/CalculatorInput_tests.cpp +++ b/module-apps/application-calculator/tests/CalculatorInput_tests.cpp @@ -159,6 +159,21 @@ SCENARIO("Input Processor tests") REQUIRE(inputField.getText() == "0.12345"); } } + + AND_WHEN("We try to enter more than 6 decimals") + { + passShortKeyPresses({KeyCodes::NumericKey4, + KeyCodes::NumericKey5, + KeyCodes::NumericKey6, + KeyCodes::NumericKey7, + KeyCodes::NumericKey8, + KeyCodes::NumericKey9}); + + THEN("The input is truncated to 6 decimals") + { + REQUIRE(inputField.getText() == "0.123456"); + } + } } WHEN("We enter zero") @@ -288,6 +303,21 @@ SCENARIO("Input Processor tests") REQUIRE(inputField.getText() == "123+4.56456"); } } + + AND_WHEN("We try to enter more than 6 decimals") + { + passShortKeyPresses({KeyCodes::NumericKey4, + KeyCodes::NumericKey5, + KeyCodes::NumericKey6, + KeyCodes::NumericKey7, + KeyCodes::NumericKey8, + KeyCodes::NumericKey9}); + + THEN("The input is truncated to 6 decimals") + { + REQUIRE(inputField.getText() == "123+4.564567"); + } + } } }