1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 | const MiniPass = require('minipass')
|
6 |
|
7 | const yaml = require('tap-yaml')
|
8 | const util = require('util')
|
9 | const assert = require('assert')
|
10 |
|
11 |
|
12 |
|
13 | const lineTypes = {
|
14 | testPoint: /^(not )?ok(?: ([0-9]+))?(?:(?: -)?( .*?))?(\{?)\n$/,
|
15 | pragma: /^pragma ([+-])([a-z]+)\n$/,
|
16 | bailout: /^bail out!(.*)\n$/i,
|
17 | version: /^TAP version ([0-9]+)\n$/i,
|
18 | childVersion: /^( )+TAP version ([0-9]+)\n$/i,
|
19 | plan: /^([0-9]+)\.\.([0-9]+)(?:\s+(?:#\s*(.*)))?\n$/,
|
20 | subtest: /^# Subtest(?:: (.*))?\n$/,
|
21 | subtestIndent: /^ # Subtest(?:: (.*))?\n$/,
|
22 | comment: /^\s*#.*\n$/
|
23 | }
|
24 |
|
25 | const lineTypeNames = Object.keys(lineTypes)
|
26 |
|
27 | const lineType = line => {
|
28 | for (let t in lineTypes) {
|
29 | const match = line.match(lineTypes[t])
|
30 | if (match)
|
31 | return [t, match]
|
32 | }
|
33 | return null
|
34 | }
|
35 |
|
36 | const parseDirective = line => {
|
37 | if (!line.trim())
|
38 | return false
|
39 |
|
40 | line = line.replace(/\{\s*$/, '').trim()
|
41 | const time = line.match(/^time=((?:[1-9][0-9]*|0)(?:\.[0-9]+)?)(ms|s)$/i)
|
42 | if (time) {
|
43 | let n = +time[1]
|
44 | if (time[2] === 's') {
|
45 |
|
46 | n *= 1000000
|
47 | n = Math.round(n)
|
48 | n /= 1000
|
49 | }
|
50 | return [ 'time', n ]
|
51 | }
|
52 |
|
53 | const type = line.match(/^(todo|skip)\b/i)
|
54 | if (!type)
|
55 | return false
|
56 |
|
57 | return [ type[1].toLowerCase(), line.substr(type[1].length).trim() || true ]
|
58 | }
|
59 |
|
60 | class Result {
|
61 | constructor (parsed, count) {
|
62 | const ok = !parsed[1]
|
63 | const id = +(parsed[2] || count + 1)
|
64 | let buffered = parsed[4]
|
65 | this.ok = ok
|
66 | this.id = id
|
67 |
|
68 | let rest = parsed[3] || ''
|
69 | let name
|
70 | rest = rest.replace(/([^\\]|^)((?:\\\\)*)#/g, '$1\n$2').split('\n')
|
71 | name = rest.shift()
|
72 | rest = rest.filter(r => r.trim()).join('#')
|
73 |
|
74 |
|
75 | const dir = parseDirective(rest.trim())
|
76 | if (!dir)
|
77 | name += (rest ? '#' + rest : '') + buffered
|
78 | else {
|
79 |
|
80 |
|
81 | const dirKey = dir[0]
|
82 | const dirValue = dir[1]
|
83 | this[dirKey] = dirValue
|
84 | }
|
85 |
|
86 | if (/\{\s*$/.test(name)) {
|
87 | name = name.replace(/\{\s*$/, '')
|
88 | buffered = '{'
|
89 | }
|
90 |
|
91 | if (buffered === '{')
|
92 | this.buffered = true
|
93 |
|
94 | if (name)
|
95 | this.name = name.trim()
|
96 | }
|
97 | }
|
98 |
|
99 | class Parser extends MiniPass {
|
100 | constructor (options, onComplete) {
|
101 | if (typeof options === 'function') {
|
102 | onComplete = options
|
103 | options = {}
|
104 | }
|
105 |
|
106 | options = options || {}
|
107 | super(options)
|
108 | this.resume()
|
109 |
|
110 | if (onComplete)
|
111 | this.on('complete', onComplete)
|
112 |
|
113 | this.comments = []
|
114 | this.results = null
|
115 | this.braceLevel = null
|
116 | this.parent = options.parent || null
|
117 | this.failures = []
|
118 | if (options.passes)
|
119 | this.passes = []
|
120 | this.level = options.level || 0
|
121 |
|
122 | this.buffer = ''
|
123 | this.bail = !!options.bail
|
124 | this.bailingOut = false
|
125 | this.bailedOut = false
|
126 | this.syntheticBailout = false
|
127 | this.syntheticPlan = false
|
128 | this.omitVersion = !!options.omitVersion
|
129 | this.planStart = -1
|
130 | this.planEnd = -1
|
131 | this.planComment = ''
|
132 | this.yamlish = ''
|
133 | this.yind = ''
|
134 | this.child = null
|
135 | this.current = null
|
136 | this.maybeSubtest = null
|
137 | this.extraQueue = []
|
138 | this.buffered = options.buffered || null
|
139 | this.aborted = false
|
140 | this.preserveWhitespace = options.preserveWhitespace || false
|
141 |
|
142 | this.count = 0
|
143 | this.pass = 0
|
144 | this.fail = 0
|
145 | this.todo = 0
|
146 | this.skip = 0
|
147 | this.ok = true
|
148 |
|
149 | this.strict = options.strict || false
|
150 | this.pragmas = { strict: this.strict }
|
151 |
|
152 | this.postPlan = false
|
153 | }
|
154 |
|
155 | get fullname () {
|
156 | return ((this.parent ? this.parent.fullname + ' ' : '') +
|
157 | (this.name || '')).trim()
|
158 | }
|
159 |
|
160 | tapError (error, line) {
|
161 | if (line)
|
162 | this.emit('line', line)
|
163 | this.ok = false
|
164 | this.fail ++
|
165 | if (typeof error === 'string') {
|
166 | error = {
|
167 | tapError: error
|
168 | }
|
169 | }
|
170 | this.failures.push(error)
|
171 | }
|
172 |
|
173 | parseTestPoint (testPoint, line) {
|
174 | this.emitResult()
|
175 | if (this.bailedOut)
|
176 | return
|
177 |
|
178 | this.emit('line', line)
|
179 | const res = new Result(testPoint, this.count)
|
180 | if (this.planStart !== -1) {
|
181 | const lessThanStart = +res.id < this.planStart
|
182 | const greaterThanEnd = +res.id > this.planEnd
|
183 | if (lessThanStart || greaterThanEnd) {
|
184 | if (lessThanStart)
|
185 | res.tapError = 'id less than plan start'
|
186 | else
|
187 | res.tapError = 'id greater than plan end'
|
188 | res.plan = { start: this.planStart, end: this.planEnd }
|
189 | this.tapError(res)
|
190 | }
|
191 | }
|
192 |
|
193 | if (res.id) {
|
194 | if (!this.first || res.id < this.first)
|
195 | this.first = res.id
|
196 | if (!this.last || res.id > this.last)
|
197 | this.last = res.id
|
198 | }
|
199 |
|
200 | if (!res.skip && !res.todo)
|
201 | this.ok = this.ok && res.ok
|
202 |
|
203 |
|
204 | this.current = res
|
205 | }
|
206 |
|
207 | nonTap (data, didLine) {
|
208 | if (this.bailingOut && /^( {4})*\}\n$/.test(data))
|
209 | return
|
210 |
|
211 | if (this.strict) {
|
212 | const err = {
|
213 | tapError: 'Non-TAP data encountered in strict mode',
|
214 | data: data
|
215 | }
|
216 | this.tapError(err)
|
217 | if (this.parent)
|
218 | this.parent.tapError(err)
|
219 | }
|
220 |
|
221 |
|
222 | if (!didLine)
|
223 | data.split('\n').slice(0, -1).forEach(line => {
|
224 | line += '\n'
|
225 | if (this.current || this.extraQueue.length)
|
226 | this.extraQueue.push(['line', line])
|
227 | else
|
228 | this.emit('line', line)
|
229 | })
|
230 |
|
231 | if (this.current || this.extraQueue.length)
|
232 | this.extraQueue.push(['extra', data])
|
233 | else
|
234 | this.emit('extra', data)
|
235 | }
|
236 |
|
237 | plan (start, end, comment, line) {
|
238 |
|
239 | if (this.planStart !== -1) {
|
240 | this.nonTap(line)
|
241 | return
|
242 | }
|
243 |
|
244 |
|
245 | if (this.child || this.yind) {
|
246 | this.nonTap(line)
|
247 | return
|
248 | }
|
249 |
|
250 | this.emitResult()
|
251 | if (this.bailedOut)
|
252 | return
|
253 |
|
254 |
|
255 | if (end < start && end !== 0 && start !== 1) {
|
256 | if (this.strict)
|
257 | this.tapError({
|
258 | tapError: 'plan end cannot be less than plan start',
|
259 | plan: {
|
260 | start: start,
|
261 | end: end
|
262 | }
|
263 | }, line)
|
264 | else
|
265 | this.nonTap(line)
|
266 | return
|
267 | }
|
268 |
|
269 | this.planStart = start
|
270 | this.planEnd = end
|
271 | const p = { start: start, end: end }
|
272 | if (comment)
|
273 | this.planComment = p.comment = comment
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | if (this.count !== 0 || this.planEnd === 0)
|
280 | this.postPlan = true
|
281 |
|
282 | this.emit('line', line)
|
283 | this.emit('plan', p)
|
284 | }
|
285 |
|
286 | resetYamlish () {
|
287 | this.yind = ''
|
288 | this.yamlish = ''
|
289 | }
|
290 |
|
291 |
|
292 | yamlGarbage () {
|
293 | const yamlGarbage = this.yind + '---\n' + this.yamlish
|
294 | this.emitResult()
|
295 | if (this.bailedOut)
|
296 | return
|
297 | this.nonTap(yamlGarbage, true)
|
298 | }
|
299 |
|
300 | yamlishLine (line) {
|
301 | if (line === this.yind + '...\n') {
|
302 |
|
303 | this.processYamlish()
|
304 | } else {
|
305 | this.yamlish += line
|
306 | }
|
307 | }
|
308 |
|
309 | processYamlish () {
|
310 | const yamlish = this.yamlish
|
311 | this.resetYamlish()
|
312 |
|
313 | let diags
|
314 | try {
|
315 | diags = yaml.parse(yamlish)
|
316 | } catch (er) {
|
317 | this.nonTap(this.yind + '---\n' + yamlish + this.yind + '...\n', true)
|
318 | return
|
319 | }
|
320 |
|
321 | this.current.diag = diags
|
322 |
|
323 |
|
324 | }
|
325 |
|
326 | write (chunk, encoding, cb) {
|
327 | if (this.aborted)
|
328 | return
|
329 |
|
330 | if (typeof encoding === 'string' && encoding !== 'utf8')
|
331 | chunk = new Buffer(chunk, encoding)
|
332 |
|
333 | if (Buffer.isBuffer(chunk))
|
334 | chunk += ''
|
335 |
|
336 | if (typeof encoding === 'function') {
|
337 | cb = encoding
|
338 | encoding = null
|
339 | }
|
340 |
|
341 | this.buffer += chunk
|
342 | do {
|
343 | const match = this.buffer.match(/^.*\r?\n/)
|
344 | if (!match)
|
345 | break
|
346 |
|
347 | this.buffer = this.buffer.substr(match[0].length)
|
348 | this.parse(match[0])
|
349 | } while (this.buffer.length)
|
350 |
|
351 | if (cb)
|
352 | process.nextTick(cb)
|
353 |
|
354 | return true
|
355 | }
|
356 |
|
357 | end (chunk, encoding, cb) {
|
358 | if (chunk) {
|
359 | if (typeof encoding === 'function') {
|
360 | cb = encoding
|
361 | encoding = null
|
362 | }
|
363 | this.write(chunk, encoding)
|
364 | }
|
365 |
|
366 | if (this.buffer)
|
367 | this.write('\n')
|
368 |
|
369 |
|
370 | if (this.yamlish)
|
371 | this.yamlGarbage()
|
372 |
|
373 | this.emitResult()
|
374 |
|
375 | if (this.syntheticBailout && this.level === 0) {
|
376 | this.syntheticBailout = false
|
377 | let reason = this.bailedOut
|
378 | if (reason === true)
|
379 | reason = ''
|
380 | else
|
381 | reason = ' ' + reason
|
382 | this.emit('line', 'Bail out!' + reason + '\n')
|
383 | }
|
384 |
|
385 | let skipAll
|
386 |
|
387 | if (this.planEnd === 0 && this.planStart === 1) {
|
388 | skipAll = true
|
389 | if (this.count === 0) {
|
390 | this.ok = true
|
391 | } else {
|
392 | this.tapError('Plan of 1..0, but test points encountered')
|
393 | }
|
394 | } else if (!this.bailedOut && this.planStart === -1) {
|
395 | if (this.count === 0 && !this.syntheticPlan) {
|
396 | this.syntheticPlan = true
|
397 | if (this.buffered) {
|
398 | this.planStart = 1
|
399 | this.planEnd = 0
|
400 | } else
|
401 | this.plan(1, 0, 'no tests found', '1..0 # no tests found\n')
|
402 | skipAll = true
|
403 | } else {
|
404 | this.tapError('no plan')
|
405 | }
|
406 | } else if (this.ok && this.count !== (this.planEnd - this.planStart + 1)) {
|
407 | this.tapError('incorrect number of tests')
|
408 | }
|
409 |
|
410 | if (this.ok && !skipAll && this.first !== this.planStart) {
|
411 | this.tapError('first test id does not match plan start')
|
412 | }
|
413 |
|
414 | if (this.ok && !skipAll && this.last !== this.planEnd) {
|
415 | this.tapError('last test id does not match plan end')
|
416 | }
|
417 |
|
418 | this.emitComplete(skipAll)
|
419 | if (cb)
|
420 | process.nextTick(cb)
|
421 | }
|
422 |
|
423 | emitComplete (skipAll) {
|
424 | if (!this.results) {
|
425 | const res = this.results = new FinalResults(!!skipAll, this)
|
426 |
|
427 | if (!res.bailout) {
|
428 |
|
429 |
|
430 | if (res.plan.end !== res.count)
|
431 | this.emitComment('test count(' + res.count +
|
432 | ') != plan(' + res.plan.end + ')', false, true)
|
433 |
|
434 | if (res.fail > 0 && !res.ok)
|
435 | this.emitComment('failed ' + res.fail +
|
436 | (res.count > 1 ? ' of ' + res.count + ' tests'
|
437 | : ' test'),
|
438 | false, true)
|
439 |
|
440 | if (res.todo > 0)
|
441 | this.emitComment('todo: ' + res.todo, false, true)
|
442 |
|
443 | if (res.skip > 0)
|
444 | this.emitComment('skip: ' + res.skip, false, true)
|
445 | }
|
446 |
|
447 | this.emit('complete', this.results)
|
448 | }
|
449 | }
|
450 |
|
451 | version (version, line) {
|
452 |
|
453 | if (version >= 13 &&
|
454 | this.planStart === -1 &&
|
455 | this.count === 0 &&
|
456 | !this.current) {
|
457 | this.emit('line', line)
|
458 | this.emit('version', version)
|
459 | } else
|
460 | this.nonTap(line)
|
461 | }
|
462 |
|
463 | pragma (key, value, line) {
|
464 |
|
465 | if (this.child) {
|
466 | this.nonTap(line)
|
467 | return
|
468 | }
|
469 |
|
470 | this.emitResult()
|
471 | if (this.bailedOut)
|
472 | return
|
473 |
|
474 | if (key === 'strict') {
|
475 | this.strict = value
|
476 | }
|
477 | this.pragmas[key] = value
|
478 | this.emit('line', line)
|
479 | this.emit('pragma', key, value)
|
480 | }
|
481 |
|
482 | bailout (reason, synthetic) {
|
483 | this.syntheticBailout = synthetic
|
484 |
|
485 | if (this.bailingOut)
|
486 | return
|
487 |
|
488 |
|
489 |
|
490 | this.bailingOut = reason || true
|
491 |
|
492 | if (!synthetic)
|
493 | this.emitResult()
|
494 | else
|
495 | this.current = null
|
496 |
|
497 | this.bailedOut = this.bailingOut
|
498 | this.ok = false
|
499 | if (!synthetic) {
|
500 |
|
501 | let line = 'Bail out!'
|
502 | if (reason)
|
503 | line += ' ' + reason
|
504 | this.emit('line', line + '\n')
|
505 | }
|
506 | this.emit('bailout', reason)
|
507 | if (this.parent) {
|
508 | this.end()
|
509 | this.parent.bailout(reason, true)
|
510 | }
|
511 | }
|
512 |
|
513 | clearExtraQueue () {
|
514 | for (let c = 0; c < this.extraQueue.length; c++) {
|
515 | this.emit(this.extraQueue[c][0], this.extraQueue[c][1])
|
516 | }
|
517 | this.extraQueue.length = 0
|
518 | }
|
519 |
|
520 | endChild () {
|
521 | if (this.child && (!this.bailingOut || this.child.count)) {
|
522 | this.child.end()
|
523 | this.child = null
|
524 | }
|
525 | }
|
526 |
|
527 | emitResult () {
|
528 | if (this.bailedOut)
|
529 | return
|
530 |
|
531 | this.endChild()
|
532 | this.resetYamlish()
|
533 |
|
534 | if (!this.current)
|
535 | return this.clearExtraQueue()
|
536 |
|
537 | const res = this.current
|
538 | this.current = null
|
539 |
|
540 | this.count++
|
541 | if (res.ok) {
|
542 | this.pass++
|
543 | if (this.passes)
|
544 | this.passes.push(res)
|
545 | } else {
|
546 | this.fail++
|
547 | if (!res.todo && !res.skip) {
|
548 | this.ok = false
|
549 | this.failures.push(res)
|
550 | }
|
551 | }
|
552 |
|
553 | if (res.skip)
|
554 | this.skip++
|
555 |
|
556 | if (res.todo)
|
557 | this.todo++
|
558 |
|
559 | this.emit('assert', res)
|
560 | if (this.bail && !res.ok && !res.todo && !res.skip && !this.bailingOut) {
|
561 | this.maybeChild = null
|
562 | const ind = new Array(this.level + 1).join(' ')
|
563 | let p
|
564 | for (p = this; p.parent; p = p.parent);
|
565 | const bailName = res.name ? ' # ' + res.name : ''
|
566 | p.parse(ind + 'Bail out!' + bailName + '\n')
|
567 | }
|
568 | this.clearExtraQueue()
|
569 | }
|
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 | startChild (line) {
|
577 | const maybeBuffered = this.current && this.current.buffered
|
578 | const unindentStream = !maybeBuffered && this.maybeChild
|
579 | const indentStream = !maybeBuffered && !unindentStream &&
|
580 | lineTypes.subtestIndent.test(line)
|
581 | const unnamed = !maybeBuffered && !unindentStream && !indentStream
|
582 |
|
583 |
|
584 |
|
585 |
|
586 | if (!maybeBuffered)
|
587 | this.emitResult()
|
588 |
|
589 | if (this.bailedOut)
|
590 | return
|
591 |
|
592 | this.child = new Parser({
|
593 | bail: this.bail,
|
594 | parent: this,
|
595 | level: this.level + 1,
|
596 | buffered: maybeBuffered,
|
597 | preserveWhitespace: this.preserveWhitespace,
|
598 | omitVersion: true,
|
599 | strict: this.strict
|
600 | })
|
601 |
|
602 | this.child.on('complete', results => {
|
603 | if (!results.ok)
|
604 | this.ok = false
|
605 | })
|
606 |
|
607 | this.child.on('line', l => {
|
608 | if (l.trim() || this.preserveWhitespace)
|
609 | l = ' ' + l
|
610 | this.emit('line', l)
|
611 | })
|
612 |
|
613 |
|
614 |
|
615 |
|
616 | line = line.substr(4)
|
617 | let subtestComment
|
618 | if (indentStream) {
|
619 | subtestComment = line
|
620 | line = null
|
621 | } else if (maybeBuffered) {
|
622 | subtestComment = '# Subtest: ' + this.current.name + '\n'
|
623 | } else {
|
624 | subtestComment = this.maybeChild || '# Subtest\n'
|
625 | }
|
626 |
|
627 | this.maybeChild = null
|
628 | this.child.name = subtestComment.substr('# Subtest: '.length).trim()
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 | if (!this.child.buffered)
|
635 | this.emit('line', subtestComment)
|
636 | this.emit('child', this.child)
|
637 | this.child.emitComment(subtestComment, true)
|
638 | if (line)
|
639 | this.child.parse(line)
|
640 | }
|
641 |
|
642 | abort (message, extra) {
|
643 | if (this.child) {
|
644 | const b = this.child.buffered
|
645 | this.child.abort(message, extra)
|
646 | extra = null
|
647 | if (b)
|
648 | this.write('\n}\n')
|
649 | }
|
650 |
|
651 | let dump
|
652 | if (extra && Object.keys(extra).length) {
|
653 | try {
|
654 | dump = yaml.stringify(extra).trimRight()
|
655 | } catch (er) {}
|
656 | }
|
657 |
|
658 | let y
|
659 | if (dump)
|
660 | y = ' ---\n ' + dump.split('\n').join('\n ') + '\n ...\n'
|
661 | else
|
662 | y = '\n'
|
663 | let n = (this.count || 0) + 1
|
664 | if (this.current)
|
665 | n += 1
|
666 |
|
667 | if (this.planEnd !== -1 && this.planEnd < n && this.parent) {
|
668 |
|
669 | this.aborted = true
|
670 | return
|
671 | }
|
672 |
|
673 | let ind = ''
|
674 | message = message.replace(/[\n\r\s\t]/g, ' ')
|
675 | let point = '\nnot ok ' + n + ' - ' + message + '\n' + y
|
676 |
|
677 | if (this.planEnd === -1)
|
678 | point += '1..' + n + '\n'
|
679 |
|
680 | this.write(point)
|
681 | this.aborted = true
|
682 | this.end()
|
683 | }
|
684 |
|
685 | emitComment (line, skipLine, noDuplicate) {
|
686 | if (line.trim().charAt(0) !== '#')
|
687 | line = '# ' + line
|
688 |
|
689 | if (line.slice(-1) !== '\n')
|
690 | line += '\n'
|
691 |
|
692 | if (noDuplicate && this.comments.indexOf(line) !== -1)
|
693 | return
|
694 |
|
695 | this.comments.push(line)
|
696 | if (this.current || this.extraQueue.length) {
|
697 |
|
698 | this.extraQueue.push(['line', line])
|
699 | this.extraQueue.push(['comment', line])
|
700 | } else {
|
701 | if (!skipLine)
|
702 | this.emit('line', line)
|
703 | this.emit('comment', line)
|
704 | }
|
705 | }
|
706 |
|
707 | parse (line) {
|
708 |
|
709 | line = line.replace(/\r\n$/, '\n')
|
710 |
|
711 |
|
712 |
|
713 | if (line === '\n') {
|
714 | if (this.child)
|
715 | line = ' ' + line
|
716 | else if (this.yind)
|
717 | line = this.yind + line
|
718 | }
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 | if (this.bailingOut) {
|
726 | if (!/^\s*}\n$/.test(line))
|
727 | return
|
728 | else if (!this.braceLevel || line.length < this.braceLevel)
|
729 | this.braceLevel = line.length
|
730 | else
|
731 | return
|
732 | }
|
733 |
|
734 |
|
735 |
|
736 |
|
737 | if (this.omitVersion && lineTypes.version.test(line) && !this.yind)
|
738 | return
|
739 |
|
740 |
|
741 |
|
742 | const indent = line.match(/^[ \t]*/)[0]
|
743 | if (indent) {
|
744 | this.parseIndent(line, indent)
|
745 | return
|
746 | }
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 |
|
753 |
|
754 |
|
755 | if (this.child && this.child.buffered && line === '}\n') {
|
756 | this.endChild()
|
757 | this.emit('line', line)
|
758 | this.emitResult()
|
759 | return
|
760 | }
|
761 |
|
762 |
|
763 | const validLine = this.preserveWhitespace || line.trim() || this.yind
|
764 | if (line === '\n')
|
765 | return validLine && this.emit('line', line)
|
766 |
|
767 |
|
768 | if (this.current && line === '{\n' &&
|
769 | !this.current.buffered &&
|
770 | !this.child) {
|
771 | this.emit('line', line)
|
772 | this.current.buffered = true
|
773 | return
|
774 | }
|
775 |
|
776 |
|
777 |
|
778 | const type = lineType(line)
|
779 | if (!type) {
|
780 | this.nonTap(line)
|
781 | return
|
782 | }
|
783 |
|
784 | if (type[0] === 'comment') {
|
785 | this.emitComment(line)
|
786 | return
|
787 | }
|
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 | if (this.yind)
|
794 | this.yamlGarbage()
|
795 |
|
796 |
|
797 |
|
798 | if (this.maybeChild) {
|
799 | this.emitComment(this.maybeChild)
|
800 | this.maybeChild = null
|
801 | }
|
802 |
|
803 |
|
804 | if (this.postPlan) {
|
805 | this.nonTap(line)
|
806 | return
|
807 | }
|
808 |
|
809 |
|
810 | if (type[0] === 'bailout') {
|
811 | this.bailout(type[1][1].trim(), false)
|
812 | return
|
813 | }
|
814 |
|
815 | if (type[0] === 'pragma') {
|
816 | const pragma = type[1]
|
817 | this.pragma(pragma[2], pragma[1] === '+', line)
|
818 | return
|
819 | }
|
820 |
|
821 | if (type[0] === 'version') {
|
822 | const version = type[1]
|
823 | this.version(parseInt(version[1], 10), line)
|
824 | return
|
825 | }
|
826 |
|
827 | if (type[0] === 'plan') {
|
828 | const plan = type[1]
|
829 | this.plan(+plan[1], +plan[2], (plan[3] || '').trim(), line)
|
830 | return
|
831 | }
|
832 |
|
833 |
|
834 | if (type[0] === 'testPoint') {
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 | this.parseTestPoint(type[1], line)
|
846 | return
|
847 | }
|
848 |
|
849 |
|
850 |
|
851 |
|
852 |
|
853 | if (type[0] === 'subtest') {
|
854 |
|
855 |
|
856 | this.maybeChild = line
|
857 | } else {
|
858 | throw new Error('Unhandled case: ' + type[0])
|
859 | }
|
860 | }
|
861 |
|
862 | parseIndent (line, indent) {
|
863 |
|
864 | if (this.child && line.substr(0, 4) === ' ') {
|
865 | line = line.substr(4)
|
866 | this.child.write(line)
|
867 | return
|
868 | }
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 | if (this.yind) {
|
880 | if (line.indexOf(this.yind) === 0) {
|
881 | this.emit('line', line)
|
882 | this.yamlishLine(line)
|
883 | return
|
884 | } else {
|
885 |
|
886 |
|
887 |
|
888 | this.yamlGarbage()
|
889 | }
|
890 | }
|
891 |
|
892 |
|
893 |
|
894 | if (this.current && !this.yind && line === indent + '---\n') {
|
895 | this.yind = indent
|
896 | this.emit('line', line)
|
897 | return
|
898 | }
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 | if (line.substr(0, 4) === ' ') {
|
905 | if (this.maybeChild ||
|
906 | this.current && this.current.buffered ||
|
907 | lineTypes.subtestIndent.test(line)) {
|
908 | this.startChild(line)
|
909 | return
|
910 | }
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 | const s = line.match(/( {4})+(.*\n)$/)
|
926 | if (s[2].charAt(0) !== ' ') {
|
927 |
|
928 | const type = lineType(s[2])
|
929 | if (type) {
|
930 | if (type[0] === 'comment') {
|
931 | this.emit('line', line)
|
932 | this.emitComment(line)
|
933 | } else {
|
934 |
|
935 | this.startChild(line)
|
936 | }
|
937 | return
|
938 | }
|
939 | }
|
940 | }
|
941 |
|
942 |
|
943 |
|
944 | if (lineTypes.comment.test(line)) {
|
945 | this.emitComment(line)
|
946 | return
|
947 | }
|
948 |
|
949 | this.nonTap(line)
|
950 | }
|
951 | }
|
952 |
|
953 | class FinalResults {
|
954 | constructor (skipAll, self) {
|
955 | this.ok = self.ok
|
956 | this.count = self.count
|
957 | this.pass = self.pass
|
958 | this.fail = self.fail || 0
|
959 | this.bailout = self.bailedOut || false
|
960 | this.todo = self.todo || 0
|
961 | this.skip = skipAll ? self.count : self.skip || 0
|
962 | this.plan = new FinalPlan(skipAll, self)
|
963 | this.failures = self.failures
|
964 | if (self.passes)
|
965 | this.passes = self.passes
|
966 | }
|
967 | }
|
968 |
|
969 | class FinalPlan {
|
970 | constructor (skipAll, self) {
|
971 | this.start = self.planStart === -1 ? null : self.planStart
|
972 | this.end = self.planStart === -1 ? null : self.planEnd
|
973 | this.skipAll = skipAll
|
974 | this.skipReason = skipAll ? self.planComment : ''
|
975 | this.comment = self.planComment || ''
|
976 | }
|
977 | }
|
978 |
|
979 | module.exports = Parser
|