KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_prettifier.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 (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <algorithm>
25#include <filesystem>
26#include <fstream>
27#include <fmt/format.h>
28#include <fmt/std.h>
29
36#include <board.h>
37#include <footprint.h>
39
40
42{
44 m_settingsManager( true /* headless */ )
45 { }
46
48};
49
50
51BOOST_FIXTURE_TEST_CASE( BoardAndFootprintPrettifier, PRETTIFIER_TEST_FIXTURE )
52{
53 std::vector<wxString> cases = {
54 "Reverb_BTDR-1V.kicad_mod",
55 "Samtec_HLE-133-02-xx-DV-PE-LC_2x33_P2.54mm_Horizontal.kicad_mod",
56 "group_and_image.kicad_pcb"
57 };
58
59 std::unique_ptr<BOARD_ITEM_CONTAINER> original, prettified, golden;
60 PCB_IO_KICAD_SEXPR plugin;
61
62 std::string tempLibPath = fmt::format( "{}/prettifier.pretty",
63 std::filesystem::temp_directory_path() );
64 std::filesystem::remove_all( tempLibPath );
65 std::filesystem::create_directory( tempLibPath );
66
67 for( const wxString& testCase : cases )
68 {
69 std::string testCaseName = testCase.ToStdString();
70
71 BOOST_TEST_CONTEXT( testCaseName )
72 {
73 std::string inPath = fmt::format( "{}prettifier/{}", KI_TEST::GetPcbnewTestDataDir(),
74 testCaseName );
75
76 std::ifstream inFp;
77 inFp.open( inPath );
78 BOOST_REQUIRE( inFp.is_open() );
79
80 std::stringstream inBuf;
81 inBuf << inFp.rdbuf();
82 std::string inData = inBuf.str();
83
84 {
85 STRING_LINE_READER reader( inData, "input file" );
86 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
87
88 BOOST_CHECK_NO_THROW(
89 original.reset( dynamic_cast<BOARD_ITEM_CONTAINER*>( parser.Parse() ) ) );
90 BOOST_REQUIRE( original.get() );
91 }
92
93 KICAD_FORMAT::Prettify( inData );
94
95 // For diagnosis of test failures
96 std::string tempPath = fmt::format( "{}/{}", tempLibPath, testCaseName );
97 std::ofstream tempFp;
98 tempFp.open( tempPath );
99 BOOST_REQUIRE( tempFp.is_open() );
100 tempFp << inData;
101 tempFp.close();
102
103 {
104 STRING_LINE_READER reader( inData, "prettified file" );
105 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
106
107 BOOST_CHECK_NO_THROW(
108 prettified.reset( dynamic_cast<BOARD_ITEM_CONTAINER*>( parser.Parse() ) ) );
109 BOOST_REQUIRE( prettified.get() );
110 }
111
112 // Hack around the fact that PAD::operator== compares footprint UUIDs, even though
113 // these UUIDs cannot be preserved through a round-trip
114 const_cast<KIID&>( prettified->m_Uuid ) = original->m_Uuid;
115
116 // File should parse the same way
117 BOOST_REQUIRE_MESSAGE( *original == *prettified,
118 "Formatted version of original board item does not parse the same way!" );
119
120 // And the formatting should match the golden
121 std::string base = testCase.BeforeLast( '.' ).ToStdString();
122 std::string ext = testCase.AfterLast( '.' ).ToStdString();
123
124 std::string goldenPath = fmt::format( "{}prettifier/{}_formatted.{}",
125 KI_TEST::GetPcbnewTestDataDir(), base, ext );
126
127 std::ifstream goldFp;
128 goldFp.open( goldenPath );
129 BOOST_REQUIRE( goldFp.is_open() );
130
131 std::stringstream goldenBuf;
132 goldenBuf << goldFp.rdbuf();
133
134 BOOST_REQUIRE_MESSAGE( goldenBuf.str().compare( inData ) == 0,
135 "Formatting result doesn't match golden!" );
136
137 std::filesystem::remove( tempPath );
138 }
139 }
140
141 std::filesystem::remove_all( tempLibPath );
142}
General utilities for PCB file IO for QA programs.
Abstract interface for BOARD_ITEMs capable of storing other items inside.
Definition: kiid.h:49
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:253
void Prettify(std::string &aSource, char aQuoteChar)
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
Pcbnew s-expression file format parser definition.
SETTINGS_MANAGER m_settingsManager
BOOST_FIXTURE_TEST_CASE(BoardAndFootprintPrettifier, PRETTIFIER_TEST_FIXTURE)
#define BOOST_TEST_CONTEXT(A)