Improve utilities for testing tageditor CLI

This commit is contained in:
Martchus 2016-08-03 17:31:28 +02:00
parent df8d942e1c
commit 994181cbf9
4 changed files with 66 additions and 8 deletions

View File

@ -85,7 +85,7 @@ public:
break;
}
}
return make_pair(unique_ptr<char[], StringDataDeleter>(outputBuffer), currentOutputOffset - outputBuffer);
return StringData(outputBuffer, currentOutputOffset - outputBuffer);
}
private:

View File

@ -5,6 +5,7 @@
#include "./binaryconversion.h"
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <initializer_list>
@ -35,6 +36,7 @@ struct LIB_EXPORT StringDataDeleter {
* \brief Type used to return string encoding conversion result.
*/
typedef std::pair<std::unique_ptr<char[], StringDataDeleter>, std::size_t> StringData;
//typedef std::pair<std::unique_ptr<char>, std::size_t> StringData; // might work too
LIB_EXPORT StringData convertString(const char *fromCharset, const char *toCharset, const char *inputBuffer, std::size_t inputBufferSize, float outputBufferSizeFactor = 1.0f);
LIB_EXPORT StringData convertUtf8ToUtf16LE(const char *inputBuffer, std::size_t inputBufferSize);
@ -163,6 +165,53 @@ template <typename StringType> LIB_EXPORT bool startsWith(const StringType &str,
return false;
}
/*!
* \brief Returns whether \a str starts with \a phrase.
*/
template <typename StringType> LIB_EXPORT bool startsWith(const StringType &str, const typename StringType::value_type *phrase)
{
for(auto stri = str.cbegin(), strend = str.cend(); stri != strend; ++stri, ++phrase) {
if(!*phrase) {
return true;
} else if(*stri != *phrase) {
return false;
}
}
return false;
}
/*!
* \brief Returns whether \a str contains the specified \a substrings.
* \remarks The \a substrings must occur in the specified order.
*/
template <typename StringType> LIB_EXPORT bool containsSubstrings(const StringType &str, std::initializer_list<StringType> substrings)
{
typename StringType::size_type currentPos = 0;
for(const auto &substr : substrings) {
if((currentPos = str.find(substr, currentPos)) == StringType::npos) {
return false;
}
currentPos += substr.size();
}
return true;
}
/*!
* \brief Returns whether \a str contains the specified \a substrings.
* \remarks The \a substrings must occur in the specified order.
*/
template <typename StringType> LIB_EXPORT bool containsSubstrings(const StringType &str, std::initializer_list<const typename StringType::value_type *> substrings)
{
typename StringType::size_type currentPos = 0;
for(const auto *substr : substrings) {
if((currentPos = str.find(substr, currentPos)) == StringType::npos) {
return false;
}
currentPos += std::strlen(substr);
}
return true;
}
/*!
* \brief Replaces all occurences of \a find with \a relpace in the specified \a str.
*/

View File

@ -163,7 +163,7 @@ void ConversionTests::testSwapOrderFunctions()
/*!
* \brief Internally used for string encoding tests to check results.
*/
void assertEqual(const char *message, const byte *expectedValues, size_t expectedSize, const pair<unique_ptr<char[], StringDataDeleter>, size_t> &actualValues)
void assertEqual(const char *message, const byte *expectedValues, size_t expectedSize, const StringData &actualValues)
{
// check whether number of elements matches
CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expectedSize, actualValues.second);
@ -228,7 +228,7 @@ void ConversionTests::testStringEncodingConversions()
*/
void ConversionTests::testStringConversions()
{
// test stringToNumber() / numberToString() with random numbers
// stringToNumber() / numberToString() with random numbers
uniform_int_distribution<int64> randomDistSigned(numeric_limits<int64>::min());
uniform_int_distribution<uint64> randomDistUnsigned(0);
for(byte b = 1; b < 100; ++b) {
@ -248,10 +248,10 @@ void ConversionTests::testStringConversions()
}
}
// test interpretIntegerAsString()
// interpretIntegerAsString()
CPPUNIT_ASSERT(interpretIntegerAsString<uint32>(0x54455354) == "TEST");
// test splitString() / joinStrings()
// splitString() / joinStrings()
auto splitJoinTest = joinStrings(splitString<vector<string> >(",a,,ab,ABC,s", ",", EmptyPartsTreat::Keep), " ", false, "(", ")");
CPPUNIT_ASSERT(splitJoinTest == "() (a) () (ab) (ABC) (s)");
splitJoinTest = joinStrings(splitString<vector<string> >(",a,,ab,ABC,s", ",", EmptyPartsTreat::Keep), " ", true, "(", ")");
@ -261,16 +261,20 @@ void ConversionTests::testStringConversions()
splitJoinTest = joinStrings(splitString<vector<string> >(",a,,ab,ABC,s", ",", EmptyPartsTreat::Merge), " ", false, "(", ")");
CPPUNIT_ASSERT(splitJoinTest == "(a,ab) (ABC) (s)");
// test findAndReplace()
// findAndReplace()
string findReplaceTest("findAndReplace()");
findAndReplace<string>(findReplaceTest, "And", "Or");
CPPUNIT_ASSERT(findReplaceTest == "findOrReplace()");
// test startsWith()
// startsWith()
CPPUNIT_ASSERT(!startsWith<string>(findReplaceTest, "findAnd"));
CPPUNIT_ASSERT(startsWith<string>(findReplaceTest, "findOr"));
// test encodeBase64() / decodeBase64() with random data
// containsSubstrings()
CPPUNIT_ASSERT(containsSubstrings<string>("this string contains foo and bar", {"foo", "bar"}));
CPPUNIT_ASSERT(!containsSubstrings<string>("this string contains foo and bar", {"bar", "foo"}));
// encodeBase64() / decodeBase64() with random data
uniform_int_distribution<byte> randomDistChar;
byte originalBase64Data[4047];
for(byte &c : originalBase64Data) {

View File

@ -237,6 +237,11 @@ int TestApplication::execApp(const char *const *args, string &stdout, string &st
if(int child = fork()) {
// parent process: read stdout and stderr from child
close(writeCoutPipe), close(writeCerrPipe);
if(child == -1) {
close(readCoutPipe), close(readCerrPipe);
throw runtime_error("Unable to create fork");
}
// TODO: use select instead of threads
thread readCoutThread([readCoutPipe, &stdout] {
char buffer[512];
ssize_t count;