Skip to content

Commit 4c44be3

Browse files
committed
Update README and tests
1 parent 0117eca commit 4c44be3

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ const Interpreter = require('gcode-interpreter');
1313

1414
const Runner = function() {
1515
const handlers = {
16-
'G0': (args) => {
17-
console.log('G0', args);
16+
'G0': (params) => {
17+
console.log('G0', params);
1818
},
19-
'G1': (args) => {
20-
console.log('G1', args);
19+
'G1': (params) => {
20+
console.log('G1', params);
2121
}
2222
};
2323

24-
return new Interpreter({ handlers: handlers })
24+
return new Interpreter({
25+
handlers: handlers,
26+
defaultHandler: (cmd, params) => {
27+
}
28+
});
2529
};
2630

2731
const runner = new Runner()
@@ -100,7 +104,11 @@ class Toolpath {
100104
constructor(options) {
101105
options = options || {};
102106

103-
return new Interpreter({ handlers: this.handlers });
107+
return new Interpreter({
108+
handlers: this.handlers,
109+
defaultHandler: (cmd, params) => {
110+
}
111+
});
104112
}
105113
}
106114

test/fixtures/default-handler.nc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
G0 X0 Y0 Z0
2+
G1 X10 Y10
3+
G9999 P1

test/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,51 @@ describe('G-code Interpreter', () => {
114114
});
115115
});
116116

117+
describe('Default handler', () => {
118+
it('should call default handler if no matching handler found.', (done) => {
119+
let i = 0;
120+
let defaultHandlerCalled = 0;
121+
const file = 'test/fixtures/default-handler.nc';
122+
const string = fs.readFileSync(file, 'utf8');
123+
const runner = new Interpreter({
124+
handlers: {
125+
'G0': (params) => {
126+
},
127+
'G1': (params) => {
128+
}
129+
},
130+
defaultHandler: (cmd, params) => {
131+
// G9999 P1
132+
133+
defaultHandlerCalled++;
134+
}
135+
});
136+
const results = runner.loadFromStringSync(string, (data, index) => {
137+
expect(i).to.be.equal(index);
138+
++i;
139+
});
140+
expect(defaultHandlerCalled).to.be.equal(1);
141+
expect(results).to.be.an('array');
142+
expect(results.length).to.be.equal(3);
143+
expect(results).to.deep.equal([
144+
{
145+
line: 'G0 X0 Y0 Z0',
146+
words: [['G', 0], ['X', 0], ['Y', 0], ['Z', 0]]
147+
},
148+
{
149+
line: 'G1 X10 Y10',
150+
words: [['G', 1], ['X', 10], ['Y', 10]]
151+
},
152+
{
153+
line: 'G9999 P1',
154+
words: [['G', 9999], ['P', 1]]
155+
}
156+
]);
157+
158+
done();
159+
});
160+
});
161+
117162
describe('G-code: circle', () => {
118163
it('should call each function with the expected number of times.', (done) => {
119164
const calls = {};

0 commit comments

Comments
 (0)