KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_fields_table_presence.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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 <boost/test/unit_test.hpp>
21
22#include <board.h>
23#include <board_commit.h>
24#include <footprint.h>
26#include <pcb_field.h>
27#include <template_fieldnames.h>
28#include <tool/tool_manager.h>
29
30
32{
34 m_footprint( new FOOTPRINT( &m_board ) ),
36 {
37 m_footprint->SetReference( wxS( "U1" ) );
38 m_board.Add( m_footprint );
39
40 m_model.AddColumn( GetDefaultFieldName( FIELD_T::REFERENCE, UNTRANSLATED ), wxS( "Reference" ), false );
41
42 int referenceCol = m_model.GetFieldNameCol( GetDefaultFieldName( FIELD_T::REFERENCE, UNTRANSLATED ) );
43 BOOST_REQUIRE( referenceCol >= 0 );
44 m_model.SetShowColumn( referenceCol, true );
45 }
46
47 void AddTestColumn( const wxString& aFieldName, bool aAddedByUser = false )
48 {
49 m_model.AddColumn( aFieldName, aFieldName, aAddedByUser );
50 m_col = m_model.GetFieldNameCol( aFieldName );
51 BOOST_REQUIRE( m_col >= 0 );
52 m_model.SetShowColumn( m_col, true );
53 m_model.RebuildRows();
54
55 BOOST_REQUIRE_EQUAL( m_model.GetNumberRows(), 1 );
56 }
57
58 void Apply( const wxString& aVariant = wxEmptyString )
59 {
60 TOOL_MANAGER toolMgr;
61 toolMgr.SetEnvironment( &m_board, nullptr, nullptr, nullptr, nullptr );
62
63 BOARD_COMMIT commit( &toolMgr, true, false );
64 TEMPLATES templates;
65
66 m_model.ApplyData( commit, templates, aVariant );
67 }
68
72 int m_col = -1;
73};
74
75
76BOOST_FIXTURE_TEST_SUITE( FootprintFieldsTablePresence, FOOTPRINT_FIELDS_TABLE_PRESENCE_FIXTURE )
77
78
79BOOST_AUTO_TEST_CASE( UntouchedMissingPresetFieldRemainsAbsent )
80{
81 const wxString fieldName = wxS( "UninstantiatedPresetField" );
82
83 AddTestColumn( fieldName );
84
85 BOOST_REQUIRE( m_model.IsCellClear( 0, m_col ) );
86
87 Apply();
88
89 BOOST_CHECK( m_footprint->GetField( fieldName ) == nullptr );
90}
91
92
93BOOST_AUTO_TEST_CASE( ExplicitEmptyValueCreatesField )
94{
95 const wxString fieldName = wxS( "ExplicitlyEmptyField" );
96
97 AddTestColumn( fieldName );
98 BOOST_REQUIRE( m_model.IsCellClear( 0, m_col ) );
99
100 m_model.SetValue( 0, m_col, wxEmptyString );
101
102 BOOST_REQUIRE( !m_model.IsCellClear( 0, m_col ) );
103 BOOST_REQUIRE( m_model.IsCellEdited( 0, m_col ) );
104
105 Apply();
106
107 const PCB_FIELD* field = m_footprint->GetField( fieldName );
108 BOOST_REQUIRE( field );
109 BOOST_CHECK( field->GetText().IsEmpty() );
110}
111
112
113BOOST_AUTO_TEST_CASE( ExistingEmptyFieldCanBeCleared )
114{
115 const wxString fieldName = wxS( "ExistingEmptyField" );
116
117 m_footprint->Add( new PCB_FIELD( m_footprint, FIELD_T::USER, fieldName ) );
118 AddTestColumn( fieldName );
119
120 BOOST_REQUIRE( !m_model.IsCellClear( 0, m_col ) );
121 BOOST_REQUIRE( !m_model.IsCellEdited( 0, m_col ) );
122
123 m_model.ClearCell( 0, m_col );
124
125 BOOST_REQUIRE( m_model.IsCellClear( 0, m_col ) );
126 BOOST_REQUIRE( m_model.IsCellEdited( 0, m_col ) );
127
128 Apply();
129
130 BOOST_CHECK( m_footprint->GetField( fieldName ) == nullptr );
131}
132
133
134BOOST_AUTO_TEST_CASE( UserAddedColumnCreatesEmptyField )
135{
136 const wxString fieldName = wxS( "UserAddedEmptyField" );
137
138 AddTestColumn( fieldName, true );
139
140 BOOST_REQUIRE( !m_model.IsCellClear( 0, m_col ) );
141 BOOST_REQUIRE( m_model.IsCellEdited( 0, m_col ) );
142
143 Apply();
144
145 const PCB_FIELD* field = m_footprint->GetField( fieldName );
146 BOOST_REQUIRE( field );
147 BOOST_CHECK( field->GetText().IsEmpty() );
148}
149
150
151BOOST_AUTO_TEST_CASE( ExcludeFromSimulationAttributeIsEditable )
152{
153 const wxString fieldName = wxS( "${EXCLUDE_FROM_SIM}" );
154
155 AddTestColumn( fieldName );
156
157 BOOST_CHECK( !m_model.ColIsReadOnly( m_col ) );
158 BOOST_CHECK_EQUAL( m_model.GetValue( 0, m_col ), wxString( wxS( "0" ) ) );
159
160 m_model.SetValue( 0, m_col, wxS( "1" ) );
161 Apply();
162
163 BOOST_CHECK( m_footprint->IsExcludedFromSim() );
164}
165
166
167BOOST_AUTO_TEST_CASE( ExcludeFromSimulationAttributeIsVariantAware )
168{
169 const wxString fieldName = wxS( "${EXCLUDE_FROM_SIM}" );
170 const wxString variantName = wxS( "Production" );
171
172 m_board.AddVariant( variantName );
173 m_model.SetCurrentVariant( variantName );
174 AddTestColumn( fieldName );
175
176 m_model.SetValue( 0, m_col, wxS( "1" ) );
177 Apply( variantName );
178
179 BOOST_CHECK( !m_footprint->IsExcludedFromSim() );
180 BOOST_CHECK( m_footprint->GetExcludedFromSimForVariant( variantName ) );
181}
182
183
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:118
This is the minimal equivalent to the SCH_REFERENCE so we can provide similar non-null guarantees thr...
Master controller class:
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Set the work environment (model, view, view controls and the parent window).
std::vector< FOOTPRINT_REF > FOOTPRINT_REFERENCE_LIST
void Apply(const wxString &aVariant=wxEmptyString)
FOOTPRINT_FIELDS_EDITOR_GRID_DATA_MODEL m_model
void AddTestColumn(const wxString &aFieldName, bool aAddedByUser=false)
wxString GetDefaultFieldName(FIELD_T aFieldId, TRANSLATION aTranslation)
Return a default symbol field name for a mandatory field type.
@ USER
The field ID hasn't been set yet; field is invalid.
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ UNTRANSLATED
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_CASE(UntouchedMissingPresetFieldRemainsAbsent)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")