<commit_before>use Test::More tests => 48;
use strict;
use warnings;

BEGIN {
    use FindBin;
    use lib "$FindBin::Bin/../lib";
    use local::lib "$FindBin::Bin/../local-lib";
}

use List::Util qw(first);
use Slic3r;
use Slic3r::Test;

{
    my $config = Slic3r::Config::new_from_defaults;
    
    my $test = sub {
        my ($conf) = @_;
        $conf ||= $config;
        
        my $print = Slic3r::Test::init_print('2x20x10', config => $conf);
        
        my $last_move_was_z_change = 0;
        Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
            my ($self, $cmd, $args, $info) = @_;
            
            if ($last_move_was_z_change && $cmd ne $config->layer_gcode) {
                fail 'custom layer G-code was not applied after Z change';
            }
            if (!$last_move_was_z_change && $cmd eq $config->layer_gcode) {
                fail 'custom layer G-code was not applied after Z change';
            }
            
            $last_move_was_z_change = (defined $info->{dist_Z} && $info->{dist_Z} > 0);
        });
        
        1;
    };
    
    $config->set('start_gcode', '_MY_CUSTOM_START_GCODE_');  # to avoid dealing with the nozzle lift in start G-code
    $config->set('layer_gcode', '_MY_CUSTOM_LAYER_GCODE_');
    ok $test->(), "custom layer G-code is applied after Z move and before other moves";
}

#==========================================================

{
    my $parser = Slic3r::GCode::PlaceholderParser->new;
    $parser->apply_config(my $config = Slic3r::Config::new_from_defaults);
    $parser->set('foo' => 0);
    $parser->set('bar' => 2);
    is $parser->process('[temperature_[foo]]'),
        $config->temperature->[0],
        "nested config options (legacy syntax)";
    is $parser->process('{temperature[foo]}'),
        $config->temperature->[0],
        "array reference";
    is $parser->process("test [ temperature_ [foo] ] \n hu"),
        "test " . $config->temperature->[0] . " \n hu",
        "whitespaces and newlines are maintained";
    is $parser->process('{2*3}'),     '6',   'math: 2*3';
    is $parser->process('{2*3/6}'),   '1',   'math: 2*3/6';
    is $parser->process('{2*3/12}'),  '0',   'math: 2*3/12';
    is $parser->process('{2.*3/12}'), '0.5', 'math: 2.*3/12';
    is $parser->process('{2*(3-12)}'), '-18', 'math: 2*(3-12)';
    is $parser->process('{2*foo*(3-12)}'), '0', 'math: 2*foo*(3-12)';
    is $parser->process('{2*bar*(3-12)}'), '-36', 'math: 2*bar*(3-12)';
}

{
    my $config = Slic3r::Config::new_from_defaults;
    $config->set('output_filename_format', 'ts_[travel_speed]_lh_[layer_height].gcode');
    $config->set('start_gcode', "TRAVEL:[travel_speed] HEIGHT:[layer_height]\n");
    my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
    
    my $output_file = $print->print->output_filepath;
    my ($t, $h) = map $config->$_, qw(travel_speed layer_height);
    ok $output_file =~ /ts_${t}_/, 'print config options are replaced in output filename';<commit_msg>Fixed a failing PlaceholderParser test case.<commit_after>use Test::More tests => 48;
use strict;
use warnings;

BEGIN {
    use FindBin;
    use lib "$FindBin::Bin/../lib";
    use local::lib "$FindBin::Bin/../local-lib";
}

use List::Util qw(first);
use Slic3r;
use Slic3r::Test;

{
    my $config = Slic3r::Config::new_from_defaults;
    
    my $test = sub {
        my ($conf) = @_;
        $conf ||= $config;
        
        my $print = Slic3r::Test::init_print('2x20x10', config => $conf);
        
        my $last_move_was_z_change = 0;
        Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
            my ($self, $cmd, $args, $info) = @_;
            
            if ($last_move_was_z_change && $cmd ne $config->layer_gcode) {
                fail 'custom layer G-code was not applied after Z change';
            }
            if (!$last_move_was_z_change && $cmd eq $config->layer_gcode) {
                fail 'custom layer G-code was not applied after Z change';
            }
            
            $last_move_was_z_change = (defined $info->{dist_Z} && $info->{dist_Z} > 0);
        });
        
        1;
    };
    
    $config->set('start_gcode', '_MY_CUSTOM_START_GCODE_');  # to avoid dealing with the nozzle lift in start G-code
    $config->set('layer_gcode', '_MY_CUSTOM_LAYER_GCODE_');
    ok $test->(), "custom layer G-code is applied after Z move and before other moves";
}

#==========================================================

{
    my $parser = Slic3r::GCode::PlaceholderParser->new;
    $parser->apply_config(my $config = Slic3r::Config::new_from_defaults);
    $parser->set('foo' => 0);
    $parser->set('bar' => 2);
    is $parser->process('[temperature_[foo]]'),
        $config->temperature->[0],
        "nested config options (legacy syntax)";
    is $parser->process('{temperature[foo]}'),
        $config->temperature->[0],
        "array reference";
    is $parser->process("test [ temperature_ [foo] ] \n hu"),
        "test " . $config->temperature->[0] . " \n hu",
        "whitespaces and newlines are maintained";
    is $parser->process('{2*3}'),     '6',   'math: 2*3';
    is $parser->process('{2*3/6}'),   '1',   'math: 2*3/6';
    is $parser->process('{2*3/12}'),  '0',   'math: 2*3/12';
    ok abs($parser->process('{2.*3/12}') - 0.5) < 1e-7, 'math: 2.*3/12';
    is $parser->process('{2*(3-12)}'), '-18', 'math: 2*(3-12)';
    is $parser->process('{2*foo*(3-12)}'), '0', 'math: 2*foo*(3-12)';
    is $parser->process('{2*bar*(3-12)}'), '-36', 'math: 2*bar*(3-12)';
}

{
    my $config = Slic3r::Config::new_from_defaults;
    $config->set('output_filename_format', 'ts_[travel_speed]_lh_[layer_height].gcode');
    $config->set('start_gcode', "TRAVEL:[travel_speed] HEIGHT:[layer_height]\n");
    my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
    
    my $output_file = $print->print->output_filepath;
    my ($t, $h) = map $config->$_, qw(travel_speed layer_height);
    ok $output_file =~ /ts_${t}_/, 'print config options are replaced in output filename';
{'fraction_non_alphanumeric': 0.2119172685489166, 'fraction_numerical': 0.028890347997373604, 'mean_word_length': 2.998031496062992, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '758988', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2142, 'n_words': 445}