mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 13:24:39 +08:00
Move some things around to allow checking for different versions.
This commit is contained in:
44
conftest.py
44
conftest.py
@@ -7,7 +7,9 @@ import os
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
import parso
|
||||||
from parso import cache
|
from parso import cache
|
||||||
|
from parso.utils import parse_version_string
|
||||||
|
|
||||||
|
|
||||||
collect_ignore = ["setup.py"]
|
collect_ignore = ["setup.py"]
|
||||||
@@ -103,3 +105,45 @@ def each_py3_version():
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def each_py2_version():
|
def each_py2_version():
|
||||||
return '2.6', '2.7'
|
return '2.6', '2.7'
|
||||||
|
|
||||||
|
|
||||||
|
def _parse(code, version=None):
|
||||||
|
code = code + "\n\n"
|
||||||
|
grammar = parso.load_grammar(version=version)
|
||||||
|
return grammar.parse(code, error_recovery=False)
|
||||||
|
|
||||||
|
|
||||||
|
def _invalid_syntax(code, version=None, **kwargs):
|
||||||
|
with pytest.raises(parso.ParserSyntaxError):
|
||||||
|
module = _parse(code, version=version, **kwargs)
|
||||||
|
# For debugging
|
||||||
|
print(module.children)
|
||||||
|
|
||||||
|
|
||||||
|
class Checker():
|
||||||
|
def __init__(self, version, is_passing):
|
||||||
|
self._version = version
|
||||||
|
self._is_passing = is_passing
|
||||||
|
|
||||||
|
def parse(self, code):
|
||||||
|
func = _parse if self._is_passing else _invalid_syntax
|
||||||
|
return func(code, version=self._version)
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def works_in_py2(each_version):
|
||||||
|
return Checker(each_version, each_version.startswith('2'))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def works_ge_py3(each_version):
|
||||||
|
version_info = parse_version_string(each_version)
|
||||||
|
return Checker(each_version, version_info >= (3, 0))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def works_ge_py35(each_version):
|
||||||
|
"""
|
||||||
|
Works only greater equal Python 3.3.
|
||||||
|
"""
|
||||||
|
version_info = parse_version_string(each_version)
|
||||||
|
return Checker(each_version, version_info >= (3, 5))
|
||||||
|
|||||||
@@ -12,36 +12,6 @@ import pytest
|
|||||||
|
|
||||||
from parso import load_grammar
|
from parso import load_grammar
|
||||||
from parso import ParserSyntaxError
|
from parso import ParserSyntaxError
|
||||||
from parso.utils import parse_version_string
|
|
||||||
|
|
||||||
|
|
||||||
class Checker():
|
|
||||||
def __init__(self, version, is_passing):
|
|
||||||
self._version = version
|
|
||||||
self._is_passing = is_passing
|
|
||||||
|
|
||||||
def parse(self, code):
|
|
||||||
func = _parse if self._is_passing else _invalid_syntax
|
|
||||||
return func(code, version=self._version)
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def works_in_py2(each_version):
|
|
||||||
return Checker(each_version, each_version.startswith('2'))
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def works_ge_py3(each_version):
|
|
||||||
version_info = parse_version_string(each_version)
|
|
||||||
return Checker(each_version, version_info >= (3, 0))
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def works_ge_py35(each_version):
|
|
||||||
"""
|
|
||||||
Works only greater equal Python 3.3.
|
|
||||||
"""
|
|
||||||
version_info = parse_version_string(each_version)
|
|
||||||
return Checker(each_version, version_info >= (3, 5))
|
|
||||||
|
|
||||||
|
|
||||||
def _parse(code, version=None):
|
def _parse(code, version=None):
|
||||||
@@ -179,19 +149,19 @@ def test_annotation_8(each_py3_version):
|
|||||||
|
|
||||||
|
|
||||||
def test_except_new(each_version):
|
def test_except_new(each_version):
|
||||||
s = """
|
s = dedent("""
|
||||||
try:
|
try:
|
||||||
x
|
x
|
||||||
except E as N:
|
except E as N:
|
||||||
y"""
|
y""")
|
||||||
_parse(s, each_version)
|
_parse(s, each_version)
|
||||||
|
|
||||||
def test_except_old(works_in_py2):
|
def test_except_old(works_in_py2):
|
||||||
s = """
|
s = dedent("""
|
||||||
try:
|
try:
|
||||||
x
|
x
|
||||||
except E, N:
|
except E, N:
|
||||||
y"""
|
y""")
|
||||||
works_in_py2.parse(s)
|
works_in_py2.parse(s)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user