Skip to content

Commit b9a5e78

Browse files
committed
Test Path
1 parent 4e4d095 commit b9a5e78

2 files changed

Lines changed: 132 additions & 3 deletions

File tree

src/__tests__/path.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { test, expect } from 'vitest';
2+
import { Path, PathType } from '../path';
3+
import { ExtrusionGeometry } from '../extrusion-geometry';
4+
import { BufferGeometry } from 'three';
5+
6+
test('.addPoint adds a point to the vertices', () => {
7+
const path = new Path(PathType.Travel, undefined, undefined, undefined);
8+
9+
path.addPoint(1, 2, 3);
10+
11+
expect(path.vertices).not.toBeNull();
12+
expect(path.vertices.length).toEqual(3);
13+
});
14+
15+
test('.addPoint adds points at the end of vertices', () => {
16+
const path = new Path(PathType.Travel, undefined, undefined, undefined);
17+
18+
path.addPoint(0, 0, 0);
19+
path.addPoint(1, 2, 3);
20+
path.addPoint(5, 6, 7);
21+
22+
expect(path.vertices).not.toBeNull();
23+
expect(path.vertices.length).toEqual(9);
24+
expect(path.vertices[6]).toEqual(5);
25+
expect(path.vertices[7]).toEqual(6);
26+
expect(path.vertices[8]).toEqual(7);
27+
});
28+
29+
test('.checkLineContinuity returns false if there are less than 3 vertices', () => {
30+
const path = new Path(PathType.Travel, undefined, undefined, undefined);
31+
32+
expect(path.checkLineContinuity(0, 0, 0)).toBeFalsy();
33+
});
34+
35+
test('.checkLineContinuity returns false if the last point is different', () => {
36+
const path = new Path(PathType.Travel, undefined, undefined, undefined);
37+
38+
path.addPoint(0, 0, 0);
39+
path.addPoint(1, 2, 3);
40+
41+
expect(path.checkLineContinuity(1, 2, 4)).toBeFalsy();
42+
});
43+
44+
test('.checkLineContinuity returns true if the last point is the same', () => {
45+
const path = new Path(PathType.Travel, undefined, undefined, undefined);
46+
47+
path.addPoint(0, 0, 0);
48+
path.addPoint(1, 2, 3);
49+
50+
expect(path.checkLineContinuity(1, 2, 3)).toBeTruthy();
51+
});
52+
53+
test('.path returns an array of Vector3', () => {
54+
const path = new Path(PathType.Travel, undefined, undefined, undefined);
55+
56+
path.addPoint(0, 0, 0);
57+
path.addPoint(1, 2, 3);
58+
59+
const result = path.path();
60+
61+
expect(result).not.toBeNull();
62+
expect(result.length).toEqual(2);
63+
expect(result[0]).toEqual({ x: 0, y: 0, z: 0 });
64+
expect(result[1]).toEqual({ x: 1, y: 2, z: 3 });
65+
});
66+
67+
test('.geometry returns an ExtrusionGeometry from the path', () => {
68+
const path = new Path(PathType.Travel, undefined, undefined, undefined);
69+
70+
path.vertices = [0, 0, 0, 1, 2, 3];
71+
72+
const result = path.geometry() as ExtrusionGeometry;
73+
74+
expect(result).not.toBeNull();
75+
expect(result).toBeInstanceOf(ExtrusionGeometry);
76+
expect(result.parameters.points.length).toEqual(2);
77+
expect(result.parameters.closed).toEqual(false);
78+
});
79+
80+
test('.geometry returns an ExtrusionGeometry with the path extrusion width', () => {
81+
const path = new Path(PathType.Travel, 9, undefined, undefined);
82+
83+
path.vertices = [0, 0, 0, 1, 2, 3];
84+
85+
const result = path.geometry() as ExtrusionGeometry;
86+
87+
expect(result.parameters.lineWidth).toEqual(9);
88+
});
89+
90+
test('.geometry returns an ExtrusionGeometry with the path line height', () => {
91+
const path = new Path(PathType.Travel, undefined, 5, undefined);
92+
93+
path.vertices = [0, 0, 0, 1, 2, 3];
94+
95+
const result = path.geometry() as ExtrusionGeometry;
96+
97+
expect(result.parameters.lineHeight).toEqual(5);
98+
});
99+
100+
test('.geometry returns an empty BufferGeometry if there are less than 3 vertices', () => {
101+
const path = new Path(PathType.Travel, undefined, undefined, undefined);
102+
103+
const result = path.geometry();
104+
105+
expect(result).not.toBeNull();
106+
expect(result).toBeInstanceOf(BufferGeometry);
107+
});
108+
109+
test('.line returns a BufferGeometry from the path', () => {
110+
const path = new Path(PathType.Travel, undefined, undefined, undefined);
111+
112+
path.vertices = [0, 0, 0, 1, 2, 3];
113+
114+
const result = path.line();
115+
116+
expect(result).not.toBeNull();
117+
expect(result).toBeInstanceOf(BufferGeometry);
118+
expect(result.getAttribute('position').count).toEqual(2);
119+
});
120+
121+
test('.line returns a BufferGeometry when there are no vertices', () => {
122+
const path = new Path(PathType.Travel, undefined, undefined, undefined);
123+
124+
const result = path.line();
125+
126+
expect(result).not.toBeNull();
127+
expect(result).toBeInstanceOf(BufferGeometry);
128+
expect(result.getAttribute('position').count).toEqual(0);
129+
});

src/__tests__/preserving-parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { test, expect } from 'vitest';
33
import { Parser } from '../gcode-parser';
44

55
test('all input should be preserved', () => {
6-
const parser = new Parser(0);
6+
const parser = new Parser();
77
const gcode = `G1 X0 Y0 Z1 E1`;
88
const parsed = parser.parseGCode(gcode);
99
expect(parsed).not.toBeNull();
@@ -12,7 +12,7 @@ test('all input should be preserved', () => {
1212
});
1313

1414
test('multiple lines should be preserved', () => {
15-
const parser = new Parser(0);
15+
const parser = new Parser();
1616
const gcode = `G1 X0 Y0 Z1 E1\nG1 X10 Y10 E10`;
1717
const parsed = parser.parseGCode(gcode);
1818
expect(parsed).not.toBeNull();
@@ -21,7 +21,7 @@ test('multiple lines should be preserved', () => {
2121
});
2222

2323
test('comments should be preserved', () => {
24-
const parser = new Parser(0);
24+
const parser = new Parser();
2525
const gcode = `G1 X0 Y0 Z1 E1; this is a comment`;
2626
const parsed = parser.parseGCode(gcode);
2727
expect(parsed).not.toBeNull();

0 commit comments

Comments
 (0)