15_config.t 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Slic3r::XS;
  5. use Test::More tests => 143;
  6. foreach my $config (Slic3r::Config->new, Slic3r::Config::Static::new_FullPrintConfig) {
  7. $config->set('layer_height', 0.3);
  8. ok abs($config->get('layer_height') - 0.3) < 1e-4, 'set/get float';
  9. is $config->opt_serialize('layer_height'), '0.3', 'serialize float';
  10. $config->set('perimeters', 2);
  11. is $config->get('perimeters'), 2, 'set/get int';
  12. is $config->opt_serialize('perimeters'), '2', 'serialize int';
  13. $config->set('extrusion_axis', 'A');
  14. is $config->get('extrusion_axis'), 'A', 'set/get string';
  15. is $config->opt_serialize('extrusion_axis'), 'A', 'serialize string';
  16. $config->set('notes', "foo\nbar");
  17. is $config->get('notes'), "foo\nbar", 'set/get string with newline';
  18. is $config->opt_serialize('notes'), 'foo\nbar', 'serialize string with newline';
  19. $config->set_deserialize('notes', 'bar\nbaz');
  20. is $config->get('notes'), "bar\nbaz", 'deserialize string with newline';
  21. foreach my $test_data (
  22. {
  23. name => 'empty',
  24. values => [],
  25. serialized => ''
  26. },
  27. {
  28. name => 'single empty',
  29. values => [''],
  30. serialized => '""'
  31. },
  32. {
  33. name => 'single noempty, simple',
  34. values => ['RGB'],
  35. serialized => 'RGB'
  36. },
  37. {
  38. name => 'multiple noempty, simple',
  39. values => ['ABC', 'DEF', '09182745@!#$*(&'],
  40. serialized => 'ABC;DEF;09182745@!#$*(&'
  41. },
  42. {
  43. name => 'multiple, simple, some empty',
  44. values => ['ABC', 'DEF', '', '09182745@!#$*(&', ''],
  45. serialized => 'ABC;DEF;;09182745@!#$*(&;'
  46. },
  47. {
  48. name => 'complex',
  49. values => ['some "quoted" notes', "yet\n some notes", "whatever \n notes", ''],
  50. serialized => '"some \"quoted\" notes";"yet\n some notes";"whatever \n notes";'
  51. }
  52. )
  53. {
  54. $config->set('filament_notes', $test_data->{values});
  55. is $config->opt_serialize('filament_notes'), $test_data->{serialized}, 'serialize multi-string value ' . $test_data->{name};
  56. $config->set_deserialize('filament_notes', '');
  57. is_deeply $config->get('filament_notes'), [], 'deserialize multi-string value - empty ' . $test_data->{name};
  58. $config->set_deserialize('filament_notes', $test_data->{serialized});
  59. is_deeply $config->get('filament_notes'), $test_data->{values}, 'deserialize complex multi-string value ' . $test_data->{name};
  60. }
  61. $config->set('first_layer_height', 0.3);
  62. ok abs($config->get('first_layer_height') - 0.3) < 1e-4, 'set/get absolute floatOrPercent';
  63. is $config->opt_serialize('first_layer_height'), '0.3', 'serialize absolute floatOrPercent';
  64. # This is no more supported after first_layer_height was moved from PrintObjectConfig to PrintConfig.
  65. # $config->set('first_layer_height', $config->get('layer_height'));
  66. # $config->get_abs_value('first_layer_height');
  67. # ok abs($config->get_abs_value('first_layer_height') - 0.15) < 1e-4, 'set/get relative floatOrPercent';
  68. # is $config->opt_serialize('first_layer_height'), '50%', 'serialize relative floatOrPercent';
  69. # Uh-oh, we have no point option to test at the moment
  70. #ok $config->set('print_center', [50,80]), 'valid point coordinates';
  71. #is_deeply $config->get('print_center'), [50,80], 'set/get point';
  72. #is $config->serialize('print_center'), '50,80', 'serialize point';
  73. #$config->set_deserialize('print_center', '20,10');
  74. #is_deeply $config->get('print_center'), [20,10], 'deserialize point';
  75. #ok !$config->set('print_center', ['t',80]), 'invalid point X';
  76. #ok !$config->set('print_center', [50,'t']), 'invalid point Y';
  77. $config->set('use_relative_e_distances', 1);
  78. is $config->get('use_relative_e_distances'), 1, 'set/get bool';
  79. is $config->opt_serialize('use_relative_e_distances'), '1', 'serialize bool';
  80. $config->set('gcode_flavor', 'teacup');
  81. is $config->get('gcode_flavor'), 'teacup', 'set/get enum';
  82. is $config->opt_serialize('gcode_flavor'), 'teacup', 'serialize enum';
  83. $config->set_deserialize('gcode_flavor', 'mach3');
  84. is $config->get('gcode_flavor'), 'mach3', 'deserialize enum (gcode_flavor)';
  85. $config->set_deserialize('gcode_flavor', 'machinekit');
  86. is $config->get('gcode_flavor'), 'machinekit', 'deserialize enum (gcode_flavor)';
  87. $config->set_deserialize('fill_pattern', 'line');
  88. is $config->get('fill_pattern'), 'line', 'deserialize enum (fill_pattern)';
  89. $config->set_deserialize('support_material_pattern', 'rectilinear');
  90. is $config->get('support_material_pattern'), 'rectilinear', 'deserialize enum (support_material_pattern)';
  91. $config->set('extruder_offset', [[10,20],[30,45]]);
  92. is_deeply [ map $_->pp, @{$config->get('extruder_offset')} ], [[10,20],[30,45]], 'set/get points';
  93. $config->set('extruder_offset', [Slic3r::Pointf->new(10,20),Slic3r::Pointf->new(30,45)]);
  94. is_deeply [ map $_->pp, @{$config->get('extruder_offset')} ], [[10,20],[30,45]], 'set/get points';
  95. is $config->opt_serialize('extruder_offset'), '10x20,30x45', 'serialize points';
  96. $config->set_deserialize('extruder_offset', '20x10');
  97. is_deeply [ map $_->pp, @{$config->get('extruder_offset')} ], [[20,10]], 'deserialize points';
  98. $config->set_deserialize('extruder_offset', '0x0');
  99. is_deeply [ map $_->pp, @{$config->get('extruder_offset')} ], [[0,0]], 'deserialize points';
  100. {
  101. my @values = ([10,20]);
  102. $values[2] = [10,20]; # implicitely extend array; this is not the same as explicitely assigning undef to second item
  103. ok !$config->set('extruder_offset', \@values), 'reject undef points';
  104. }
  105. # truncate ->get() to first decimal digit
  106. $config->set('nozzle_diameter', [0.2,3]);
  107. is_deeply [ map int($_*10)/10, @{$config->get('nozzle_diameter')} ], [0.2,3], 'set/get floats';
  108. is $config->opt_serialize('nozzle_diameter'), '0.2,3', 'serialize floats';
  109. $config->set_deserialize('nozzle_diameter', '0.1,0.4');
  110. is_deeply [ map int($_*10)/10, @{$config->get('nozzle_diameter')} ], [0.1,0.4], 'deserialize floats';
  111. $config->set_deserialize('nozzle_diameter', '3');
  112. is_deeply [ map int($_*10)/10, @{$config->get('nozzle_diameter')} ], [3], 'deserialize a single float';
  113. {
  114. my @values = (0.4);
  115. $values[2] = 2; # implicitely extend array; this is not the same as explicitely assigning undef to second item
  116. ok !$config->set('nozzle_diameter', \@values), 'reject undef floats';
  117. }
  118. $config->set('temperature', [180,210]);
  119. is_deeply $config->get('temperature'), [180,210], 'set/get ints';
  120. is $config->opt_serialize('temperature'), '180,210', 'serialize ints';
  121. $config->set_deserialize('temperature', '195,220');
  122. is_deeply $config->get('temperature'), [195,220], 'deserialize ints';
  123. {
  124. my @values = (180);
  125. $values[2] = 200; # implicitely extend array; this is not the same as explicitely assigning undef to second item
  126. ok !$config->set('temperature', \@values), 'reject undef ints';
  127. }
  128. $config->set('wipe', [1,0]);
  129. is_deeply $config->get('wipe'), [1,0], 'set/get bools';
  130. is $config->get_at('wipe', 0), 1, 'get_at bools';
  131. is $config->get_at('wipe', 1), 0, 'get_at bools';
  132. is $config->get_at('wipe', 9), 1, 'get_at bools';
  133. is $config->opt_serialize('wipe'), '1,0', 'serialize bools';
  134. $config->set_deserialize('wipe', '0,1,1');
  135. is_deeply $config->get('wipe'), [0,1,1], 'deserialize bools';
  136. $config->set_deserialize('wipe', '');
  137. is_deeply $config->get('wipe'), [], 'deserialize bools from empty string';
  138. $config->set_deserialize('retract_layer_change', 0);
  139. is_deeply $config->get('retract_layer_change'), [0], 'deserialize bools from non-string value';
  140. {
  141. my @values = (1);
  142. $values[2] = 1; # implicitely extend array; this is not the same as explicitely assigning undef to second item
  143. ok !$config->set('wipe', \@values), 'reject undef bools';
  144. }
  145. $config->set('post_process', ['foo','bar']);
  146. is_deeply $config->get('post_process'), ['foo','bar'], 'set/get strings';
  147. is $config->opt_serialize('post_process'), 'foo;bar', 'serialize strings';
  148. $config->set_deserialize('post_process', 'bar;baz');
  149. is_deeply $config->get('post_process'), ['bar','baz'], 'deserialize strings';
  150. {
  151. my @values = ('foo');
  152. $values[2] = 'bar'; # implicitely extend array; this is not the same as explicitely assigning undef to second item
  153. ok !$config->set('post_process', \@values), 'reject undef strings';
  154. }
  155. is_deeply [ sort @{$config->get_keys} ], [ sort keys %{$config->as_hash} ], 'get_keys and as_hash';
  156. }
  157. {
  158. my $config = Slic3r::Config->new;
  159. $config->set('perimeters', 2);
  160. # test that no crash happens when using set_deserialize() with a key that hasn't been set() yet
  161. $config->set_deserialize('filament_diameter', '3');
  162. my $config2 = Slic3r::Config::Static::new_FullPrintConfig;
  163. $config2->apply_dynamic($config);
  164. is $config2->get('perimeters'), 2, 'apply_dynamic';
  165. }
  166. {
  167. my $config = Slic3r::Config::Static::new_FullPrintConfig;
  168. my $config2 = Slic3r::Config->new;
  169. $config2->apply_static($config);
  170. is $config2->get('perimeters'), Slic3r::Config::print_config_def()->{perimeters}{default}, 'apply_static and print_config_def';
  171. $config->set('top_solid_infill_speed', 70);
  172. is $config->get_abs_value('top_solid_infill_speed'), 70, 'get_abs_value() works when ratio_over references a floatOrPercent option';
  173. }
  174. {
  175. my $config = Slic3r::Config->new;
  176. $config->set('fill_pattern', 'line');
  177. my $config2 = Slic3r::Config->new;
  178. $config2->set('fill_pattern', 'hilbertcurve');
  179. is $config->get('fill_pattern'), 'line', 'no interferences between DynamicConfig objects';
  180. }
  181. {
  182. my $config = Slic3r::Config->new;
  183. # the pair [0,0] is part of the test, since it checks whether the 0x0 serialized value is correctly parsed
  184. $config->set('extruder_offset', [ [0,0], [20,0], [0,20] ]);
  185. my $config2 = Slic3r::Config->new;
  186. $config2->apply($config);
  187. is_deeply [ map $_->pp, @{$config->get('extruder_offset')} ], [ map $_->pp, @{$config2->get('extruder_offset')} ],
  188. 'apply dynamic over dynamic';
  189. }
  190. {
  191. my $config = Slic3r::Config->new;
  192. $config->set('extruder', 2);
  193. $config->set('perimeter_extruder', 3);
  194. $config->normalize_fdm;
  195. ok !$config->has('extruder'), 'extruder option is removed after normalize()';
  196. is $config->get('infill_extruder'), 2, 'undefined extruder is populated with default extruder';
  197. is $config->get('perimeter_extruder'), 3, 'defined extruder is not overwritten by default extruder';
  198. }
  199. {
  200. my $config = Slic3r::Config->new;
  201. $config->set('infill_extruder', 2);
  202. $config->normalize_fdm;
  203. is $config->get('solid_infill_extruder'), 2, 'undefined solid infill extruder is populated with infill extruder';
  204. }
  205. {
  206. my $config = Slic3r::Config->new;
  207. $config->set('spiral_vase', 1);
  208. $config->set('retract_layer_change', [1,0]);
  209. $config->normalize_fdm;
  210. is_deeply $config->get('retract_layer_change'), [0,0], 'retract_layer_change is disabled with spiral_vase';
  211. }
  212. {
  213. use Cwd qw(abs_path);
  214. use File::Basename qw(dirname);
  215. my $path = abs_path($0);
  216. my $config = Slic3r::Config::load(dirname($path)."/inc/22_config_bad_config_options.ini");
  217. ok 1, 'did not crash on reading invalid items in config';
  218. }
  219. __END__