File size: 1,306 Bytes
5a79264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29c82ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import annotations

from gcode_viewer import parse_gcode_path


def test_parse_gcode_classifies_g0_as_travel_and_g1_as_print() -> None:
    parsed = parse_gcode_path(
        "\n".join(
            [
                "G91",
                "G0 X1 Y0 ; travel",
                "G1 X0 Y1 ; print",
            ]
        )
    )

    assert len(parsed["travel_segments"]) == 1
    assert len(parsed["print_segments"]) == 1


def test_parse_gcode_reads_scientific_notation_instead_of_mantissa() -> None:
    parsed = parse_gcode_path(
        "\n".join(
            [
                "G91",
                "G0 X-5.1e-08 Y0.8 ; tiny float-noise travel",
                "G1 X4.0 Y0.0 ; print",
            ]
        )
    )

    (segment,) = parsed["print_segments"]
    start, end = segment[0], segment[-1]
    # "X-5.1e-08" must move the head by ~0, not by -5.1.
    assert abs(start[0]) < 1e-6
    assert abs(end[0] - 4.0) < 1e-6


def test_parse_gcode_keeps_uppercase_extrusion_token_out_of_x_axis() -> None:
    parsed = parse_gcode_path(
        "\n".join(
            [
                "G91",
                "G1 X1.2E3 Y0.5",  # compact G-code: X=1.2, E=3 (not X=1200)
            ]
        )
    )

    (segment,) = parsed["print_segments"]
    assert abs(segment[-1][0] - 1.2) < 1e-9