diff --git a/safe_cli/safe_completer.py b/safe_cli/safe_completer.py index df92fcf1..2db06acc 100644 --- a/safe_cli/safe_completer.py +++ b/safe_cli/safe_completer.py @@ -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: diff --git a/safe_cli/utils.py b/safe_cli/utils.py index 61e6f073..b3e96f87 100644 --- a/safe_cli/utils.py +++ b/safe_cli/utils.py @@ -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": diff --git a/tests/test_safe_completer.py b/tests/test_safe_completer.py new file mode 100644 index 00000000..23758692 --- /dev/null +++ b/tests/test_safe_completer.py @@ -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() diff --git a/tests/test_safe_creator.py b/tests/test_safe_creator.py new file mode 100644 index 00000000..ab04a215 --- /dev/null +++ b/tests/test_safe_creator.py @@ -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() diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..729c1ee9 --- /dev/null +++ b/tests/test_utils.py @@ -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()