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