From 4f44c290219b1c1658e2ef634f310c963ae02230 Mon Sep 17 00:00:00 2001 From: Wassim Chegham Date: Mon, 27 Jun 2022 17:23:17 +0200 Subject: [PATCH] test_runner: add tests for TapParser --- lib/internal/test_runner/tap_lexer.js | 11 +- lib/internal/test_runner/tap_parser.js | 3 +- test/parallel/test-runner-parser.js | 386 +++++++++++++++++++++++++ 3 files changed, 396 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-runner-parser.js diff --git a/lib/internal/test_runner/tap_lexer.js b/lib/internal/test_runner/tap_lexer.js index 6c3756e9b6ccc2..38890d9b606aa6 100644 --- a/lib/internal/test_runner/tap_lexer.js +++ b/lib/internal/test_runner/tap_lexer.js @@ -29,10 +29,15 @@ class Token { this.value = value; this.location = { line: stream.line, - column: Math.max(stream.column - ('' + value).length + 1, 0), - start: stream.pos - ('' + value).length - 1, // zero based - end: stream.pos - 2, // zero based + column: Math.max(stream.column - ('' + value).length + 1, 1), + start: Math.max(stream.pos - ('' + value).length, 0), // zero based + end: stream.pos - 1, // zero based }; + + if (value === TokenKind.EOF) { + this.location.end = stream.pos; + this.location.column = stream.column; + } } } diff --git a/lib/internal/test_runner/tap_parser.js b/lib/internal/test_runner/tap_parser.js index f8394ef4949b9d..40f831a739e9aa 100644 --- a/lib/internal/test_runner/tap_parser.js +++ b/lib/internal/test_runner/tap_parser.js @@ -104,6 +104,7 @@ class TapParser { TokenKind.LITERAL, TokenKind.NUMERIC, TokenKind.DASH, + TokenKind.PLUS, TokenKind.WHITESPACE, TokenKind.ESCAPE, ].includes(nextToken.kind) @@ -557,7 +558,7 @@ class TapParser { const pragmas = {}; let nextToken = this.peek(); - while (nextToken) { + while (nextToken && [TokenKind.EOL, TokenKind.EOF].includes(nextToken.kind) === false) { let isEnabled = true; const pragmaKeySign = this.next(); if (pragmaKeySign.kind === TokenKind.PLUS) { diff --git a/test/parallel/test-runner-parser.js b/test/parallel/test-runner-parser.js new file mode 100644 index 00000000000000..e517037ea696d3 --- /dev/null +++ b/test/parallel/test-runner-parser.js @@ -0,0 +1,386 @@ +'use strict'; +// Flags: --expose-internals + +require('../common'); +const assert = require('assert'); + +const { TapParser } = require('internal/test_runner/tap_parser'); + +function TAPParser(input) { + const parser = new TapParser(input, { debug: false }); + return parser.parse(); +} + +{ + const ast = TAPParser(`TAP version 14`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + version: '14', + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`1..5 # reason`); + assert.deepStrictEqual(ast, { + root: { + documents: [{ plan: { start: '1', end: '5', reason: 'reason' } }], + }, + }); +} + +{ + const ast = TAPParser( + `1..5 # reason "\\ !"\\#$%&'()*+,\\-./:;<=>?@[]^_\`{|}~` + ); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + plan: { + start: '1', + end: '5', + reason: 'reason " !"\\#$%&\'()*+,-./:;<=>?@[]^_`{|}~', + }, + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`ok`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + status: 'passed', + id: '90001', + description: '', + }, + ], + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`not ok`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + status: 'failed', + id: '90001', + description: '', + }, + ], + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`ok 1`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + status: 'passed', + id: '1', + description: '', + }, + ], + }, + ], + }, + }); +} + +{ + const ast = TAPParser(` +ok 1 +not ok 2 +`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + status: 'passed', + id: '1', + }, + { + status: 'failed', + id: '2', + }, + ], + }, + ], + }, + }); +} + +{ + // a subtest is indented by 4 spaces + const ast = TAPParser(` +ok 1 + ok 1 +`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + status: 'passed', + id: '1', + }, + ], + documents: [ + { + tests: [ + { + status: 'passed', + id: '1', + }, + ], + }, + ], + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`ok 1 description`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + status: 'passed', + id: '1', + description: 'description', + }, + ], + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`ok 1 - description`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + status: 'passed', + id: '1', + description: 'description', + }, + ], + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`ok 1 - description # todo`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + status: 'todo', + id: '1', + description: 'description', + }, + ], + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`ok 1 - description \\# todo`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + status: 'passed', + id: '1', + description: 'description # todo', + }, + ], + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`ok 1 - description \\ # todo`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + status: 'passed', + id: '1', + description: 'description # todo', + }, + ], + }, + ], + }, + }); +} + +{ + const ast = TAPParser( + `ok 1 description \\# \\\\ world # TODO escape \\# characters with \\\\` + ); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + status: 'todo', + id: '1', + description: 'description # \\ world', + reason: 'escape # characters with \\', + }, + ], + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`ok 1 - description # ##`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + status: 'passed', + id: '1', + description: 'description', + reason: '##', + }, + ], + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`# comment`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + comments: ['comment'], + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`#`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + comments: [''], + }, + ], + }, + }); +} + +{ + // note the leading 2 spaces! + const ast = TAPParser(` + --- + message: "description" + severity: fail + ... +`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + tests: [ + { + diagnostics: [ + ' message: "description"', + ' severity: fail', + ' ', + ], + }, + ], + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`pragma +strict, -warnings`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + pragmas: { + strict: true, + warnings: false, + }, + }, + ], + }, + }); +} + +{ + const ast = TAPParser(`Bail out! Error`); + assert.deepStrictEqual(ast, { + root: { + documents: [ + { + bailout: 'Error', + }, + ], + }, + }); +}