hbutils.testing.compare¶
TextAligner¶
-
class
hbutils.testing.compare.
TextAligner
(line_rstrip: bool = True, keep_empty_tail: bool = False, text_func=None, line_func=None, ls_func=None)[source]¶ - Overview:
Text aligner for comparing texts in unittest.
-
__call__
(text: Union[str, List[str]]) → str[source]¶ Transform the original text or lines to aligned text.
- Parameters:
text – Original text or lines.
- Returns:
Aligned text.
- Examples::
>>> from hbutils.testing import TextAligner >>> text_align = TextAligner() >>> print(text_align('''Python 3.6.5 ... Hello world!! ... Do not see me like this... ... \n\n\t\n ... ''')) Python 3.6.5 Hello world!! Do not see me like this...
>>> print(text_align.lstrip()('''Python 3.6.5 ... Hello world!! ... Do not see me like this... ... \n\n\t\n ... ''')) Python 3.6.5 Hello world!! Do not see me like this...
-
__getattr__
(item: str) → hbutils.testing.compare.text._StrMethodProxy[source]¶ Append postprocess from
str
of each line.- Parameters:
item – Method name.
- Examples::
>>> from hbutils.testing import TextAligner >>> text_align = TextAligner() >>> print(text_align.lower().multiple_lines()(''' ... Python 3.6.5 ... Hello world!! ... Do not see me like this... ... \n\n\t\n ... ''')) python 3.6.5 hello world!! do not see me like this...
-
__init__
(line_rstrip: bool = True, keep_empty_tail: bool = False, text_func=None, line_func=None, ls_func=None)[source]¶ Constructor of
TextAligner
.- Parameters:
line_rstrip – Right strip each line, default is
True
.keep_empty_tail – Keep the empty tail of the text, default is
False
, which means the empty tails will be ignored.text_func – Function for preprocessing whole text.
line_func – Function for processing each line.
ls_func – Function for processing lines list.
-
assert_equal
(expect: Union[str, List[str]], actual: Union[str, List[str]], max_diff: int = 3, max_extra: int = 5)[source]¶ Assert two string is equal.
- Parameters:
expect – Expected text or lines.
actual – Actual text or lines.
max_diff – Max different lines to show in assertion message, default is
3
.max_extra – Max extra lines to show in assertion message, default is
5
.
- Examples::
>>> from hbutils.testing import TextAligner >>> text_align = TextAligner() >>> text_align.multiple_lines().assert_equal('''Python 3.6.5 ... Hello world!! ... Do not see me like this... ... \n\n\t\n ... ''', ''' ... Python 3.6.5 ... Hello world!! ... Do not see me like this... ... ''') # this is okay
>>> text_align.multiple_lines().assert_equal('''Python 3.6.5 ... Hello world!! ... Do not see me like this... ... \n\n\t\n ... ''', ''' ... Python 3.6.5 ... Hello world!! ... Do not see me like this... ... ''') AssertionError: Difference found in line 3: Expect: Do not see me like this... Actual: Do not see me like this...
-
assert_not_equal
(expect: Union[str, List[str]], actual: Union[str, List[str]])[source]¶ Assert two string is not equal, which is similar to
assert_equal()
.- Parameters:
expect – Expected text or lines.
actual – Actual text or lines.
-
line_map
(line_func)[source]¶ Mapping for the text of each line.
- Parameters:
line_func – New line process function.
- Returns:
A new
TextAligner
object withline_func
process.
-
ls_trans
(ls_func)[source]¶ Transformation for the separated lines.
- Parameters:
ls_func – New lines process function.
- Returns:
A new
TextAligner
object withls_func
process.
-
multiple_lines
(lstrip: bool = True, dedent: bool = True)[source]¶ Switch to multiple-line mode.
- Parameters:
lstrip – Left strip the original text.
dedent – Dedent the text.
- Examples::
>>> from hbutils.testing import TextAligner >>> text_align = TextAligner() >>> print(text_align.multiple_lines()(''' ... Python 3.6.5 ... Hello world!! ... Do not see me like this... ... \n\n\t\n ... ''')) Python 3.6.5 Hello world!! Do not see me like this...
Note
With
multiple_lines()
, the text’s comparison will be compatible with text wrapper.-
splitlines
(text: Union[str, List[str]]) → List[str][source]¶ Transform the original text or lines to list of aligned lines.
- Parameters:
text – Original text or lines.
- Returns:
List of split lines.
- Examples::
>>> from hbutils.testing import TextAligner >>> text_align = TextAligner() >>> print(text_align.splitlines('''Python 3.6.5 ... Hello world!! ... Do not see me like this... ... \n\n\t\n ... ''')) ['Python 3.6.5', 'Hello world!!', ' Do not see me like this...']
>>> print(text_align.lstrip().splitlines('''Python 3.6.5 ... Hello world!! ... Do not see me like this... ... \n\n\t\n ... ''')) ['Python 3.6.5', 'Hello world!!', 'Do not see me like this...']
-
text_trans
(text_func)[source]¶ Transformation for the original text.
- Parameters:
text_func – New text process function.
- Returns:
A new
TextAligner
object withtext_func
process.