Skip to content

Commit

Permalink
Fix all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiedeziel committed Oct 14, 2024
1 parent dfc0202 commit 678a96f
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 117 deletions.
72 changes: 41 additions & 31 deletions src/__tests__/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ test('.G0 starts a path if the job has none', () => {

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

expect(job.paths.length).toEqual(1);
expect(job.paths[0].vertices.length).toEqual(6);
expect(job.paths[0].vertices[0]).toEqual(0);
expect(job.paths[0].vertices[1]).toEqual(0);
expect(job.paths[0].vertices[2]).toEqual(0);
expect(job.paths[0].vertices[3]).toEqual(1);
expect(job.paths[0].vertices[4]).toEqual(2);
expect(job.paths[0].vertices[5]).toEqual(0);
expect(job.paths.length).toEqual(0);
expect(job.inprogressPath).not.toBeNull();
expect(job.inprogressPath?.vertices.length).toEqual(6);
expect(job.inprogressPath?.vertices[0]).toEqual(0);
expect(job.inprogressPath?.vertices[1]).toEqual(0);
expect(job.inprogressPath?.vertices[2]).toEqual(0);
expect(job.inprogressPath?.vertices[3]).toEqual(1);
expect(job.inprogressPath?.vertices[4]).toEqual(2);
expect(job.inprogressPath?.vertices[5]).toEqual(0);
});

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

interpreter.execute([command], job);

expect(job.paths.length).toEqual(1);
expect(job.paths[0].vertices.length).toEqual(6);
expect(job.paths[0].vertices[0]).toEqual(3);
expect(job.paths[0].vertices[1]).toEqual(4);
expect(job.paths[0].vertices[2]).toEqual(0);
expect(job.paths[0].tool).toEqual(5);
expect(job.paths.length).toEqual(0);
expect(job.inprogressPath?.vertices.length).toEqual(6);
expect(job.inprogressPath?.vertices[0]).toEqual(3);
expect(job.inprogressPath?.vertices[1]).toEqual(4);
expect(job.inprogressPath?.vertices[2]).toEqual(0);
expect(job.inprogressPath?.tool).toEqual(5);
});

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

interpreter.G0(command2, job);

expect(job.paths.length).toEqual(1);
expect(job.paths[0].vertices.length).toEqual(9);
expect(job.paths[0].vertices[6]).toEqual(3);
expect(job.paths[0].vertices[7]).toEqual(4);
expect(job.paths[0].vertices[8]).toEqual(5);
expect(job.paths.length).toEqual(0);
expect(job.inprogressPath?.vertices.length).toEqual(9);
expect(job.inprogressPath?.vertices[6]).toEqual(3);
expect(job.inprogressPath?.vertices[7]).toEqual(4);
expect(job.inprogressPath?.vertices[8]).toEqual(5);
});

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

interpreter.G0(command, job);

expect(job.paths.length).toEqual(1);
expect(job.paths[0].travelType).toEqual(PathType.Travel);
expect(job.paths.length).toEqual(0);
expect(job.inprogressPath?.travelType).toEqual(PathType.Travel);
});

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

interpreter.G0(command, job);

expect(job.paths.length).toEqual(1);
expect(job.paths[0].travelType).toEqual('Extrusion');
expect(job.paths.length).toEqual(0);
expect(job.inprogressPath?.travelType).toEqual('Extrusion');
});

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

interpreter.G0(command, job);

expect(job.paths.length).toEqual(1);
expect(job.paths[0].travelType).toEqual('Travel');
expect(job.paths.length).toEqual(0);
expect(job.inprogressPath?.travelType).toEqual('Travel');
});

test('.G0 assigns the travel type if the extrusion is a retraction', () => {
const command = new GCodeCommand('G0 E-2', 'g0', { e: -2 });
const interpreter = new Interpreter();
const job = new Job();

interpreter.G0(command, job);

expect(job.paths.length).toEqual(0);
expect(job.inprogressPath?.travelType).toEqual('Travel');
});

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

interpreter.G0(command2, job);

expect(job.paths.length).toEqual(2);
expect(job.paths[0].travelType).toEqual('Travel');
expect(job.paths[1].travelType).toEqual('Extrusion');
expect(job.paths.length).toEqual(1);
expect(job.inprogressPath?.travelType).toEqual('Extrusion');
});

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

interpreter.G0(command2, job);

expect(job.paths.length).toEqual(2);
expect(job.paths[0].travelType).toEqual('Extrusion');
expect(job.paths[1].travelType).toEqual('Travel');
expect(job.paths.length).toEqual(1);
expect(job.inprogressPath?.travelType).toEqual('Travel');
});

test('.G1 is an alias to .G0', () => {
Expand Down
150 changes: 119 additions & 31 deletions src/__tests__/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,49 @@ describe('.isPlanar', () => {
test('returns true if all extrusions are on the same plane', () => {
const job = new Job();

append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 0]);
append_path(job, PathType.Extrusion, [
[0, 0, 0],
[1, 2, 0]
]);
append_path(job, PathType.Extrusion, [
[1, 2, 0],
[5, 6, 0]
]);

expect(job.isPlanar()).toEqual(true);
});

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

append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 1]);
append_path(job, PathType.Extrusion, [
[0, 0, 0],
[1, 2, 0]
]);
append_path(job, PathType.Extrusion, [
[1, 2, 0],
[5, 6, 1]
]);

expect(job.isPlanar()).toEqual(false);
});

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

append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 1, 1, 2, 0]);
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 0]);
append_path(job, PathType.Extrusion, [
[0, 0, 0],
[1, 2, 0]
]);
append_path(job, PathType.Travel, [
[5, 6, 0],
[5, 6, 1],
[1, 2, 0]
]);
append_path(job, PathType.Extrusion, [
[1, 2, 0],
[5, 6, 0]
]);

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

append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
append_path(job, PathType.Extrusion, [5, 6, 0, 5, 6, 1]);
append_path(job, PathType.Extrusion, [
[0, 0, 0],
[1, 2, 0]
]);
append_path(job, PathType.Extrusion, [
[5, 6, 0],
[5, 6, 1]
]);

expect(job.layers).toEqual(null);
});

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

append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 0]);
append_path(job, PathType.Extrusion, [
[0, 0, 0],
[1, 2, 0]
]);
append_path(job, PathType.Travel, [
[5, 6, 0],
[5, 6, 0]
]);

const layers = job.layers;

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

append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 1]);
append_path(job, PathType.Extrusion, [
[0, 0, 0],
[1, 2, 0]
]);
append_path(job, PathType.Travel, [
[5, 6, 0],
[5, 6, 1]
]);

const layers = job.layers;

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

append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 2]);
append_path(job, PathType.Travel, [5, 6, 2, 5, 6, 0]);
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 2]);
append_path(job, PathType.Extrusion, [
[0, 0, 0],
[1, 2, 0]
]);
append_path(job, PathType.Travel, [
[5, 6, 0],
[5, 6, 2]
]);
append_path(job, PathType.Travel, [
[5, 6, 2],
[5, 6, 0]
]);
append_path(job, PathType.Travel, [
[5, 6, 0],
[5, 6, 2]
]);

const layers = job.layers;

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

append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 2]);
append_path(job, PathType.Travel, [5, 6, 2, 5, 6, 0]);
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 2]);
append_path(job, PathType.Extrusion, [5, 6, 2, 5, 6, 2]);
append_path(job, PathType.Extrusion, [
[0, 0, 0],
[1, 2, 0]
]);
append_path(job, PathType.Travel, [
[5, 6, 0],
[5, 6, 2]
]);
append_path(job, PathType.Travel, [
[5, 6, 2],
[5, 6, 0]
]);
append_path(job, PathType.Travel, [
[5, 6, 0],
[5, 6, 2]
]);
append_path(job, PathType.Extrusion, [
[5, 6, 2],
[5, 6, 2]
]);

const layers = job.layers;

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

append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 0]);
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 0]);
append_path(job, PathType.Extrusion, [
[0, 0, 0],
[1, 2, 0]
]);
append_path(job, PathType.Travel, [
[5, 6, 0],
[5, 6, 0]
]);
append_path(job, PathType.Extrusion, [
[1, 2, 0],
[5, 6, 0]
]);

const extrusions = job.extrusions;

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

append_path(job, PathType.Extrusion, [0, 0, 0, 1, 2, 0]);
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 0]);
append_path(job, PathType.Extrusion, [1, 2, 0, 5, 6, 0]);
append_path(job, PathType.Travel, [5, 6, 0, 5, 6, 0]);
append_path(job, PathType.Extrusion, [
[0, 0, 0],
[1, 2, 0]
]);
append_path(job, PathType.Travel, [
[5, 6, 0],
[5, 6, 0]
]);
append_path(job, PathType.Extrusion, [
[1, 2, 0],
[5, 6, 0]
]);
append_path(job, PathType.Travel, [
[5, 6, 0],
[5, 6, 0]
]);

const travels = job.travels;

Expand All @@ -152,8 +240,8 @@ describe('.travels', () => {
});
});

function append_path(job, travelType, vertices) {
function append_path(job, travelType, points) {
const path = new Path(travelType, 0.6, 0.2, job.state.tool);
path.vertices = vertices;
points.forEach((point: [number, number, number]) => path.addPoint(...point));
job.addPath(path);
}
12 changes: 8 additions & 4 deletions src/__tests__/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ test('.path returns an array of Vector3', () => {
test('.geometry returns an ExtrusionGeometry from the path', () => {
const path = new Path(PathType.Travel, undefined, undefined, undefined);

path.vertices = [0, 0, 0, 1, 2, 3];
path.addPoint(0, 0, 0);
path.addPoint(1, 2, 3);

const result = path.geometry() as ExtrusionGeometry;

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

path.vertices = [0, 0, 0, 1, 2, 3];
path.addPoint(0, 0, 0);
path.addPoint(1, 2, 3);

const result = path.geometry() as ExtrusionGeometry;

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

path.vertices = [0, 0, 0, 1, 2, 3];
path.addPoint(0, 0, 0);
path.addPoint(1, 2, 3);

const result = path.geometry() as ExtrusionGeometry;

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

path.vertices = [0, 0, 0, 1, 2, 3];
path.addPoint(0, 0, 0);
path.addPoint(1, 2, 3);

const result = path.line();

Expand Down
Loading

0 comments on commit 678a96f

Please sign in to comment.