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 The 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
48
49
50BOOST_FIXTURE_TEST_CASE( BoardAndFootprintPrettifier, PRETTIFIER_TEST_FIXTURE )
51{
52 std::vector<wxString> cases = {
53 "Reverb_BTDR-1V.kicad_mod",
54 "Samtec_HLE-133-02-xx-DV-PE-LC_2x33_P2.54mm_Horizontal.kicad_mod",
55 "group_and_image.kicad_pcb"
56 };
57
58 std::unique_ptr<BOARD_ITEM_CONTAINER> original, prettified, golden;
59 PCB_IO_KICAD_SEXPR plugin;
60
61 std::string tempLibPath = fmt::format( "{}/prettifier.pretty",
62 std::filesystem::temp_directory_path() );
63 std::filesystem::remove_all( tempLibPath );
64 std::filesystem::create_directory( tempLibPath );
65
66 for( const wxString& testCase : cases )
67 {
68 std::string testCaseName = testCase.ToStdString();
69
70 BOOST_TEST_CONTEXT( testCaseName )
71 {
72 std::string inPath = fmt::format( "{}prettifier/{}", KI_TEST::GetPcbnewTestDataDir(),
73 testCaseName );
74
75 std::ifstream inFp;
76 inFp.open( inPath );
77 BOOST_REQUIRE( inFp.is_open() );
78
79 std::stringstream inBuf;
80 inBuf << inFp.rdbuf();
81 std::string inData = inBuf.str();
82
83 {
84 STRING_LINE_READER reader( inData, "input file" );
85 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
86
87 BOOST_CHECK_NO_THROW(
88 original.reset( dynamic_cast<BOARD_ITEM_CONTAINER*>( parser.Parse() ) ) );
89 BOOST_REQUIRE( original.get() );
90 }
91
92 KICAD_FORMAT::Prettify( inData );
93
94 // For diagnosis of test failures
95 std::string tempPath = fmt::format( "{}/{}", tempLibPath, testCaseName );
96 std::ofstream tempFp;
97 tempFp.open( tempPath );
98 BOOST_REQUIRE( tempFp.is_open() );
99 tempFp << inData;
100 tempFp.close();
101
102 {
103 STRING_LINE_READER reader( inData, "prettified file" );
104 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
105
106 BOOST_CHECK_NO_THROW(
107 prettified.reset( dynamic_cast<BOARD_ITEM_CONTAINER*>( parser.Parse() ) ) );
108 BOOST_REQUIRE( prettified.get() );
109 }
110
111 // Hack around the fact that PAD::operator== compares footprint UUIDs, even though
112 // these UUIDs cannot be preserved through a round-trip
113 const_cast<KIID&>( prettified->m_Uuid ) = original->m_Uuid;
114
115 // File should parse the same way
116 BOOST_REQUIRE_MESSAGE( *original == *prettified,
117 "Formatted version of original board item does not parse the same way!" );
118
119 // And the formatting should match the golden
120 std::string base = testCase.BeforeLast( '.' ).ToStdString();
121 std::string ext = testCase.AfterLast( '.' ).ToStdString();
122
123 std::string goldenPath = fmt::format( "{}prettifier/{}_formatted.{}",
124 KI_TEST::GetPcbnewTestDataDir(), base, ext );
125
126 std::ifstream goldFp;
127 goldFp.open( goldenPath );
128 BOOST_REQUIRE( goldFp.is_open() );
129
130 std::stringstream goldenBuf;
131 goldenBuf << goldFp.rdbuf();
132
133 BOOST_REQUIRE_MESSAGE( goldenBuf.str().compare( inData ) == 0,
134 "Formatting result doesn't match golden!" );
135
136 std::filesystem::remove( tempPath );
137 }
138 }
139
140 std::filesystem::remove_all( tempLibPath );
141}
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:254
void Prettify(std::string &aSource, FORMAT_MODE aMode)
Pretty-prints s-expression text according to KiCad format rules.
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_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_FIXTURE_TEST_CASE(BoardAndFootprintPrettifier, PRETTIFIER_TEST_FIXTURE)
BOOST_TEST_CONTEXT("Test Clearance")