KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue23687_sim_value_field.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
21#include <dialog_sim_model.h>
22#include <sim/sim_model.h>
23#include <sim/sim_value.h>
24#include <sch_field.h>
25#include <sch_pin.h>
26#include <lib_symbol.h>
27#include <reporter.h>
28
36BOOST_AUTO_TEST_SUITE( Issue23687SimValueField )
37
38
39
45static wxString buildDialogFields( LIB_SYMBOL& aSymbol, std::vector<SCH_FIELD>& aFields )
46{
47 aFields.clear();
48 aFields.emplace_back( aSymbol.GetReferenceField() );
49 aFields.emplace_back( aSymbol.GetValueField() );
50
51 wxString deviceType;
52 wxString modelType;
53 wxString modelParams;
54 wxString pinMap;
55
56 BOOST_REQUIRE( SIM_MODEL::InferSimModel( aSymbol, &aFields, false, 0,
57 SIM_VALUE_GRAMMAR::NOTATION::SI, &deviceType, &modelType,
58 &modelParams, &pinMap ) );
59
60 SetFieldValue( aFields, SIM_DEVICE_FIELD, deviceType.ToStdString() );
61
62 if( !modelType.IsEmpty() )
63 SetFieldValue( aFields, SIM_DEVICE_SUBTYPE_FIELD, modelType.ToStdString() );
64
65 SetFieldValue( aFields, SIM_PARAMS_FIELD, modelParams.ToStdString() );
66 SetFieldValue( aFields, SIM_PINS_FIELD, pinMap.ToStdString() );
67
68 SCH_FIELD* valueField = FindField( aFields, FIELD_T::VALUE );
69 wxString original = valueField->GetText();
70 valueField->SetText( wxT( "${SIM.PARAMS}" ) );
71
72 return original;
73}
74
75
76BOOST_AUTO_TEST_CASE( ValuePreservedWhenNotStored )
77{
78 // "3R3" normalizes to "3.3" in the model, so a normalization-based restore would visibly change
79 // the user's value. The original text must survive verbatim.
80 auto symbol = std::make_unique<LIB_SYMBOL>( "symbol", nullptr );
81 symbol->AddDrawItem( new SCH_PIN( symbol.get() ) );
82 symbol->AddDrawItem( new SCH_PIN( symbol.get() ) );
83 symbol->GetReferenceField().SetText( "R1" );
84 symbol->GetValueField().SetText( "3R3" );
85
86 std::vector<SCH_PIN*> pins = symbol->GetPins();
87 std::vector<SCH_FIELD> fields;
88 wxString original = buildDialogFields( *symbol, fields );
89
91 std::unique_ptr<SIM_MODEL> model = SIM_MODEL::Create( fields, false, 0, pins, reporter );
92
93 // Mirror DIALOG_SIM_MODEL::TransferDataFromWindow() with the checkbox unchecked.
94 model->SetIsStoredInValue( false );
95 model->WriteFields( fields );
97 model->IsStoredInValue() );
98
99 BOOST_CHECK_EQUAL( GetFieldValue( &fields, FIELD_T::VALUE ), wxString( "3R3" ) );
100}
101
102
103BOOST_AUTO_TEST_CASE( ValueReplacedWhenStored )
104{
105 auto symbol = std::make_unique<LIB_SYMBOL>( "symbol", nullptr );
106 symbol->AddDrawItem( new SCH_PIN( symbol.get() ) );
107 symbol->AddDrawItem( new SCH_PIN( symbol.get() ) );
108 symbol->GetReferenceField().SetText( "R1" );
109 symbol->GetValueField().SetText( "3R3" );
110
111 std::vector<SCH_PIN*> pins = symbol->GetPins();
112 std::vector<SCH_FIELD> fields;
113 wxString original = buildDialogFields( *symbol, fields );
114
116 std::unique_ptr<SIM_MODEL> model = SIM_MODEL::Create( fields, false, 0, pins, reporter );
117
118 // Mirror DIALOG_SIM_MODEL::TransferDataFromWindow() with the checkbox checked. WriteFields()
119 // replaces the placeholder with the model value and the restore is a no-op.
120 model->SetIsStoredInValue( true );
121 model->WriteFields( fields );
123 model->IsStoredInValue() );
124
125 wxString value = GetFieldValue( &fields, FIELD_T::VALUE );
126 BOOST_CHECK_MESSAGE( value != wxT( "${SIM.PARAMS}" ),
127 "Value left as placeholder when parameters stored in Value" );
128 BOOST_CHECK( !value.IsEmpty() );
129}
130
131
static void RestoreInferredValue(std::vector< SCH_FIELD > &aFields, const wxString &aOriginalValue, bool aOverwritten, bool aStoredInValue)
Restore an inferred passive's Value field after WriteFields().
Define a library symbol object.
Definition lib_symbol.h:79
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:128
void SetText(const wxString &aText) override
static bool InferSimModel(T &aSymbol, std::vector< SCH_FIELD > *aFields, bool aResolve, int aDepth, SIM_VALUE_GRAMMAR::NOTATION aNotation, wxString *aDeviceType, wxString *aModelType, wxString *aModelParams, wxString *aPinMap)
static std::unique_ptr< SIM_MODEL > Create(TYPE aType, const std::vector< SCH_PIN * > &aPins, REPORTER &aReporter)
A wrapper for reporting to a wxString object.
Definition reporter.h:189
STL namespace.
void SetFieldValue(std::vector< SCH_FIELD > &aFields, const wxString &aFieldName, const std::string &aValue, bool aIsVisible=true, const SCH_SHEET_PATH *aSheetPath=nullptr, const wxString &aVariantName=wxEmptyString)
Definition sch_field.h:446
wxString GetFieldValue(const std::vector< SCH_FIELD > *aFields, FIELD_T aFieldType)
Definition sch_field.h:421
const SCH_FIELD * FindField(const std::vector< SCH_FIELD > &aFields, FIELD_T aFieldId)
Definition sch_field.h:383
#define SIM_PINS_FIELD
Definition sim_model.h:50
#define SIM_DEVICE_FIELD
Definition sim_model.h:48
#define SIM_PARAMS_FIELD
Definition sim_model.h:52
#define SIM_DEVICE_SUBTYPE_FIELD
Definition sim_model.h:49
@ VALUE
Field Value of part, i.e. "3.3K".
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(ValuePreservedWhenNotStored)
static wxString buildDialogFields(LIB_SYMBOL &aSymbol, std::vector< SCH_FIELD > &aFields)
Regression coverage for https://gitlab.com/kicad/code/kicad/-/issues/23687.
IbisParser parser & reporter
KIBIS_MODEL * model
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_CHECK_EQUAL(result, "25.4")