Skip to content

Commit

Permalink
fix: use fallback types for python 3.7
Browse files Browse the repository at this point in the history
Closes #56
  • Loading branch information
rcarriga committed Jul 18, 2021
1 parent 43ec7b4 commit 06f965a
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9]
python-version: [3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down
3 changes: 2 additions & 1 deletion rplugin/python3/ultest/models/file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from dataclasses import dataclass, field
from typing import List, Literal
from typing import List

from .base import BasePosition
from .types import Literal


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion rplugin/python3/ultest/models/namespace.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
from typing import Literal

from .base import BasePosition
from .types import Literal


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion rplugin/python3/ultest/models/test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
from typing import Literal

from .base import BasePosition
from .types import Literal


@dataclass
Expand Down
4 changes: 3 additions & 1 deletion rplugin/python3/ultest/models/tree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Callable, Generic, Iterator, List, Optional, Protocol, TypeVar
from typing import Callable, Generic, Iterator, List, Optional, TypeVar

from .types import Protocol

TreeData = TypeVar("TreeData")

Expand Down
15 changes: 15 additions & 0 deletions rplugin/python3/ultest/models/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sys
from typing import Any

if sys.version_info >= (3, 8):
from typing import Literal, Protocol
else:

class _Literal:
def __getitem__(self, a):
return Any

Literal = _Literal()

class Protocol:
...
4 changes: 1 addition & 3 deletions tests/mocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ def get_output(runner: str) -> List[str]:

def get_test_file(name: str) -> str:
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, "test_files", name)
with open(filename) as output:
return output.read()
return os.path.join(dirname, "test_files", name)
100 changes: 58 additions & 42 deletions tests/unit/handler/parsers/test_file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import Mock, mock_open, patch
from unittest.mock import Mock, patch

import pytest

Expand All @@ -13,7 +13,6 @@
file_parser = FileParser(vim)


@patch("builtins.open", mock_open(read_data=get_test_file("python")))
@patch("os.path.isfile", lambda _: True)
@pytest.mark.asyncio
async def test_parse_python_tests():
Expand All @@ -22,14 +21,23 @@ async def test_parse_python_tests():
"namespace": [r"\v^\s*class (\w+)"],
}

tests = list(await file_parser.parse_file_structure("", patterns))
file_name = get_test_file("python")
tests = list(await file_parser.parse_file_structure(file_name, patterns))

expected = [
File(id="", name="", file="", line=0, col=0, namespaces=[], type="file"),
File(
id=file_name,
name=file_name,
file=file_name,
line=0,
col=0,
namespaces=[],
type="file",
),
Test(
id=tests[1].id,
name="test_a",
file="",
file=file_name,
line=4,
col=1,
running=0,
Expand All @@ -39,7 +47,7 @@ async def test_parse_python_tests():
Namespace(
id=tests[2].id,
name="TestAgain",
file="",
file=file_name,
line=9,
col=1,
running=0,
Expand All @@ -49,7 +57,7 @@ async def test_parse_python_tests():
Test(
id=tests[3].id,
name="test_d",
file="",
file=file_name,
line=10,
col=1,
running=0,
Expand All @@ -59,7 +67,7 @@ async def test_parse_python_tests():
Test(
id=tests[4].id,
name="test_a",
file="",
file=file_name,
line=16,
col=1,
running=0,
Expand All @@ -69,7 +77,7 @@ async def test_parse_python_tests():
Namespace(
id=tests[5].id,
name="TestMyClass",
file="",
file=file_name,
line=25,
col=1,
running=0,
Expand All @@ -79,7 +87,7 @@ async def test_parse_python_tests():
Test(
id=tests[6].id,
name="test_d",
file="",
file=file_name,
line=26,
col=1,
running=0,
Expand All @@ -89,7 +97,7 @@ async def test_parse_python_tests():
Test(
id=tests[7].id,
name="test_a",
file="",
file=file_name,
line=29,
col=1,
running=0,
Expand All @@ -99,7 +107,7 @@ async def test_parse_python_tests():
Test(
id=tests[8].id,
name="test_b",
file="",
file=file_name,
line=33,
col=1,
running=0,
Expand All @@ -111,7 +119,6 @@ async def test_parse_python_tests():
assert tests == expected


@patch("builtins.open", mock_open(read_data=get_test_file("java")))
@patch("os.path.isfile", lambda _: True)
@pytest.mark.asyncio
async def test_parse_java_tests():
Expand All @@ -120,13 +127,14 @@ async def test_parse_java_tests():
"namespace": [r"\v^\s*%(\zspublic\s+\ze)?class\s+(\w+)"],
}

tests = list(await file_parser.parse_file_structure("", patterns))
file_name = get_test_file("java")
tests = list(await file_parser.parse_file_structure(file_name, patterns))

expected = [
File(
id="",
name="",
file="",
id=file_name,
name=file_name,
file=file_name,
line=0,
col=0,
running=0,
Expand All @@ -136,7 +144,7 @@ async def test_parse_java_tests():
Namespace(
id=tests[1].id,
name="TestJunit1",
file="",
file=file_name,
line=5,
col=1,
running=0,
Expand All @@ -146,7 +154,7 @@ async def test_parse_java_tests():
Test(
id=tests[2].id,
name="testPrintMessage",
file="",
file=file_name,
line=11,
col=1,
running=0,
Expand All @@ -158,7 +166,6 @@ async def test_parse_java_tests():
assert tests == expected


@patch("builtins.open", mock_open(read_data=get_test_file("jest")))
@patch("os.path.isfile", lambda _: True)
@pytest.mark.asyncio
async def test_parse_jest_tests():
Expand All @@ -169,13 +176,14 @@ async def test_parse_jest_tests():
],
}

tests = list(await file_parser.parse_file_structure("", patterns))
file_name = get_test_file("jest")
tests = list(await file_parser.parse_file_structure(file_name, patterns))

expected = [
File(
id="",
name="",
file="",
id=file_name,
name=file_name,
file=file_name,
line=0,
col=0,
running=0,
Expand All @@ -185,7 +193,7 @@ async def test_parse_jest_tests():
Namespace(
id=tests[1].id,
name='First namespace", () => {',
file="",
file=file_name,
line=1,
col=1,
running=0,
Expand All @@ -195,7 +203,7 @@ async def test_parse_jest_tests():
Namespace(
id=tests[2].id,
name='Second namespace", () => {',
file="",
file=file_name,
line=2,
col=1,
running=0,
Expand All @@ -205,7 +213,7 @@ async def test_parse_jest_tests():
Test(
id=tests[3].id,
name="it shouldn't pass\", () => {",
file="",
file=file_name,
line=3,
col=1,
running=0,
Expand All @@ -220,7 +228,6 @@ async def test_parse_jest_tests():
assert tests == expected


@patch("builtins.open", mock_open(read_data=get_test_file("python")))
@patch("os.path.isfile", lambda _: True)
@pytest.mark.asyncio
async def test_parse_namespace_structure():
Expand All @@ -229,14 +236,23 @@ async def test_parse_namespace_structure():
"namespace": [r"\v^\s*class (\w+)"],
}

tests = await file_parser.parse_file_structure("", patterns)
file_name = get_test_file("python")
tests = await file_parser.parse_file_structure(file_name, patterns)

expected = [
File(id="", name="", file="", line=0, col=0, namespaces=[], type="file"),
File(
id=file_name,
name=file_name,
file=file_name,
line=0,
col=0,
namespaces=[],
type="file",
),
Test(
id=tests[1].id,
name="test_a",
file="",
file=file_name,
line=4,
col=1,
running=0,
Expand All @@ -247,7 +263,7 @@ async def test_parse_namespace_structure():
Namespace(
id=tests[2].id,
name="TestAgain",
file="",
file=file_name,
line=9,
col=1,
running=0,
Expand All @@ -257,29 +273,29 @@ async def test_parse_namespace_structure():
Test(
id=tests[3].id,
name="test_d",
file="",
file=file_name,
line=10,
col=1,
running=0,
namespaces=["TestAgain-8458139203682520985"],
namespaces=[tests[2].id],
type="test",
),
Test(
id=tests[4].id,
name="test_a",
file="",
file=file_name,
line=16,
col=1,
running=0,
namespaces=["TestAgain-8458139203682520985"],
namespaces=[tests[2].id],
type="test",
),
],
[
Namespace(
id=tests[5].id,
name="TestMyClass",
file="",
file=file_name,
line=25,
col=1,
running=0,
Expand All @@ -289,28 +305,28 @@ async def test_parse_namespace_structure():
Test(
id=tests[6].id,
name="test_d",
file="",
file=file_name,
line=26,
col=1,
running=0,
namespaces=["TestMyClass-8458139203682520985"],
namespaces=[tests[5].id],
type="test",
),
Test(
id=tests[7].id,
name="test_a",
file="",
file=file_name,
line=29,
col=1,
running=0,
namespaces=["TestMyClass-8458139203682520985"],
namespaces=[tests[5].id],
type="test",
),
],
Test(
id=tests[8].id,
name="test_b",
file="",
file=file_name,
line=33,
col=1,
running=0,
Expand Down

0 comments on commit 06f965a

Please sign in to comment.