From 8b01b4208e5ac55c7a0c2b293ca471ab90303fab Mon Sep 17 00:00:00 2001 From: Llewellyn Falco Date: Sun, 11 Feb 2024 19:07:58 +0000 Subject: [PATCH] - F Added parse inputs from docs string Co-Authored-By: Kevin Maes Co-Authored-By: 4dsherwood <4dsherwood@users.noreply.github.com> Co-Authored-By: blade290 <43077216+blade290@users.noreply.github.com> Co-Authored-By: Nitsan Avni Co-Authored-By: Susan Fung <38925660+susanfung@users.noreply.github.com> Co-Authored-By: Nazee Hajebi <2491283+NazeeHajebi@users.noreply.github.com> Co-Authored-By: T. E. Green <78671457+Tegsy@users.noreply.github.com> --- TODO.md | 12 +++++++++++ approvaltests/inline/parse_docstring.py | 6 ++++++ approvaltests/namer/inline_comparator.py | 8 +++++--- ...rovals.test_docstring_parsing.approved.txt | 10 ++++++++++ tests/test_inline_approvals.py | 20 +++++++++++++++---- 5 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 approvaltests/inline/parse_docstring.py create mode 100644 tests/approved_files/test_inline_approvals.test_docstring_parsing.approved.txt diff --git a/TODO.md b/TODO.md index b58651d..9efd7bb 100644 --- a/TODO.md +++ b/TODO.md @@ -2,6 +2,7 @@ Test are running on all CI machine Test fail if mariadb is in the requirements for linux 1. [ ] update Cyber-dojo to have latest version of approvaltests.py and + a. [x] verify which version of approval tests is in the new image that we just created 2. [ ] put an example of inline approvals into cyber-dojo 3. [ ] add JQ (nitsan created repo and submitted an issue) @@ -23,6 +24,17 @@ Test fail if mariadb is in the requirements for linux Key x = done, - = started notes: +1. [ ] update Cyber-dojo to have latest version of approvaltests.py and + +What have we learned? +- How does approvals work in cyber-dojo? +- There are two docker images with python approvals: 1. pytest 2. unittest +- is the version number important? +- Does it use the pytest plugin at all? Or just pytest? +- set up Gitpod +- + + Possible problem: we have linked the Execute command and the _____ when you run it diff --git a/approvaltests/inline/parse_docstring.py b/approvaltests/inline/parse_docstring.py new file mode 100644 index 0000000..2772c15 --- /dev/null +++ b/approvaltests/inline/parse_docstring.py @@ -0,0 +1,6 @@ +from approvaltests.namer.inline_comparator import InlineComparator + + +def parse_docstring(): + lines = InlineComparator.get_test_method_doc_string().split("\n")[:-1] + return [line.split("->")[0].strip() for line in lines ] diff --git a/approvaltests/namer/inline_comparator.py b/approvaltests/namer/inline_comparator.py index 39725ea..33e9e4d 100644 --- a/approvaltests/namer/inline_comparator.py +++ b/approvaltests/namer/inline_comparator.py @@ -18,12 +18,14 @@ def get_approved_filename(self, base: Optional[str] = None) -> str: def get_received_filename(self, base: Optional[str] = None) -> str: return tempfile.NamedTemporaryFile(suffix=".received.txt", delete=False).name - def get_test_method_doc_string(self): + @staticmethod + def get_test_method_doc_string(): test_stack_frame: FrameInfo = StackFrameNamer.get_test_frame() - method: Callable[..., Any] = self.get_caller_method(test_stack_frame) + method: Callable[..., Any] = InlineComparator.get_caller_method(test_stack_frame) return remove_indentation_from(method.__doc__) - def get_caller_method(self, caller_frame) -> Callable: + @staticmethod + def get_caller_method(caller_frame) -> Callable: caller_function_name: str = caller_frame[3] caller_function_object = caller_frame.frame.f_globals.get( caller_function_name, None diff --git a/tests/approved_files/test_inline_approvals.test_docstring_parsing.approved.txt b/tests/approved_files/test_inline_approvals.test_docstring_parsing.approved.txt new file mode 100644 index 0000000..39b3e5c --- /dev/null +++ b/tests/approved_files/test_inline_approvals.test_docstring_parsing.approved.txt @@ -0,0 +1,10 @@ +inputs + +0) 1 +1) 2 +2) Fizz +3) 4 +4) Buzz +5) Fizz +6) 7 +7) 8 diff --git a/tests/test_inline_approvals.py b/tests/test_inline_approvals.py index 29a6399..6b53a1a 100644 --- a/tests/test_inline_approvals.py +++ b/tests/test_inline_approvals.py @@ -9,9 +9,10 @@ assert_equal_with_reporter, Options, Reporter, - verify, + verify, verify_all, ) -from approvaltests.reporters import MultiReporter, ReporterThatAutomaticallyApproves +from approvaltests.inline.parse_docstring import parse_docstring +from approvaltests.reporters import MultiReporter def get_approved_via_doc_string(): @@ -92,5 +93,16 @@ def verify_inline(actual): assert_equal_with_reporter(get_approved_via_doc_string(), actual, options=options) -# -# +def test_docstring_parsing(): + """ + 1 + 2 -> 2 + Fizz + 4 + Buzz + Fizz + 7 + 8 + """ + verify_all('inputs', parse_docstring()) +