PlaceholderParser extended with {if}/{elsif}{else} blocks and

+ - * / == != <> numeric expressions.
This commit is contained in:
bubnikv 2017-11-26 09:59:14 +01:00
parent 9205c8aab4
commit 708f416c84
4 changed files with 485 additions and 196 deletions

View file

@ -1,4 +1,4 @@
use Test::More tests => 15;
use Test::More tests => 39;
use strict;
use warnings;
@ -51,7 +51,10 @@ use Slic3r::Test;
$parser->set('foo' => 0);
is $parser->process('[temperature_[foo]]'),
$config->temperature->[0],
"nested config options";
"nested config options (legacy syntax)";
is $parser->process('{temperature[foo]}'),
$config->temperature->[0],
"array reference";
}
{
@ -90,20 +93,39 @@ use Slic3r::Test;
ok $gcode =~ /M104 S205 T1/, 'temperature set correctly for second extruder';
}
$config->set('start_gcode', qq!
my @start_gcode = (qq!
;__temp0:[first_layer_temperature_0]__
;__temp1:[first_layer_temperature_1]__
;__temp2:[first_layer_temperature_2]__
!, qq!
;__temp0:{first_layer_temperature[0]}__
;__temp1:{first_layer_temperature[1]}__
;__temp2:{first_layer_temperature[2]}__
!);
my @syntax_description = (' (legacy syntax)', ' (new syntax)');
for my $i (0, 1) {
$config->set('start_gcode', $start_gcode[$i]);
{
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $gcode = Slic3r::Test::gcode($print);
# we use the [infill_extruder] placeholder to make sure this test doesn't
# catch a false positive caused by the unparsed start G-code option itself
# being embedded in the G-code
ok $gcode =~ /temp0:200/, 'temperature placeholder for first extruder correctly populated' . $syntax_description[$i];
ok $gcode =~ /temp1:205/, 'temperature placeholder for second extruder correctly populated' . $syntax_description[$i];
ok $gcode =~ /temp2:200/, 'temperature placeholder for unused extruder populated with first value' . $syntax_description[$i];
}
}
$config->set('start_gcode', qq!
;substitution:{if infill_extruder==1}extruder1
{elsif infill_extruder==2}extruder2
{else}extruder3{endif}
!);
{
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $gcode = Slic3r::Test::gcode($print);
# we use the [infill_extruder] placeholder to make sure this test doesn't
# catch a false positive caused by the unparsed start G-code option itself
# being embedded in the G-code
ok $gcode =~ /temp0:200/, 'temperature placeholder for first extruder correctly populated';
ok $gcode =~ /temp1:205/, 'temperature placeholder for second extruder correctly populated';
ok $gcode =~ /temp2:200/, 'temperature placeholder for unused extruder populated with first value';
ok $gcode =~ /substitution:extruder1/, 'if / else / endif - first block returned';
}
}
@ -132,4 +154,57 @@ use Slic3r::Test;
'layer_num grows continously'; # i.e. no duplicates or regressions
}
{
my $config = Slic3r::Config->new;
$config->set('start_gcode', qq!
;substitution:{if infill_extruder==1}if block
{elsif infill_extruder==2}elsif block 1
{elsif infill_extruder==3}elsif block 2
{elsif infill_extruder==4}elsif block 3
{else}endif block{endif}
!);
my @returned = ('', 'if block', 'elsif block 1', 'elsif block 2', 'elsif block 3', 'endif block');
for my $i (1,2,3,4,5) {
$config->set('infill_extruder', $i);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $gcode = Slic3r::Test::gcode($print);
my $found_other = 0;
for my $j (1,2,3,4,5) {
next if $i == $j;
$found_other = 1 if $gcode =~ /substitution:$returned[$j]/;
}
ok $gcode =~ /substitution:$returned[$i]/, 'if / else / endif - ' . $returned[$i] . ' returned';
ok !$found_other, 'if / else / endif - only ' . $returned[$i] . ' returned';
}
}
{
my $config = Slic3r::Config->new;
$config->set('start_gcode',
';substitution:{if infill_extruder==1}{if perimeter_extruder==1}block11{else}block12{endif}' .
'{elsif infill_extruder==2}{if perimeter_extruder==1}block21{else}block22{endif}' .
'{else}{if perimeter_extruder==1}block31{else}block32{endif}{endif}:end');
for my $i (1,2,3) {
$config->set('infill_extruder', $i);
for my $j (1,2) {
$config->set('perimeter_extruder', $j);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $gcode = Slic3r::Test::gcode($print);
ok $gcode =~ /substitution:block$i$j:end/, "two level if / else / endif - block$i$j returned";
}
}
}
{
my $config = Slic3r::Config->new;
$config->set('start_gcode',
';substitution:{if notes=="MK2"}MK2{elsif notes=="MK3"}MK3{else}MK1{endif}:end');
for my $printer_name ("MK2", "MK3", "MK1") {
$config->set('notes', $printer_name);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $gcode = Slic3r::Test::gcode($print);
ok $gcode =~ /substitution:$printer_name:end/, "printer name $printer_name matched";
}
}
__END__