import unittest from GcodeFileSplitter import GcodeFileAnalyzer dummyGcode = [ 'header01', 'header02', ';Generated with Cura_SteamEngine 3.5.1', ';LAYER_COUNT:4', ';LAYER:0', 'G0 Z0.3', 'G1 E0', ';TIME_ELAPSED:1.11', ';LAYER:1', 'M106 S255', 'G0 Z0.5', 'G1 E11.11', ';TIME_ELAPSED:2.11', ';LAYER:2', 'G0 Z0.7', 'G1 E22.22', ';TIME_ELAPSED:3.11', ';LAYER:3', 'G0 Z0.9', 'G1 E33.33', ';TIME_ELAPSED:4.11', 'G1 F1500 E30', 'footer02', ] class GcodeTest(unittest.TestCase): def test_Analyzer(self): gcode = GcodeFileAnalyzer(dummyGcode) self.assertEqual(gcode.header, ['header01', 'header02', ';Generated with Cura_SteamEngine 3.5.1', ';LAYER_COUNT:4']) self.assertEqual(gcode.footer, ['G1 F1500 E30', 'footer02']) self.assertEqual(gcode.layers[0].lines, [';LAYER:0', 'G0 Z0.3', 'G1 E0', ';TIME_ELAPSED:1.11',]) self.assertEqual(gcode.layers[1].lines, [';LAYER:1', 'M106 S255', 'G0 Z0.5', 'G1 E11.11', ';TIME_ELAPSED:2.11',]) self.assertEqual(gcode.layers[2].lines, [';LAYER:2', 'G0 Z0.7', 'G1 E22.22', ';TIME_ELAPSED:3.11',]) self.assertEqual(gcode.layers[3].lines, [';LAYER:3', 'G0 Z0.9', 'G1 E33.33', ';TIME_ELAPSED:4.11',]) self.assertEqual(gcode.layerCount, 4) self.assertEqual(gcode.fanSpeedCode, 'M106 S255') self.assertEqual(gcode.layerCount, len(gcode.layers)) def test_SplitInTwo(self): gcode = GcodeFileAnalyzer(dummyGcode) lines1, lines2 = gcode.ExportSplitted([3]) # Check first file # header + layer 1 and 2 self.assertEqual(lines1[:lines1.index(';TIME_ELAPSED:2.11')], dummyGcode[:dummyGcode.index(';TIME_ELAPSED:2.11')]) # footer self.assertEqual(lines1[-6:], ['', ';PATCHED_FOOTER', 'G1 F1500 E4.610', ''] + ['; ' + dummyGcode[-2]] + dummyGcode[-1:]) # Check second file # header self.assertEqual(lines2[:lines2.index(';LAYER_COUNT:4')], dummyGcode[:dummyGcode.index(';LAYER_COUNT:4')]) # extra init code self.assertEqual(lines2[lines2.index(';LAYER_COUNT:4') + 1:lines2.index(';LAYER:2')], [ '', ';GENERATED_EXTRA_INIT', 'M106 S255', 'G0 Z1.700', 'G92 E12.220', '' ]) # layer 3 and 4 + footer self.assertEqual(lines2[lines2.index(';LAYER:2'):], dummyGcode[dummyGcode.index(';LAYER:2'):-2] + ['G1 F1500 E30', 'footer02']) def test_SplitInThree(self): gcode = GcodeFileAnalyzer(dummyGcode) lines1, lines2, lines3 = gcode.ExportSplitted([3, 4]) # Check first file # header + layer 1 and 2 self.assertEqual(lines1[:lines1.index(';TIME_ELAPSED:2.11')], dummyGcode[:dummyGcode.index(';TIME_ELAPSED:2.11')]) # footer self.assertEqual(lines1[-6:], ['', ';PATCHED_FOOTER', 'G1 F1500 E4.610', ''] + ['; ' + dummyGcode[-2]] + dummyGcode[-1:]) # Check second file # header self.assertEqual(lines2[:lines2.index(';LAYER_COUNT:4')], dummyGcode[:dummyGcode.index(';LAYER_COUNT:4')]) # extra init code self.assertEqual(lines2[lines2.index(';LAYER_COUNT:4') + 1:lines2.index(';LAYER:2')], [ '', ';GENERATED_EXTRA_INIT', 'M106 S255', 'G0 Z1.700', 'G92 E12.220', '' ]) # layer 3 self.assertEqual(lines2[lines2.index(';LAYER:2'):], dummyGcode[dummyGcode.index(';LAYER:2'):dummyGcode.index(';LAYER:3')] + ['', ';PATCHED_FOOTER', 'G1 F1500 E15.720', ''] + ['; G1 F1500 E30', 'footer02']) # Check third file # header self.assertEqual(lines3[:lines3.index(';LAYER_COUNT:4')], dummyGcode[:dummyGcode.index(';LAYER_COUNT:4')]) # extra init code self.assertEqual(lines3[lines3.index(';LAYER_COUNT:4') + 1:lines3.index(';LAYER:3')], [ '', ';GENERATED_EXTRA_INIT', 'M106 S255', 'G0 Z1.900', 'G92 E23.330', '' ]) # layer 3 self.assertEqual(lines3[lines3.index(';LAYER:3'):], dummyGcode[dummyGcode.index(';LAYER:3'):])