Skip to content

Commit 23b9d38

Browse files
committed
Fix all tests
1 parent 0736104 commit 23b9d38

6 files changed

Lines changed: 244 additions & 115 deletions

File tree

src/__tests__/interpreter.ts

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { GCodeCommand } from '../gcode-parser';
33
import { Interpreter } from '../interpreter';
44
import { Job } from '../job';
55
import { PathType } from '../path';
6+
import { Path, PathType } from '../path';
67

78
test('.execute returns a stateful job', () => {
89
const command = new GCodeCommand('G0 X1 Y2 Z3', 'g0', { x: 1, y: 2, z: 3 });
@@ -74,14 +75,15 @@ test('.G0 starts a path if the job has none', () => {
7475

7576
const job = interpreter.execute([command]);
7677

77-
expect(job.paths.length).toEqual(1);
78-
expect(job.paths[0].vertices.length).toEqual(6);
79-
expect(job.paths[0].vertices[0]).toEqual(0);
80-
expect(job.paths[0].vertices[1]).toEqual(0);
81-
expect(job.paths[0].vertices[2]).toEqual(0);
82-
expect(job.paths[0].vertices[3]).toEqual(1);
83-
expect(job.paths[0].vertices[4]).toEqual(2);
84-
expect(job.paths[0].vertices[5]).toEqual(0);
78+
expect(job.paths.length).toEqual(0);
79+
expect(job.inprogressPath).not.toBeNull();
80+
expect(job.inprogressPath.vertices.length).toEqual(6);
81+
expect(job.inprogressPath.vertices[0]).toEqual(0);
82+
expect(job.inprogressPath.vertices[1]).toEqual(0);
83+
expect(job.inprogressPath.vertices[2]).toEqual(0);
84+
expect(job.inprogressPath.vertices[3]).toEqual(1);
85+
expect(job.inprogressPath.vertices[4]).toEqual(2);
86+
expect(job.inprogressPath.vertices[5]).toEqual(0);
8587
});
8688

8789
test('.G0 starts a path if the job has none, starting at the job current state', () => {
@@ -94,12 +96,12 @@ test('.G0 starts a path if the job has none, starting at the job current state',
9496

9597
interpreter.execute([command], job);
9698

97-
expect(job.paths.length).toEqual(1);
98-
expect(job.paths[0].vertices.length).toEqual(6);
99-
expect(job.paths[0].vertices[0]).toEqual(3);
100-
expect(job.paths[0].vertices[1]).toEqual(4);
101-
expect(job.paths[0].vertices[2]).toEqual(0);
102-
expect(job.paths[0].tool).toEqual(5);
99+
expect(job.paths.length).toEqual(0);
100+
expect(job.inprogressPath.vertices.length).toEqual(6);
101+
expect(job.inprogressPath.vertices[0]).toEqual(3);
102+
expect(job.inprogressPath.vertices[1]).toEqual(4);
103+
expect(job.inprogressPath.vertices[2]).toEqual(0);
104+
expect(job.inprogressPath.tool).toEqual(5);
103105
});
104106

105107
test('.G0 continues the path if the job has one', () => {
@@ -113,11 +115,11 @@ test('.G0 continues the path if the job has one', () => {
113115

114116
interpreter.G0(command2, job);
115117

116-
expect(job.paths.length).toEqual(1);
117-
expect(job.paths[0].vertices.length).toEqual(9);
118-
expect(job.paths[0].vertices[6]).toEqual(3);
119-
expect(job.paths[0].vertices[7]).toEqual(4);
120-
expect(job.paths[0].vertices[8]).toEqual(5);
118+
expect(job.paths.length).toEqual(0);
119+
expect(job.inprogressPath.vertices.length).toEqual(9);
120+
expect(job.inprogressPath.vertices[6]).toEqual(3);
121+
expect(job.inprogressPath.vertices[7]).toEqual(4);
122+
expect(job.inprogressPath.vertices[8]).toEqual(5);
121123
});
122124

123125
test(".G0 assigns the travel type if there's no extrusion", () => {
@@ -127,8 +129,8 @@ test(".G0 assigns the travel type if there's no extrusion", () => {
127129

128130
interpreter.G0(command, job);
129131

130-
expect(job.paths.length).toEqual(1);
131-
expect(job.paths[0].travelType).toEqual(PathType.Travel);
132+
expect(job.paths.length).toEqual(0);
133+
expect(job.inprogressPath.travelType).toEqual(PathType.Travel);
132134
});
133135

134136
test(".G0 assigns the extrusion type if there's extrusion", () => {
@@ -138,8 +140,8 @@ test(".G0 assigns the extrusion type if there's extrusion", () => {
138140

139141
interpreter.G0(command, job);
140142

141-
expect(job.paths.length).toEqual(1);
142-
expect(job.paths[0].travelType).toEqual('Extrusion');
143+
expect(job.paths.length).toEqual(0);
144+
expect(job.inprogressPath.travelType).toEqual('Extrusion');
143145
});
144146

145147
test('.G0 assigns the travel type if the extrusion is a retraction', () => {
@@ -149,8 +151,19 @@ test('.G0 assigns the travel type if the extrusion is a retraction', () => {
149151

150152
interpreter.G0(command, job);
151153

152-
expect(job.paths.length).toEqual(1);
153-
expect(job.paths[0].travelType).toEqual('Travel');
154+
expect(job.paths.length).toEqual(0);
155+
expect(job.inprogressPath.travelType).toEqual('Travel');
156+
});
157+
158+
test('.G0 assigns the travel type if the extrusion is a retraction', () => {
159+
const command = new GCodeCommand('G0 E-2', 'g0', { e: -2 });
160+
const interpreter = new Interpreter();
161+
const job = new Job();
162+
163+
interpreter.G0(command, job);
164+
165+
expect(job.paths.length).toEqual(0);
166+
expect(job.inprogressPath.travelType).toEqual('Travel');
154167
});
155168

156169
test('.G0 starts a new path if the travel type changes from Travel to Extrusion', () => {
@@ -162,9 +175,8 @@ test('.G0 starts a new path if the travel type changes from Travel to Extrusion'
162175

163176
interpreter.G0(command2, job);
164177

165-
expect(job.paths.length).toEqual(2);
166-
expect(job.paths[0].travelType).toEqual('Travel');
167-
expect(job.paths[1].travelType).toEqual('Extrusion');
178+
expect(job.paths.length).toEqual(1);
179+
expect(job.inprogressPath.travelType).toEqual('Extrusion');
168180
});
169181

170182
test('.G0 starts a new path if the travel type changes from Extrusion to Travel', () => {
@@ -176,9 +188,8 @@ test('.G0 starts a new path if the travel type changes from Extrusion to Travel'
176188

177189
interpreter.G0(command2, job);
178190

179-
expect(job.paths.length).toEqual(2);
180-
expect(job.paths[0].travelType).toEqual('Extrusion');
181-
expect(job.paths[1].travelType).toEqual('Travel');
191+
expect(job.paths.length).toEqual(1);
192+
expect(job.inprogressPath.travelType).toEqual('Travel');
182193
});
183194

184195
test('.G1 is an alias to .G0', () => {

src/__tests__/job.ts

Lines changed: 119 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,49 @@ describe('.isPlanar', () => {
1212
test('returns true if all extrusions are on the same plane', () => {
1313
const job = new Job();
1414

15-
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
16-
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 0]);
15+
append_path(job, PathType.Extrusion, [
16+
[0, 0, 0],
17+
[1, 2, 0]
18+
]);
19+
append_path(job, PathType.Extrusion, [
20+
[1, 2, 0],
21+
[5, 6, 0]
22+
]);
1723

1824
expect(job.isPlanar()).toEqual(true);
1925
});
2026

2127
test('returns false if any extrusions are on a different plane', () => {
2228
const job = new Job();
2329

24-
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
25-
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 1]);
30+
append_path(job, PathType.Extrusion, [
31+
[0, 0, 0],
32+
[1, 2, 0]
33+
]);
34+
append_path(job, PathType.Extrusion, [
35+
[1, 2, 0],
36+
[5, 6, 1]
37+
]);
2638

2739
expect(job.isPlanar()).toEqual(false);
2840
});
2941

3042
test('ignores travel paths', () => {
3143
const job = new Job();
3244

33-
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
34-
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 1, 1, 2, 0]);
35-
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 0]);
45+
append_path(job, PathType.Extrusion, [
46+
[0, 0, 0],
47+
[1, 2, 0]
48+
]);
49+
append_path(job, PathType.Travel, [
50+
[5, 6, 0],
51+
[5, 6, 1],
52+
[1, 2, 0]
53+
]);
54+
append_path(job, PathType.Extrusion, [
55+
[1, 2, 0],
56+
[5, 6, 0]
57+
]);
3658

3759
expect(job.isPlanar()).toEqual(true);
3860
});
@@ -42,17 +64,29 @@ describe('.layers', () => {
4264
test('returns null if the job is not planar', () => {
4365
const job = new Job();
4466

45-
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
46-
append_path(job, PathType.Extrusion, [5, 6, 0, 5, 6, 1]);
67+
append_path(job, PathType.Extrusion, [
68+
[0, 0, 0],
69+
[1, 2, 0]
70+
]);
71+
append_path(job, PathType.Extrusion, [
72+
[5, 6, 0],
73+
[5, 6, 1]
74+
]);
4775

4876
expect(job.layers).toEqual(null);
4977
});
5078

5179
test('paths without z changes are on the same layer', () => {
5280
const job = new Job();
5381

54-
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
55-
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 0]);
82+
append_path(job, PathType.Extrusion, [
83+
[0, 0, 0],
84+
[1, 2, 0]
85+
]);
86+
append_path(job, PathType.Travel, [
87+
[5, 6, 0],
88+
[5, 6, 0]
89+
]);
5690

5791
const layers = job.layers;
5892

@@ -65,8 +99,14 @@ describe('.layers', () => {
6599
test('travel paths moving z create a new layer', () => {
66100
const job = new Job();
67101

68-
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
69-
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 1]);
102+
append_path(job, PathType.Extrusion, [
103+
[0, 0, 0],
104+
[1, 2, 0]
105+
]);
106+
append_path(job, PathType.Travel, [
107+
[5, 6, 0],
108+
[5, 6, 1]
109+
]);
70110

71111
const layers = job.layers;
72112

@@ -80,10 +120,22 @@ describe('.layers', () => {
80120
test('multiple travels in a row are on the same layer', () => {
81121
const job = new Job();
82122

83-
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
84-
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 2]);
85-
append_path(job, PathType.Travel, [5, 6, 2, 5, 6, 0]);
86-
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 2]);
123+
append_path(job, PathType.Extrusion, [
124+
[0, 0, 0],
125+
[1, 2, 0]
126+
]);
127+
append_path(job, PathType.Travel, [
128+
[5, 6, 0],
129+
[5, 6, 2]
130+
]);
131+
append_path(job, PathType.Travel, [
132+
[5, 6, 2],
133+
[5, 6, 0]
134+
]);
135+
append_path(job, PathType.Travel, [
136+
[5, 6, 0],
137+
[5, 6, 2]
138+
]);
87139

88140
const layers = job.layers;
89141

@@ -97,11 +149,26 @@ describe('.layers', () => {
97149
test('extrusions after travels are on the same layer', () => {
98150
const job = new Job();
99151

100-
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
101-
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 2]);
102-
append_path(job, PathType.Travel, [5, 6, 2, 5, 6, 0]);
103-
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 2]);
104-
append_path(job, PathType.Extrusion, [5, 6, 2, 5, 6, 2]);
152+
append_path(job, PathType.Extrusion, [
153+
[0, 0, 0],
154+
[1, 2, 0]
155+
]);
156+
append_path(job, PathType.Travel, [
157+
[5, 6, 0],
158+
[5, 6, 2]
159+
]);
160+
append_path(job, PathType.Travel, [
161+
[5, 6, 2],
162+
[5, 6, 0]
163+
]);
164+
append_path(job, PathType.Travel, [
165+
[5, 6, 0],
166+
[5, 6, 2]
167+
]);
168+
append_path(job, PathType.Extrusion, [
169+
[5, 6, 2],
170+
[5, 6, 2]
171+
]);
105172

106173
const layers = job.layers;
107174

@@ -117,9 +184,18 @@ describe('.extrusions', () => {
117184
test('returns all extrusion paths', () => {
118185
const job = new Job();
119186

120-
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
121-
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 0]);
122-
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 0]);
187+
append_path(job, PathType.Extrusion, [
188+
[0, 0, 0],
189+
[1, 2, 0]
190+
]);
191+
append_path(job, PathType.Travel, [
192+
[5, 6, 0],
193+
[5, 6, 0]
194+
]);
195+
append_path(job, PathType.Extrusion, [
196+
[1, 2, 0],
197+
[5, 6, 0]
198+
]);
123199

124200
const extrusions = job.extrusions;
125201

@@ -136,10 +212,22 @@ describe('.travels', () => {
136212
test('returns all travel paths', () => {
137213
const job = new Job();
138214

139-
append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
140-
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 0]);
141-
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 0]);
142-
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 0]);
215+
append_path(job, PathType.Extrusion, [
216+
[0, 0, 0],
217+
[1, 2, 0]
218+
]);
219+
append_path(job, PathType.Travel, [
220+
[5, 6, 0],
221+
[5, 6, 0]
222+
]);
223+
append_path(job, PathType.Extrusion, [
224+
[1, 2, 0],
225+
[5, 6, 0]
226+
]);
227+
append_path(job, PathType.Travel, [
228+
[5, 6, 0],
229+
[5, 6, 0]
230+
]);
143231

144232
const travels = job.travels;
145233

@@ -152,8 +240,8 @@ describe('.travels', () => {
152240
});
153241
});
154242

155-
function append_path(job, travelType, vertices) {
243+
function append_path(job, travelType, points) {
156244
const path = new Path(travelType, 0.6, 0.2, job.state.tool);
157-
path.vertices = vertices;
245+
points.forEach((point: [number, number, number]) => path.addPoint(...point));
158246
job.addPath(path);
159247
}

0 commit comments

Comments
 (0)