Remove CppUnit boilerplate from XRC unit test

No real changes, just simplify by using CATCH macros and a simple
fixture instead of CppUnit::TestCase.
This commit is contained in:
Vadim Zeitlin
2021-03-22 15:31:51 +01:00
parent d1c0d3b18c
commit 71eff92873

View File

@@ -119,110 +119,88 @@ void CreateXrc()
// So save it as a file, from which it can be loaded // So save it as a file, from which it can be loaded
wxStringInputStream sis(xrcText); wxStringInputStream sis(xrcText);
wxFFileOutputStream fos(TEST_XRC_FILE); wxFFileOutputStream fos(TEST_XRC_FILE);
CPPUNIT_ASSERT(fos.IsOk()); REQUIRE(fos.IsOk());
fos.Write(sis); fos.Write(sis);
CPPUNIT_ASSERT(fos.Close()); REQUIRE(fos.Close());
} }
} // anon namespace } // anon namespace
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// test class // test fixture and the tests using it
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class XrcTestCase : public CppUnit::TestCase class XrcTestCase
{ {
public: public:
XrcTestCase() {} XrcTestCase() { CreateXrc(); }
~XrcTestCase() { wxRemoveFile(TEST_XRC_FILE); }
virtual void setUp() wxOVERRIDE { CreateXrc(); }
virtual void tearDown() wxOVERRIDE { wxRemoveFile(TEST_XRC_FILE); }
private: private:
CPPUNIT_TEST_SUITE( XrcTestCase );
CPPUNIT_TEST( ObjectReferences );
CPPUNIT_TEST( IDRanges );
CPPUNIT_TEST_SUITE_END();
void ObjectReferences();
void IDRanges();
wxDECLARE_NO_COPY_CLASS(XrcTestCase); wxDECLARE_NO_COPY_CLASS(XrcTestCase);
}; };
// register in the unnamed registry so that these tests are run by default TEST_CASE_METHOD(XrcTestCase, "XRC::ObjectReferences", "[xrc]")
CPPUNIT_TEST_SUITE_REGISTRATION( XrcTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XrcTestCase, "XrcTestCase" );
void XrcTestCase::ObjectReferences()
{ {
wxXmlResource::Get()->InitAllHandlers(); wxXmlResource::Get()->InitAllHandlers();
for ( int n = 0; n < 2; ++n ) for ( int n = 0; n < 2; ++n )
{ {
// Load the xrc file we're just created // Load the xrc file we're just created
CPPUNIT_ASSERT( wxXmlResource::Get()->Load(TEST_XRC_FILE) ); REQUIRE( wxXmlResource::Get()->Load(TEST_XRC_FILE) );
// In xrc there's now a dialog containing two panels, one an object // In xrc there's now a dialog containing two panels, one an object
// reference of the other // reference of the other
wxDialog dlg; wxDialog dlg;
CPPUNIT_ASSERT( wxXmlResource::Get()->LoadDialog(&dlg, NULL, "dialog") ); REQUIRE( wxXmlResource::Get()->LoadDialog(&dlg, NULL, "dialog") );
// Might as well test XRCCTRL too // Might as well test XRCCTRL too
wxPanel* panel1 = XRCCTRL(dlg,"panel1",wxPanel); wxPanel* panel1 = XRCCTRL(dlg,"panel1",wxPanel);
wxPanel* panel2 = XRCCTRL(dlg,"ref_of_panel1",wxPanel); wxPanel* panel2 = XRCCTRL(dlg,"ref_of_panel1",wxPanel);
// Check that the object reference panel is a different object // Check that the object reference panel is a different object
CPPUNIT_ASSERT( panel2 != panel1 ); CHECK( panel2 != panel1 );
// Unload the xrc, so it can be reloaded and the test rerun // Unload the xrc, so it can be reloaded and the test rerun
CPPUNIT_ASSERT( wxXmlResource::Get()->Unload(TEST_XRC_FILE) ); CHECK( wxXmlResource::Get()->Unload(TEST_XRC_FILE) );
} }
} }
void XrcTestCase::IDRanges() TEST_CASE_METHOD(XrcTestCase, "XRC::IDRanges", "[xrc]")
{ {
// Tests ID ranges // Tests ID ranges
for ( int n = 0; n < 2; ++n ) for ( int n = 0; n < 2; ++n )
{ {
// Load the xrc file we're just created // Load the xrc file we're just created
CPPUNIT_ASSERT( wxXmlResource::Get()->Load(TEST_XRC_FILE) ); REQUIRE( wxXmlResource::Get()->Load(TEST_XRC_FILE) );
// foo[start] should == foo[0] // foo[start] should == foo[0]
CPPUNIT_ASSERT_EQUAL( XRCID("SecondCol[start]"), XRCID("SecondCol[0]") ); CHECK( XRCID("SecondCol[start]") == XRCID("SecondCol[0]") );
// foo[start] should be < foo[end]. Usually that means more negative // foo[start] should be < foo[end]. Usually that means more negative
CPPUNIT_ASSERT( XRCID("SecondCol[start]") < XRCID("SecondCol[end]") ); CHECK( XRCID("SecondCol[start]") < XRCID("SecondCol[end]") );
// Check it works for the positive values in FirstCol too // Check it works for the positive values in FirstCol too
CPPUNIT_ASSERT( XRCID("FirstCol[start]") < XRCID("FirstCol[end]") ); CHECK( XRCID("FirstCol[start]") < XRCID("FirstCol[end]") );
// Check that values are adjacent // Check that values are adjacent
CPPUNIT_ASSERT_EQUAL( XRCID("SecondCol[0]")+1, XRCID("SecondCol[1]") ); CHECK( XRCID("SecondCol[0]")+1 == XRCID("SecondCol[1]") );
CPPUNIT_ASSERT_EQUAL( XRCID("SecondCol[1]")+1, XRCID("SecondCol[2]") ); CHECK( XRCID("SecondCol[1]")+1 == XRCID("SecondCol[2]") );
// And for the positive range // And for the positive range
CPPUNIT_ASSERT_EQUAL( XRCID("FirstCol[2]")+1, XRCID("FirstCol[3]") ); CHECK( XRCID("FirstCol[2]")+1 == XRCID("FirstCol[3]") );
// Check that a large-enough range was created, despite the small // Check that a large-enough range was created, despite the small
// 'size' parameter // 'size' parameter
CPPUNIT_ASSERT_EQUAL CHECK( XRCID("FirstCol[end]") - XRCID("FirstCol[start]") + 1 == 4 );
(
4,
XRCID("FirstCol[end]") - XRCID("FirstCol[start]") + 1
);
// Check that the far-too-large size range worked off the scale too // Check that the far-too-large size range worked off the scale too
CPPUNIT_ASSERT( XRCID("SecondCol[start]") < XRCID("SecondCol[90]") ); CHECK( XRCID("SecondCol[start]") < XRCID("SecondCol[90]") );
CPPUNIT_ASSERT( XRCID("SecondCol[90]") < XRCID("SecondCol[end]") ); CHECK( XRCID("SecondCol[90]") < XRCID("SecondCol[end]") );
CPPUNIT_ASSERT_EQUAL( XRCID("SecondCol[90]")+1, XRCID("SecondCol[91]") ); CHECK( XRCID("SecondCol[90]")+1 == XRCID("SecondCol[91]") );
// Check that the positive range-start parameter worked, even after a // Check that the positive range-start parameter worked, even after a
// reload // reload
CPPUNIT_ASSERT_EQUAL( XRCID("FirstCol[start]"), 10000 ); CHECK( XRCID("FirstCol[start]") == 10000 );
// Unload the xrc, so it can be reloaded and the tests rerun // Unload the xrc, so it can be reloaded and the tests rerun
CPPUNIT_ASSERT( wxXmlResource::Get()->Unload(TEST_XRC_FILE) ); CHECK( wxXmlResource::Get()->Unload(TEST_XRC_FILE) );
} }
} }