Get rid of CppUnit boilerplate in wxSpinCtrlDouble unit test

This was already done for wxSpinCtrl unit tests in a4928c0fde (Use Catch
in wxSpinCtrl unit tests, 2020-07-12), do it for wxSpinCtrlDouble too
now.
This commit is contained in:
Vadim Zeitlin
2021-04-19 21:57:17 +02:00
parent 11967af49f
commit 34ab87ce4d

View File

@@ -19,76 +19,59 @@
#include "wx/uiaction.h" #include "wx/uiaction.h"
#include "wx/spinctrl.h" #include "wx/spinctrl.h"
class SpinCtrlDoubleTestCase : public CppUnit::TestCase class SpinCtrlDoubleTestCase
{ {
public: public:
SpinCtrlDoubleTestCase() { } SpinCtrlDoubleTestCase(int style = wxSP_ARROW_KEYS)
: m_spin(new wxSpinCtrlDouble(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize,
style))
{
}
void setUp() wxOVERRIDE; ~SpinCtrlDoubleTestCase()
void tearDown() wxOVERRIDE; {
delete m_spin;
}
private: protected:
CPPUNIT_TEST_SUITE( SpinCtrlDoubleTestCase ); wxSpinCtrlDouble* const m_spin;
CPPUNIT_TEST( NoEventsInCtor );
WXUISIM_TEST( Arrows );
WXUISIM_TEST( Wrap );
CPPUNIT_TEST( Range );
CPPUNIT_TEST( Value );
WXUISIM_TEST( Increment );
CPPUNIT_TEST( Digits );
CPPUNIT_TEST_SUITE_END();
void NoEventsInCtor();
void Arrows();
void Wrap();
void Range();
void Value();
void Increment();
void Digits();
wxSpinCtrlDouble* m_spin;
wxDECLARE_NO_COPY_CLASS(SpinCtrlDoubleTestCase); wxDECLARE_NO_COPY_CLASS(SpinCtrlDoubleTestCase);
}; };
// register in the unnamed registry so that these tests are run by default class SpinCtrlDoubleTestCaseWrap : public SpinCtrlDoubleTestCase
CPPUNIT_TEST_SUITE_REGISTRATION( SpinCtrlDoubleTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SpinCtrlDoubleTestCase, "SpinCtrlDoubleTestCase" );
void SpinCtrlDoubleTestCase::setUp()
{ {
m_spin = new wxSpinCtrlDouble(wxTheApp->GetTopWindow()); public:
} SpinCtrlDoubleTestCaseWrap()
: SpinCtrlDoubleTestCase(wxSP_ARROW_KEYS | wxSP_WRAP)
{
}
};
void SpinCtrlDoubleTestCase::tearDown()
{
wxDELETE(m_spin);
}
void SpinCtrlDoubleTestCase::NoEventsInCtor() TEST_CASE("SpinCtrlDouble::NoEventsInCtor", "[spinctrl][spinctrldouble]")
{ {
// Verify that creating the control does not generate any events. This is // Verify that creating the control does not generate any events. This is
// unexpected and shouldn't happen. // unexpected and shouldn't happen.
wxWindow* const parent = m_spin->GetParent(); wxSpinCtrlDouble *m_spin = new wxSpinCtrlDouble;
delete m_spin;
m_spin = new wxSpinCtrlDouble;
EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE); EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE);
EventCounter updatedText(m_spin, wxEVT_TEXT); EventCounter updatedText(m_spin, wxEVT_TEXT);
m_spin->Create(parent, wxID_ANY, "", m_spin->Create(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize, 0, wxDefaultPosition, wxDefaultSize, 0,
0., 100., 17.); 0., 100., 17.);
CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount()); CHECK( updatedSpin.GetCount() == 0 );
CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount()); CHECK( updatedText.GetCount() == 0 );
} }
void SpinCtrlDoubleTestCase::Arrows()
{
#if wxUSE_UIACTIONSIMULATOR #if wxUSE_UIACTIONSIMULATOR
TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Arrows", "[spinctrl][spinctrldouble]")
{
EventCounter updated(m_spin, wxEVT_SPINCTRLDOUBLE); EventCounter updated(m_spin, wxEVT_SPINCTRLDOUBLE);
wxUIActionSimulator sim; wxUIActionSimulator sim;
@@ -99,26 +82,20 @@ void SpinCtrlDoubleTestCase::Arrows()
sim.Char(WXK_UP); sim.Char(WXK_UP);
wxYield(); wxYield();
CPPUNIT_ASSERT_EQUAL(1, updated.GetCount()); CHECK( updated.GetCount() == 1 );
CPPUNIT_ASSERT_EQUAL(1.0, m_spin->GetValue()); CHECK( m_spin->GetValue() == 1.0 );
updated.Clear(); updated.Clear();
sim.Char(WXK_DOWN); sim.Char(WXK_DOWN);
wxYield(); wxYield();
CPPUNIT_ASSERT_EQUAL(1, updated.GetCount()); CHECK( updated.GetCount() == 1 );
CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetValue()); CHECK( m_spin->GetValue() == 0.0 );
#endif
} }
void SpinCtrlDoubleTestCase::Wrap() TEST_CASE_METHOD(SpinCtrlDoubleTestCaseWrap,
"SpinCtrlDouble::Wrap", "[spinctrl][spinctrldouble]")
{ {
#if wxUSE_UIACTIONSIMULATOR
wxDELETE(m_spin);
m_spin = new wxSpinCtrlDouble(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize,
wxSP_ARROW_KEYS | wxSP_WRAP);
wxUIActionSimulator sim; wxUIActionSimulator sim;
m_spin->SetFocus(); m_spin->SetFocus();
@@ -128,20 +105,21 @@ void SpinCtrlDoubleTestCase::Wrap()
wxYield(); wxYield();
CPPUNIT_ASSERT_EQUAL(100.0, m_spin->GetValue()); CHECK( m_spin->GetValue() == 100.0 );
sim.Char(WXK_UP); sim.Char(WXK_UP);
wxYield(); wxYield();
CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetValue()); CHECK( m_spin->GetValue() == 0.0 );
#endif
} }
#endif // wxUSE_UIACTIONSIMULATOR
void SpinCtrlDoubleTestCase::Range() TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Range", "[spinctrl][spinctrldouble]")
{ {
CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetMin()); CHECK( m_spin->GetMin() == 0.0 );
CPPUNIT_ASSERT_EQUAL(100.0, m_spin->GetMax()); CHECK( m_spin->GetMax() == 100.0 );
// Test that the value is adjusted to be inside the new valid range but // Test that the value is adjusted to be inside the new valid range but
// that this doesn't result in any events (as this is not something done by // that this doesn't result in any events (as this is not something done by
@@ -151,26 +129,27 @@ void SpinCtrlDoubleTestCase::Range()
EventCounter updatedText(m_spin, wxEVT_TEXT); EventCounter updatedText(m_spin, wxEVT_TEXT);
m_spin->SetRange(1., 10.); m_spin->SetRange(1., 10.);
CPPUNIT_ASSERT_EQUAL(1., m_spin->GetValue()); CHECK( m_spin->GetValue() == 1. );
CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount()); CHECK( updatedSpin.GetCount() == 0 );
CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount()); CHECK( updatedText.GetCount() == 0 );
} }
//Test negative ranges //Test negative ranges
m_spin->SetRange(-10.0, 10.0); m_spin->SetRange(-10.0, 10.0);
CPPUNIT_ASSERT_EQUAL(-10.0, m_spin->GetMin()); CHECK( m_spin->GetMin() == -10.0 );
CPPUNIT_ASSERT_EQUAL(10.0, m_spin->GetMax()); CHECK( m_spin->GetMax() == 10.0 );
//Test backwards ranges //Test backwards ranges
m_spin->SetRange(75.0, 50.0); m_spin->SetRange(75.0, 50.0);
CPPUNIT_ASSERT_EQUAL(75.0, m_spin->GetMin()); CHECK( m_spin->GetMin() == 75.0 );
CPPUNIT_ASSERT_EQUAL(50.0, m_spin->GetMax()); CHECK( m_spin->GetMax() == 50.0 );
} }
void SpinCtrlDoubleTestCase::Value() TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Value", "[spinctrl][spinctrldouble]")
{ {
EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE); EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE);
EventCounter updatedText(m_spin, wxEVT_TEXT); EventCounter updatedText(m_spin, wxEVT_TEXT);
@@ -178,28 +157,30 @@ void SpinCtrlDoubleTestCase::Value()
m_spin->SetDigits(2); m_spin->SetDigits(2);
m_spin->SetIncrement(0.1); m_spin->SetIncrement(0.1);
CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetValue()); CHECK( m_spin->GetValue() == 0.0 );
m_spin->SetValue(50.0); m_spin->SetValue(50.0);
CPPUNIT_ASSERT_EQUAL(50.0, m_spin->GetValue()); CHECK( m_spin->GetValue() == 50.0 );
m_spin->SetValue(49.1); m_spin->SetValue(49.1);
CPPUNIT_ASSERT_EQUAL(49.1, m_spin->GetValue()); CHECK( m_spin->GetValue() == 49.1 );
// Calling SetValue() shouldn't have generated any events. // Calling SetValue() shouldn't have generated any events.
CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount()); CHECK( updatedSpin.GetCount() == 0 );
CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount()); CHECK( updatedText.GetCount() == 0 );
} }
void SpinCtrlDoubleTestCase::Increment()
{
#if wxUSE_UIACTIONSIMULATOR #if wxUSE_UIACTIONSIMULATOR
CPPUNIT_ASSERT_EQUAL(1.0, m_spin->GetIncrement());
TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Increment", "[spinctrl][spinctrldouble]")
{
CHECK( m_spin->GetIncrement() == 1.0 );
m_spin->SetDigits(1); m_spin->SetDigits(1);
m_spin->SetIncrement(0.1); m_spin->SetIncrement(0.1);
CPPUNIT_ASSERT_EQUAL(0.1, m_spin->GetIncrement()); CHECK( m_spin->GetIncrement() == 0.1 );
wxUIActionSimulator sim; wxUIActionSimulator sim;
@@ -210,15 +191,17 @@ void SpinCtrlDoubleTestCase::Increment()
wxYield(); wxYield();
CPPUNIT_ASSERT_EQUAL(0.1, m_spin->GetValue()); CHECK( m_spin->GetValue() == 0.1 );
#endif
} }
void SpinCtrlDoubleTestCase::Digits() #endif // wxUSE_UIACTIONSIMULATOR
TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Digits", "[spinctrl][spinctrldouble]")
{ {
m_spin->SetDigits(5); m_spin->SetDigits(5);
CPPUNIT_ASSERT_EQUAL(5, m_spin->GetDigits()); CHECK( m_spin->GetDigits() == 5 );
} }
static inline unsigned int GetInitialDigits(double inc) static inline unsigned int GetInitialDigits(double inc)
@@ -230,7 +213,7 @@ static inline unsigned int GetInitialDigits(double inc)
return digits; return digits;
} }
TEST_CASE("SpinCtrlDoubleTestCase::InitialDigits", "[spinctrldouble][initialdigits]") TEST_CASE("SpinCtrlDouble::InitialDigits", "[spinctrldouble][initialdigits]")
{ {
REQUIRE(GetInitialDigits(15) == 0); REQUIRE(GetInitialDigits(15) == 0);
REQUIRE(GetInitialDigits(10) == 0); REQUIRE(GetInitialDigits(10) == 0);