@@ -35,8 +35,9 @@ describe('Pass a null value as the first argument', () => {
35
35
36
36
describe ( 'Pass an empty text as the first argument' , ( ) => {
37
37
it ( 'should get empty results.' , ( done ) => {
38
- const sampleText = '' ;
39
- parseString ( sampleText , ( err , results ) => {
38
+ const inputText = '' ;
39
+ parseString ( inputText , ( err , results ) => {
40
+ expect ( err ) . toBeNull ( ) ;
40
41
expect ( results . length ) . toBe ( 0 ) ;
41
42
done ( ) ;
42
43
} ) ;
@@ -144,9 +145,38 @@ describe('Commands', () => {
144
145
} ) ;
145
146
} ) ;
146
147
147
- describe ( 'Comments' , ( ) => {
148
+ describe ( 'Stripping comments' , ( ) => {
149
+ it ( 'should correctly parse a semicolon comment before parentheses' , ( ) => {
150
+ const line = 'M6 ; comment (tool change) T1' ;
151
+ const data = parseLine ( line , { lineMode : 'stripped' } ) ;
152
+ expect ( data . line ) . toBe ( 'M6' ) ;
153
+ expect ( data . comments ) . toEqual ( [
154
+ 'comment (tool change) T1' ,
155
+ ] ) ;
156
+ } ) ;
157
+
158
+ it ( 'should correctly parse nested parentheses containing a semicolon' , ( ) => {
159
+ const line = 'M6 (outer (inner;)) T1 ; comment' ;
160
+ const data = parseLine ( line , { lineMode : 'stripped' } ) ;
161
+ expect ( data . line ) . toBe ( 'M6 T1' ) ;
162
+ expect ( data . comments ) . toEqual ( [
163
+ 'outer (inner;)' ,
164
+ 'comment' ,
165
+ ] ) ;
166
+ } ) ;
167
+
168
+ it ( 'should correctly parse multiple comments in a line' , ( ) => {
169
+ const line = 'M6 (first comment) T1 ; second comment' ;
170
+ const data = parseLine ( line , { lineMode : 'stripped' } ) ;
171
+ expect ( data . line ) . toBe ( 'M6 T1' ) ;
172
+ expect ( data . comments ) . toEqual ( [
173
+ 'first comment' ,
174
+ 'second comment' ,
175
+ ] ) ;
176
+ } ) ;
177
+
148
178
it ( 'should strip everything after a semi-colon to the end of the loine including preceding spaces.' , ( done ) => {
149
- const sampleText = [
179
+ const inputText = [
150
180
' % ' ,
151
181
' #' ,
152
182
'; Operation: 0' ,
@@ -161,17 +191,26 @@ describe('Comments', () => {
161
191
' ' // empty line
162
192
] . join ( '\n' ) ;
163
193
164
- parseString ( sampleText , ( err , results ) => {
165
- results = results . filter ( result => result . length > 0 ) ;
166
- expect ( results . length ) . toBe ( 0 ) ;
194
+ parseString ( inputText , { lineMode : 'stripped' } , ( err , results ) => {
195
+ expect ( results ) . toEqual ( [
196
+ { line : '%' , words : [ ] , cmds : [ '%' ] } ,
197
+ { line : '#' , words : [ ] } ,
198
+ { line : '' , words : [ ] , comments : [ 'Operation: 0' ] } ,
199
+ { line : '' , words : [ ] , comments : [ 'Name:' ] } ,
200
+ { line : '' , words : [ ] , comments : [ 'Type: Pocket' ] } ,
201
+ { line : '' , words : [ ] , comments : [ 'Paths: 3' ] } ,
202
+ { line : '' , words : [ ] , comments : [ 'Direction: Conventional' ] } ,
203
+ { line : '' , words : [ ] , comments : [ 'Cut Depth: 3.175' ] } ,
204
+ { line : '' , words : [ ] , comments : [ 'Pass Depth: 1.9999999999999998' ] } ,
205
+ { line : '' , words : [ ] , comments : [ 'Plunge rate: 127' ] } ,
206
+ { line : '' , words : [ ] , comments : [ 'Cut rate: 1016' ] }
207
+ ] ) ;
167
208
done ( ) ;
168
209
} ) ;
169
210
} ) ;
170
- } ) ;
171
211
172
- describe ( 'Parentheses' , ( ) => {
173
212
it ( 'should remove anything inside parentheses.' , ( done ) => {
174
- const sampleText = [
213
+ const inputText = [
175
214
'(Generated with: DXF2GCODE, Version: Py3.4.4 PyQt5.4.1, Date: $Date: Sun Apr 17 16:32:22 2016 +0200 $)' ,
176
215
'(Created from file: G:/Dropbox/Konstruktionen/20161022 - MicroCopter 180/complete.dxf)' ,
177
216
'(Time: Sun Oct 23 12:30:46 2016)' ,
@@ -187,80 +226,60 @@ describe('Parentheses', () => {
187
226
] . join ( '\n' ) ;
188
227
const expectedResults = [
189
228
{
190
- gcode : '' ,
191
- cmds : undefined ,
229
+ line : '' ,
192
230
comments : [ 'Generated with: DXF2GCODE, Version: Py3.4.4 PyQt5.4.1, Date: $Date: Sun Apr 17 16:32:22 2016 +0200 $' ] ,
193
231
} ,
194
232
{
195
- gcode : '' ,
196
- cmds : undefined ,
233
+ line : '' ,
197
234
comments : [ 'Created from file: G:/Dropbox/Konstruktionen/20161022 - MicroCopter 180/complete.dxf' ] ,
198
235
} ,
199
236
{
200
- gcode : '' ,
201
- cmds : undefined ,
237
+ line : '' ,
202
238
comments : [ 'Time: Sun Oct 23 12:30:46 2016' ] ,
203
239
} ,
204
240
{
205
- gcode : 'G21G90' ,
206
- cmds : undefined ,
241
+ line : 'G21G90' ,
207
242
comments : [ 'Units in millimeters' , 'Absolute programming' ] ,
208
243
} ,
209
244
{
210
- gcode : '' ,
211
- cmds : [ '$H' ] ,
245
+ line : '$H' ,
212
246
comments : undefined ,
213
247
} ,
214
248
{
215
- gcode : 'F1000' ,
216
- cmds : undefined ,
249
+ line : 'F1000' ,
217
250
comments : undefined ,
218
251
} ,
219
252
{
220
- gcode : '' ,
221
- cmds : undefined ,
253
+ line : '' ,
222
254
comments : [ '*** LAYER: 0 ***' ] ,
223
255
} ,
224
256
{
225
- gcode : 'T5M6' ,
226
- cmds : undefined ,
257
+ line : 'T5M06' ,
227
258
comments : undefined ,
228
259
} ,
229
260
{
230
- gcode : 'S200' ,
231
- cmds : undefined ,
261
+ line : 'S200' ,
232
262
comments : undefined ,
233
263
} ,
234
264
{
235
- gcode : '' ,
236
- cmds : undefined ,
265
+ line : '' ,
237
266
comments : [ '* SHAPE Nr: 0 *' ] ,
238
267
} ,
239
268
{
240
- gcode : 'G0X180.327Y137.08' ,
241
- cmds : undefined ,
269
+ line : 'G0X180.327Y137.080' ,
242
270
comments : undefined ,
243
271
} ,
244
272
{
245
- gcode : 'M3' ,
246
- cmds : undefined ,
273
+ line : 'M03' ,
247
274
comments : undefined ,
248
275
} ,
249
276
] ;
250
277
251
- parseString ( sampleText , ( err , results ) => {
252
- results = results . map ( result => {
253
- const gcode = result . words . map ( word => {
254
- return word . join ( '' ) ;
255
- } ) . join ( '' ) ;
256
- const cmds = result . cmds ;
257
- const comments = result . comments ;
258
- return {
259
- gcode,
260
- cmds,
261
- comments,
262
- } ;
263
- } ) ;
278
+ parseString ( inputText , { lineMode : 'compact' } , ( err , results ) => {
279
+ results = results . map ( result => ( {
280
+ line : result . line ,
281
+ comments : result . comments ,
282
+ } ) ) ;
264
283
expect ( results ) . toEqual ( expectedResults ) ;
265
284
done ( ) ;
266
285
} ) ;
0 commit comments