From 73706fa04e6d17a26ab4a7b24f8984d5228bb438 Mon Sep 17 00:00:00 2001 From: John Jones-Steele Date: Mon, 26 Jul 2021 16:10:21 +0100 Subject: [PATCH] Added Tests to check code is correct Signed-off-by: John Jones-Steele --- .../AzQtComponents/Utilities/Conversions.cpp | 2 +- .../AzToolsFramework/Tests/SpinBoxTests.cpp | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp index c9327d199e..bef41bb487 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp @@ -43,7 +43,7 @@ namespace AzQtComponents { const QChar decimalPoint = locale.decimalPoint(); const QChar zeroDigit = locale.zeroDigit(); - const int numToStringDecimals = numDecimals < 8 ? 8 : numDecimals; + const int numToStringDecimals = AZStd::max(numDecimals, 20); // We want to truncate, not round. toString will round, so we add extra decimal places to the formatting // so we can remove the last values diff --git a/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp b/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp index d1c3ee5b3c..eb09c68cee 100644 --- a/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp +++ b/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp @@ -77,6 +77,19 @@ namespace UnitTest m_intSpinBox.reset(); } + QString setupTruncationTest(QString textValue) + { + QString retval; + m_doubleSpinBoxWithLineEdit->setDecimals(7); + m_doubleSpinBoxWithLineEdit->setDisplayDecimals(3); + m_doubleSpinBoxWithLineEdit->setFocus(); + m_doubleSpinBoxWithLineEdit->GetLineEdit()->setText(textValue); + m_doubleSpinBoxWithLineEdit->clearFocus(); + + return m_doubleSpinBoxWithLineEdit->textFromValue(m_doubleSpinBoxWithLineEdit->value()); + } + + AZStd::unique_ptr m_dummyWidget; AZStd::unique_ptr m_intSpinBox; AZStd::unique_ptr m_doubleSpinBox; @@ -277,4 +290,34 @@ namespace UnitTest // test would result in a crash EXPECT_TRUE(m_intSpinBox.get() == nullptr); } + + TEST_F(SpinBoxFixture, SpinBoxCheckHighValueTruncatesCorrectly) + { + QString value = setupTruncationTest("0.9999999"); + + EXPECT_TRUE(value == "0.999"); + } + + TEST_F(SpinBoxFixture, SpinBoxCheckLowValueTruncatesCorrectly) + { + QString value = setupTruncationTest("0.0000001"); + + EXPECT_TRUE(value == "0.0"); + } + + TEST_F(SpinBoxFixture, SpinBoxCheckBugValuesTruncatesCorrectly) + { + QString value = setupTruncationTest("0.12395"); + + EXPECT_TRUE(value == "0.123"); + + value = setupTruncationTest("0.94496"); + + EXPECT_TRUE(value == "0.944"); + + value = setupTruncationTest("0.0009999"); + + EXPECT_TRUE(value == "0.0"); + } + } // namespace UnitTest