-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new test util called: Patch_Print
- Loading branch information
Showing
4 changed files
with
113 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from unittest.mock import patch, MagicMock, _patch | ||
|
||
from osbot_utils.utils.Dev import pprint | ||
|
||
from osbot_utils.base_classes.Kwargs_To_Self import Kwargs_To_Self | ||
|
||
|
||
class Patch_Print(Kwargs_To_Self): | ||
enabled : bool = True | ||
expected_calls : list | ||
mocked_print : MagicMock | ||
patched_print : _patch | ||
print_calls : bool | ||
|
||
|
||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
def __enter__(self): | ||
if self.enabled: | ||
self.patched_print = patch('builtins.print') | ||
self.mocked_print = self.patched_print.start() | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
if self.enabled: | ||
self.patched_print.stop() | ||
|
||
if self.print_calls: | ||
pprint(self.calls()) | ||
#print(self.mocked_print.call_args_list) | ||
|
||
if self.expected_calls: | ||
assert self.calls() == self.expected_calls | ||
|
||
def calls(self): | ||
calls_data = [] | ||
if self.mocked_print: | ||
for call in self.mocked_print.call_args_list: | ||
if len(call.args) == 0 and call.kwargs == {}: | ||
call_data = '' | ||
elif len(call.args) == 1 and call.kwargs == {}: | ||
call_data = call.args[0] | ||
else: | ||
call_data = (call.args, call.kwargs) | ||
calls_data.append(call_data) | ||
return calls_data | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from unittest import TestCase | ||
|
||
from osbot_utils.utils.Objects import obj_info | ||
|
||
from osbot_utils.utils.Dev import pprint | ||
|
||
from osbot_utils.testing.Patch_Print import Patch_Print | ||
|
||
|
||
class test_Patch_Print(TestCase): | ||
|
||
def setUp(self): | ||
self.patch_print = Patch_Print() | ||
|
||
def test__init__(self): | ||
assert self.patch_print.__locals__() == {'enabled' : True , | ||
'expected_calls': [] , | ||
'mocked_print' : self.patch_print.mocked_print , | ||
'patched_print' : None , | ||
'print_calls' : False } | ||
|
||
|
||
def test__enter__exit__(self): | ||
with self.patch_print as _: | ||
print('in test__enter__exit__',12,1) | ||
print("aaaa", end='__end__') | ||
|
||
assert _.calls() == [(('in test__enter__exit__', 12, 1), {}), (('aaaa',), {'end': '__end__'})] | ||
|
||
def test_calls(self): | ||
with self.patch_print as _: | ||
print('simple print') | ||
print('with only text') | ||
|
||
assert _.calls() == ['simple print', 'with only text'] | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters