15_config.t 11 KB

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