Skip to content

Commit

Permalink
Add tests for safe completer and utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Uxio0 committed Oct 13, 2023
1 parent 001a921 commit 9f14b74
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
9 changes: 5 additions & 4 deletions safe_cli/safe_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@


class SafeCompleter(Completer):
"""Command Completer
This class will perform the utilities regarding auto-completion of known user input commands
"""
This class will handle auto-completion of known user input commands
"""

def get_completions(
self, document: Document, complete_event: CompleteEvent
) -> Completion:
"""Get Completions
This will function will provide the completions for param types and function name
"""
Provide command completion for function names and param types
:param document:
:param complete_event:
:return:
Expand Down
7 changes: 6 additions & 1 deletion safe_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ def get_erc_20_list(
return addresses


def get_input(*args, **kwargs):
return input(*args, **kwargs)


def yes_or_no_question(question: str, default_no: bool = True) -> bool:
if "PYTEST_CURRENT_TEST" in os.environ:
return True # Ignore confirmations when running tests

choices = " [y/N]: " if default_no else " [Y/n]: "
default_answer = "n" if default_no else "y"
reply = str(input(question + choices)).lower().strip() or default_answer
reply = str(get_input(question + choices)).lower().strip() or default_answer
if reply[0] == "y":
return True
if reply[0] == "n":
Expand Down
29 changes: 29 additions & 0 deletions tests/test_safe_completer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest

from prompt_toolkit.completion import CompleteEvent, Completion
from prompt_toolkit.document import Document

from safe_cli.safe_completer import SafeCompleter


class TestSafeCompleter(unittest.TestCase):
def test_get_completions(self):
safe_completer = SafeCompleter()

with self.assertRaises(StopIteration):
next(
safe_completer.get_completions(
Document("not-supported-command"), CompleteEvent()
)
)

self.assertIsInstance(
next(
safe_completer.get_completions(Document("send_ether"), CompleteEvent())
),
Completion,
)


if __name__ == "__main__":
unittest.main()
12 changes: 12 additions & 0 deletions tests/test_safe_creator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unittest

from .safe_cli_test_case_mixin import SafeCliTestCaseMixin


class TestSafeCreator(SafeCliTestCaseMixin, unittest.TestCase):
def test_main(self):
pass


if __name__ == "__main__":
unittest.main()
39 changes: 39 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import unittest
from unittest import mock
from unittest.mock import MagicMock

from safe_cli.utils import yes_or_no_question


class TestUtils(unittest.TestCase):
@mock.patch("safe_cli.utils.get_input")
def test_yes_or_no_question(self, input_mock: MagicMock):
# Input defaults to `yes` if running tests
pytest_current_test = os.environ.pop("PYTEST_CURRENT_TEST")
input_mock.return_value = "yes"
self.assertTrue(yes_or_no_question(""))

input_mock.return_value = "yay"
self.assertTrue(yes_or_no_question(""))

input_mock.return_value = "Y"
self.assertTrue(yes_or_no_question(""))

input_mock.return_value = "Nope"
self.assertFalse(yes_or_no_question(""))

input_mock.return_value = "No"
self.assertFalse(yes_or_no_question(""))

input_mock.return_value = "n"
self.assertFalse(yes_or_no_question(""))

input_mock.return_value = "random"
self.assertFalse(yes_or_no_question(""))

os.environ["PYTEST_CURRENT_TEST"] = pytest_current_test


if __name__ == "__main__":
unittest.main()

0 comments on commit 9f14b74

Please sign in to comment.