Skip to content

Commit

Permalink
Add unit tests for mypy plugin
Browse files Browse the repository at this point in the history
* Add test cases for issues jspahrsummers#21, jspahrsummers#25, jspahrsummers#26
* Test case for jspahrsummers#21 is active because it passes with mypy==0.711
* Test cases for jspahrsummers#25 and jspahrsummers#26 are deactivated because they fail
  • Loading branch information
wchresta committed Dec 28, 2019
1 parent 2484f2c commit 6e7c8bc
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/source_files/issue21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from adt import adt, Case


@adt
class Cmd:
CASE0: Case
CASE1: Case[str]
CASE2: Case[str, str]
CASE3: Case[str, str, str]
9 changes: 9 additions & 0 deletions tests/source_files/issue25.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from adt import adt, Case


@adt
class Expression:
LITERAL: Case[float]


result: None = Expression.LITERAL(0.1).match(literal=lambda n: None)
15 changes: 15 additions & 0 deletions tests/source_files/issue26.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import Optional
from adt import adt, Case


@adt
class ExampleADT:
EMPTY: Case
INTEGER: Case[int]
STRING_PAIR: Case[str, str]

@property
def safe_integer(self) -> Optional[int]:
return self.match(empty=lambda: None,
integer=lambda n: n,
string_pair=lambda a, b: None)
60 changes: 60 additions & 0 deletions tests/test_mypy_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import unittest
import mypy.main
import mypy.version
import os
import sys
import io


class SimpleBuffer(io.TextIOBase):
"""Simple memory buffer that behaves like a file"""

def __init__(self):
self.__buffer = []

def write(self, text):
self.__buffer.append(text)
print(text, end="")
return len(text)

def read(self):
return ''.join(self.__buffer)

def __str__(self):
return self.read()


class TestMyPyPlugin(unittest.TestCase):
def test_issue21(self) -> None:
self._call_mypy_on_source_file("issue21.py")

def _test_issue25(self) -> None:
# TODO: This is a deactivated test for issue #25.
# Activate it when working on it.
# cf. https://github.com/jspahrsummers/adt/issues/25
self._call_mypy_on_source_file("issue25.py")

def _test_issue26(self) -> None:
# TODO: This is a deactivated test for issue #26.
# Activate it when working on it.
# cf. https://github.com/jspahrsummers/adt/issues/26
self._call_mypy_on_source_file("issue26.py")

def _test_readme_examples(self) -> None:
# TODO: This fails because of issue #26. Deactivated this test.
self._call_mypy_on_source_file("readme_examples.py")

def _call_mypy_on_source_file(self, source_file_name: str):
print(
f"Testing {source_file_name} with mypy=={mypy.version.__version__}"
)
self._call_mypy_on_path(
os.path.join(os.path.dirname(__file__), "source_files",
source_file_name))

def _call_mypy_on_path(self, testfile):
output = SimpleBuffer()
try:
mypy.main.main(None, output, sys.stderr, args=[testfile])
except SystemExit:
self.fail(msg="Error during type-check:\n{}".format(output))

0 comments on commit 6e7c8bc

Please sign in to comment.