29#include <boost/test/unit_test.hpp>
34#include <wx/filename.h>
51 std::string m_msgIdPlural;
52 std::vector<std::string> m_msgStrs;
59std::string unquote(
const std::string& aLine )
64 for(
size_t i = 0; i < aLine.size(); ++i )
76 if( ch ==
'\\' && i + 1 < aLine.size() )
92std::vector<PO_ENTRY> parsePo(
const wxString& aPath )
94 std::vector<PO_ENTRY> entries;
95 std::ifstream file(
ToFsPath( aPath ), std::ios::binary );
101 bool haveCur =
false;
102 bool pendingFuzzy =
false;
106 auto starts = [](
const std::string& aStr,
const char* aPrefix )
108 return aStr.rfind( aPrefix, 0 ) == 0;
114 while( std::getline( file, rawLine ) )
117 const size_t first = rawLine.find_first_not_of(
" \t\r" );
118 std::string line = first == std::string::npos
120 : rawLine.substr( first, rawLine.find_last_not_of(
" \t\r" ) - first + 1 );
122 if( starts( line,
"#," ) )
125 if( line.find(
"fuzzy" ) != std::string::npos )
128 else if( starts( line,
"msgid_plural" ) )
131 cur.m_msgIdPlural = unquote( line );
135 else if( starts( line,
"msgid " ) )
138 entries.push_back( cur );
141 cur.m_msgId = unquote( line );
143 cur.m_fuzzy = pendingFuzzy;
144 pendingFuzzy =
false;
148 else if( starts( line,
"msgstr[" ) )
152 int idx = std::atoi( line.c_str() +
sizeof(
"msgstr[" ) - 1 );
155 if( idx >= 0 && idx < 64 )
157 strIdx =
static_cast<size_t>( idx );
159 if( cur.m_msgStrs.size() <= strIdx )
160 cur.m_msgStrs.resize( strIdx + 1 );
162 cur.m_msgStrs[strIdx] = unquote( line );
168 else if( starts( line,
"msgstr " ) )
172 cur.m_msgStrs.assign( 1, unquote( line ) );
178 else if( !line.empty() && line[0] ==
'"' )
180 if( haveCur && state == ID )
181 cur.m_msgId += unquote( line );
182 else if( haveCur && state == IDP )
183 cur.m_msgIdPlural += unquote( line );
184 else if( haveCur && state == STR && strIdx < cur.m_msgStrs.size() )
185 cur.m_msgStrs[strIdx] += unquote( line );
187 else if( line.empty() )
198 entries.push_back( cur );
209std::map<char, int> consumingSpecs(
const std::string& aStr )
211 static const std::string flags =
"-+#0";
212 static const std::string convs =
"diouxXeEfFgGaAcsSCp";
214 std::map<char, int> counts;
215 std::map<char, std::set<int>> posArgs;
217 for(
size_t i = 0; i < aStr.size(); ++i )
224 if( j < aStr.size() && aStr[j] ==
'%' )
234 while( k < aStr.size() && std::isdigit(
static_cast<unsigned char>( aStr[k] ) ) )
237 if( k < aStr.size() && aStr[k] ==
'$' && k > j )
239 position = std::atoi( aStr.c_str() + j );
243 while( j < aStr.size() && flags.find( aStr[j] ) != std::string::npos )
247 if( j < aStr.size() && aStr[j] ==
'*' )
254 while( j < aStr.size() && std::isdigit(
static_cast<unsigned char>( aStr[j] ) ) )
258 if( j < aStr.size() && aStr[j] ==
'.' )
263 if( j < aStr.size() && aStr[j] ==
'*' )
270 while( j < aStr.size() && std::isdigit(
static_cast<unsigned char>( aStr[j] ) ) )
275 static const std::string lengths =
"lhzjtL";
277 while( j < aStr.size() && lengths.find( aStr[j] ) != std::string::npos )
280 if( j < aStr.size() && convs.find( aStr[j] ) != std::string::npos )
283 posArgs[aStr[j]].insert( position );
291 for(
const auto& [conv, positions] : posArgs )
292 counts[conv] +=
static_cast<int>( positions.size() );
300 wxFileName dir = wxFileName::DirName( wxString::FromUTF8( QA_SRC_ROOT ) );
301 dir.AppendDir( wxS(
"translation" ) );
302 dir.AppendDir( wxS(
"pofiles" ) );
317 wxString poPath = poDir().GetFullPath();
320 "Translation pofiles directory not found: " << poPath.utf8_string() );
323 wxDir::GetAllFiles( poPath, &files, wxS(
"*.po" ), wxDIR_FILES );
329 for(
const wxString& po : files )
331 wxFileName poFile( po );
333 for(
const PO_ENTRY& entry : parsePo( po ) )
339 std::map<char, int> src = consumingSpecs( entry.m_msgId );
341 for(
const auto& [conv, count] : consumingSpecs( entry.m_msgIdPlural ) )
342 src[conv] = std::max( src[conv], count );
349 for(
const std::string& msgStr : entry.m_msgStrs )
354 std::map<char, int> dst = consumingSpecs( msgStr );
356 for(
const auto& [conv, count] : dst )
358 if( count > src[conv] )
361 BOOST_ERROR( poFile.GetFullName().utf8_string()
362 <<
":" << entry.m_line <<
" translation adds extra '%" << conv
363 <<
"' specifier (msgid=\"" << entry.m_msgId <<
"\" msgstr=\""
364 << msgStr <<
"\")" );
std::filesystem::path ToFsPath(const wxString &aPath)
Give aPath to std::filesystem without a detour through the C locale.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
BOOST_AUTO_TEST_SUITE_END()