From cad2baa636ee134313fc0b2a4c462b8aceb0bc1c Mon Sep 17 00:00:00 2001 From: wilcob Date: Thu, 2 Apr 2015 16:06:33 +0200 Subject: [PATCH 1/3] Add support for wide characters. --- include/CppUTest/CommandLineTestRunner.h | 1 + src/CppUTest/CommandLineTestRunner.cpp | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/CppUTest/CommandLineTestRunner.h b/include/CppUTest/CommandLineTestRunner.h index 2a5477999..132ec9990 100644 --- a/include/CppUTest/CommandLineTestRunner.h +++ b/include/CppUTest/CommandLineTestRunner.h @@ -49,6 +49,7 @@ class CommandLineTestRunner static int RunAllTests(int ac, const char** av); static int RunAllTests(int ac, char** av); + static int RunAllTests(int ac, wchar_t** av); CommandLineTestRunner(int ac, const char** av, TestOutput*, TestRegistry* registry); virtual ~CommandLineTestRunner(); diff --git a/src/CppUTest/CommandLineTestRunner.cpp b/src/CppUTest/CommandLineTestRunner.cpp index 36d83d371..02246e90f 100644 --- a/src/CppUTest/CommandLineTestRunner.cpp +++ b/src/CppUTest/CommandLineTestRunner.cpp @@ -69,6 +69,29 @@ int CommandLineTestRunner::RunAllTests(int ac, const char** av) return result; } +int CommandLineTestRunner::RunAllTests( int ac, wchar_t** av ) +{ + char** avc = new char*[ac]; + for ( int i = 0; i < ac; i++ ) { + size_t len = wcslen( av[i] ) + 1; + avc[i] = new char[len]; + wcstombs_s( 0 , avc[i], sizeof(char)*len, av[i], len ); + } + int result = 0; + + try { + RunAllTests( ac, avc ); + } + catch ( ... ) { + } + + for ( int i = 0; i < ac; i++ ) { + delete[] avc[i]; + } + delete[] avc; + return result; +} + int CommandLineTestRunner::runAllTestsMain() { int testResult = 0; From 22592065cec1b5ccde9c5d0b0d71c6941e75601b Mon Sep 17 00:00:00 2001 From: wilcob Date: Tue, 7 Apr 2015 12:23:39 +0200 Subject: [PATCH 2/3] Added test and processed comments from the merge thread. --- include/CppUTest/CommandLineTestRunner.h | 5 +++- src/CppUTest/CommandLineTestRunner.cpp | 31 ++++++++++++++---------- tests/CommandLineTestRunnerTest.cpp | 16 ++++++++++++ 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/include/CppUTest/CommandLineTestRunner.h b/include/CppUTest/CommandLineTestRunner.h index 132ec9990..3ec7193e7 100644 --- a/include/CppUTest/CommandLineTestRunner.h +++ b/include/CppUTest/CommandLineTestRunner.h @@ -49,9 +49,12 @@ class CommandLineTestRunner static int RunAllTests(int ac, const char** av); static int RunAllTests(int ac, char** av); - static int RunAllTests(int ac, wchar_t** av); + static int RunAllTests(int ac, const wchar_t** av); + static int RunAllTests( int ac, wchar_t** av ); CommandLineTestRunner(int ac, const char** av, TestOutput*, TestRegistry* registry); + static char** ConvertWideArgumentsToNormal( int ac, const wchar_t** av ); + virtual ~CommandLineTestRunner(); int runAllTestsMain(); diff --git a/src/CppUTest/CommandLineTestRunner.cpp b/src/CppUTest/CommandLineTestRunner.cpp index 02246e90f..37dfa0a1e 100644 --- a/src/CppUTest/CommandLineTestRunner.cpp +++ b/src/CppUTest/CommandLineTestRunner.cpp @@ -69,29 +69,34 @@ int CommandLineTestRunner::RunAllTests(int ac, const char** av) return result; } -int CommandLineTestRunner::RunAllTests( int ac, wchar_t** av ) +int CommandLineTestRunner::RunAllTests( int ac, wchar_t** av ) { + return RunAllTests( ac, const_cast ( av ) ); +} + +int CommandLineTestRunner::RunAllTests( int ac, const wchar_t** av ) { - char** avc = new char*[ac]; - for ( int i = 0; i < ac; i++ ) { - size_t len = wcslen( av[i] ) + 1; - avc[i] = new char[len]; - wcstombs_s( 0 , avc[i], sizeof(char)*len, av[i], len ); - } - int result = 0; + char** avc = ConvertWideArgumentsToNormal( ac, av ); - try { - RunAllTests( ac, avc ); - } - catch ( ... ) { - } + int result = RunAllTests( ac, avc ); for ( int i = 0; i < ac; i++ ) { delete[] avc[i]; } delete[] avc; + return result; } +char** CommandLineTestRunner::ConvertWideArgumentsToNormal( int ac, const wchar_t** av ) { + char** avc = new char*[ac]; + for( int i = 0; i < ac; i++ ) { + size_t len = wcslen( av[i] ) + 1; + avc[i] = new char[len]; + wcstombs( avc[i], av[i], len ); + } + return avc; +} + int CommandLineTestRunner::runAllTestsMain() { int testResult = 0; diff --git a/tests/CommandLineTestRunnerTest.cpp b/tests/CommandLineTestRunnerTest.cpp index 8acd3fc56..47a08f976 100644 --- a/tests/CommandLineTestRunnerTest.cpp +++ b/tests/CommandLineTestRunnerTest.cpp @@ -96,6 +96,22 @@ TEST(CommandLineTestRunner, NoPluginsAreInstalledAtTheEndOfARunWhenTheArgumentsA LONGS_EQUAL(0, registry.countPlugins()); } +TEST( CommandLineTestRunner, VerifyWideToNormalConversions ) { + const wchar_t* argv[] = { L"dummy.exe", L"-fdskjnfkds" }; + int ac = 2; + + char** avc = CommandLineTestRunner::ConvertWideArgumentsToNormal( ac, argv ); + + STRCMP_EQUAL( "dummy.exe", avc[0] ); + STRCMP_EQUAL( "-fdskjnfkds", avc[1] ); + + for( int i = 0; i < ac; i++ ) { + delete[] avc[i]; + } + delete[] avc; +} + + struct TestOutputCheckingCommandLineTestRunner : public CommandLineTestRunner { TestOutputCheckingCommandLineTestRunner(int ac, const char** av, TestOutput* output, TestRegistry* registry) : From a587cbf3b0f6096dd436eea84ec774cedbf3d139 Mon Sep 17 00:00:00 2001 From: wilcob Date: Tue, 7 Apr 2015 13:38:24 +0200 Subject: [PATCH 3/3] Educated guess to fix the clang compiler complaining about wcslen. --- include/CppUTest/CommandLineTestRunner.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/CppUTest/CommandLineTestRunner.h b/include/CppUTest/CommandLineTestRunner.h index 3ec7193e7..2ca3d7c67 100644 --- a/include/CppUTest/CommandLineTestRunner.h +++ b/include/CppUTest/CommandLineTestRunner.h @@ -32,6 +32,7 @@ #include "TestOutput.h" #include "CommandLineArguments.h" #include "TestFilter.h" +#include class JUnitTestOutput; class TestRegistry;