From 5ec283a3592dfdef7aff34ab00ca8685c4d37470 Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Sun, 6 Sep 2020 07:53:35 -0500 Subject: Overhaul Javascript numeric literals (#1534) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Rename the "Javascript" tests to reflect that they are for CoffeeScript This change also modifies the module docstring to reflect the file's purpose. * Overhaul the Javascript numeric literal parsing Fixes #307 This patch contains the following changes: * Adds 50+ unit tests for Javascript numeric literals * Forces ASCII numbers for float literals (so, now reject `.рнк`) * Adds support for Javascript's BigInt notation (`100n`) * Adds support for leading-zero-only octal notation (`0777`) * Adds support for scientific notation with no significand (`1e10`) Numeric literal parsing is based on information at: * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures --- tests/test_coffeescript.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 tests/test_coffeescript.py (limited to 'tests/test_coffeescript.py') diff --git a/tests/test_coffeescript.py b/tests/test_coffeescript.py new file mode 100644 index 00000000..41ca8e0d --- /dev/null +++ b/tests/test_coffeescript.py @@ -0,0 +1,85 @@ +# -*- coding: utf-8 -*- +""" + CoffeeScript tests + ~~~~~~~~~~~~~~~~~~ + + :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +import pytest + +from pygments.lexers import CoffeeScriptLexer +from pygments.token import Token + +COFFEE_SLASH_GOLDEN = [ + # input_str, slashes_are_regex_here + (r'/\\/', True), + (r'/\\/i', True), + (r'/\//', True), + (r'/(\s)/', True), + ('/a{2,8}/', True), + ('/b*c?d+/', True), + ('/(capture-match)/', True), + ('/(?:do-not-capture-match)/', True), + ('/this|or|that/', True), + ('/[char-set]/', True), + ('/[^neg-char_st]/', True), + ('/^.*$/', True), + (r'/\n(\f)\0\1\d\b\cm\u1234/', True), + (r'/^.?([^/\\\n\w]*)a\1+$/.something(or_other) # something more complex', True), + ("foo = (str) ->\n /'|\"/.test str", True), + ('a = a / b / c', False), + ('a = a/b/c', False), + ('a = a/b/ c', False), + ('a = a /b/c', False), + ('a = 1 + /d/.test(a)', True), +] + + +@pytest.fixture(scope='module') +def lexer(): + yield CoffeeScriptLexer() + + +@pytest.mark.parametrize('golden', COFFEE_SLASH_GOLDEN) +def test_coffee_slashes(lexer, golden): + input_str, slashes_are_regex_here = golden + output = list(lexer.get_tokens(input_str)) + print(output) + for t, s in output: + if '/' in s: + is_regex = t is Token.String.Regex + assert is_regex == slashes_are_regex_here, (t, s) + + +def test_mixed_slashes(lexer): + fragment = u'a?/foo/:1/2;\n' + tokens = [ + (Token.Name.Other, u'a'), + (Token.Operator, u'?'), + (Token.Literal.String.Regex, u'/foo/'), + (Token.Operator, u':'), + (Token.Literal.Number.Integer, u'1'), + (Token.Operator, u'/'), + (Token.Literal.Number.Integer, u'2'), + (Token.Punctuation, u';'), + (Token.Text, u'\n'), + ] + assert list(lexer.get_tokens(fragment)) == tokens + + +def test_beware_infinite_loop(lexer): + # This demonstrates the case that "This isn't really guarding" comment + # refers to. + fragment = '/a/x;\n' + tokens = [ + (Token.Text, ''), + (Token.Operator, '/'), + (Token.Name.Other, 'a'), + (Token.Operator, '/'), + (Token.Name.Other, 'x'), + (Token.Punctuation, ';'), + (Token.Text, '\n'), + ] + assert list(lexer.get_tokens(fragment)) == tokens -- cgit v1.2.1