-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialect_test.go
More file actions
182 lines (154 loc) · 4.42 KB
/
dialect_test.go
File metadata and controls
182 lines (154 loc) · 4.42 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package gcode_test
import (
"strings"
"testing"
"github.com/lestrrat-go/gcode"
"github.com/lestrrat-go/gcode/dialects/klipper"
"github.com/lestrrat-go/gcode/dialects/marlin"
"github.com/lestrrat-go/gcode/dialects/reprap"
"github.com/stretchr/testify/require"
)
func TestNewDialect(t *testing.T) {
t.Parallel()
d := gcode.NewDialect("test")
require.Equal(t, "test", d.Name())
}
func TestDialectRegisterAndLookup(t *testing.T) {
t.Parallel()
d := gcode.NewDialect("test")
d.Register(gcode.CommandDef{
Name: "G0",
Description: "rapid",
Params: []gcode.ParamDef{{Key: "X"}, {Key: "Y"}},
})
def, ok := d.LookupCommand("G0")
require.True(t, ok)
require.Equal(t, "rapid", def.Description)
require.Len(t, def.Params, 2)
_, ok = d.LookupCommand("G99")
require.False(t, ok)
}
func TestDialectExtendIndependence(t *testing.T) {
t.Parallel()
parent := gcode.NewDialect("parent")
parent.Register(gcode.CommandDef{Name: "G0"})
child := parent.Extend("child")
require.Equal(t, "child", child.Name())
_, ok := child.LookupCommand("G0")
require.True(t, ok)
child.Register(gcode.CommandDef{Name: "G1"})
_, ok = parent.LookupCommand("G1")
require.False(t, ok)
}
func TestDialectCommands(t *testing.T) {
t.Parallel()
d := gcode.NewDialect("test")
d.Register(gcode.CommandDef{Name: "G0"})
d.Register(gcode.CommandDef{Name: "G1"})
d.Register(gcode.CommandDef{Name: "M104"})
require.Len(t, d.Commands(), 3)
}
func TestMarlinDialect(t *testing.T) {
t.Parallel()
d := marlin.Dialect()
require.Equal(t, "marlin", d.Name())
for _, name := range []string{"G0", "G1", "G28", "G92.1", "M104", "T0"} {
_, ok := d.LookupCommand(name)
require.True(t, ok, "expected dialect to define %s", name)
}
}
func TestRepRapDialect(t *testing.T) {
t.Parallel()
d := reprap.Dialect()
require.Equal(t, "reprap", d.Name())
// Inherited.
_, ok := d.LookupCommand("G0")
require.True(t, ok)
// RepRap-specific.
_, ok = d.LookupCommand("G10")
require.True(t, ok)
_, ok = d.LookupCommand("M557")
require.True(t, ok)
}
func TestKlipperDialectCore(t *testing.T) {
t.Parallel()
d := klipper.Dialect()
require.Equal(t, "klipper", d.Name())
// Always-available core extended commands.
for _, name := range []string{
"G1", "M104", // inherited Marlin
"SET_PRESSURE_ADVANCE",
"SET_VELOCITY_LIMIT",
"SAVE_GCODE_STATE",
"RESTORE_GCODE_STATE",
"SET_PRINT_STATS_INFO",
} {
_, ok := d.LookupCommand(name)
require.True(t, ok, "expected klipper core dialect to define %s", name)
}
// Optional features must NOT be in the core singleton.
for _, name := range []string{
"BED_MESH_CALIBRATE",
"EXCLUDE_OBJECT_DEFINE",
"SET_FAN_SPEED",
"TIMELAPSE_TAKE_FRAME",
} {
_, ok := d.LookupCommand(name)
require.False(t, ok, "expected %s to be opt-in via With...", name)
}
}
func TestKlipperWithHelpersClone(t *testing.T) {
t.Parallel()
base := klipper.Dialect()
withMesh := klipper.WithBedMesh(base)
withMeshAndExclude := klipper.WithExcludeObject(withMesh)
// Helpers do not mutate the inputs.
_, ok := base.LookupCommand("BED_MESH_CALIBRATE")
require.False(t, ok, "WithBedMesh must not mutate input")
_, ok = withMesh.LookupCommand("EXCLUDE_OBJECT_DEFINE")
require.False(t, ok, "WithExcludeObject must not mutate input")
// Returned dialects accumulate features.
_, ok = withMesh.LookupCommand("BED_MESH_CALIBRATE")
require.True(t, ok)
_, ok = withMeshAndExclude.LookupCommand("BED_MESH_CALIBRATE")
require.True(t, ok)
_, ok = withMeshAndExclude.LookupCommand("EXCLUDE_OBJECT_DEFINE")
require.True(t, ok)
}
func TestKlipperParsesExtendedWithOptIn(t *testing.T) {
t.Parallel()
src := `EXCLUDE_OBJECT_DEFINE NAME=part_0 CENTER=135.5,136 POLYGON=[[1,2],[3,4]]
EXCLUDE_OBJECT_START NAME=part_0
G1 X10 Y20
EXCLUDE_OBJECT_END NAME=part_0
`
d := klipper.WithExcludeObject(klipper.Dialect())
r := gcode.NewReader(
strings.NewReader(src),
gcode.WithDialect(d),
gcode.WithStrict(),
)
count := 0
for line, err := range r.All() {
require.NoError(t, err)
require.True(t, line.HasCommand)
count++
}
require.Equal(t, 4, count)
}
func TestWithDialect(t *testing.T) {
t.Parallel()
d := gcode.NewDialect("test")
require.NotNil(t, gcode.WithDialect(d))
}
func TestDialectRegistry(t *testing.T) {
t.Parallel()
r := gcode.NewDialectRegistry()
d := gcode.NewDialect("test")
r.Register(d)
found, ok := r.Lookup("test")
require.True(t, ok)
require.Equal(t, "test", found.Name())
_, ok = r.Lookup("nonexistent")
require.False(t, ok)
}