Upgrade format unit tests to Catch2

This commit is contained in:
Blake-Madden
2025-09-08 09:57:50 -04:00
parent 8a02590d6e
commit 1780193eab
+125 -186
View File
@@ -31,7 +31,7 @@
// I've put in some checks, such as this which will flag up any platforms
// where this is not the case:
//
// CPPUNIT_ASSERT(wxString::Format(wxT("%hs"), "test") == wxT("test"));
// CHECK(wxString::Format(wxT("%hs"), "test") == wxT("test"));
//
// For compilers that support precompilation, includes "wx/wx.h".
@@ -44,7 +44,6 @@
#endif
using CppUnit::TestCase;
using std::string;
///////////////////////////////////////////////////////////////////////////////
@@ -54,155 +53,184 @@ using std::string;
// equally, therefore it is enough to choose just one other for testing, %d
// will do.
class FormatConverterTestCase : public TestCase
void check(const wxString& input, const wxString& expectedScanf,
const wxString& expectedUtf8,
const wxString& expectedWcharUnix,
const wxString& expectedWcharWindows)
{
CPPUNIT_TEST_SUITE(FormatConverterTestCase);
CPPUNIT_TEST(format_d);
CPPUNIT_TEST(format_hd);
CPPUNIT_TEST(format_ld);
CPPUNIT_TEST(format_s);
CPPUNIT_TEST(format_hs);
CPPUNIT_TEST(format_ls);
CPPUNIT_TEST(format_c);
CPPUNIT_TEST(format_hc);
CPPUNIT_TEST(format_lc);
CPPUNIT_TEST(format_S);
CPPUNIT_TEST(format_hS);
CPPUNIT_TEST(format_lS);
CPPUNIT_TEST(format_C);
CPPUNIT_TEST(format_hC);
CPPUNIT_TEST(format_lC);
CPPUNIT_TEST(testLonger);
CPPUNIT_TEST_SUITE_END();
INFO("expectedScanf: " + expectedScanf);
INFO("expectedUtf8: " + expectedUtf8);
INFO("expectedWcharUnix: " + expectedWcharUnix);
INFO("expectedWcharWindows: " + expectedWcharWindows);
void format_d();
void format_hd();
void format_ld();
void format_s();
void format_hs();
void format_ls();
void format_c();
void format_hc();
void format_lc();
wxString result, msg;
void format_S();
void format_hS();
void format_lS();
void format_C();
void format_hC();
void format_lC();
void testLonger();
#ifndef __WINDOWS__
// on windows, wxScanf() string needs no modifications
result = wxScanfConvertFormatW(input.wc_str());
void doTest(const char *input, const char *expectedScanf,
const char *expectedUtf8,
const char *expectedWcharUnix,
const char *expectedWcharWindows);
void check(const wxString& input, const wxString& expectedScanf,
const wxString& expectedUtf8,
const wxString& expectedWcharUnix,
const wxString& expectedWcharWindows);
};
msg = wxT("input: '") + input +
wxT("', result (scanf): '") + result +
wxT("', expected: '") + expectedScanf + wxT("'");
CHECK(result == expectedScanf);
#endif // !__WINDOWS__
void FormatConverterTestCase::format_d()
{
doTest("d", "d", "d", "d", "d");
CPPUNIT_ASSERT(wxString::Format(wxT("%d"), 255) == wxT("255"));
CPPUNIT_ASSERT(wxString::Format(wxT("%05d"), 255) == wxT("00255"));
CPPUNIT_ASSERT(wxString::Format(wxT("% 5d"), 255) == wxT(" 255"));
CPPUNIT_ASSERT(wxString::Format(wxT("% 5d"), -255) == wxT(" -255"));
CPPUNIT_ASSERT(wxString::Format(wxT("%-5d"), -255) == wxT("-255 "));
CPPUNIT_ASSERT(wxString::Format(wxT("%+5d"), 255) == wxT(" +255"));
CPPUNIT_ASSERT(wxString::Format(wxT("%*d"), 5, 255) == wxT(" 255"));
#if wxUSE_UNICODE_UTF8
result = (const char*)wxFormatString(input);
msg = wxT("input: '") + input +
wxT("', result (UTF-8): '") + result +
wxT("', expected: '") + expectedUtf8 + wxT("'");
CHECK(result == expectedUtf8);
#endif // wxUSE_UNICODE_UTF8
#if !wxUSE_UTF8_LOCALE_ONLY
result = (const wchar_t*)wxFormatString(input);
#if defined(__WINDOWS__) && \
!defined(__CYGWIN__) && \
!defined(__MINGW32__)
wxString expectedWchar(expectedWcharWindows);
#else
wxString expectedWchar(expectedWcharUnix);
#endif
msg = wxT("input: '") + input +
wxT("', result (wchar_t): '") + result +
wxT("', expected: '") + expectedWchar + wxT("'");
CHECK(result == expectedWchar);
#endif // !wxUSE_UTF8_LOCALE_ONLY
}
void FormatConverterTestCase::format_hd()
void doTest(const char* input, const char* expectedScanf,
const char* expectedUtf8,
const char* expectedWcharUnix,
const char* expectedWcharWindows)
{
static const wxChar* flag_width[] =
{ wxT(""), wxT("*"), wxT("10"), wxT("-*"), wxT("-10"), nullptr };
static const wxChar* precision[] =
{ wxT(""), wxT(".*"), wxT(".10"), nullptr };
static const wxChar* empty[] =
{ wxT(""), nullptr };
// no precision for %c or %C
const wxChar** precs = wxTolower(input[wxStrlen(input) - 1]) == wxT('c') ?
empty : precision;
wxString fmt(wxT("%"));
// try the test for a variety of combinations of flag, width and precision
for (const wxChar** prec = precs; *prec; prec++)
for (const wxChar** width = flag_width; *width; width++)
check(fmt + *width + *prec + input,
fmt + *width + *prec + expectedScanf,
fmt + *width + *prec + expectedUtf8,
fmt + *width + *prec + expectedWcharUnix,
fmt + *width + *prec + expectedWcharWindows);
}
TEST_CASE("Format::d", "[formatconverter]")
{
doTest("d", "d", "d", "d", "d");
CHECK(wxString::Format(wxT("%d"), 255) == wxT("255"));
CHECK(wxString::Format(wxT("%05d"), 255) == wxT("00255"));
CHECK(wxString::Format(wxT("% 5d"), 255) == wxT(" 255"));
CHECK(wxString::Format(wxT("% 5d"), -255) == wxT(" -255"));
CHECK(wxString::Format(wxT("%-5d"), -255) == wxT("-255 "));
CHECK(wxString::Format(wxT("%+5d"), 255) == wxT(" +255"));
CHECK(wxString::Format(wxT("%*d"), 5, 255) == wxT(" 255"));
}
TEST_CASE("Format::hd", "[formatconverter]")
{
doTest("hd", "hd", "hd", "hd", "hd");
short s = 32767;
CPPUNIT_ASSERT(wxString::Format(wxT("%hd"), s) == wxT("32767"));
CHECK(wxString::Format(wxT("%hd"), s) == wxT("32767"));
}
void FormatConverterTestCase::format_ld()
TEST_CASE("Format::ld", "[formatconverter]")
{
doTest("ld", "ld", "ld", "ld", "ld");
long l = 2147483647L;
CPPUNIT_ASSERT(wxString::Format(wxT("%ld"), l) == wxT("2147483647"));
CHECK(wxString::Format(wxT("%ld"), l) == wxT("2147483647"));
}
void FormatConverterTestCase::format_s()
TEST_CASE("Format::s", "[formatconverter]")
{
doTest("s", "ls", "s", "ls", "s");
CPPUNIT_ASSERT(wxString::Format(wxT("%s!"), wxT("test")) == wxT("test!"));
CPPUNIT_ASSERT(wxString::Format(wxT("%6s!"), wxT("test")) == wxT(" test!"));
CPPUNIT_ASSERT(wxString::Format(wxT("%-6s!"), wxT("test")) == wxT("test !"));
CPPUNIT_ASSERT(wxString::Format(wxT("%.6s!"), wxT("test")) == wxT("test!"));
CPPUNIT_ASSERT(wxString::Format(wxT("%6.4s!"), wxT("testing")) == wxT(" test!"));
CHECK(wxString::Format(wxT("%s!"), wxT("test")) == wxT("test!"));
CHECK(wxString::Format(wxT("%6s!"), wxT("test")) == wxT(" test!"));
CHECK(wxString::Format(wxT("%-6s!"), wxT("test")) == wxT("test !"));
CHECK(wxString::Format(wxT("%.6s!"), wxT("test")) == wxT("test!"));
CHECK(wxString::Format(wxT("%6.4s!"), wxT("testing")) == wxT(" test!"));
}
void FormatConverterTestCase::format_hs()
TEST_CASE("Format::hs", "[formatconverter]")
{
doTest("hs", "hs", "s", "ls", "s");
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%hs!")), "test") == wxT("test!"));
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%6hs!")), "test") == wxT(" test!"));
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%-6hs!")), "test") == wxT("test !"));
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%.6hs!")), "test") == wxT("test!"));
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%6.4hs!")), "testing") == wxT(" test!"));
CHECK(wxString::Format(wxString(wxT("%hs!")), "test") == wxT("test!"));
CHECK(wxString::Format(wxString(wxT("%6hs!")), "test") == wxT(" test!"));
CHECK(wxString::Format(wxString(wxT("%-6hs!")), "test") == wxT("test !"));
CHECK(wxString::Format(wxString(wxT("%.6hs!")), "test") == wxT("test!"));
CHECK(wxString::Format(wxString(wxT("%6.4hs!")), "testing") == wxT(" test!"));
}
void FormatConverterTestCase::format_ls()
TEST_CASE("Format::ls", "[formatconverter]")
{
doTest("ls", "ls", "s", "ls", "s");
CPPUNIT_ASSERT(wxString::Format(wxT("%ls!"), L"test") == wxT("test!"));
CPPUNIT_ASSERT(wxString::Format(wxT("%6ls!"), L"test") == wxT(" test!"));
CPPUNIT_ASSERT(wxString::Format(wxT("%-6ls!"), L"test") == wxT("test !"));
CPPUNIT_ASSERT(wxString::Format(wxT("%.6ls!"), L"test") == wxT("test!"));
CPPUNIT_ASSERT(wxString::Format(wxT("%6.4ls!"), L"testing") == wxT(" test!"));
CHECK(wxString::Format(wxT("%ls!"), L"test") == wxT("test!"));
CHECK(wxString::Format(wxT("%6ls!"), L"test") == wxT(" test!"));
CHECK(wxString::Format(wxT("%-6ls!"), L"test") == wxT("test !"));
CHECK(wxString::Format(wxT("%.6ls!"), L"test") == wxT("test!"));
CHECK(wxString::Format(wxT("%6.4ls!"), L"testing") == wxT(" test!"));
}
void FormatConverterTestCase::format_c()
TEST_CASE("Format::c", "[formatconverter]")
{
doTest("c", "lc", "lc", "lc", "c");
CPPUNIT_ASSERT(wxString::Format(wxT("%c"), wxT('x')) == wxT("x"));
CPPUNIT_ASSERT(wxString::Format(wxT("%2c"), wxT('x')) == wxT(" x"));
CPPUNIT_ASSERT(wxString::Format(wxT("%-2c"), wxT('x')) == wxT("x "));
CHECK(wxString::Format(wxT("%c"), wxT('x')) == wxT("x"));
CHECK(wxString::Format(wxT("%2c"), wxT('x')) == wxT(" x"));
CHECK(wxString::Format(wxT("%-2c"), wxT('x')) == wxT("x "));
}
void FormatConverterTestCase::format_hc()
TEST_CASE("Format::hc", "[formatconverter]")
{
doTest("hc", "hc", "lc", "lc", "c");
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%hc")), 'x') == wxT("x"));
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%2hc")), 'x') == wxT(" x"));
CPPUNIT_ASSERT(wxString::Format(wxString(wxT("%-2hc")), 'x') == wxT("x "));
CHECK(wxString::Format(wxString(wxT("%hc")), 'x') == wxT("x"));
CHECK(wxString::Format(wxString(wxT("%2hc")), 'x') == wxT(" x"));
CHECK(wxString::Format(wxString(wxT("%-2hc")), 'x') == wxT("x "));
}
void FormatConverterTestCase::format_lc()
TEST_CASE("Format::lc", "[formatconverter]")
{
doTest("lc", "lc", "lc", "lc", "c");
CPPUNIT_ASSERT(wxString::Format(wxT("%lc"), L'x') == wxT("x"));
CPPUNIT_ASSERT(wxString::Format(wxT("%2lc"), L'x') == wxT(" x"));
CPPUNIT_ASSERT(wxString::Format(wxT("%-2lc"), L'x') == wxT("x "));
CHECK(wxString::Format(wxT("%lc"), L'x') == wxT("x"));
CHECK(wxString::Format(wxT("%2lc"), L'x') == wxT(" x"));
CHECK(wxString::Format(wxT("%-2lc"), L'x') == wxT("x "));
}
void FormatConverterTestCase::format_S()
TEST_CASE("Format::S", "[formatconverter]")
{ doTest("S", "s", "s", "ls", "s"); }
void FormatConverterTestCase::format_hS()
TEST_CASE("Format::hS", "[formatconverter]")
{ doTest("hS", "s", "s", "ls", "s"); }
void FormatConverterTestCase::format_lS()
TEST_CASE("Format::lS", "[formatconverter]")
{ doTest("lS", "ls", "s", "ls", "s"); }
void FormatConverterTestCase::format_C()
TEST_CASE("Format::C", "[formatconverter]")
{ doTest("C", "c", "lc", "lc", "c"); }
void FormatConverterTestCase::format_hC()
TEST_CASE("Format::hC", "[formatconverter]")
{ doTest("hC", "c", "lc", "lc", "c"); }
void FormatConverterTestCase::format_lC()
TEST_CASE("Format::lC", "[formatconverter]")
{ doTest("lC", "lc", "lc", "lc", "c"); }
// It's possible that although a format converts correctly alone, it leaves
// the converter in a bad state that will affect subsequent formats, so
// check with a selection of longer patterns.
//
void FormatConverterTestCase::testLonger()
TEST_CASE("Test Longer", "[formatconverter]")
{
struct {
const char *input;
@@ -242,92 +270,3 @@ void FormatConverterTestCase::testLonger()
}
}
}
void FormatConverterTestCase::doTest(const char *input,
const char *expectedScanf,
const char *expectedUtf8,
const char *expectedWcharUnix,
const char *expectedWcharWindows)
{
static const wxChar *flag_width[] =
{ wxT(""), wxT("*"), wxT("10"), wxT("-*"), wxT("-10"), nullptr };
static const wxChar *precision[] =
{ wxT(""), wxT(".*"), wxT(".10"), nullptr };
static const wxChar *empty[] =
{ wxT(""), nullptr };
// no precision for %c or %C
const wxChar **precs = wxTolower(input[wxStrlen(input)-1]) == wxT('c') ?
empty : precision;
wxString fmt(wxT("%"));
// try the test for a variety of combinations of flag, width and precision
for (const wxChar **prec = precs; *prec; prec++)
for (const wxChar **width = flag_width; *width; width++)
check(fmt + *width + *prec + input,
fmt + *width + *prec + expectedScanf,
fmt + *width + *prec + expectedUtf8,
fmt + *width + *prec + expectedWcharUnix,
fmt + *width + *prec + expectedWcharWindows);
}
void FormatConverterTestCase::check(const wxString& input,
const wxString& expectedScanf,
const wxString& expectedUtf8,
const wxString& expectedWcharUnix,
const wxString& expectedWcharWindows)
{
// all of them are unused in some build configurations
wxUnusedVar(expectedScanf);
wxUnusedVar(expectedUtf8);
wxUnusedVar(expectedWcharUnix);
wxUnusedVar(expectedWcharWindows);
wxString result, msg;
#ifndef __WINDOWS__
// on windows, wxScanf() string needs no modifications
result = wxScanfConvertFormatW(input.wc_str());
msg = wxT("input: '") + input +
wxT("', result (scanf): '") + result +
wxT("', expected: '") + expectedScanf + wxT("'");
CPPUNIT_ASSERT_MESSAGE(string(msg.mb_str()), result == expectedScanf);
#endif // !__WINDOWS__
#if wxUSE_UNICODE_UTF8
result = (const char*)wxFormatString(input);
msg = wxT("input: '") + input +
wxT("', result (UTF-8): '") + result +
wxT("', expected: '") + expectedUtf8 + wxT("'");
CPPUNIT_ASSERT_MESSAGE(string(msg.mb_str()), result == expectedUtf8);
#endif // wxUSE_UNICODE_UTF8
#if !wxUSE_UTF8_LOCALE_ONLY
result = (const wchar_t*)wxFormatString(input);
#if defined(__WINDOWS__) && \
!defined(__CYGWIN__) && \
!defined(__MINGW32__)
wxString expectedWchar(expectedWcharWindows);
#else
wxString expectedWchar(expectedWcharUnix);
#endif
msg = wxT("input: '") + input +
wxT("', result (wchar_t): '") + result +
wxT("', expected: '") + expectedWchar + wxT("'");
CPPUNIT_ASSERT_MESSAGE(string(msg.mb_str()), result == expectedWchar);
#endif // !wxUSE_UTF8_LOCALE_ONLY
}
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION(FormatConverterTestCase);
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FormatConverterTestCase,
"FormatConverterTestCase");