Skip to content

Commit

Permalink
test_runner: emit diagnostic data with test points
Browse files Browse the repository at this point in the history
Also updated the lexer to better detect EOL and new lines
  • Loading branch information
manekinekko committed Oct 12, 2022
1 parent c0c9bc0 commit 5700383
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 104 deletions.
16 changes: 4 additions & 12 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class FileTest extends Test {

switch (kind) {
case TokenKind.TAP_VERSION:
// TODO(manekinekko): handle TAP version
// TODO(manekinekko): handle TAP version coming from the parser.
// this.reporter.version(node.version);
break;

Expand All @@ -149,8 +149,7 @@ class FileTest extends Test {
this.reporter.subtest(indent, node.name);
break;

case TokenKind.TAP_TEST_OK:
case TokenKind.TAP_TEST_NOTOK:
case TokenKind.TAP_TEST_POINT:
// eslint-disable-next-line no-case-declarations
const { todo, skip, pass } = node.status;
// eslint-disable-next-line no-case-declarations
Expand All @@ -169,27 +168,20 @@ class FileTest extends Test {
indent,
node.id,
node.description,
null,
details(node.diagnostics),
directive
);
} else {
this.reporter.fail(
indent,
node.id,
node.description,
null,
details(node.diagnostics),
directive
);
}
break;

case TokenKind.TAP_YAML_START:
break; // skip

case TokenKind.TAP_YAML_END:
this.reporter.details(indent, details(node.diagnostics));
break;

case TokenKind.COMMENT:
if (indent === kDefaultIndent) {
// Ignore file top level diagnostics
Expand Down
58 changes: 36 additions & 22 deletions lib/internal/test_runner/tap_lexer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
'use strict';

const { SafeSet, MathMax } = primordials;
const { SafeSet, MathMax, StringPrototypeIncludes } = primordials;
const {
codes: { ERR_TAP_LEXER_ERROR },
} = require('internal/errors');

const kEOL = '';
const kEOF = '';

const TokenKind = {
EOF: 'EOF',
EOL: 'EOL',
NEWLINE: 'NewLine',
NUMERIC: 'Numeric',
LITERAL: 'Literal',
KEYWORD: 'Keyword',
Expand Down Expand Up @@ -43,7 +47,7 @@ class Token {
line: stream.line,
column: MathMax(stream.column - valueLength + 1, 1), // 1 based
start: MathMax(stream.pos - valueLength, 0), // zero based
end: stream.pos - 1, // zero based
end: stream.pos - (value === '' ? 0 : 1), // zero based
};

// EOF is a special case
Expand Down Expand Up @@ -116,7 +120,7 @@ class TapLexer {
this.#source = new InputStream(source);
this.#lastScannedToken = new Token({
kind: TokenKind.EOL,
value: TokenKind.EOL,
value: kEOL,
stream: this.#source,
});
}
Expand All @@ -132,8 +136,8 @@ class TapLexer {
this.#lastScannedToken = token;
}

if (token.kind === TokenKind.EOL) {
// Store the current chunk + EOL token
if (token.kind === TokenKind.NEWLINE) {
// Store the current chunk + NEWLINE token
tokens.push([...chunk, token]);
chunk = [];
} else {
Expand All @@ -142,14 +146,11 @@ class TapLexer {
}

if (chunk.length > 0) {
tokens.push(chunk);
tokens.push([...chunk, this.#scanEOL()]);
}

if (tokens.length > 0) {
tokens.at(-1).push(this.#scanEOF());
} else {
tokens.push([this.#scanEOF()]);
}
// send EOF as a separate chunk
tokens.push([this.#scanEOF()]);

return tokens;
}
Expand All @@ -171,8 +172,8 @@ class TapLexer {

if (this.#isEOFSymbol(char)) {
return this.#scanEOF();
} else if (this.#isEOLSymbol(char)) {
return this.#scanEOL(char);
} else if (this.#isNewLineSymbol(char)) {
return this.#scanNewLine(char);
} else if (this.#isNumericSymbol(char)) {
return this.#scanNumeric(char);
} else if (this.#isDashSymbol(char)) {
Expand All @@ -196,7 +197,7 @@ class TapLexer {
);
}

#scanEOL(char) {
#scanNewLine(char) {
// In case of odd number of ESCAPE symbols, we need to clear the remaining
// escape chars from the stack and start fresh for the next line.
this.#escapeStack = [];
Expand All @@ -205,18 +206,26 @@ class TapLexer {
this.#isComment = false;

return new Token({
kind: TokenKind.EOL,
kind: TokenKind.NEWLINE,
value: char,
stream: this.#source,
});
}

#scanEOL() {
return new Token({
kind: TokenKind.EOL,
value: kEOL,
stream: this.#source,
});
}

#scanEOF() {
this.#isComment = false;

return new Token({
kind: TokenKind.EOF,
value: TokenKind.EOF,
value: kEOF,
stream: this.#source,
});
}
Expand Down Expand Up @@ -292,7 +301,7 @@ class TapLexer {

// If we encounter a hash symbol at the beginning of a line,
// we consider it as a comment
if (!lastCharacter || this.#isEOLSymbol(lastCharacter)) {
if (!lastCharacter || this.#isNewLineSymbol(lastCharacter)) {
this.#isComment = true;
return new Token({
kind: TokenKind.COMMENT,
Expand Down Expand Up @@ -371,7 +380,12 @@ class TapLexer {
}

#scanTAPKeyword(word) {
if (word === 'TAP' && this.#lastScannedToken.kind === TokenKind.EOL) {
const isLastScannedTokenEOLorNewLine = StringPrototypeIncludes(
[TokenKind.EOL, TokenKind.NEWLINE],
this.#lastScannedToken.kind
);

if (word === 'TAP' && isLastScannedTokenEOLorNewLine) {
return new Token({
kind: TokenKind.TAP,
value: word,
Expand All @@ -395,7 +409,7 @@ class TapLexer {
});
}

if (word === 'not' && this.#lastScannedToken.kind === TokenKind.EOL) {
if (word === 'not' && isLastScannedTokenEOLorNewLine) {
return new Token({
kind: TokenKind.TAP_TEST_NOTOK,
value: word,
Expand All @@ -406,7 +420,7 @@ class TapLexer {
if (
word === 'ok' &&
(this.#lastScannedToken.kind === TokenKind.TAP_TEST_NOTOK ||
this.#lastScannedToken.kind === TokenKind.EOL)
isLastScannedTokenEOLorNewLine)
) {
return new Token({
kind: TokenKind.TAP_TEST_OK,
Expand All @@ -415,7 +429,7 @@ class TapLexer {
});
}

if (word === 'pragma' && this.#lastScannedToken.kind === TokenKind.EOL) {
if (word === 'pragma' && isLastScannedTokenEOLorNewLine) {
return new Token({
kind: TokenKind.TAP_PRAGMA,
value: word,
Expand Down Expand Up @@ -476,7 +490,7 @@ class TapLexer {
return char === undefined;
}

#isEOLSymbol(char) {
#isNewLineSymbol(char) {
return char === '\n' || char === '\r';
}

Expand Down

0 comments on commit 5700383

Please sign in to comment.