Skip to content

Commit c9f013e

Browse files
committed
Merge branch 'dev'
2 parents a3be180 + 6a8a8ec commit c9f013e

File tree

3 files changed

+64
-17
lines changed

3 files changed

+64
-17
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gcode-interpreter",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "G-code Interpreter",
55
"author": "Cheton Wu <cheton@gmail.com>",
66
"homepage": "https://github.com/cheton/gcode-interpreter",
@@ -26,7 +26,7 @@
2626
},
2727
"main": "lib/index.js",
2828
"dependencies": {
29-
"gcode-parser": "^1.0.0"
29+
"gcode-parser": "^1.1.0"
3030
},
3131
"devDependencies": {
3232
"babel-core": "^6.21.0",

src/index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { parseFile, parseStream, parseString } from 'gcode-parser';
1+
import {
2+
parseStream,
3+
parseFile,
4+
parseFileSync,
5+
parseString,
6+
parseStringSync
7+
} from 'gcode-parser';
28

39
const noop = () => {};
410

@@ -105,13 +111,27 @@ class GCodeInterpreter {
105111
});
106112
return s;
107113
}
114+
loadFromFileSync(file) {
115+
const list = parseFileSync(file);
116+
for (let i = 0; i < list.length; ++i) {
117+
interpret(this, list[i]);
118+
}
119+
return list;
120+
}
108121
loadFromString(str, callback = noop) {
109122
const s = parseString(str, callback);
110123
s.on('data', (data) => {
111124
interpret(this, data);
112125
});
113126
return s;
114127
}
128+
loadFromStringSync(str) {
129+
const list = parseStringSync(str);
130+
for (let i = 0; i < list.length; ++i) {
131+
interpret(this, list[i]);
132+
}
133+
return list;
134+
}
115135
}
116136

117137
export {

test/index.js

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ describe('G-code Interpreter', () => {
8888
expect(results.length).to.be.equal(7);
8989
});
9090
});
91+
92+
it('loadFromFileSync() should return expected result.', (done) => {
93+
const file = 'test/fixtures/circle.nc';
94+
const runner = new GCodeInterpreter();
95+
const results = runner.loadFromFileSync(file);
96+
expect(results).to.be.an('array');
97+
expect(results.length).to.be.equal(7);
98+
done();
99+
});
100+
101+
it('loadFromStringSync() should return expected result.', (done) => {
102+
const file = 'test/fixtures/circle.nc';
103+
const string = fs.readFileSync(file, 'utf8');
104+
const runner = new GCodeInterpreter();
105+
const results = runner.loadFromStringSync(string);
106+
expect(results).to.be.an('array');
107+
expect(results.length).to.be.equal(7);
108+
done();
109+
});
91110
});
92111

93112
describe('G-code: circle (calls GCodeInterpreter)', () => {
@@ -125,10 +144,8 @@ describe('G-code Interpreter', () => {
125144
done();
126145
});
127146
});
128-
129147
});
130148

131-
132149
describe('G-code: circle (extends GCodeInterpreter)', () => {
133150
const calls = {};
134151

@@ -151,13 +168,18 @@ describe('G-code Interpreter', () => {
151168
}
152169

153170
it('should call each function with the expected number of times.', (done) => {
171+
const file = 'test/fixtures/circle.nc';
172+
const string = fs.readFileSync(file, 'utf8');
154173
const runner = new GCodeRunner();
155-
runner.loadFromFile('test/fixtures/circle.nc', (err, results) => {
156-
expect(calls.G0).to.equal(2);
157-
expect(calls.G1).to.equal(1);
158-
expect(calls.G2).to.equal(4);
159-
done();
160-
});
174+
runner.loadFromFileSync(file);
175+
expect(calls.G0).to.equal(2);
176+
expect(calls.G1).to.equal(1);
177+
expect(calls.G2).to.equal(4);
178+
runner.loadFromStringSync(string);
179+
expect(calls.G0).to.equal(2 * 2);
180+
expect(calls.G1).to.equal(1 * 2);
181+
expect(calls.G2).to.equal(4 * 2);
182+
done();
161183
});
162184
});
163185

@@ -198,13 +220,18 @@ describe('G-code Interpreter', () => {
198220
}
199221

200222
it('should call each function with the expected number of times.', (done) => {
223+
const file = 'test/fixtures/one-inch-circle.nc';
224+
const string = fs.readFileSync(file, 'utf8');
201225
const runner = new GCodeRunner();
202-
runner.loadFromFile('test/fixtures/one-inch-circle.nc', (err, results) => {
203-
expect(calls.G0).to.equal(4);
204-
expect(calls.G1).to.equal(2);
205-
expect(calls.G2).to.equal(4);
206-
done();
207-
});
226+
runner.loadFromFileSync(file);
227+
expect(calls.G0).to.equal(4);
228+
expect(calls.G1).to.equal(2);
229+
expect(calls.G2).to.equal(4);
230+
runner.loadFromStringSync(string);
231+
expect(calls.G0).to.equal(4 * 2);
232+
expect(calls.G1).to.equal(2 * 2);
233+
expect(calls.G2).to.equal(4 * 2);
234+
done();
208235
});
209236
});
210237

0 commit comments

Comments
 (0)