|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import { Tokenizer } from '../Tokenizer'; |
| 5 | +import { Parser } from '../Parser'; |
| 6 | +import { AstScript } from '../AstNode'; |
| 7 | + |
| 8 | +function escape(s: string): string { |
| 9 | + return s.replace(/\n/g, '[n]') |
| 10 | + .replace(/\r/g, '[r]') |
| 11 | + .replace(/\t/g, '[t]') |
| 12 | + .replace(/\\/g, '[b]'); |
| 13 | +} |
| 14 | + |
| 15 | +function matchSnapshot(input: string): void { |
| 16 | + const tokenizer: Tokenizer = new Tokenizer(input); |
| 17 | + const parser: Parser = new Parser(tokenizer); |
| 18 | + const result: AstScript = parser.parse(); |
| 19 | + expect({ |
| 20 | + input: escape(tokenizer.input.toString()), |
| 21 | + tree: result.getDump() |
| 22 | + }).toMatchSnapshot(); |
| 23 | +} |
| 24 | + |
| 25 | +function matchErrorSnapshot(input: string): void { |
| 26 | + const tokenizer: Tokenizer = new Tokenizer(input); |
| 27 | + const parser: Parser = new Parser(tokenizer); |
| 28 | + let error: Error | undefined = undefined; |
| 29 | + try { |
| 30 | + const result: AstScript = parser.parse(); |
| 31 | + } catch (e) { |
| 32 | + error = e; |
| 33 | + } |
| 34 | + expect({ |
| 35 | + input: escape(tokenizer.input.toString()), |
| 36 | + reportedError: error |
| 37 | + }).toMatchSnapshot(); |
| 38 | +} |
| 39 | + |
| 40 | +test('00: basic inputs', () => { |
| 41 | + matchSnapshot('command arg1 arg2'); |
| 42 | +}); |
| 43 | + |
| 44 | +test('01: basic errors', () => { |
| 45 | + matchErrorSnapshot('@bad'); |
| 46 | + matchErrorSnapshot('command @bad'); |
| 47 | +}); |
0 commit comments