2020-01-21 11:10:02 +00:00
# include <catch2/catch.hpp>
# include "libslic3r/PlaceholderParser.hpp"
# include "libslic3r/PrintConfig.hpp"
using namespace Slic3r ;
SCENARIO ( " Placeholder parser scripting " , " [PlaceholderParser] " ) {
PlaceholderParser parser ;
auto config = DynamicPrintConfig : : full_print_config ( ) ;
Support for forward compatibility of configurations, user and system
config bundles, project files (3MFs, AMFs). When loading these files,
the caller may decide whether to substitute some of the configuration
values the current PrusaSlicer version does not understand with
some reasonable default value, and whether to report it. If substitution
is disabled, an exception is being thrown as before this commit.
If substitution is enabled, list of substitutions is returned by the
API to be presented to the user. This allows us to introduce for example
new firmware flavor key in PrusaSlicer 2.4 while letting PrusaSlicer
2.3.2 to fall back to some default and to report it to the user.
When slicing from command line, substutions are performed by default
and reported into the console, however substitutions may be either
disabled or made silent with the new "config-compatibility" command
line option.
Substitute enums and bools only. Allow booleans to be parsed as
true: "1", "enabled", "on" case insensitive
false: "0", "disabled", "off" case insensitive
This will allow us in the future for example to switch the draft_shield
boolean to an enum with the following values: "disabled" / "enabled" / "limited".
Added "enum_bitmask.hpp" - support for type safe sets of options.
See for example PresetBundle::load_configbundle(...
LoadConfigBundleAttributes flags) for an example of intended usage.
WIP: GUI for reporting the list of config substitutions needs to be
implemented by @YuSanka.
2021-06-27 14:04:23 +00:00
config . set_deserialize_strict ( {
2020-01-21 11:10:02 +00:00
{ " printer_notes " , " PRINTER_VENDOR_PRUSA3D PRINTER_MODEL_MK2 " } ,
{ " nozzle_diameter " , " 0.6;0.6;0.6;0.6 " } ,
{ " temperature " , " 357;359;363;378 " }
} ) ;
2021-04-21 13:31:06 +00:00
// To test the "first_layer_extrusion_width" over "first_layer_heigth".
// "first_layer_heigth" over "layer_height" is no more supported after first_layer_height was moved from PrintObjectConfig to PrintConfig.
// config.option<ConfigOptionFloatOrPercent>("first_layer_height")->value = 150.;
// config.option<ConfigOptionFloatOrPercent>("first_layer_height")->percent = true;
config . option < ConfigOptionFloatOrPercent > ( " first_layer_height " ) - > value = 1.5 * config . opt_float ( " layer_height " ) ;
config . option < ConfigOptionFloatOrPercent > ( " first_layer_height " ) - > percent = false ;
2020-02-04 13:43:58 +00:00
// To let the PlaceholderParser throw when referencing first_layer_speed if it is set to percent, as the PlaceholderParser does not know
// a percent to what.
config . option < ConfigOptionFloatOrPercent > ( " first_layer_speed " ) - > value = 50. ;
config . option < ConfigOptionFloatOrPercent > ( " first_layer_speed " ) - > percent = true ;
2023-02-28 17:30:04 +00:00
ConfigOptionFloatsNullable * opt_filament_retract_length = config . option < ConfigOptionFloatsNullable > ( " filament_retract_length " , true ) ;
opt_filament_retract_length - > values = { 5. , ConfigOptionFloatsNullable : : nil_value ( ) , 3. } ;
2020-02-04 13:43:58 +00:00
parser . apply_config ( config ) ;
2020-01-21 11:10:02 +00:00
parser . set ( " foo " , 0 ) ;
parser . set ( " bar " , 2 ) ;
parser . set ( " num_extruders " , 4 ) ;
2023-01-04 12:38:18 +00:00
parser . set ( " gcode_flavor " , " marlin " ) ;
2020-01-21 11:10:02 +00:00
SECTION ( " nested config options (legacy syntax) " ) { REQUIRE ( parser . process ( " [temperature_[foo]] " ) = = " 357 " ) ; }
SECTION ( " array reference " ) { REQUIRE ( parser . process ( " {temperature[foo]} " ) = = " 357 " ) ; }
SECTION ( " whitespaces and newlines are maintained " ) { REQUIRE ( parser . process ( " test [ temperature_ [foo] ] \n hu " ) = = " test 357 \n hu " ) ; }
2023-02-28 17:30:04 +00:00
SECTION ( " nullable is not null " ) { REQUIRE ( parser . process ( " {is_nil(filament_retract_length[0])} " ) = = " false " ) ; }
SECTION ( " nullable is null " ) { REQUIRE ( parser . process ( " {is_nil(filament_retract_length[1])} " ) = = " true " ) ; }
SECTION ( " nullable is not null 2 " ) { REQUIRE ( parser . process ( " {is_nil(filament_retract_length[2])} " ) = = " false " ) ; }
2020-01-21 11:10:02 +00:00
// Test the math expressions.
SECTION ( " math: 2*3 " ) { REQUIRE ( parser . process ( " {2*3} " ) = = " 6 " ) ; }
SECTION ( " math: 2*3/6 " ) { REQUIRE ( parser . process ( " {2*3/6} " ) = = " 1 " ) ; }
SECTION ( " math: 2*3/12 " ) { REQUIRE ( parser . process ( " {2*3/12} " ) = = " 0 " ) ; }
SECTION ( " math: 2.*3/12 " ) { REQUIRE ( std : : stod ( parser . process ( " {2.*3/12} " ) ) = = Approx ( 0.5 ) ) ; }
2020-01-21 16:11:41 +00:00
SECTION ( " math: 10 % 2.5 " ) { REQUIRE ( std : : stod ( parser . process ( " {10%2.5} " ) ) = = Approx ( 0. ) ) ; }
SECTION ( " math: 11 % 2.5 " ) { REQUIRE ( std : : stod ( parser . process ( " {11%2.5} " ) ) = = Approx ( 1. ) ) ; }
2020-01-21 11:10:02 +00:00
SECTION ( " math: 2*(3-12) " ) { REQUIRE ( parser . process ( " {2*(3-12)} " ) = = " -18 " ) ; }
SECTION ( " math: 2*foo*(3-12) " ) { REQUIRE ( parser . process ( " {2*foo*(3-12)} " ) = = " 0 " ) ; }
SECTION ( " math: 2*bar*(3-12) " ) { REQUIRE ( parser . process ( " {2*bar*(3-12)} " ) = = " -36 " ) ; }
SECTION ( " math: 2.5*bar*(3-12) " ) { REQUIRE ( std : : stod ( parser . process ( " {2.5*bar*(3-12)} " ) ) = = Approx ( - 45 ) ) ; }
SECTION ( " math: min(12, 14) " ) { REQUIRE ( parser . process ( " {min(12, 14)} " ) = = " 12 " ) ; }
SECTION ( " math: max(12, 14) " ) { REQUIRE ( parser . process ( " {max(12, 14)} " ) = = " 14 " ) ; }
SECTION ( " math: min(13.4, -1238.1) " ) { REQUIRE ( std : : stod ( parser . process ( " {min(13.4, -1238.1)} " ) ) = = Approx ( - 1238.1 ) ) ; }
SECTION ( " math: max(13.4, -1238.1) " ) { REQUIRE ( std : : stod ( parser . process ( " {max(13.4, -1238.1)} " ) ) = = Approx ( 13.4 ) ) ; }
2020-01-21 16:11:41 +00:00
SECTION ( " math: int(13.4) " ) { REQUIRE ( parser . process ( " {int(13.4)} " ) = = " 13 " ) ; }
SECTION ( " math: int(-13.4) " ) { REQUIRE ( parser . process ( " {int(-13.4)} " ) = = " -13 " ) ; }
2021-10-29 11:36:26 +00:00
SECTION ( " math: round(13.4) " ) { REQUIRE ( parser . process ( " {round(13.4)} " ) = = " 13 " ) ; }
SECTION ( " math: round(-13.4) " ) { REQUIRE ( parser . process ( " {round(-13.4)} " ) = = " -13 " ) ; }
SECTION ( " math: round(13.6) " ) { REQUIRE ( parser . process ( " {round(13.6)} " ) = = " 14 " ) ; }
SECTION ( " math: round(-13.6) " ) { REQUIRE ( parser . process ( " {round(-13.6)} " ) = = " -14 " ) ; }
SECTION ( " math: digits(5, 15) " ) { REQUIRE ( parser . process ( " {digits(5, 15)} " ) = = " 5 " ) ; }
SECTION ( " math: digits(5., 15) " ) { REQUIRE ( parser . process ( " {digits(5., 15)} " ) = = " 5 " ) ; }
SECTION ( " math: zdigits(5, 15) " ) { REQUIRE ( parser . process ( " {zdigits(5, 15)} " ) = = " 000000000000005 " ) ; }
SECTION ( " math: zdigits(5., 15) " ) { REQUIRE ( parser . process ( " {zdigits(5., 15)} " ) = = " 000000000000005 " ) ; }
SECTION ( " math: digits(5, 15, 8) " ) { REQUIRE ( parser . process ( " {digits(5, 15, 8)} " ) = = " 5.00000000 " ) ; }
SECTION ( " math: digits(5., 15, 8) " ) { REQUIRE ( parser . process ( " {digits(5, 15, 8)} " ) = = " 5.00000000 " ) ; }
SECTION ( " math: zdigits(5, 15, 8) " ) { REQUIRE ( parser . process ( " {zdigits(5, 15, 8)} " ) = = " 000005.00000000 " ) ; }
SECTION ( " math: zdigits(5., 15, 8) " ) { REQUIRE ( parser . process ( " {zdigits(5, 15, 8)} " ) = = " 000005.00000000 " ) ; }
SECTION ( " math: digits(13.84375892476, 15, 8) " ) { REQUIRE ( parser . process ( " {digits(13.84375892476, 15, 8)} " ) = = " 13.84375892 " ) ; }
SECTION ( " math: zdigits(13.84375892476, 15, 8) " ) { REQUIRE ( parser . process ( " {zdigits(13.84375892476, 15, 8)} " ) = = " 000013.84375892 " ) ; }
2023-03-21 10:14:46 +00:00
SECTION ( " math: interpolate_table(13.84375892476, (0, 0), (20, 20)) " ) { REQUIRE ( std : : stod ( parser . process ( " {interpolate_table(13.84375892476, (0, 0), (20, 20))} " ) ) = = Approx ( 13.84375892476 ) ) ; }
SECTION ( " math: interpolate_table(13, (0, 0), (20, 20), (30, 20)) " ) { REQUIRE ( std : : stod ( parser . process ( " {interpolate_table(13, (0, 0), (20, 20), (30, 20))} " ) ) = = Approx ( 13. ) ) ; }
SECTION ( " math: interpolate_table(25, (0, 0), (20, 20), (30, 20)) " ) { REQUIRE ( std : : stod ( parser . process ( " {interpolate_table(25, (0, 0), (20, 20), (30, 20))} " ) ) = = Approx ( 20. ) ) ; }
2020-01-21 11:10:02 +00:00
2020-02-04 13:43:58 +00:00
// Test the "coFloatOrPercent" and "xxx_extrusion_width" substitutions.
2021-04-21 13:31:06 +00:00
// first_layer_extrusion_width ratio_over first_layer_heigth.
2020-02-04 13:43:58 +00:00
SECTION ( " perimeter_extrusion_width " ) { REQUIRE ( std : : stod ( parser . process ( " {perimeter_extrusion_width} " ) ) = = Approx ( 0.67500001192092896 ) ) ; }
SECTION ( " first_layer_extrusion_width " ) { REQUIRE ( std : : stod ( parser . process ( " {first_layer_extrusion_width} " ) ) = = Approx ( 0.9 ) ) ; }
SECTION ( " support_material_xy_spacing " ) { REQUIRE ( std : : stod ( parser . process ( " {support_material_xy_spacing} " ) ) = = Approx ( 0.3375 ) ) ; }
// external_perimeter_speed over perimeter_speed
SECTION ( " external_perimeter_speed " ) { REQUIRE ( std : : stod ( parser . process ( " {external_perimeter_speed} " ) ) = = Approx ( 30. ) ) ; }
// infill_overlap over perimeter_extrusion_width
SECTION ( " infill_overlap " ) { REQUIRE ( std : : stod ( parser . process ( " {infill_overlap} " ) ) = = Approx ( 0.16875 ) ) ; }
// If first_layer_speed is set to percent, then it is applied over respective extrusion types by overriding their respective speeds.
// The PlaceholderParser has no way to know which extrusion type the caller has in mind, therefore it throws.
SECTION ( " first_layer_speed " ) { REQUIRE_THROWS ( parser . process ( " {first_layer_speed} " ) ) ; }
2020-01-21 11:10:02 +00:00
// Test the boolean expression parser.
auto boolean_expression = [ & parser ] ( const std : : string & templ ) { return parser . evaluate_boolean_expression ( templ , parser . config ( ) ) ; } ;
SECTION ( " boolean expression parser: 12 == 12 " ) { REQUIRE ( boolean_expression ( " 12 == 12 " ) ) ; }
SECTION ( " boolean expression parser: 12 != 12 " ) { REQUIRE ( ! boolean_expression ( " 12 != 12 " ) ) ; }
SECTION ( " boolean expression parser: regex matches " ) { REQUIRE ( boolean_expression ( " \" has some PATTERN embedded \" =~ /.*PATTERN.*/ " ) ) ; }
SECTION ( " boolean expression parser: regex does not match " ) { REQUIRE ( ! boolean_expression ( " \" has some PATTERN embedded \" =~ /.*PTRN.*/ " ) ) ; }
SECTION ( " boolean expression parser: accessing variables, equal " ) { REQUIRE ( boolean_expression ( " foo + 2 == bar " ) ) ; }
SECTION ( " boolean expression parser: accessing variables, not equal " ) { REQUIRE ( ! boolean_expression ( " foo + 3 == bar " ) ) ; }
SECTION ( " boolean expression parser: (12 == 12) and (13 != 14) " ) { REQUIRE ( boolean_expression ( " (12 == 12) and (13 != 14) " ) ) ; }
SECTION ( " boolean expression parser: (12 == 12) && (13 != 14) " ) { REQUIRE ( boolean_expression ( " (12 == 12) && (13 != 14) " ) ) ; }
SECTION ( " boolean expression parser: (12 == 12) or (13 == 14) " ) { REQUIRE ( boolean_expression ( " (12 == 12) or (13 == 14) " ) ) ; }
SECTION ( " boolean expression parser: (12 == 12) || (13 == 14) " ) { REQUIRE ( boolean_expression ( " (12 == 12) || (13 == 14) " ) ) ; }
SECTION ( " boolean expression parser: (12 == 12) and not (13 == 14) " ) { REQUIRE ( boolean_expression ( " (12 == 12) and not (13 == 14) " ) ) ; }
SECTION ( " boolean expression parser: ternary true " ) { REQUIRE ( boolean_expression ( " (12 == 12) ? (1 - 1 == 0) : (2 * 2 == 3) " ) ) ; }
SECTION ( " boolean expression parser: ternary false " ) { REQUIRE ( ! boolean_expression ( " (12 == 21/2) ? (1 - 1 == 0) : (2 * 2 == 3) " ) ) ; }
SECTION ( " boolean expression parser: ternary false 2 " ) { REQUIRE ( boolean_expression ( " (12 == 13) ? (1 - 1 == 3) : (2 * 2 == 4) " ) ) ; }
SECTION ( " boolean expression parser: ternary true 2 " ) { REQUIRE ( ! boolean_expression ( " (12 == 2 * 6) ? (1 - 1 == 3) : (2 * 2 == 4) " ) ) ; }
SECTION ( " boolean expression parser: lower than - false " ) { REQUIRE ( ! boolean_expression ( " 12 < 3 " ) ) ; }
SECTION ( " boolean expression parser: lower than - true " ) { REQUIRE ( boolean_expression ( " 12 < 22 " ) ) ; }
SECTION ( " boolean expression parser: greater than - true " ) { REQUIRE ( boolean_expression ( " 12 > 3 " ) ) ; }
SECTION ( " boolean expression parser: greater than - false " ) { REQUIRE ( ! boolean_expression ( " 12 > 22 " ) ) ; }
SECTION ( " boolean expression parser: lower than or equal- false " ) { REQUIRE ( ! boolean_expression ( " 12 <= 3 " ) ) ; }
SECTION ( " boolean expression parser: lower than or equal - true " ) { REQUIRE ( boolean_expression ( " 12 <= 22 " ) ) ; }
SECTION ( " boolean expression parser: greater than or equal - true " ) { REQUIRE ( boolean_expression ( " 12 >= 3 " ) ) ; }
SECTION ( " boolean expression parser: greater than or equal - false " ) { REQUIRE ( ! boolean_expression ( " 12 >= 22 " ) ) ; }
SECTION ( " boolean expression parser: lower than or equal (same values) - true " ) { REQUIRE ( boolean_expression ( " 12 <= 12 " ) ) ; }
SECTION ( " boolean expression parser: greater than or equal (same values) - true " ) { REQUIRE ( boolean_expression ( " 12 >= 12 " ) ) ; }
2023-03-20 14:55:18 +00:00
SECTION ( " boolean expression parser: one_of( \" a \" , \" a \" , \" b \" , \" c \" ) " ) { REQUIRE ( boolean_expression ( " one_of( \" a \" , \" a \" , \" b \" , \" c \" ) " ) ) ; }
SECTION ( " boolean expression parser: one_of( \" b \" , \" a \" , \" b \" , \" c \" ) " ) { REQUIRE ( boolean_expression ( " one_of( \" b \" , \" a \" , \" b \" , \" c \" ) " ) ) ; }
SECTION ( " boolean expression parser: one_of( \" c \" , \" a \" , \" b \" , \" c \" ) " ) { REQUIRE ( boolean_expression ( " one_of( \" c \" , \" a \" , \" b \" , \" c \" ) " ) ) ; }
SECTION ( " boolean expression parser: one_of( \" d \" , \" a \" , \" b \" , \" c \" ) " ) { REQUIRE ( ! boolean_expression ( " one_of( \" d \" , \" a \" , \" b \" , \" c \" ) " ) ) ; }
SECTION ( " boolean expression parser: one_of( \" a \" ) " ) { REQUIRE ( ! boolean_expression ( " one_of( \" a \" ) " ) ) ; }
SECTION ( " boolean expression parser: one_of( \" a \" , \" a \" ) " ) { REQUIRE ( boolean_expression ( " one_of( \" a \" , \" a \" ) " ) ) ; }
SECTION ( " boolean expression parser: one_of( \" b \" , \" a \" ) " ) { REQUIRE ( ! boolean_expression ( " one_of( \" b \" , \" a \" ) " ) ) ; }
SECTION ( " boolean expression parser: one_of( \" abcdef \" , /.*c.*/) " ) { REQUIRE ( boolean_expression ( " one_of( \" abcdef \" , /.*c.*/) " ) ) ; }
SECTION ( " boolean expression parser: one_of( \" abcdef \" , /.*f.*/, /.*c.*/) " ) { REQUIRE ( boolean_expression ( " one_of( \" abcdef \" , /.*f.*/, /.*c.*/) " ) ) ; }
SECTION ( " boolean expression parser: one_of( \" abcdef \" , ~ \" .*f.* \" , ~ \" .*c.* \" ) " ) { REQUIRE ( boolean_expression ( " one_of( \" abcdef \" , ~ \" .*f.* \" , ~ \" .*c.* \" ) " ) ) ; }
SECTION ( " boolean expression parser: one_of( \" ghij \" , /.*f.*/, /.*c.*/) " ) { REQUIRE ( ! boolean_expression ( " one_of( \" ghij \" , /.*f.*/, /.*c.*/) " ) ) ; }
SECTION ( " boolean expression parser: one_of( \" ghij \" , ~ \" .*f.* \" , ~ \" .*c.* \" ) " ) { REQUIRE ( ! boolean_expression ( " one_of( \" ghij \" , ~ \" .*f.* \" , ~ \" .*c.* \" ) " ) ) ; }
2020-01-21 11:10:02 +00:00
SECTION ( " complex expression " ) { REQUIRE ( boolean_expression ( " printer_notes=~/.*PRINTER_VENDOR_PRUSA3D.*/ and printer_notes=~/.*PRINTER_MODEL_MK2.*/ and nozzle_diameter[0]==0.6 and num_extruders>1 " ) ) ; }
SECTION ( " complex expression2 " ) { REQUIRE ( boolean_expression ( " printer_notes=~/.*PRINTER_VEwerfNDOR_PRUSA3D.*/ or printer_notes=~/.*PRINTertER_MODEL_MK2.*/ or (nozzle_diameter[0]==0.6 and num_extruders>1) " ) ) ; }
SECTION ( " complex expression3 " ) { REQUIRE ( ! boolean_expression ( " printer_notes=~/.*PRINTER_VEwerfNDOR_PRUSA3D.*/ or printer_notes=~/.*PRINTertER_MODEL_MK2.*/ or (nozzle_diameter[0]==0.3 and num_extruders>1) " ) ) ; }
2023-01-04 12:38:18 +00:00
SECTION ( " enum expression " ) { REQUIRE ( boolean_expression ( " gcode_flavor == \" marlin \" " ) ) ; }
2023-03-20 06:48:26 +00:00
SECTION ( " write to a scalar variable " ) {
DynamicConfig config_outputs ;
config_outputs . set_key_value ( " writable_string " , new ConfigOptionString ( ) ) ;
parser . process ( " {writable_string = \" Written \" } " , 0 , nullptr , & config_outputs , nullptr ) ;
REQUIRE ( parser . process ( " {writable_string} " , 0 , nullptr , & config_outputs , nullptr ) = = " Written " ) ;
}
SECTION ( " write to a vector variable " ) {
DynamicConfig config_outputs ;
config_outputs . set_key_value ( " writable_floats " , new ConfigOptionFloats ( { 0. , 0. , 0. } ) ) ;
parser . process ( " {writable_floats[1] = 33} " , 0 , nullptr , & config_outputs , nullptr ) ;
REQUIRE ( config_outputs . opt_float ( " writable_floats " , 1 ) = = Approx ( 33. ) ) ;
}
2020-01-21 11:10:02 +00:00
}