I wrote some unit tests when I came across a script that hit me several times already.
I need to generate multiple lines to test a JSON write object. Since the script supports both UTF16 and UTF8 inputs, I want to test it with both.
Consider the following test:
class UTF8;
class UTF16;
template < typename String, typename SourceEncoding >
void writeJson(std::map<String, String> & data)
{
}
void generateStringData(std::map<std::string, std::string> & data)
{
data.emplace("Lorem", "Lorem Ipsum is simply dummy text of the printing and typesetting industry.");
data.emplace("Ipsum", "Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book");
data.emplace("Contrary", "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old");
}
void generateStringData(std::map<std::wstring, std::wstring> & data)
{
data.emplace(L"Lorem", L"Lorem Ipsum is simply dummy text of the printing and typesetting industry.");
data.emplace(L"Ipsum", L"Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book");
data.emplace(L"Contrary", L"Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old");
}
template < typename String, typename SourceEncoding >
void testWriter() {
std::map<String, String> data;
generateStringData(data);
writeJson<String, SourceEncoding>(data);
}
int main() {
testWriter<std::string, UTF8>();
testWriter<std::wstring, UTF16>();
}
I manage to completely wrap everything except the duplicate method generateStringData()
. And I wandered if it is possible to combine both methods generateStringData()
into one?
I know that I can use one method to generate strings in UTF8, and then use an additional method to convert strings to UTF16, but I'm trying to figure out if there is another way.
What have I reviewed / tried?
_T()
TCHAR
#ifdef UNICODE
, , Unicode (, Win >= 7)std::wstring
-, L""
, , wchar_t- char char ,
L''
""s
, charT