Skip to content

Commit e32e879

Browse files
committed
Fix all tests
1 parent 0d70a16 commit e32e879

File tree

6 files changed

+245
-117
lines changed

6 files changed

+245
-117
lines changed

src/__tests__/interpreter.ts

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ test('.G0 starts a path if the job has none', () => {
7474

7575
const job = interpreter.execute([command]);
7676

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);
77+
expect(job.paths.length).toEqual(0);
78+
expect(job.inprogressPath).not.toBeNull();
79+
expect(job.inprogressPath?.vertices.length).toEqual(6);
80+
expect(job.inprogressPath?.vertices[0]).toEqual(0);
81+
expect(job.inprogressPath?.vertices[1]).toEqual(0);
82+
expect(job.inprogressPath?.vertices[2]).toEqual(0);
83+
expect(job.inprogressPath?.vertices[3]).toEqual(1);
84+
expect(job.inprogressPath?.vertices[4]).toEqual(2);
85+
expect(job.inprogressPath?.vertices[5]).toEqual(0);
8586
});
8687

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

9596
interpreter.execute([command], job);
9697

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);
98+
expect(job.paths.length).toEqual(0);
99+
expect(job.inprogressPath?.vertices.length).toEqual(6);
100+
expect(job.inprogressPath?.vertices[0]).toEqual(3);
101+
expect(job.inprogressPath?.vertices[1]).toEqual(4);
102+
expect(job.inprogressPath?.vertices[2]).toEqual(0);
103+
expect(job.inprogressPath?.tool).toEqual(5);
103104
});
104105

105106
test('.G0 continues the path if the job has one', () => {
@@ -113,11 +114,11 @@ test('.G0 continues the path if the job has one', () => {
113114

114115
interpreter.G0(command2, job);
115116

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);
117+
expect(job.paths.length).toEqual(0);
118+
expect(job.inprogressPath?.vertices.length).toEqual(9);
119+
expect(job.inprogressPath?.vertices[6]).toEqual(3);
120+
expect(job.inprogressPath?.vertices[7]).toEqual(4);
121+
expect(job.inprogressPath?.vertices[8]).toEqual(5);
121122
});
122123

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

128129
interpreter.G0(command, job);
129130

130-
expect(job.paths.length).toEqual(1);
131-
expect(job.paths[0].travelType).toEqual(PathType.Travel);
131+
expect(job.paths.length).toEqual(0);
132+
expect(job.inprogressPath?.travelType).toEqual(PathType.Travel);
132133
});
133134

134135
test(".G0 assigns the extrusion type if there's extrusion", () => {
@@ -138,8 +139,8 @@ test(".G0 assigns the extrusion type if there's extrusion", () => {
138139

139140
interpreter.G0(command, job);
140141

141-
expect(job.paths.length).toEqual(1);
142-
expect(job.paths[0].travelType).toEqual('Extrusion');
142+
expect(job.paths.length).toEqual(0);
143+
expect(job.inprogressPath?.travelType).toEqual('Extrusion');
143144
});
144145

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

150151
interpreter.G0(command, job);
151152

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

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

163175
interpreter.G0(command2, job);
164176

165-
expect(job.paths.length).toEqual(2);
166-
expect(job.paths[0].travelType).toEqual('Travel');
167-
expect(job.paths[1].travelType).toEqual('Extrusion');
177+
expect(job.paths.length).toEqual(1);
178+
expect(job.inprogressPath?.travelType).toEqual('Extrusion');
168179
});
169180

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

177188
interpreter.G0(command2, job);
178189

179-
expect(job.paths.length).toEqual(2);
180-
expect(job.paths[0].travelType).toEqual('Extrusion');
181-
expect(job.paths[1].travelType).toEqual('Travel');
190+
expect(job.paths.length).toEqual(1);
191+
expect(job.inprogressPath?.travelType).toEqual('Travel');
182192
});
183193

184194
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
}

src/__tests__/path.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ test('.path returns an array of Vector3', () => {
6767
test('.geometry returns an ExtrusionGeometry from the path', () => {
6868
const path = new Path(PathType.Travel, undefined, undefined, undefined);
6969

70-
path.vertices = [0, 0, 0, 1, 2, 3];
70+
path.addPoint(0, 0, 0);
71+
path.addPoint(1, 2, 3);
7172

7273
const result = path.geometry() as ExtrusionGeometry;
7374

@@ -80,7 +81,8 @@ test('.geometry returns an ExtrusionGeometry from the path', () => {
8081
test('.geometry returns an ExtrusionGeometry with the path extrusion width', () => {
8182
const path = new Path(PathType.Travel, 9, undefined, undefined);
8283

83-
path.vertices = [0, 0, 0, 1, 2, 3];
84+
path.addPoint(0, 0, 0);
85+
path.addPoint(1, 2, 3);
8486

8587
const result = path.geometry() as ExtrusionGeometry;
8688

@@ -90,7 +92,8 @@ test('.geometry returns an ExtrusionGeometry with the path extrusion width', ()
9092
test('.geometry returns an ExtrusionGeometry with the path line height', () => {
9193
const path = new Path(PathType.Travel, undefined, 5, undefined);
9294

93-
path.vertices = [0, 0, 0, 1, 2, 3];
95+
path.addPoint(0, 0, 0);
96+
path.addPoint(1, 2, 3);
9497

9598
const result = path.geometry() as ExtrusionGeometry;
9699

@@ -109,7 +112,8 @@ test('.geometry returns an empty BufferGeometry if there are less than 3 vertice
109112
test('.line returns a BufferGeometry from the path', () => {
110113
const path = new Path(PathType.Travel, undefined, undefined, undefined);
111114

112-
path.vertices = [0, 0, 0, 1, 2, 3];
115+
path.addPoint(0, 0, 0);
116+
path.addPoint(1, 2, 3);
113117

114118
const result = path.line();
115119

0 commit comments

Comments
 (0)