KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_design_block_io.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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <cstdint>
21#include <cstdlib>
22#include <cstring>
23
24#include <wx/arrstr.h>
25#include <wx/buffer.h>
26#include <wx/translation.h>
27
29
30#include <design_block_io.h>
31
32
33namespace
34{
35
36// The Finnish/Arabic/Tamil/Thai catalogs that triggered issue 24659 translate the literal "KiCad".
37const char* const ORIG_STRING = "KiCad";
38const char* const TRANSLATED_STRING = "KiCad (ohjelma)";
39
40
46wxScopedCharBuffer MakeSingleEntryMo( const char* aOrig, const char* aTranslated )
47{
48 const uint32_t origLen = static_cast<uint32_t>( std::strlen( aOrig ) );
49 const uint32_t transLen = static_cast<uint32_t>( std::strlen( aTranslated ) );
50
51 const uint32_t headerSize = 28;
52 const uint32_t numStrings = 1;
53 const uint32_t origTableOffset = headerSize;
54 const uint32_t transTableOffset = origTableOffset + numStrings * 8;
55 const uint32_t origDataOffset = transTableOffset + numStrings * 8;
56 const uint32_t transDataOffset = origDataOffset + origLen + 1;
57 const uint32_t totalSize = transDataOffset + transLen + 1;
58
59 // wxScopedCharBuffer::CreateOwned releases with free(), so the block must come from malloc().
60 char* buf = static_cast<char*>( std::malloc( totalSize ) );
61 std::memset( buf, 0, totalSize );
62
63 auto put32 = [&]( uint32_t aOffset, uint32_t aValue )
64 {
65 std::memcpy( buf + aOffset, &aValue, sizeof( aValue ) );
66 };
67
68 put32( 0, 0x950412de ); // magic, native byte order
69 put32( 4, 0 ); // revision
70 put32( 8, numStrings );
71 put32( 12, origTableOffset );
72 put32( 16, transTableOffset );
73
74 put32( origTableOffset, origLen );
75 put32( origTableOffset + 4, origDataOffset );
76 put32( transTableOffset, transLen );
77 put32( transTableOffset + 4, transDataOffset );
78
79 std::memcpy( buf + origDataOffset, aOrig, origLen );
80 std::memcpy( buf + transDataOffset, aTranslated, transLen );
81
82 return wxScopedCharBuffer::CreateOwned( buf, totalSize );
83}
84
85
91class IN_MEMORY_LOADER : public wxTranslationsLoader
92{
93public:
94 wxMsgCatalog* LoadCatalog( const wxString& aDomain, const wxString& aLang ) override
95 {
96 return wxMsgCatalog::CreateFromData( MakeSingleEntryMo( ORIG_STRING, TRANSLATED_STRING ),
97 aDomain );
98 }
99
100 wxArrayString GetAvailableTranslations( const wxString& aDomain ) const override
101 {
102 wxArrayString langs;
103 langs.Add( wxT( "fi" ) );
104 return langs;
105 }
106};
107
108} // namespace
109
110
118{
120 {
121 wxTranslations* trans = new wxTranslations();
122 trans->SetLoader( new IN_MEMORY_LOADER() );
123 wxTranslations::Set( trans );
124 trans->AddCatalog( wxT( "kicad" ) );
125
126 m_active = trans->GetTranslatedString( ORIG_STRING ) != nullptr;
127 }
128
129 ~TRANSLATED_KICAD_FIXTURE() { wxTranslations::Set( new wxTranslations() ); }
130
131 bool m_active = false;
132};
133
134
135BOOST_FIXTURE_TEST_SUITE( DesignBlockIo, TRANSLATED_KICAD_FIXTURE )
136
137
138
145BOOST_AUTO_TEST_CASE( ShowTypeIsLocaleIndependent )
146{
147 BOOST_REQUIRE( m_active );
148 BOOST_REQUIRE_EQUAL( wxGetTranslation( wxString::FromUTF8( ORIG_STRING ) ),
149 wxString::FromUTF8( TRANSLATED_STRING ) );
150
152 wxString( wxT( "KiCad" ) ) );
153}
154
155
160BOOST_AUTO_TEST_CASE( EnumFromStrRoundTrips )
161{
163
164 BOOST_CHECK_EQUAL( static_cast<int>( DESIGN_BLOCK_IO_MGR::EnumFromStr( token ) ),
165 static_cast<int>( DESIGN_BLOCK_IO_MGR::KICAD_SEXP ) );
166
167 BOOST_CHECK_EQUAL( static_cast<int>( DESIGN_BLOCK_IO_MGR::EnumFromStr( wxT( "KiCad" ) ) ),
168 static_cast<int>( DESIGN_BLOCK_IO_MGR::KICAD_SEXP ) );
169}
170
171
176BOOST_AUTO_TEST_CASE( EnumFromStrAcceptsLegacyTranslatedToken )
177{
178 BOOST_REQUIRE( m_active );
179
180 BOOST_CHECK_EQUAL( static_cast<int>(
181 DESIGN_BLOCK_IO_MGR::EnumFromStr( wxString::FromUTF8( TRANSLATED_STRING ) ) ),
182 static_cast<int>( DESIGN_BLOCK_IO_MGR::KICAD_SEXP ) );
183}
184
185
@ KICAD_SEXP
S-expression KiCad file format.
static const wxString ShowType(DESIGN_BLOCK_FILE_T aFileType)
static DESIGN_BLOCK_FILE_T EnumFromStr(const wxString &aFileType)
Installs a wxTranslations instance that actually translates "KiCad".
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(ShowTypeIsLocaleIndependent)
The type token serialized into the design-block-lib-table must be the ASCII literal "KiCad",...
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")