def setUp(self): self.lexer = GcodeLexer()
def setUp(self): self.parser = GcodeParser() self.lexer = GcodeLexer()
class GcodeLexerTest(unittest.TestCase): def setUp(self): self.lexer = GcodeLexer() def compare_output(self, line, expected): self.assertEqual(self.lexer.scan_line(line), expected) def test_empty(self): line = '' expected = ('', {}, '') self.compare_output(line, expected) def test_whitespace(self): line = '\t ' expected = ('', {}, '') self.compare_output(line, expected) def test_comments(self): comment1 = '; M107 ; turn fan off' expected = ('', {}, comment1) self.compare_output(comment1, expected) comment2 = '(**** begin homing ****)' expected = ('', {}, comment2) self.compare_output(comment2, expected) comment3 = comment2 + comment1 expected = ('', {}, comment3) self.compare_output(comment3, expected) def test_no_args_no_comment(self): line = 'G21' expected = (line, {}, '') self.compare_output(line, expected) def test_no_args(self): line = 'G21 ; set units to millimeters' expected = ('G21', {}, '; set units to millimeters') self.compare_output(line, expected) def test_int_arg(self): line = 'G92 E0 ; reset extrusion distance' expected = ('G92', {'E': 0}, '; reset extrusion distance') self.compare_output(line, expected) def test_multiple_args(self): line = 'G1 X81.430 Y77.020 E1.08502 ; skirt' expected = ('G1', {'X': 81.43, 'Y': 77.02, 'E': 1.08502}, '; skirt') self.compare_output(line, expected) def test_slic3r_file(self): fname = 'tests/data/gcode/slic3r.gcode' with open(fname, 'r') as f: self.lexer.load(f) result = list(self.lexer.scan()) def test_skeinforge_file(self): fname = 'tests/data/gcode/top.gcode' with open(fname, 'r') as f: self.lexer.load(f) result = list(self.lexer.scan()) def test_string_input(self): s = """ M108 S255 (set extruder speed to maximum) M104 S205 T0 (set extruder temperature) M109 S105 T0 (set heated-build-platform temperature) """ self.lexer.load(s) result = list(self.lexer.scan()) self.assertEqual(len(result), 3)
class GcodeParserTest(unittest.TestCase): def setUp(self): self.parser = GcodeParser() self.lexer = GcodeLexer() def test_args_dict(self): xyz = (7.27, 2.67, 0.91) x, y, z = xyz args = ArgsDict({'X': x, 'Y': y}) self.assertEqual(args['X'], x) self.assertEqual(args['Y'], y) self.assertIsNone(args['Z']) def test_command_coords(self): xyz = (7.27, 2.67, 0.91) x, y, z = xyz gcode = 'G1' args = ArgsDict({'X': x, 'Y': y, 'Z': z}) coords = self.parser.command_coords(gcode, args, args) self.assertEqual(coords, xyz) gcode = '' coords = self.parser.command_coords(gcode, args, args) self.assertIsNone(coords) def test_is_new_layer(self): dst = (0, 0, 0) is_new_layer = self.parser.is_new_layer(dst, '', '') self.assertFalse(is_new_layer) is_new_layer = self.parser.is_new_layer(dst, '', '(<layer> 1.272 )') self.assertTrue(is_new_layer) self.parser.src = (0, 0, 0) dst = (0, 0, 0.2) is_new_layer = self.parser.is_new_layer(dst, 'G1', '') self.assertTrue(is_new_layer) def test_set_flags(self): commands = ( ('', ArgsDict(), '(<perimeter> outer )'), ('', ArgsDict(), '(<loop> outer )'), ('', ArgsDict(), '(</loop>)'), ('M101', ArgsDict(), ''), ('M103', ArgsDict(), ''), ('M101', ArgsDict(), ''), ) for command in commands: self.parser.set_flags(command) flags = (Movement.FLAG_PERIMETER | Movement.FLAG_PERIMETER_OUTER | Movement.FLAG_EXTRUDER_ON) self.assertEqual(self.parser.flags, flags) def test_parse(self): gcode = """ G1 Z0.400 F12000.000 ; move to next layer G1 X78.810 Y79.100 ; move to first skirt point G1 F600.000 E1.00000 ; compensate retraction G1 X79.600 Y78.350 F1482.000 E1.02761 ; skirt """ self.parser.load(gcode) result = self.parser.parse() self.assertEqual(len(result), 1) self.assertEqual(len(result[0]), 3) def test_update_args(self): oldargs = ArgsDict({'X': 0, 'Y': 0, 'Z': 0, 'F': 12000, 'E': 0}) args = self.parser.update_args(oldargs, {'X': 1, 'Y': 1}) self.assertEqual(args['X'], 1) self.assertEqual(args['Y'], 1) self.assertEqual(args['F'], 12000) def test_parse_new_layer_from_z(self): gcode = """ G1 X-1.06 Y13.13 Z0.91 F1440.0 G1 X-1.06 Y13.18 Z0.91 F1440.0 M103 G1 X-1.09 Y13.49 Z1.27 F1920.0 M101 G1 X-2.84 Y13.3 Z1.27 F1920.0 """ self.parser.load(gcode) result = self.parser.parse() self.assertEqual(len(result), 2) self.assertEqual(len(result[0]), 3) self.assertEqual(len(result[1]), 1) def test_cura(self): gcode = """ M117 Printing stuff now... """ self.lexer.load(gcode) for command, args, comment in self.lexer.scan(): self.assertEqual(command, 'M117') self.assertEqual(len(args), 0) self.assertEqual(comment.strip(), 'Printing stuff now...')