20#include <boost/test/unit_test.hpp>
25#include <api/schematic/schematic_types.pb.h>
46#include <wx/filename.h>
57 for(
SCH_ITEM* item : m_schematic->RootScreen()->Items() )
59 switch( item->Type() )
66 return std::make_unique<SCH_JUNCTION>();
75 return std::make_unique<SCH_NO_CONNECT>();
84 return std::make_unique<SCH_BUS_WIRE_ENTRY>();
93 return std::make_unique<SCH_BUS_BUS_ENTRY>();
102 return std::make_unique<SCH_LINE>();
111 return std::make_unique<SCH_SHAPE>();
120 return std::make_unique<SCH_RULE_AREA>();
129 return std::make_unique<SCH_BITMAP>();
138 return std::make_unique<SCH_TEXT>();
147 return std::make_unique<SCH_TEXTBOX>();
156 return std::make_unique<SCH_LABEL>();
165 return std::make_unique<SCH_GLOBALLABEL>();
174 return std::make_unique<SCH_HIERLABEL>();
183 return std::make_unique<SCH_DIRECTIVE_LABEL>();
192 return std::make_unique<SCH_GROUP>( m_schematic->RootScreen() );
201 return std::make_unique<SCH_SHEET>( m_schematic->RootScreen() );
210 return std::make_unique<SCH_TABLE>();
221 kiapi::common::commands::ItemStatus status;
222 kiapi::schematic::types::SchematicSymbolInstance symbolProto;
224 BOOST_REQUIRE_MESSAGE(
result,
"Serialization failed" );
226 std::unique_ptr<SCH_SYMBOL> output = std::make_unique<SCH_SYMBOL>();
230 output->SetParent( m_schematic->RootScreen() );
233 BOOST_REQUIRE_MESSAGE(
result,
"Deserialization failed" );
236 m_schematic.get() ) );
238 kiapi::schematic::types::SchematicSymbolInstance outputProto;
240 BOOST_REQUIRE_MESSAGE(
result,
"Second serialization failed" );
242 if( !google::protobuf::util::MessageDifferencer::Equals( symbolProto, outputProto ) )
246 BOOST_TEST_FAIL(
"Round-tripped protobuf does not match" );
249 if( !output->operator==( *symbol ) )
250 BOOST_TEST_FAIL(
"Round-tripped object does not match" );
279 const wxString variantName( wxS(
"api-variant" ) );
280 const bool baseDNP = symbol->
GetDNP();
282 kiapi::schematic::types::SchematicSymbolInstance proto;
285 kiapi::common::commands::ItemStatus status;
286 kiapi::schematic::types::SchematicSymbolVariant* variant = proto.mutable_variants()->add_variants();
287 variant->set_name( variantName.ToUTF8() );
288 variant->mutable_attributes()->set_do_not_populate( !baseDNP );
294 BOOST_CHECK( m_schematic->GetVariantNames().count( variantName ) );
297 kiapi::schematic::types::SchematicSymbolInstance untouched;
298 untouched.mutable_reference_field()->mutable_text()->set_text( symbol->
GetRef( &
path ).ToUTF8() );
306 kiapi::schematic::types::SchematicSymbolInstance cleared;
308 BOOST_REQUIRE_EQUAL( cleared.variants().variants_size(), 1 );
309 cleared.mutable_variants()->clear_variants();
314 BOOST_CHECK( m_schematic->GetVariantNames().count( variantName ) );
334 const wxString variantName( wxS(
"api-variant" ) );
337 const wxString overrideValue( wxS(
"Override" ) );
339 kiapi::common::commands::ItemStatus status;
340 kiapi::schematic::types::SchematicSymbolInstance proto;
343 kiapi::schematic::types::SchematicSymbolVariant* variant = proto.mutable_variants()->add_variants();
344 variant->set_name( variantName.ToUTF8() );
345 variant->mutable_attributes()->set_do_not_populate(
true );
346 ( *variant->mutable_fields() )[std::string( fieldName.ToUTF8() )] = overrideValue.ToUTF8();
351 BOOST_CHECK( symbol->
GetDNP( &
path, variantName ) );
353 kiapi::schematic::types::SchematicSymbolInstance withoutField;
355 BOOST_REQUIRE_EQUAL( withoutField.variants().variants_size(), 1 );
357 withoutField.mutable_variants()->mutable_variants( 0 )->clear_fields();
362 BOOST_CHECK( symbol->
GetDNP( &
path, variantName ) );
364 kiapi::schematic::types::SchematicSymbolInstance cleared;
366 cleared.mutable_variants()->clear_variants();
370 BOOST_CHECK( !symbol->
GetDNP( &
path, variantName ) );
371 BOOST_CHECK( m_schematic->GetVariantNames().count( variantName ) );
378 fn.AppendDir( wxS(
"variants" ) );
379 fn.SetName( wxS(
"variants" ) );
380 fn.SetExt( wxS(
"kicad_sch" ) );
395 kiapi::schematic::types::SheetSymbol proto;
399 BOOST_REQUIRE_EQUAL( proto.variants().variants_size(), 1 );
401 kiapi::common::commands::ItemStatus status;
402 const kiapi::schematic::types::SheetVariant& variant = proto.variants().variants( 0 );
404 BOOST_CHECK( variant.exclude_from_sim() );
406 std::unique_ptr<SCH_SHEET> output = std::make_unique<SCH_SHEET>( m_schematic->RootScreen() );
407 output->SetParent( m_schematic->RootScreen() );
408 output->SetScreen(
new SCH_SCREEN( m_schematic.get() ) );
417 BOOST_CHECK( output->GetExcludedFromSim( &parentPath, wxS(
"Variant 1" ) ) );
419 kiapi::schematic::types::SheetSymbol outputProto;
423 BOOST_REQUIRE_EQUAL( outputProto.variants().variants_size(), 1 );
426 proto.variants().variants( 0 ).exclude_from_sim() );
446 const wxString variantName( wxS(
"api-sheet-variant" ) );
447 const bool baseDNP = sheet->
GetDNP( &parentPath );
449 kiapi::common::commands::ItemStatus status;
450 kiapi::schematic::types::SheetSymbol proto;
453 kiapi::schematic::types::SheetVariant* variant = proto.mutable_variants()->add_variants();
454 variant->set_name( variantName.ToUTF8() );
455 variant->set_dnp( !baseDNP );
461 BOOST_CHECK( m_schematic->GetVariantNames().count( variantName ) );
463 ( *variant->mutable_fields() )[
"Sheetfile"] =
"should_not_be_allowed";
464 BOOST_CHECK(
ApplySheetInstance( &status, sheet, proto, parentPath, m_schematic.get() ) ==
false );
465 BOOST_CHECK( status.code() == kiapi::common::commands::ItemStatusCode::ISC_INVALID_DATA );
467 kiapi::schematic::types::SheetSymbol untouched;
469 untouched.clear_variants();
471 if( untouched.page_number().empty() )
472 untouched.set_page_number(
"1" );
478 kiapi::schematic::types::SheetSymbol cleared;
480 BOOST_REQUIRE_EQUAL( cleared.variants().variants_size(), 1 );
481 cleared.mutable_variants()->clear_variants();
486 BOOST_CHECK( m_schematic->GetVariantNames().count( variantName ) );
492 fn.AppendDir( wxS(
"variants" ) );
493 fn.SetName( wxS(
"variants" ) );
494 fn.SetExt( wxS(
"kicad_sch" ) );
498 BOOST_REQUIRE_GE( hierarchy.size(), 2u );
505 if(
path.size() > 1 )
512 BOOST_REQUIRE_GT( subPath.
size(), 1u );
514 std::vector<SCH_ITEM*> subSymbols;
517 subSymbols.push_back( item );
519 BOOST_REQUIRE_GE( subSymbols.size(), 2u );
533 auto target =
ResolveFocusItems( *m_schematic, { subSymbols[0]->m_Uuid, subSymbols[1]->m_Uuid }, std::nullopt );
534 BOOST_REQUIRE_MESSAGE( target.has_value(), ( target ? std::string() : target.error().error_message() ) );
535 BOOST_CHECK( target->Sheet == subPath );
538 expected.Merge( subSymbols[1]->GetBoundingBox() );
539 BOOST_CHECK( target->BBox ==
expected );
545 BOOST_CHECK( !target );
551 BOOST_REQUIRE_MESSAGE( target.has_value(), ( target ? std::string() : target.error().error_message() ) );
552 BOOST_CHECK( target->Sheet == subPath );
555 BOOST_CHECK( !offSheet );
tl::expected< bool, ApiResponseStatus > UnpackSheet(SCH_SHEET *aOutput, const kiapi::schematic::types::SheetSymbol &aInput)
Unpack the every placement data from the input.
bool ApplySymbolInstance(kiapi::common::commands::ItemStatus *aStatus, SCH_SYMBOL *aSymbol, const kiapi::schematic::types::SchematicSymbolInstance &aInput, const SCH_SHEET_PATH &aPath, SCHEMATIC *aSchematic)
Apply placement-specific data to an aSymbol at aPath: reference, unit, and the per-placement attribut...
bool PackSheet(kiapi::schematic::types::SheetSymbol *aOutput, const SCH_SHEET *aInput, const SCH_SHEET_PATH &aPath)
bool ApplySheetInstance(commands::ItemStatus *aStatus, SCH_SHEET *aSheet, const kiapi::schematic::types::SheetSymbol &aInput, const SCH_SHEET_PATH &aParentPath, SCHEMATIC *aSchematic)
bool PackSymbol(kiapi::schematic::types::SchematicSymbolInstance *aOutput, const SCH_SYMBOL *aInput, const SCH_SHEET_PATH &aPath)
tl::expected< SCH_FOCUS_TARGET, ApiResponseStatus > ResolveFocusItems(SCHEMATIC &aSchematic, const std::vector< KIID > &aIds, const std::optional< KIID_PATH > &aSheetPath)
Resolve the items of a FocusOnItems request to the sheet that shows them and their combined bounding ...
bool UnpackSymbol(SCH_SYMBOL *aOutput, const kiapi::schematic::types::SchematicSymbolInstance &aInput)
Unpack the geometry, the library definition, fields, and the default-variant attributes that are shar...
void testProtoFromKiCadObject(KiCadClass *aInput, Factory &&aCreateOutput)
EE_TYPE OfType(KICAD_T aType) const
std::string AsStdString() const
A generic fixture for loading schematics and associated settings for qa tests.
Object to handle a bitmap image that can be inserted in a schematic.
Class for a bus to bus entry.
Class for a wire to bus entry.
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
A set of SCH_ITEMs (i.e., without duplicates).
Base class for any item which can be embedded within the SCHEMATIC container class,...
Segment description base class to describe items which have 2 end points (track, wire,...
EE_RTREE & Items()
Get the full RTree, usually for iterating.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
KIID_PATH Path() const
Get the sheet path as an KIID_PATH.
SCH_SCREEN * LastScreen()
wxString GetPageNumber() const
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
size_t size() const
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
bool GetDNP(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
Set or clear the 'Do Not Populate' flags.
virtual bool GetDNP(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
Set or clear the 'Do Not Populate' flag.
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
wxString GetFieldText(const SCH_FIELD *aField, const wxString &aFieldName, const SCH_SHEET_PATH *aPath, const wxString &aVariantName) const
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
Class to handle a set of SCH_ITEMs.
static void LoadSchematic(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxString &aFileName)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ VALUE
Field Value of part, i.e. "3.3K".
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
VECTOR3I expected(15, 30, 45)
BOOST_AUTO_TEST_CASE(KitchenSink)
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")