Coverage for tests/integration/test_gcodeparser.py: 99%
74 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-20 05:56 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-20 05:56 +0000
1import array
2import unittest
3from tatlin.lib.model.gcode.parser import (
4 GcodeParser,
5 GcodeLexer,
6 GcodeParserError,
7 Movement,
8 ArgsDict,
9)
12class GcodeParserTest(unittest.TestCase):
13 def setUp(self):
14 self.parser = GcodeParser()
15 self.lexer = GcodeLexer()
17 def test_movement(self):
18 m = Movement(array.array("f", [0, 0.5, 0]), 0, 0, 0)
19 self.assertEqual(str(m), "(array('f', [0.0, 0.5, 0.0]))")
20 self.assertEqual(repr(m), "Movement(array('f', [0.0, 0.5, 0.0]), 0, 0, 0)")
22 def test_args_dict(self):
23 xyz = (7.27, 2.67, 0.91)
24 x, y, z = xyz
26 args = ArgsDict({"X": x, "Y": y})
28 self.assertEqual(args["X"], x)
29 self.assertEqual(args["Y"], y)
30 self.assertIsNone(args["Z"])
32 def test_command_coords(self):
33 xyz = (7.27, 2.67, 0.91)
34 x, y, z = xyz
36 gcode = "G1"
37 args = ArgsDict({"X": x, "Y": y, "Z": z})
38 coords = self.parser.command_coords(gcode, args, args)
39 self.assertEqual(coords, xyz)
41 gcode = ""
42 coords = self.parser.command_coords(gcode, args, args)
43 self.assertIsNone(coords)
45 def test_set_flags(self):
46 commands = (
47 ("", ArgsDict(), "(<perimeter> outer )"),
48 ("", ArgsDict(), "(<loop> outer )"),
49 ("", ArgsDict(), "(</loop>)"),
50 ("M101", ArgsDict(), ""),
51 ("M103", ArgsDict(), ""),
52 ("M101", ArgsDict(), ""),
53 )
54 for command in commands:
55 self.parser.set_flags(command)
57 flags = (
58 Movement.FLAG_PERIMETER
59 | Movement.FLAG_PERIMETER_OUTER
60 | Movement.FLAG_EXTRUDER_ON
61 )
62 self.assertEqual(self.parser.flags, flags)
64 def test_parse(self):
65 gcode = """
66 G1 Z0.000 F12000.000 ; move to first layer
67 G1 X78.810 Y79.100 ; move to first skirt point
68 G1 F600.000 E1.00000 ; compensate retraction
69 G1 X79.600 Y78.350 F1482.000 E1.02761 ; skirt
70 """
71 self.parser.load(gcode)
72 result = self.parser.parse()
74 self.assertEqual(len(result), 1)
75 self.assertEqual(len(result[0]), 3)
77 def test_update_args(self):
78 oldargs = ArgsDict({"X": 0, "Y": 0, "Z": 0, "F": 12000, "E": 0})
79 args = self.parser.update_args(oldargs, {"X": 1, "Y": 1})
81 self.assertEqual(args["X"], 1)
82 self.assertEqual(args["Y"], 1)
83 self.assertEqual(args["F"], 12000)
85 def test_parse_new_layer_from_z(self):
86 gcode = """
87 G1 X-1.06 Y13.12 Z0.00 F1440.0 E0.000
88 G1 X-1.06 Y13.25 Z0.00 F1440.0 E0.025
89 G1 X-1.06 Y13.37 Z0.00 F1440.0 E0.050
90 M103
91 G1 X-1.09 Y13.49 Z0.36 F1920.0 E0.075
92 M101
93 G1 X-2.84 Y13.3 Z0.36 F1920.0 E0.100
94 """
95 self.parser.load(gcode)
96 result = self.parser.parse()
98 self.assertEqual(len(result), 2)
99 self.assertEqual(len(result[0]), 3)
100 self.assertEqual(len(result[1]), 2)
102 def test_cura(self):
103 gcode = """
104 M117 Printing stuff now...
105 """
106 self.lexer.load(gcode)
107 for command, args, comment in self.lexer.scan():
108 self.assertEqual(command, "M117")
109 self.assertEqual(len(args), 0)
110 self.assertEqual(comment.strip(), "Printing stuff now...")
112 def test_slic3r(self):
113 gcode = """
114 ; generated by Slic3r 0.6.1-dev on 2012-02-25 at 07:01:16
115 G1 Z0.400 F12000.000 ; move to next layer
116 G1 X78.810 Y79.100 ; move to first skirt point
117 G1 F600.000 E1.00000 ; compensate retraction
118 G1 X79.600 Y78.350 F1482.000 E1.02761 ; skirt
119 G1 X92.780 Y92.780 F12000.000 ; move to first perimeter point
120 """
121 self.parser.load(gcode)
122 self.parser.parse()
124 def test_invalid_gcode(self):
125 with self.assertRaises(GcodeParserError):
126 self.parser.load("")
127 self.parser.parse()
129 def test_flags(self):
130 gcode = """
131 G20 ; inches
132 G28 ; move to origin
133 G28 X0 Y0 Z0 ; move to origin with coords
134 G91 ; relative positioning
135 G92 ; set position to 0, 0, 0
136 G1 Z0.400 F12000.000
137 G1 X78.810 Y79.100
138 G1 F600.000 E1.00000
139 G1 X79.600 Y78.350 F1482.000 E1.02761
140 """
141 self.parser.load(gcode)
142 self.parser.parse()
145if __name__ == "__main__":
146 unittest.main()