2023-09-24 18:58:19 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2024-01-01 09:57:32 +00:00
|
|
|
* Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
|
2023-09-24 18:58:19 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <fstream>
|
|
|
|
#include <fmt/format.h>
|
2023-11-29 19:25:24 +00:00
|
|
|
#include <fmt/std.h>
|
2023-09-24 18:58:19 +00:00
|
|
|
|
|
|
|
#include <qa_utils/wx_utils/unit_test_utils.h>
|
|
|
|
#include <pcbnew_utils/board_test_utils.h>
|
|
|
|
#include <pcbnew_utils/board_file_utils.h>
|
2023-12-24 01:21:58 +00:00
|
|
|
#include <pcb_io/kicad_sexpr/pcb_io_kicad_sexpr.h>
|
2024-01-01 18:38:16 +00:00
|
|
|
#include <pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.h>
|
|
|
|
#include <io/kicad/kicad_io_utils.h>
|
2023-09-24 18:58:19 +00:00
|
|
|
#include <board.h>
|
|
|
|
#include <footprint.h>
|
|
|
|
#include <settings/settings_manager.h>
|
|
|
|
|
|
|
|
|
|
|
|
struct PRETTIFIER_TEST_FIXTURE
|
|
|
|
{
|
|
|
|
PRETTIFIER_TEST_FIXTURE() :
|
|
|
|
m_settingsManager( true /* headless */ )
|
|
|
|
{ }
|
|
|
|
|
|
|
|
SETTINGS_MANAGER m_settingsManager;
|
|
|
|
};
|
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE( BoardAndFootprintPrettifier, PRETTIFIER_TEST_FIXTURE )
|
2023-09-24 18:58:19 +00:00
|
|
|
{
|
2024-01-01 18:38:16 +00:00
|
|
|
std::vector<wxString> cases = {
|
|
|
|
"Reverb_BTDR-1V.kicad_mod",
|
|
|
|
"Samtec_HLE-133-02-xx-DV-PE-LC_2x33_P2.54mm_Horizontal.kicad_mod",
|
|
|
|
"group_and_image.kicad_pcb"
|
2023-09-24 18:58:19 +00:00
|
|
|
};
|
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
std::unique_ptr<BOARD_ITEM_CONTAINER> original, prettified, golden;
|
2023-12-24 01:21:58 +00:00
|
|
|
PCB_IO_KICAD_SEXPR plugin;
|
2023-09-24 18:58:19 +00:00
|
|
|
|
|
|
|
std::string tempLibPath = fmt::format( "{}/prettifier.pretty",
|
2023-11-29 19:25:24 +00:00
|
|
|
std::filesystem::temp_directory_path() );
|
2023-09-24 18:58:19 +00:00
|
|
|
std::filesystem::remove_all( tempLibPath );
|
|
|
|
std::filesystem::create_directory( tempLibPath );
|
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
for( const wxString& testCase : cases )
|
2023-09-24 18:58:19 +00:00
|
|
|
{
|
2024-01-01 18:38:16 +00:00
|
|
|
std::string testCaseName = testCase.ToStdString();
|
|
|
|
|
|
|
|
BOOST_TEST_CONTEXT( testCaseName )
|
2023-09-24 18:58:19 +00:00
|
|
|
{
|
2024-01-01 18:38:16 +00:00
|
|
|
std::string inPath = fmt::format( "{}prettifier/{}", KI_TEST::GetPcbnewTestDataDir(),
|
|
|
|
testCaseName );
|
|
|
|
|
|
|
|
std::ifstream inFp;
|
|
|
|
inFp.open( inPath );
|
|
|
|
BOOST_REQUIRE( inFp.is_open() );
|
|
|
|
|
|
|
|
std::stringstream inBuf;
|
|
|
|
inBuf << inFp.rdbuf();
|
|
|
|
std::string inData = inBuf.str();
|
|
|
|
|
|
|
|
{
|
|
|
|
STRING_LINE_READER reader( inData, "input file" );
|
|
|
|
PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
|
2023-09-24 18:58:19 +00:00
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
BOOST_CHECK_NO_THROW(
|
|
|
|
original.reset( dynamic_cast<BOARD_ITEM_CONTAINER*>( parser.Parse() ) ) );
|
|
|
|
BOOST_REQUIRE( original.get() );
|
|
|
|
}
|
2023-09-24 18:58:19 +00:00
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
KICAD_FORMAT::Prettify( inData );
|
2023-09-24 18:58:19 +00:00
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
// For diagnosis of test failures
|
|
|
|
std::string tempPath = fmt::format( "{}/{}", tempLibPath, testCaseName );
|
|
|
|
std::ofstream tempFp;
|
|
|
|
tempFp.open( tempPath );
|
|
|
|
BOOST_REQUIRE( tempFp.is_open() );
|
|
|
|
tempFp << inData;
|
|
|
|
tempFp.close();
|
2023-09-24 18:58:19 +00:00
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
{
|
|
|
|
STRING_LINE_READER reader( inData, "prettified file" );
|
|
|
|
PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
|
|
|
|
|
|
|
|
BOOST_CHECK_NO_THROW(
|
|
|
|
prettified.reset( dynamic_cast<BOARD_ITEM_CONTAINER*>( parser.Parse() ) ) );
|
|
|
|
BOOST_REQUIRE( prettified.get() );
|
|
|
|
}
|
2023-09-24 18:58:19 +00:00
|
|
|
|
|
|
|
// Hack around the fact that PAD::operator== compares footprint UUIDs, even though
|
|
|
|
// these UUIDs cannot be preserved through a round-trip
|
2024-01-01 18:38:16 +00:00
|
|
|
const_cast<KIID&>( prettified->m_Uuid ) = original->m_Uuid;
|
2023-09-24 18:58:19 +00:00
|
|
|
|
|
|
|
// File should parse the same way
|
2024-01-01 18:38:16 +00:00
|
|
|
BOOST_REQUIRE_MESSAGE( *original == *prettified,
|
|
|
|
"Formatted version of original board item does not parse the same way!" );
|
2023-09-24 18:58:19 +00:00
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
// And the formatting should match the golden
|
|
|
|
std::string base = testCase.BeforeLast( '.' ).ToStdString();
|
|
|
|
std::string ext = testCase.AfterLast( '.' ).ToStdString();
|
2023-12-16 12:30:06 +00:00
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
std::string goldenPath = fmt::format( "{}prettifier/{}_formatted.{}",
|
|
|
|
KI_TEST::GetPcbnewTestDataDir(), base, ext );
|
2024-01-01 09:57:32 +00:00
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
std::ifstream goldFp;
|
|
|
|
goldFp.open( goldenPath );
|
|
|
|
BOOST_REQUIRE( goldFp.is_open() );
|
2023-12-16 12:30:06 +00:00
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
std::stringstream goldenBuf;
|
|
|
|
goldenBuf << goldFp.rdbuf();
|
2023-09-24 18:58:19 +00:00
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
BOOST_REQUIRE_MESSAGE( goldenBuf.str().compare( inData ) == 0,
|
|
|
|
"Formatting result doesn't match golden!" );
|
2023-09-24 18:58:19 +00:00
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
std::filesystem::remove( tempPath );
|
2023-09-24 18:58:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-01 18:38:16 +00:00
|
|
|
std::filesystem::remove_all( tempLibPath );
|
|
|
|
}
|