22#include <boost/test/data/test_case.hpp>
82 bool error = !compiler.
Compile( expr, &ucode, &preflightContext );
86 if( error != expectError )
102 ok = (
result->EqualTo( &context, &expectedResult ) );
124 compiler.
Compile( aExpr, &ucode, &preflight );
126 BOOST_CHECK_MESSAGE( compiler.
IsErrorPending(),
"Expected a compile error for: " << aExpr.mb_str() );
136 compiler.
Compile( aExpr, &ucode, &preflight );
138 BOOST_CHECK_MESSAGE( !compiler.
IsErrorPending(),
"Expected expression to compile: " << aExpr.mb_str() );
150 test(
"10mm + 20 mm",
VAL( 30e6 ) );
151 test(
"3*(7+8)",
VAL( 3 * ( 7 + 8 ) ) );
152 test(
"3*7+8",
VAL( 3 * 7 + 8 ) );
153 test(
"(3*7)+8",
VAL( 3 * 7 + 8 ) );
166 test(
"1.5 + 0.2 + 0.1",
VAL( 1.8 ) );
172 test(
"1 + 2 + 10 + 1000.05",
VAL( 1013.05 ) );
175 test(
"1 + 2 - 4 * 20 / 2",
VAL( -37 ) );
181 test(
"-(1 + (2 - 4)) * 20.8 / 2",
VAL( 10.4 ) );
188 test(
"(2 || 0) == 1",
VAL( 1 ) );
189 test(
"(7 || 0) + 5",
VAL( 6 ) );
202 std::shared_ptr<NETCLASS> netclass1(
new NETCLASS(
"HV_LINE" ) );
203 std::shared_ptr<NETCLASS> netclass2(
new NETCLASS(
"otherClass" ) );
214 trackA.
SetNet( net1info );
215 trackB.
SetNet( net2info );
228 test(
"A.type == 'Pad' && B.type == 'Pad' && (A.existsOnLayer('F.Cu'))",
VAL( 0.0 ) );
229 test(
"A.Width > B.Width",
VAL( 0.0 ) );
231 test(
"A.Netclass",
VAL(
"HV_LINE" ) );
232 test(
"A.Net_Class == 'HV_LINE'",
VAL( 1.0 ) );
233 test(
"(A.Netclass == 'HV_LINE') && (B.netclass == 'otherClass') && (B.netclass != 'F.Cu')",
VAL( 1.0 ) );
234 test(
"A.Netclass + 1.0",
VAL( 1.0 ) );
235 test(
"A.hasNetclass('HV_LINE')",
VAL( 1.0 ) );
236 test(
"A.hasNetclass('HV_*')",
VAL( 1.0 ) );
237 test(
"A.existsOnLayer(B.Layer)",
VAL( 1.0 ) );
238 test(
"A.type == 'Track' && B.type == 'Track' && A.layer == 'F.Cu'",
VAL( 1.0 ) );
239 test(
"(A.type == 'Track') && (B.type == 'Track') && (A.layer == 'F.Cu')",
VAL( 1.0 ) );
240 test(
"A.type == 'Via' && A.isMicroVia()",
VAL( 0.0 ) );
258 testEvalExpr( wxT(
"A.Hatch_Orientation == 45deg" ),
VAL( 1.0 ), &zone, &zone );
259 testEvalExpr( wxT(
"A.Minimum_Island_Area == 123456789" ),
VAL( 1.0 ), &zone, &zone );
264 testEvalExpr( wxT(
"A.Ellipse_Rotation == 30deg" ),
VAL( 1.0 ), &ellipse, &ellipse );
269 testEvalExpr( wxT(
"A.Solderpaste_Margin_Ratio_Override == 0.125" ),
VAL( 1.0 ), &footprint, &footprint );
273 testEvalExpr( wxT(
"A.Solderpaste_Margin_Ratio_Override == null" ),
VAL( 1.0 ), &footprint, &footprint );
289 pad->SetNumber( wxT(
"2" ) );
291 text.SetText( wxT(
"4k7" ) );
295 testEvalExpr( wxT(
"A.Reference == 'R1'" ),
VAL( 1.0 ), &footprint, &footprint );
299 testEvalExpr( wxT(
"A.getField('Value') > 3300" ),
VAL( 1.0 ), &footprint, &footprint );
300 testEvalExpr( wxT(
"A.getField('Value') > 3k3" ),
VAL( 1.0 ), &footprint, &footprint );
319 testEvalExpr( wxT(
"A.Origin_X == 1mm" ),
VAL( 1.0 ), &track, &track );
320 testEvalExpr( wxT(
"A.Origin_Y == 2mm" ),
VAL( 1.0 ), &track, &track );
321 testEvalExpr( wxT(
"A.Origin_X != A.End_X" ),
VAL( 1.0 ), &track, &track );
322 testEvalExpr( wxT(
"A.origin_y != A.End_Y" ),
VAL( 1.0 ), &track, &track );
338 [&](
const COLOR4D& aColor,
const wxString& aExpectedCss )
340 table.SetBorderColor( aColor );
343 wxString expression = wxT(
"A.Border_Color == '" ) + aExpectedCss + wxT(
"'" );
347 checkColor(
COLOR4D( 0.25, 0.5, 0.75, 0.5 ), wxT(
"rgba(64, 128, 191, 0.502)" ) );
348 checkColor(
COLOR4D( 1.0, 0.0, 0.0, 1.0 ), wxT(
"rgb(255, 0, 0)" ) );
357 std::set<std::pair<TYPE_ID, PROPERTY_BASE*>> seen;
358 std::map<wxString, std::set<LIBEVAL::VAR_TYPE_T>> fieldTypes;
367 if( !seen.emplace( cls.type, prop ).second )
373 "Unsupported expression property: class=" << cls.name.mb_str()
374 <<
", property=" << prop->
Name().mb_str()
375 <<
", type_hash=" << prop->
TypeHash() );
384 auto shapeTypes = fieldTypes.find( wxT(
"Shape" ) );
387 fieldTypes.erase( shapeTypes );
389 for(
const auto& [field, types] : fieldTypes )
391 BOOST_CHECK_MESSAGE( types.size() == 1,
"Incompatible expression types for property: " << field.mb_str() );
426 netSettings->SetNetChainClass( wxT(
"ChainHS" ), wxT(
"HighSpeed" ) );
432 netUnclassified->
SetNetChain( wxT(
"ChainOrphan" ) );
439 trackUnclassified.
SetNet( netUnclassified );
440 trackClassified.
SetNet( netClassified );
441 trackNoChain.
SetNet( netNoChain );
445 testEvalExpr( wxT(
"A.inNetChainClass('*')" ),
VAL( 0.0 ), &trackUnclassified, &trackUnclassified );
446 testEvalExpr( wxT(
"A.inNetChainClass('HighSpeed')" ),
VAL( 0.0 ), &trackUnclassified, &trackUnclassified );
449 testEvalExpr( wxT(
"A.inNetChainClass('*')" ),
VAL( 0.0 ), &trackNoChain, &trackNoChain );
452 testEvalExpr( wxT(
"A.inNetChainClass('*')" ),
VAL( 1.0 ), &trackClassified, &trackClassified );
453 testEvalExpr( wxT(
"A.inNetChainClass('HighSpeed')" ),
VAL( 1.0 ), &trackClassified, &trackClassified );
454 testEvalExpr( wxT(
"A.inNetChainClass('High*')" ),
VAL( 1.0 ), &trackClassified, &trackClassified );
455 testEvalExpr( wxT(
"A.inNetChainClass('LowSpeed')" ),
VAL( 0.0 ), &trackClassified, &trackClassified );
471 via->SetPosition( aPos );
472 via->SetLayerPair( aTop, aBottom );
497 testEvalExpr( wxT(
"A.isStackedVia()" ),
VAL( 1.0 ), upperHop, upperHop );
498 testEvalExpr( wxT(
"A.isStackedVia()" ),
VAL( 1.0 ), lowerHop, lowerHop );
504 testEvalExpr( wxT(
"A.isStackedVia()" ),
VAL( 0.0 ), through, through );
507 testEvalExpr( wxT(
"A.isMicroVia() && !A.isStackedVia()" ),
VAL( 1.0 ), lone, lone );
514 testEvalExpr( wxT(
"A.isStackedVia()" ),
VAL( 1.0 ), landing, landing );
530 text->SetText( wxT(
"J1" ) );
610 variant->
SetFieldValue( wxT(
"Value" ), wxT(
"VariantVal" ) );
616 testEvalExpr( wxT(
"A.getField('Value') == 'VariantVal'" ),
VAL( 1.0 ), &fp, &fp );
622 testEvalExpr( wxT(
"A.getField('Value') == 'BaseVal'" ),
VAL( 1.0 ), &fp, &fp );
630 std::vector<PROPERTY_ROW_DATA>
rows;
636 wxS(
"A.getField('Value') == 'X'" ) );
645 wxS(
"A.getField('My Field') == 'X'" ) );
668 compiler.
Compile( wxT(
"L.Layer == '*.Paste'" ), &ucode, &preflight );
676 paste.SetItems( &track, &track );
708 for(
const wxString& expression : {
709 wxString(
"A.intersectsFrontCourtyard(B.Parent)" ),
710 wxString(
"A.intersectsCourtyard(B.Parent.Reference)" ),
711 wxString(
"A.intersectsFrontCourtyard(B.Parent.getField('Reference'))" ) } )
724 context.
SetItems( &graphic, &targetChild );
727 context.
SetItems( &graphic, &otherChild );
730 context.
SetItems( &graphic, &targetChild );
744 expectCompileError( wxS(
"A.intersectsFrontCourtyard(B.Parent.unknownFunction())" ) );
761 context.
SetErrorCallback( [&](
const wxString& aMessage,
int ) { error = aMessage; } );
773 wxString args = wxS(
"'F.Cu'" );
776 args += wxS(
", 'F.Cu'" );
783 &literalCode, &preflight ) );
785 wxS(
"Expression evaluation stack overflow" ) );
793 wxString expression = wxS(
"A.Layer == 'F.Cu' && A.existsOnLayer(B.Layer, " )
808 context.
SetErrorCallback( [&](
const wxString& aMessage,
int ) { error = aMessage; } );
837 wxString args = wxS(
"'F.Cu'" );
839 for(
int i = 1; i < argumentCount; ++i )
840 args += wxS(
", 'F.Cu'" );
842 wxString
source = wxS(
"(version 1)(rule test (condition \"A.existsOnLayer(" )
843 + args + wxS(
")\")(constraint clearance (min 0.2mm)))" );
844 std::vector<std::shared_ptr<DRC_RULE>> rules;
855 BOOST_CHECK( !rules.front()->m_Condition->EvaluateFor(
858 wxString messages =
reporter.GetMessages();
859 BOOST_CHECK( !messages.Contains( wxS(
"stack overflow" ) ) );
860 BOOST_CHECK_EQUAL( messages.Replace( wxS(
"Malformed expression" ), wxEmptyString ), 2 );
863 BOOST_CHECK( !rules.front()->m_Condition->EvaluateFor(
879 for(
const wxString& constraint : { wxString(
"assertion \"A.Type == 'Graphic'\"" ),
880 wxString(
"hole_size (min 0.2mm)" ),
881 wxString(
"text_height (min 1mm)" ),
882 wxString(
"text_thickness (min 0.1mm)" ),
883 wxString(
"track_segment_length (min 1mm)" ),
884 wxString(
"annular_width (min 0.1mm)" ),
885 wxString(
"solder_mask_expansion (opt 0mm)" ),
886 wxString(
"solder_paste_abs_margin (opt 0mm)" ),
887 wxString(
"solder_paste_rel_margin (opt 0mm)" ),
888 wxString(
"disallow track" ),
889 wxString(
"via_diameter (min 0.5mm)" ),
890 wxString(
"length (max 100mm)" ),
891 wxString(
"net_chain_length (max 100mm)" ),
892 wxString(
"stub_length (max 1mm)" ),
893 wxString(
"return_path (layer 'B.Cu')" ),
894 wxString(
"skew (max 1mm)" ),
895 wxString(
"via_count (max 2)" ),
896 wxString(
"via_dangling (max 0)" ),
897 wxString(
"bridged_mask (min 0)" ),
898 wxString(
"microvia_stack_depth (max 2)" ),
899 wxString(
"hole_aspect_ratio (max 1)" ) } )
901 for(
bool conditionFirst : {
true,
false } )
905 wxString condition = wxS(
"(condition \"B.Type == 'Graphic'\")" );
906 wxString body = wxString::Format( wxS(
"(constraint %s)" ), constraint );
907 wxString
source = wxS(
"(version 1)\n(rule test\n" )
908 + ( conditionFirst ? condition + wxS(
"\n" ) + body
909 : body + wxS(
"\n" ) + condition ) + wxS(
"\n)" );
910 std::vector<std::shared_ptr<DRC_RULE>> rules;
912 wxString controlSource =
source;
913 controlSource.Replace( wxS(
"B.Type" ), wxS(
"A.Type" ) );
914 DRC_RULES_PARSER controlParser( controlSource, wxS(
"single item control" ) );
921 BOOST_CHECK_MESSAGE(
reporter.GetMessages().Contains( wxS(
"Item 'B'" ) ),
928 return error.What().Contains( wxS(
"Item 'B' is not available" ) )
929 && error.lineNumber == ( conditionFirst ? 3 : 4 );
941 for(
const wxString& body : {
942 wxString(
"(condition \"A.intersectsFrontCourtyard(B.Parent)\")"
943 "(constraint assertion \"A.Type == 'Graphic'\")" ),
944 wxString(
"(condition \"AB.isCoupledDiffPair()\")(constraint hole_size (min 0.2mm))" ),
945 wxString(
"(constraint assertion \"B.Type == 'Graphic'\")" ),
946 wxString(
"(constraint assertion \"A.intersectsFrontCourtyard(B.Parent.getField('Reference'))\")" ),
947 wxString(
"(condition \"B.Type == 'Graphic'\")(constraint clearance (min 0.2mm))"
948 "(constraint hole_size (min 0.2mm))" ),
949 wxString(
"(condition \"AB.isCoupledDiffPair() && B.Type == 'Track'\")"
950 "(constraint length (max 100mm))" ) } )
954 std::vector<std::shared_ptr<DRC_RULE>> rules;
955 DRC_RULES_PARSER parser( wxS(
"(version 1)(rule test " ) + body + wxS(
")" ), wxS(
"preflight" ) );
959 return error.What().Contains( wxS(
"Item 'B' is not available" ) );
964 for(
const wxString& body : {
965 wxString(
"(condition \"A.Reference == 'B.Width'\")(constraint hole_size (min 0.2mm))" ),
966 wxString(
"(constraint assertion \"A.Type == 'Graphic'\")" ),
967 wxString(
"(condition \"A.intersectsFrontCourtyard(B.Parent)\")"
968 "(constraint physical_clearance (min 100mm))" ),
969 wxString(
"(condition \"AB.isCoupledDiffPair()\")(constraint diff_pair_gap (min 0.2mm))" ),
970 wxString(
"(condition \"AB.isCoupledDiffPair()\")(constraint track_width (min 0.2mm))" ),
971 wxString(
"(condition \"AB.isCoupledDiffPair()\")(constraint diff_pair_uncoupled (max 1mm))" ),
972 wxString(
"(condition \"AB.isCoupledDiffPair()\")(constraint length (max 100mm))" ),
973 wxString(
"(condition \"AB.isCoupledDiffPair()\")(constraint net_chain_length (max 100mm))" ),
974 wxString(
"(condition \"AB.isCoupledDiffPair()\")(constraint skew (max 1mm))" ),
975 wxString(
"(condition \"B.Type == 'Track'\")(constraint track_width (min 0.2mm))" ),
976 wxString(
"(condition \"B.Type == 'Track'\")(constraint track_angle (min 45))" ),
977 wxString(
"(condition \"B.Type == 'Zone'\")(constraint thermal_relief_gap (min 0.2mm))" ) } )
981 std::vector<std::shared_ptr<DRC_RULE>> rules;
983 DRC_RULES_PARSER parser( wxS(
"(version 1)(rule test " ) + body + wxS(
")" ), wxS(
"preflight" ) );
985 BOOST_CHECK_MESSAGE( !
reporter.GetMessages().Contains( wxS(
"ERROR:" ) ),
reporter.GetMessages() );
993 auto requiresPairItems =
994 [](
const wxString& aExpr )
1004 BOOST_CHECK( !requiresPairItems( wxT(
"A.Width == 1mm" ) ) );
1005 BOOST_CHECK( requiresPairItems( wxT(
"B.Width == 1mm" ) ) );
1006 BOOST_CHECK( requiresPairItems( wxT(
"AB.isCoupledDiffPair()" ) ) );
1007 BOOST_CHECK( !requiresPairItems( wxT(
"A.Reference == 'B.Width'" ) ) );
constexpr EDA_IU_SCALE pcbIUScale
Construction utilities for PCB tests.
virtual void SetNet(NETINFO_ITEM *aNetInfo)
Set a NET_INFO object for the item.
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
std::shared_ptr< NET_SETTINGS > m_NetSettings
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Information pertinent to a Pcbnew printed circuit board.
const NETINFO_LIST & GetNetInfo() const
void SetCurrentVariant(const wxString &aVariant)
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
void AddVariant(const wxString &aVariantName)
void IncrementTimeStamp()
void SetCopperLayerCount(int aCount)
BOARD_DESIGN_SETTINGS & GetDesignSettings()
void Parse(std::vector< std::shared_ptr< DRC_RULE > > &aRules, REPORTER *aReporter)
bool Compile(REPORTER *aReporter, int aSourceLine=0, int aSourceOffset=0)
bool EvaluateFor(const BOARD_ITEM *aItemA, const BOARD_ITEM *aItemB, int aConstraint, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
A color representation with 4 components: red, green, blue, alpha.
wxString ToCSSString() const
bool IsErrorPending() const
const ERROR_STATUS & GetError() const
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
void SetErrorCallback(ERROR_CALLBACK aCallback)
static constexpr int STACK_CAPACITY
VALUE * Run(CONTEXT *ctx)
virtual const wxString & AsString() const
virtual double AsDouble() const
VAR_TYPE_T GetType() const
A collection of nets and the parameters used to route or test these nets.
Handle the data for a net.
void SetNetChain(const wxString &aNetChain)
void SetNetClass(const std::shared_ptr< NETCLASS > &aNetClass)
Container for NETINFO_ITEM elements, which are the nets.
static constexpr PCB_LAYER_ID ALL_LAYERS
! The layer identifier to use for the single defintion on normal padstacks
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
bool RequiresPairItems() const
static PCBEXPR_PROPERTY_KIND ClassifyProperty(const PROPERTY_BASE *aProperty)
static LIBEVAL::VAR_TYPE_T ExpressionType(PCBEXPR_PROPERTY_KIND aKind)
void SetWidth(int aWidth) override
void SetEnd(const VECTOR2I &aEnd) override
void SetEllipseRotation(const EDA_ANGLE &aA) override
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
void SetStart(const VECTOR2I &aStart) override
void SetEnd(const VECTOR2I &aEnd)
void SetStart(const VECTOR2I &aStart)
virtual void SetWidth(int aWidth)
void SetDrill(int aDrill)
void SetPosition(const VECTOR2I &aPoint) override
void SetLayerPair(PCB_LAYER_ID aTopLayer, PCB_LAYER_ID aBottomLayer)
For a via m_layer contains the top layer, the other layer is in m_bottomLayer/.
void SetViaType(VIATYPE aViaType)
void SetWidth(int aWidth) override
virtual size_t TypeHash() const =0
Return type-id of the property type.
const wxString & Name() const
static PROPERTY_MANAGER & Instance()
void Rebuild()
Rebuild the list of all registered properties.
std::vector< CLASS_INFO > GetAllClasses()
bool IsOfType(TYPE_ID aDerived, TYPE_ID aBase) const
Return true if aDerived is inherited from aBase.
A wrapper for reporting to a wxString object.
Handle a list of polygons defining a copper zone.
void SetHatchOrientation(const EDA_ANGLE &aStep)
virtual void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
void SetIsRuleArea(bool aEnable)
void SetAssignedPriority(unsigned aPriority)
void SetMinIslandArea(long long int aArea)
wxString normalizeQueryFieldAliases(const wxString &aExpression, const std::vector< PROPERTY_ROW_DATA > &aRows)
PCB_LAYER_ID
A quick note on layer IDs:
#define TR_UOP_PUSH_VALUE
void DrawRect(FOOTPRINT &aFootprint, const VECTOR2I &aPos, const VECTOR2I &aSize, int aRadius, int aWidth, PCB_LAYER_ID aLayer)
Draw a rectangle on a footprint.
#define TYPE_HASH(x)
Macro to generate unique identifier for a type.
friend std::ostream & operator<<(std::ostream &os, const EXPR_TO_TEST &expr)
LIBEVAL::VALUE expectedResult
A filename or source description, a problem input line, a line number, a byte offset,...
PROPERTY_MATCH_MODE matchMode
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
std::vector< DATABASE_CONNECTION::ROW > rows
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
IbisParser parser & reporter
static void expectCompileError(const wxString &aExpr)
static void expectCompileSuccess(const wxString &aExpr)
static bool testEvalExpr(const wxString &expr, const LIBEVAL::VALUE &expectedResult, BOARD_ITEM *itemA, BOARD_ITEM *itemB, bool expectError=false)
BOOST_AUTO_TEST_CASE(SimpleExpressions)
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I