Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Fix comment order #21

Merged
merged 4 commits into from
Jun 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- "0.8"
- "0.10"
- "0.12"
- "iojs"
50 changes: 35 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function Parser (options, onComplete) {
this.yind = ''
this.child = null
this.current = null
this.commentQueue = []

this.count = 0
this.pass = 0
Expand Down Expand Up @@ -263,6 +264,13 @@ Parser.prototype.bailout = function (reason) {
this.emit('bailout', reason)
}

Parser.prototype.clearCommentQueue = function () {
for (var c = 0; c < this.commentQueue.length; c++) {
this.emit('comment', this.commentQueue[c])
}
this.commentQueue.length = 0
}

Parser.prototype.emitResult = function () {
if (this.child) {
this.child.end()
Expand All @@ -273,7 +281,7 @@ Parser.prototype.emitResult = function () {
this.yind = ''

if (!this.current)
return
return this.clearCommentQueue()

var res = this.current
this.current = null
Expand All @@ -294,6 +302,7 @@ Parser.prototype.emitResult = function () {
this.todo++

this.emit('assert', res)
this.clearCommentQueue()
}

Parser.prototype.startChild = function (indent, line) {
Expand All @@ -320,6 +329,13 @@ Parser.prototype.startChild = function (indent, line) {
this.child.write(line.substr(indent.length))
}

Parser.prototype.emitComment = function (line) {
if (this.current || this.commentQueue.length)
this.commentQueue.push(line)
else
this.emit('comment', line)
}

Parser.prototype._parse = function (line) {
// normalize line endings
line = line.replace(/\r\n$/, '\n')
Expand All @@ -334,13 +350,6 @@ Parser.prototype._parse = function (line) {

this.emit('line', line)

// comment, but let "# Subtest:" comments start a child
var c = line.match(/^(\s+)?#(.*)/)
if (c && !(c[1] && /^ Subtest: /.test(c[2]))) {
this.emit('comment', line)
return
}

var bailout = line.match(/^bail out!(.*)\n$/i)
if (bailout) {
this.sawValidTap = true
Expand All @@ -359,20 +368,17 @@ Parser.prototype._parse = function (line) {
return
}

// if we got a plan at the end, or a 1..0 plan, then we can't
// have any more results, yamlish, or child sets.
if (this.postPlan) {
this.emit('extra', line)
return
}

// still belongs to the child.
if (this.child) {
if (line.indexOf(this.child.indent) === 0) {
line = line.substr(this.child.indent.length)
this.child.write(line)
return
}
if (line.trim().charAt(0) === '#') {
this.emitComment(line)
return
}
// a child test can only end when we get an test point line.
// anything else is extra.
if (this.child.sawValidTap && !/^(not )?ok/.test(line)) {
Expand All @@ -381,6 +387,20 @@ Parser.prototype._parse = function (line) {
}
}

// comment, but let "# Subtest:" comments start a child
var c = line.match(/^(\s+)?#(.*)/)
if (c && !(c[1] && /^ Subtest: /.test(c[2]))) {
this.emitComment(line)
return
}

// if we got a plan at the end, or a 1..0 plan, then we can't
// have any more results, yamlish, or child sets.
if (this.postPlan) {
this.emit('extra', line)
return
}

var indent = line.match(/^[ \t]+/)
if (indent) {
indent = indent[0]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"devDependencies": {
"glob": "^5.0.2",
"tap": "~0.4.6",
"tap": "^1.2.0",
"tape": "^3.5.0"
},
"scripts": {
Expand Down
12 changes: 8 additions & 4 deletions test/fixtures/child-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ module.exports =
[ 'comment', '# Subtest: test/debug-test.js\n' ],
[ 'line', 'TAP version 13\n' ],
[ 'version', '13' ],
[ 'line', '# debug test\n' ],
[ 'comment', '# debug test\n' ],
[ 'line', 'ok 1 Should output debugger message\n' ],
[ 'line', '1..1\n' ],
[ 'assert',
{ ok: true, id: 1, name: 'Should output debugger message' } ],
[ 'plan', { start: 1, end: 1 } ],
[ 'line', '# tests 1\n' ],
[ 'comment', '# tests 1\n' ],
[ 'line', '# pass 1\n' ],
[ 'comment', '# pass 1\n' ],
[ 'line', '# ok\n' ],
[ 'comment', '# ok\n' ],
[ 'complete',
{ ok: true, count: 1, pass: 1, plan: { start: 1, end: 1 } } ] ] ],
[ 'line', 'debug test\n' ],
Expand All @@ -22,17 +30,13 @@ module.exports =
[ 'extra', '\'Debugger listening on port 5858\\n\'\n' ],
[ 'line', ' TAP version 13\n' ],
[ 'line', ' # debug test\n' ],
[ 'comment', ' # debug test\n' ],
[ 'line', ' ok 1 Should output debugger message\n' ],
[ 'line', ' \n' ],
[ 'line', ' 1..1\n' ],
[ 'line', ' # tests 1\n' ],
[ 'comment', ' # tests 1\n' ],
[ 'line', ' # pass 1\n' ],
[ 'comment', ' # pass 1\n' ],
[ 'line', ' \n' ],
[ 'line', ' # ok\n' ],
[ 'comment', ' # ok\n' ],
[ 'line', 'ok 1 - test/debug-test.js # time=537.383ms\n' ],
[ 'line', '1..1\n' ],
[ 'assert',
Expand Down
59 changes: 59 additions & 0 deletions test/fixtures/comment-mid-diag-postplan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports =
[ [ 'line', '# before version\n' ],
[ 'comment', '# before version\n' ],
[ 'line', 'TAP version 13\n' ],
[ 'version', '13' ],
[ 'line', '# after version, before result\n' ],
[ 'comment', '# after version, before result\n' ],
[ 'line', 'not ok 1 - please keep my diags\n' ],
[ 'line', ' # before diag\n' ],
[ 'line', ' ---\n' ],
[ 'line', ' # mid diag\n' ],
[ 'line', ' after: comment\n' ],
[ 'line', ' ...\n' ],
[ 'assert',
{ ok: false,
id: 1,
name: 'please keep my diags',
diag: { after: 'comment' } } ],
[ 'comment', ' # before diag\n' ],
[ 'comment', ' # mid diag\n' ],
[ 'line', ' # after diag\n' ],
[ 'comment', ' # after diag\n' ],
[ 'line', ' # Subtest: child\n' ],
[ 'child',
[ [ 'line', '# Subtest: child\n' ],
[ 'comment', '# Subtest: child\n' ],
[ 'line', '1..2\n' ],
[ 'plan', { start: 1, end: 2 } ],
[ 'line', '# before 1\n' ],
[ 'comment', '# before 1\n' ],
[ 'line', 'ok 1\n' ],
[ 'line', '# before 2\n' ],
[ 'line', 'ok 2\n' ],
[ 'assert', { ok: true, id: 1 } ],
[ 'comment', '# before 2\n' ],
[ 'assert', { ok: true, id: 2 } ],
[ 'complete',
{ ok: true, count: 2, pass: 2, plan: { start: 1, end: 2 } } ] ] ],
[ 'line', ' 1..2\n' ],
[ 'line', ' # before 1\n' ],
[ 'line', ' ok 1\n' ],
[ 'line', ' # before 2\n' ],
[ 'line', ' ok 2\n' ],
[ 'line', '# before 2\n' ],
[ 'comment', '# before 2\n' ],
[ 'line', 'ok 2 - child\n' ],
[ 'line', '# after 2, brefore plan\n' ],
[ 'line', '1..2\n' ],
[ 'assert', { ok: true, id: 2, name: 'child' } ],
[ 'comment', '# after 2, brefore plan\n' ],
[ 'plan', { start: 1, end: 2 } ],
[ 'line', '# after plan\n' ],
[ 'comment', '# after plan\n' ],
[ 'complete',
{ ok: false,
count: 2,
pass: 1,
fail: 1,
plan: { start: 1, end: 2 } } ] ]
21 changes: 21 additions & 0 deletions test/fixtures/comment-mid-diag-postplan.tap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# before version
TAP version 13
# after version, before result
not ok 1 - please keep my diags
# before diag
---
# mid diag
after: comment
...
# after diag
# Subtest: child
1..2
# before 1
ok 1
# before 2
ok 2
# before 2
ok 2 - child
# after 2, brefore plan
1..2
# after plan
38 changes: 38 additions & 0 deletions test/fixtures/comment-mid-diag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports =
[ [ 'line', '# before version\n' ],
[ 'comment', '# before version\n' ],
[ 'line', 'TAP version 13\n' ],
[ 'version', '13' ],
[ 'line', '# after version, before plan\n' ],
[ 'comment', '# after version, before plan\n' ],
[ 'line', '1..2\n' ],
[ 'plan', { start: 1, end: 2 } ],
[ 'line', '# before result\n' ],
[ 'comment', '# before result\n' ],
[ 'line', 'not ok 1 - please keep my diags\n' ],
[ 'line', ' # before diag\n' ],
[ 'line', ' ---\n' ],
[ 'line', ' # mid diag\n' ],
[ 'line', ' after: comment\n' ],
[ 'line', ' ...\n' ],
[ 'assert',
{ ok: false,
id: 1,
name: 'please keep my diags',
diag: { after: 'comment' } } ],
[ 'comment', ' # before diag\n' ],
[ 'comment', ' # mid diag\n' ],
[ 'line', ' # after diag\n' ],
[ 'comment', ' # after diag\n' ],
[ 'line', '# before 2\n' ],
[ 'comment', '# before 2\n' ],
[ 'line', 'ok 2\n' ],
[ 'line', '# after 2\n' ],
[ 'assert', { ok: true, id: 2 } ],
[ 'comment', '# after 2\n' ],
[ 'complete',
{ ok: false,
count: 2,
pass: 1,
fail: 1,
plan: { start: 1, end: 2 } } ] ]
15 changes: 15 additions & 0 deletions test/fixtures/comment-mid-diag.tap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# before version
TAP version 13
# after version, before plan
1..2
# before result
not ok 1 - please keep my diags
# before diag
---
# mid diag
after: comment
...
# after diag
# before 2
ok 2
# after 2
3 changes: 2 additions & 1 deletion test/fixtures/extra-in-child.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ module.exports =
[ 'assert',
{ ok: true, id: 2, time: 24.16, name: 'update email' } ],
[ 'plan', { start: 1, end: 2 } ],
[ 'line', '# time=174.236ms\n' ],
[ 'comment', '# time=174.236ms\n' ],
[ 'complete',
{ ok: true, count: 2, pass: 2, plan: { start: 1, end: 2 } } ] ] ],
[ 'line', ' TAP version 13\n' ],
Expand Down Expand Up @@ -146,7 +148,6 @@ module.exports =
[ 'line', ' ok 2 - update email # time=24.16ms\n' ],
[ 'line', ' 1..2\n' ],
[ 'line', ' # time=174.236ms\n' ],
[ 'comment', ' # time=174.236ms\n' ],
[ 'line', 'ok 1 - test/01c-user-updates.js # time=339.14ms\n' ],
[ 'line', '1..1\n' ],
[ 'assert',
Expand Down
26 changes: 14 additions & 12 deletions test/fixtures/implicit-counter.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
module.exports =
[ [ 'line', 'TAP version 13\n' ],
[ 'version', '13' ],
[ 'line', '# beep\n' ],
[ 'comment', '# beep\n' ],
[ 'line', 'ok should be equal\n' ],
[ 'line', 'ok should be equivalent\n' ],
[ 'assert', { ok: true, id: 1, name: 'should be equal' } ],
[ 'line', '# boop\n' ],
[ 'comment', '# boop\n' ],
[ 'line', 'ok should be equal\n' ],
[ 'assert', { ok: true, id: 2, name: 'should be equivalent' } ],
[ 'line', 'ok (unnamed assert)\n' ],
[ 'assert', { ok: true, id: 3, name: 'should be equal' } ],
[ 'line', '# before 1\n' ],
[ 'comment', '# before 1\n' ],
[ 'line', 'ok one\n' ],
[ 'line', 'ok two\n' ],
[ 'assert', { ok: true, id: 1, name: 'one' } ],
[ 'line', '# before 3\n' ],
[ 'line', 'ok three\n' ],
[ 'assert', { ok: true, id: 2, name: 'two' } ],
[ 'comment', '# before 3\n' ],
[ 'line', 'ok four\n' ],
[ 'assert', { ok: true, id: 3, name: 'three' } ],
[ 'line', '# after 4\n' ],
[ 'line', '1..4\n' ],
[ 'assert', { ok: true, id: 4, name: '(unnamed assert)' } ],
[ 'assert', { ok: true, id: 4, name: 'four' } ],
[ 'comment', '# after 4\n' ],
[ 'plan', { start: 1, end: 4 } ],
[ 'line', '# tests 4\n' ],
[ 'comment', '# tests 4\n' ],
Expand Down
13 changes: 7 additions & 6 deletions test/fixtures/implicit-counter.tap
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
TAP version 13
# beep
ok should be equal
ok should be equivalent
# boop
ok should be equal
ok (unnamed assert)
# before 1
ok one
ok two
# before 3
ok three
ok four
# after 4

1..4
# tests 4
Expand Down
Loading