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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <filesystem>
21#include <fstream>
22
24#include <qa_utils/file_utils.h>
30#include <board.h>
31#include <footprint.h>
33
34
42
43
44BOOST_FIXTURE_TEST_CASE( BoardAndFootprintPrettifier, PRETTIFIER_TEST_FIXTURE )
45{
46 std::vector<wxString> cases = {
47 "Reverb_BTDR-1V.kicad_mod",
48 "Samtec_HLE-133-02-xx-DV-PE-LC_2x33_P2.54mm_Horizontal.kicad_mod",
49 "group_and_image.kicad_pcb"
50 };
51
52 std::unique_ptr<BOARD_ITEM_CONTAINER> original, prettified, golden;
53 PCB_IO_KICAD_SEXPR plugin;
54
55 KI_TEST::SCOPED_TEMP_DIR tempLib( wxS( "prettifier" ) );
56
57 for( const wxString& testCase : cases )
58 {
59 std::string testCaseName = testCase.ToStdString();
60
61 BOOST_TEST_CONTEXT( testCaseName )
62 {
63 const std::filesystem::path inPath =
64 std::filesystem::path( KI_TEST::GetPcbnewTestDataDir() ) / "prettifier" / testCaseName;
65
66 std::ifstream inFp( inPath );
67 BOOST_REQUIRE( inFp.is_open() );
68
69 std::stringstream inBuf;
70 inBuf << inFp.rdbuf();
71 std::string inData = inBuf.str();
72
73 {
74 STRING_LINE_READER reader( inData, "input file" );
75 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
76
77 BOOST_CHECK_NO_THROW(
78 original.reset( dynamic_cast<BOARD_ITEM_CONTAINER*>( parser.Parse() ) ) );
79 BOOST_REQUIRE( original.get() );
80 }
81
82 KICAD_FORMAT::Prettify( inData );
83
84 // For diagnosis of test failures
85 std::filesystem::path tempPath = tempLib.CreateChildFile( testCase );
86 std::ofstream tempFp( tempPath );
87
88 BOOST_REQUIRE( tempFp.is_open() );
89 tempFp << inData;
90 tempFp.close();
91
92 {
93 STRING_LINE_READER reader( inData, "prettified file" );
94 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
95
96 BOOST_CHECK_NO_THROW(
97 prettified.reset( dynamic_cast<BOARD_ITEM_CONTAINER*>( parser.Parse() ) ) );
98 BOOST_REQUIRE( prettified.get() );
99 }
100
101 // Hack around the fact that PAD::operator== compares footprint UUIDs, even though
102 // these UUIDs cannot be preserved through a round-trip
103 const_cast<KIID&>( prettified->m_Uuid ) = original->m_Uuid;
104
105 // File should parse the same way
106 BOOST_REQUIRE_MESSAGE( *original == *prettified,
107 "Formatted version of original board item does not parse the same way!" );
108
109 // And the formatting should match the golden
110 std::string base = testCase.BeforeLast( '.' ).ToStdString();
111 std::string ext = testCase.AfterLast( '.' ).ToStdString();
112
113 const std::filesystem::path goldenPath = std::filesystem::path( KI_TEST::GetPcbnewTestDataDir() )
114 / "prettifier" / ( base + "_formatted." + ext );
115
116 std::ifstream goldFp( goldenPath );
117 BOOST_REQUIRE( goldFp.is_open() );
118
119 std::stringstream goldenBuf;
120 goldenBuf << goldFp.rdbuf();
121
122 BOOST_REQUIRE_MESSAGE( goldenBuf.str().compare( inData ) == 0,
123 "Formatting result doesn't match golden!" );
124 }
125 }
126}
General utilities for PCB file IO for QA programs.
Abstract interface for BOARD_ITEMs capable of storing other items inside.
Definition kiid.h:46
std::filesystem::path CreateChildFile(const wxString &aName) const
Create and return the path to a direct child file.
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
A PCB_IO 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:225
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")