KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_drc_text_var_issue24442.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
26#include <board.h>
28#include <drc/drc_engine.h>
29#include <drc/drc_item.h>
31
32
40
41
42// Verifies that ${DRC_ERROR ...} and ${DRC_WARNING ...} tokens trigger a DRC
43// violation regardless of position in the text. Prior to the fix the regex
44// was anchored with `^...$` so only text starting with the token would fire,
45// breaking placeholder patterns like "Fill in: ${DRC_ERROR ...}".
47{
48 KI_TEST::LoadBoard( m_settingsManager, "issue24442", m_board );
49
50 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
51
52 // Disable DRC tests not useful or not handled in this testcase
53 for( int ii = DRCE_FIRST; ii <= DRCE_LAST; ++ii )
55
58
59 // testTextVars() (the phase that handles ${DRC_ERROR}/${DRC_WARNING}) only
60 // runs when DRCE_UNRESOLVED_VARIABLE is enabled; keep it active so the
61 // phase is reached.
63
64 int genericErrors = 0;
65 int genericWarnings = 0;
66 int unresolvedVars = 0;
67 std::vector<wxString> errorMessages;
68 std::vector<wxString> warningMessages;
69
71 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
72 const std::function<void( PCB_MARKER* )>& aPathGenerator )
73 {
74 if( aItem->GetErrorCode() == DRCE_GENERIC_ERROR )
75 {
76 genericErrors++;
77 errorMessages.push_back( aItem->GetErrorMessage( false ) );
78 }
79 else if( aItem->GetErrorCode() == DRCE_GENERIC_WARNING )
80 {
81 genericWarnings++;
82 warningMessages.push_back( aItem->GetErrorMessage( false ) );
83 }
84 else if( aItem->GetErrorCode() == DRCE_UNRESOLVED_VARIABLE )
85 {
86 unresolvedVars++;
87 }
88 } );
89
90 bds.m_DRCEngine->RunTests( EDA_UNITS::MM, true, false );
91
92 // The fixture board contains three texts/textboxes referencing ${DRC_ERROR ...}:
93 // - bare "${DRC_ERROR start_of_text}"
94 // - "Fill in: ${DRC_ERROR placeholder_text}"
95 // - "Note: ${DRC_ERROR text_box_error}" (textbox)
96 // And two texts referencing ${DRC_WARNING ...}:
97 // - "${DRC_WARNING this_is_warning} trailing"
98 // - "prefix ${DRC_WARNING embedded_warning} suffix"
99 // Plus an escaped "literal: \${DRC_ERROR not_a_real_error}" which must NOT
100 // trigger DRCE_GENERIC_ERROR -- the escape is reserved for literal display.
101 BOOST_CHECK_EQUAL( genericErrors, 3 );
102 BOOST_CHECK_EQUAL( genericWarnings, 2 );
103
104 // A matched marker is the intended diagnostic, so those five texts must not
105 // also be flagged as unresolved variables. Only the escaped literal, whose
106 // displayed text still carries a bare ${...}, remains an unresolved variable.
107 BOOST_CHECK_EQUAL( unresolvedVars, 1 );
108
109 auto containsMsg =
110 []( const std::vector<wxString>& aList, const wxString& aNeedle )
111 {
112 for( const wxString& msg : aList )
113 {
114 if( msg.Contains( aNeedle ) )
115 return true;
116 }
117
118 return false;
119 };
120
121 BOOST_CHECK( containsMsg( errorMessages, "start_of_text" ) );
122 BOOST_CHECK( containsMsg( errorMessages, "placeholder_text" ) );
123 BOOST_CHECK( containsMsg( errorMessages, "text_box_error" ) );
124 BOOST_CHECK( containsMsg( warningMessages, "this_is_warning" ) );
125 BOOST_CHECK( containsMsg( warningMessages, "embedded_warning" ) );
126
127 // The escaped literal must never surface as a generic error.
128 BOOST_CHECK( !containsMsg( errorMessages, "not_a_real_error" ) );
129}
Container for design settings for a BOARD object.
std::map< int, SEVERITY > m_DRCSeverities
std::shared_ptr< DRC_ENGINE > m_DRCEngine
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints, BOARD_COMMIT *aCommit=nullptr)
Run the DRC tests.
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition drc_engine.h:164
@ DRCE_GENERIC_ERROR
Definition drc_item.h:89
@ DRCE_FIRST
Definition drc_item.h:36
@ DRCE_UNRESOLVED_VARIABLE
Definition drc_item.h:86
@ DRCE_LAST
Definition drc_item.h:122
@ DRCE_GENERIC_WARNING
Definition drc_item.h:88
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_IGNORE
BOOST_FIXTURE_TEST_CASE(DRCTextVarIssue24442, DRC_TEXT_VAR_TEST_FIXTURE)
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683