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/spinctrl.h"
class SpinCtrlDoubleTestCase : public CppUnit::TestCase
class SpinCtrlDoubleTestCase
{
public:
SpinCtrlDoubleTestCase() { }
SpinCtrlDoubleTestCase(int style = wxSP_ARROW_KEYS)
: m_spin(new wxSpinCtrlDouble(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize,
style))
{
}
void setUp() wxOVERRIDE;
void tearDown() wxOVERRIDE;
~SpinCtrlDoubleTestCase()
{
delete m_spin;
}
private:
CPPUNIT_TEST_SUITE( SpinCtrlDoubleTestCase );
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;
protected:
wxSpinCtrlDouble* const m_spin;
wxDECLARE_NO_COPY_CLASS(SpinCtrlDoubleTestCase);
};
// register in the unnamed registry so that these tests are run by default
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()
class SpinCtrlDoubleTestCaseWrap : public SpinCtrlDoubleTestCase
{
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
// unexpected and shouldn't happen.
wxWindow* const parent = m_spin->GetParent();
delete m_spin;
m_spin = new wxSpinCtrlDouble;
wxSpinCtrlDouble *m_spin = new wxSpinCtrlDouble;
EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE);
EventCounter updatedText(m_spin, wxEVT_TEXT);
m_spin->Create(parent, wxID_ANY, "",
m_spin->Create(wxTheApp->GetTopWindow(), wxID_ANY, "",
wxDefaultPosition, wxDefaultSize, 0,
0., 100., 17.);
CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
CHECK( updatedSpin.GetCount() == 0 );
CHECK( updatedText.GetCount() == 0 );
}
void SpinCtrlDoubleTestCase::Arrows()
{
#if wxUSE_UIACTIONSIMULATOR
TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Arrows", "[spinctrl][spinctrldouble]")
{
EventCounter updated(m_spin, wxEVT_SPINCTRLDOUBLE);
wxUIActionSimulator sim;
@@ -99,26 +82,20 @@ void SpinCtrlDoubleTestCase::Arrows()
sim.Char(WXK_UP);
wxYield();
CPPUNIT_ASSERT_EQUAL(1, updated.GetCount());
CPPUNIT_ASSERT_EQUAL(1.0, m_spin->GetValue());
CHECK( updated.GetCount() == 1 );
CHECK( m_spin->GetValue() == 1.0 );
updated.Clear();
sim.Char(WXK_DOWN);
wxYield();
CPPUNIT_ASSERT_EQUAL(1, updated.GetCount());
CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetValue());
#endif
CHECK( updated.GetCount() == 1 );
CHECK( m_spin->GetValue() == 0.0 );
}
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;
m_spin->SetFocus();
@@ -128,20 +105,21 @@ void SpinCtrlDoubleTestCase::Wrap()
wxYield();
CPPUNIT_ASSERT_EQUAL(100.0, m_spin->GetValue());
CHECK( m_spin->GetValue() == 100.0 );
sim.Char(WXK_UP);
wxYield();
CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetValue());
#endif
CHECK( m_spin->GetValue() == 0.0 );
}
#endif // wxUSE_UIACTIONSIMULATOR
void SpinCtrlDoubleTestCase::Range()
TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Range", "[spinctrl][spinctrldouble]")
{
CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetMin());
CPPUNIT_ASSERT_EQUAL(100.0, m_spin->GetMax());
CHECK( m_spin->GetMin() == 0.0 );
CHECK( m_spin->GetMax() == 100.0 );
// 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
@@ -151,26 +129,27 @@ void SpinCtrlDoubleTestCase::Range()
EventCounter updatedText(m_spin, wxEVT_TEXT);
m_spin->SetRange(1., 10.);
CPPUNIT_ASSERT_EQUAL(1., m_spin->GetValue());
CHECK( m_spin->GetValue() == 1. );
CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
CHECK( updatedSpin.GetCount() == 0 );
CHECK( updatedText.GetCount() == 0 );
}
//Test negative ranges
m_spin->SetRange(-10.0, 10.0);
CPPUNIT_ASSERT_EQUAL(-10.0, m_spin->GetMin());
CPPUNIT_ASSERT_EQUAL(10.0, m_spin->GetMax());
CHECK( m_spin->GetMin() == -10.0 );
CHECK( m_spin->GetMax() == 10.0 );
//Test backwards ranges
m_spin->SetRange(75.0, 50.0);
CPPUNIT_ASSERT_EQUAL(75.0, m_spin->GetMin());
CPPUNIT_ASSERT_EQUAL(50.0, m_spin->GetMax());
CHECK( m_spin->GetMin() == 75.0 );
CHECK( m_spin->GetMax() == 50.0 );
}
void SpinCtrlDoubleTestCase::Value()
TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Value", "[spinctrl][spinctrldouble]")
{
EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE);
EventCounter updatedText(m_spin, wxEVT_TEXT);
@@ -178,28 +157,30 @@ void SpinCtrlDoubleTestCase::Value()
m_spin->SetDigits(2);
m_spin->SetIncrement(0.1);
CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetValue());
CHECK( m_spin->GetValue() == 0.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);
CPPUNIT_ASSERT_EQUAL(49.1, m_spin->GetValue());
CHECK( m_spin->GetValue() == 49.1 );
// Calling SetValue() shouldn't have generated any events.
CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
CHECK( updatedSpin.GetCount() == 0 );
CHECK( updatedText.GetCount() == 0 );
}
void SpinCtrlDoubleTestCase::Increment()
{
#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->SetIncrement(0.1);
CPPUNIT_ASSERT_EQUAL(0.1, m_spin->GetIncrement());
CHECK( m_spin->GetIncrement() == 0.1 );
wxUIActionSimulator sim;
@@ -210,15 +191,17 @@ void SpinCtrlDoubleTestCase::Increment()
wxYield();
CPPUNIT_ASSERT_EQUAL(0.1, m_spin->GetValue());
#endif
CHECK( m_spin->GetValue() == 0.1 );
}
void SpinCtrlDoubleTestCase::Digits()
#endif // wxUSE_UIACTIONSIMULATOR
TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Digits", "[spinctrl][spinctrldouble]")
{
m_spin->SetDigits(5);
CPPUNIT_ASSERT_EQUAL(5, m_spin->GetDigits());
CHECK( m_spin->GetDigits() == 5 );
}
static inline unsigned int GetInitialDigits(double inc)
@@ -230,7 +213,7 @@ static inline unsigned int GetInitialDigits(double inc)
return digits;
}
TEST_CASE("SpinCtrlDoubleTestCase::InitialDigits", "[spinctrldouble][initialdigits]")
TEST_CASE("SpinCtrlDouble::InitialDigits", "[spinctrldouble][initialdigits]")
{
REQUIRE(GetInitialDigits(15) == 0);
REQUIRE(GetInitialDigits(10) == 0);