~aleteoryx/muditaos

7ce0e8901eb39396304bea65e41c3df8c033d252 — Artur Śleszyński 4 years ago a702bbd
[EGD-6561] Limit calculator decimals to 6 digits

Limit to 6 since our `utils::to_string(double)' function also
limits to 6 decimals.
M module-apps/application-calculator/data/CalculatorInputProcessor.hpp => module-apps/application-calculator/data/CalculatorInputProcessor.hpp +2 -0
@@ 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;

M module-apps/application-calculator/data/CalculatorInputProcessorText.cpp => module-apps/application-calculator/data/CalculatorInputProcessorText.cpp +20 -1
@@ 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<int> 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();

M module-apps/application-calculator/data/CalculatorInputProcessorText.hpp => module-apps/application-calculator/data/CalculatorInputProcessorText.hpp +4 -1
@@ 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};

M module-apps/application-calculator/tests/CalculatorInput_tests.cpp => module-apps/application-calculator/tests/CalculatorInput_tests.cpp +30 -0
@@ 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");
                        }
                    }
                }
            }