From 6c16d0231b8db85ceecfdb7bbb191e25d4a20bf2 Mon Sep 17 00:00:00 2001 From: wchresta <34962284+wchresta@users.noreply.github.com> Date: Fri, 27 Dec 2019 23:36:29 -0500 Subject: [PATCH] Add unit tests for mypy plugin * Add test cases for issues #21, #25, #26 * Test case for #21 is active because it passes with mypy==0.711 * Test cases for #25 and #26 are deactivated because they fail --- tests/source_files/issue21.py | 9 +++++ tests/source_files/issue25.py | 9 +++++ tests/source_files/issue26.py | 15 +++++++++ tests/test_mypy_plugin.py | 62 +++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 tests/source_files/issue21.py create mode 100644 tests/source_files/issue25.py create mode 100644 tests/source_files/issue26.py create mode 100644 tests/test_mypy_plugin.py diff --git a/tests/source_files/issue21.py b/tests/source_files/issue21.py new file mode 100644 index 0000000..08a374f --- /dev/null +++ b/tests/source_files/issue21.py @@ -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] diff --git a/tests/source_files/issue25.py b/tests/source_files/issue25.py new file mode 100644 index 0000000..0afb045 --- /dev/null +++ b/tests/source_files/issue25.py @@ -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) diff --git a/tests/source_files/issue26.py b/tests/source_files/issue26.py new file mode 100644 index 0000000..ca9c3d3 --- /dev/null +++ b/tests/source_files/issue26.py @@ -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) diff --git a/tests/test_mypy_plugin.py b/tests/test_mypy_plugin.py new file mode 100644 index 0000000..3d28d5f --- /dev/null +++ b/tests/test_mypy_plugin.py @@ -0,0 +1,62 @@ +import unittest +import mypy.main +import mypy.version +import os +import sys +import io +from typing import List, Optional + + +class SimpleBuffer(io.TextIOBase): + """Simple memory buffer that behaves like a file""" + + def __init__(self) -> None: + self.__buffer: List[str] = [] + + def write(self, text: str) -> int: + self.__buffer.append(text) + print(text, end="") + return len(text) + + def read(self, size: Optional[int] = None) -> str: + return ''.join(self.__buffer) + + def __str__(self) -> str: + 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) -> None: + 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: str) -> None: + output = SimpleBuffer() + try: + mypy.main.main( # type: ignore + None, output, sys.stderr, args=[testfile]) + except SystemExit: + self.fail(msg="Error during type-check:\n{}".format(output))