KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_translation_format_strings.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
28
29#include <boost/test/unit_test.hpp>
30
31#include <gestfich.h>
32
33#include <wx/dir.h>
34#include <wx/filename.h>
35
36#include <algorithm>
37#include <cctype>
38#include <cstdlib>
39#include <fstream>
40#include <map>
41#include <set>
42#include <string>
43#include <vector>
44
45
46namespace
47{
48struct PO_ENTRY
49{
50 std::string m_msgId;
51 std::string m_msgIdPlural;
52 std::vector<std::string> m_msgStrs;
53 int m_line = 0;
54 bool m_fuzzy = false;
55};
56
57
58// Concatenate the double-quoted runs of a .po line, honoring backslash escapes.
59std::string unquote( const std::string& aLine )
60{
61 std::string out;
62 bool inQuote = false;
63
64 for( size_t i = 0; i < aLine.size(); ++i )
65 {
66 char ch = aLine[i];
67
68 if( !inQuote )
69 {
70 if( ch == '"' )
71 inQuote = true;
72
73 continue;
74 }
75
76 if( ch == '\\' && i + 1 < aLine.size() )
77 {
78 out += aLine[++i];
79 continue;
80 }
81
82 if( ch == '"' )
83 inQuote = false;
84 else
85 out += ch;
86 }
87
88 return out;
89}
90
91
92std::vector<PO_ENTRY> parsePo( const wxString& aPath )
93{
94 std::vector<PO_ENTRY> entries;
95 std::ifstream file( ToFsPath( aPath ), std::ios::binary );
96
97 if( !file.is_open() )
98 return entries;
99
100 PO_ENTRY cur;
101 bool haveCur = false;
102 bool pendingFuzzy = false;
103 enum { NONE, ID, IDP, STR } state = NONE;
104 size_t strIdx = 0;
105
106 auto starts = []( const std::string& aStr, const char* aPrefix )
107 {
108 return aStr.rfind( aPrefix, 0 ) == 0;
109 };
110
111 int lineNo = 0;
112 std::string rawLine;
113
114 while( std::getline( file, rawLine ) )
115 {
116 ++lineNo;
117 const size_t first = rawLine.find_first_not_of( " \t\r" );
118 std::string line = first == std::string::npos
119 ? std::string()
120 : rawLine.substr( first, rawLine.find_last_not_of( " \t\r" ) - first + 1 );
121
122 if( starts( line, "#," ) )
123 {
124 // A fuzzy flag may share its "#," line with others, so accumulate rather than overwrite.
125 if( line.find( "fuzzy" ) != std::string::npos )
126 pendingFuzzy = true;
127 }
128 else if( starts( line, "msgid_plural" ) )
129 {
130 if( haveCur )
131 cur.m_msgIdPlural = unquote( line );
132
133 state = IDP;
134 }
135 else if( starts( line, "msgid " ) )
136 {
137 if( haveCur )
138 entries.push_back( cur );
139
140 cur = PO_ENTRY();
141 cur.m_msgId = unquote( line );
142 cur.m_line = lineNo;
143 cur.m_fuzzy = pendingFuzzy;
144 pendingFuzzy = false;
145 haveCur = true;
146 state = ID;
147 }
148 else if( starts( line, "msgstr[" ) )
149 {
150 if( haveCur )
151 {
152 int idx = std::atoi( line.c_str() + sizeof( "msgstr[" ) - 1 );
153
154 // No language has dozens of plural forms; reject wild indices to bound the resize.
155 if( idx >= 0 && idx < 64 )
156 {
157 strIdx = static_cast<size_t>( idx );
158
159 if( cur.m_msgStrs.size() <= strIdx )
160 cur.m_msgStrs.resize( strIdx + 1 );
161
162 cur.m_msgStrs[strIdx] = unquote( line );
163 }
164 }
165
166 state = STR;
167 }
168 else if( starts( line, "msgstr " ) )
169 {
170 if( haveCur )
171 {
172 cur.m_msgStrs.assign( 1, unquote( line ) );
173 strIdx = 0;
174 }
175
176 state = STR;
177 }
178 else if( !line.empty() && line[0] == '"' )
179 {
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 );
186 }
187 else if( line.empty() )
188 {
189 // Blank lines separate entries; the next msgid flushes.
190 }
191 else
192 {
193 state = NONE;
194 }
195 }
196
197 if( haveCur )
198 entries.push_back( cur );
199
200 return entries;
201}
202
203
209std::map<char, int> consumingSpecs( const std::string& aStr )
210{
211 static const std::string flags = "-+#0";
212 static const std::string convs = "diouxXeEfFgGaAcsSCp";
213
214 std::map<char, int> counts; // non-positional and '*' consumptions
215 std::map<char, std::set<int>> posArgs; // distinct positional indices per conversion
216
217 for( size_t i = 0; i < aStr.size(); ++i )
218 {
219 if( aStr[i] != '%' )
220 continue;
221
222 size_t j = i + 1;
223
224 if( j < aStr.size() && aStr[j] == '%' )
225 {
226 i = j;
227 continue;
228 }
229
230 // Optional positional argument "<digits>$".
231 int position = 0;
232 size_t k = j;
233
234 while( k < aStr.size() && std::isdigit( static_cast<unsigned char>( aStr[k] ) ) )
235 ++k;
236
237 if( k < aStr.size() && aStr[k] == '$' && k > j )
238 {
239 position = std::atoi( aStr.c_str() + j );
240 j = k + 1;
241 }
242
243 while( j < aStr.size() && flags.find( aStr[j] ) != std::string::npos )
244 ++j;
245
246 // A '*' width consumes an int argument; digits are a literal width.
247 if( j < aStr.size() && aStr[j] == '*' )
248 {
249 counts['*']++;
250 ++j;
251 }
252 else
253 {
254 while( j < aStr.size() && std::isdigit( static_cast<unsigned char>( aStr[j] ) ) )
255 ++j;
256 }
257
258 if( j < aStr.size() && aStr[j] == '.' )
259 {
260 ++j;
261
262 // A '*' precision likewise consumes an int argument.
263 if( j < aStr.size() && aStr[j] == '*' )
264 {
265 counts['*']++;
266 ++j;
267 }
268 else
269 {
270 while( j < aStr.size() && std::isdigit( static_cast<unsigned char>( aStr[j] ) ) )
271 ++j;
272 }
273 }
274
275 static const std::string lengths = "lhzjtL";
276
277 while( j < aStr.size() && lengths.find( aStr[j] ) != std::string::npos )
278 ++j;
279
280 if( j < aStr.size() && convs.find( aStr[j] ) != std::string::npos )
281 {
282 if( position > 0 )
283 posArgs[aStr[j]].insert( position );
284 else
285 counts[aStr[j]]++;
286
287 i = j;
288 }
289 }
290
291 for( const auto& [conv, positions] : posArgs )
292 counts[conv] += static_cast<int>( positions.size() );
293
294 return counts;
295}
296
297
298wxFileName poDir()
299{
300 wxFileName dir = wxFileName::DirName( wxString::FromUTF8( QA_SRC_ROOT ) );
301 dir.AppendDir( wxS( "translation" ) );
302 dir.AppendDir( wxS( "pofiles" ) );
303 return dir;
304}
305} // namespace
306
307
308BOOST_AUTO_TEST_SUITE( TranslationFormatStrings )
309
310
311
315BOOST_AUTO_TEST_CASE( NoExtraFormatSpecifiers )
316{
317 wxString poPath = poDir().GetFullPath();
318
319 BOOST_REQUIRE_MESSAGE( wxDir::Exists( poPath ),
320 "Translation pofiles directory not found: " << poPath.utf8_string() );
321
322 wxArrayString files;
323 wxDir::GetAllFiles( poPath, &files, wxS( "*.po" ), wxDIR_FILES );
324
325 BOOST_REQUIRE_MESSAGE( !files.empty(), "No .po files found in " << poPath.utf8_string() );
326
327 int violations = 0;
328
329 for( const wxString& po : files )
330 {
331 wxFileName poFile( po );
332
333 for( const PO_ENTRY& entry : parsePo( po ) )
334 {
335 if( entry.m_fuzzy )
336 continue;
337
338 // A plural form may reference either source string, so budget the per-conversion max.
339 std::map<char, int> src = consumingSpecs( entry.m_msgId );
340
341 for( const auto& [conv, count] : consumingSpecs( entry.m_msgIdPlural ) )
342 src[conv] = std::max( src[conv], count );
343
344 // Only msgids that are already format strings are in scope; literal-percent labels are
345 // skipped to avoid false positives.
346 if( src.empty() )
347 continue;
348
349 for( const std::string& msgStr : entry.m_msgStrs )
350 {
351 if( msgStr.empty() )
352 continue;
353
354 std::map<char, int> dst = consumingSpecs( msgStr );
355
356 for( const auto& [conv, count] : dst )
357 {
358 if( count > src[conv] )
359 {
360 ++violations;
361 BOOST_ERROR( poFile.GetFullName().utf8_string()
362 << ":" << entry.m_line << " translation adds extra '%" << conv
363 << "' specifier (msgid=\"" << entry.m_msgId << "\" msgstr=\""
364 << msgStr << "\")" );
365 break;
366 }
367 }
368 }
369 }
370 }
371
372 BOOST_CHECK_EQUAL( violations, 0 );
373}
374
375
@ NONE
Definition eda_fill.h:42
std::filesystem::path ToFsPath(const wxString &aPath)
Give aPath to std::filesystem without a detour through the C locale.
Definition gestfich.cpp:977
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()