From 4700b9b46c218ec7b97fd033996a93519308554f Mon Sep 17 00:00:00 2001 From: Vegard Stikbakke Date: Thu, 18 Apr 2019 20:06:36 +0200 Subject: [PATCH 1/4] Truncate long local variable values with unittest utils --- marbles/core/marbles/core/marbles.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/marbles/core/marbles/core/marbles.py b/marbles/core/marbles/core/marbles.py index 750dca9..3137c17 100644 --- a/marbles/core/marbles/core/marbles.py +++ b/marbles/core/marbles/core/marbles.py @@ -339,7 +339,10 @@ def _format_local(cls, name, value): @classmethod def _format_locals(cls, locals_): - return '\n'.join(cls._format_local(k, v) for k, v in locals_.items()) + local_variable_names, local_variable_values = zip(*locals_.items()) + shortened_local_variable_values = unittest.util._common_shorten_repr(*local_variable_values) + new_locals_items_representation = zip(local_variable_names, shortened_local_variable_values) + return '\n'.join(cls._format_local(k, v) for k, v in new_locals_items_representation) @staticmethod def _find_assert_stmt(filename, linenumber, leading=1, following=2, From f1f108235ad804929032c3817f31213e206a8872 Mon Sep 17 00:00:00 2001 From: Vegard Stikbakke Date: Wed, 24 Apr 2019 22:16:05 +0200 Subject: [PATCH 2/4] Truncate one-line values with safe_repr with short keyword instead --- marbles/core/marbles/core/marbles.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/marbles/core/marbles/core/marbles.py b/marbles/core/marbles/core/marbles.py index 3137c17..f49f33c 100644 --- a/marbles/core/marbles/core/marbles.py +++ b/marbles/core/marbles/core/marbles.py @@ -335,14 +335,12 @@ def _format_local(cls, name, value): value_str = textwrap.indent(value_str, '\t\t') return '\t{0} =\n{1}'.format(name, value_str) else: - return '\t{0} = {1}'.format(name, value_str) + short_value_str = unittest.util.safe_repr(value_str, short=True) + return '\t{0} = {1}'.format(name, short_value_str) @classmethod def _format_locals(cls, locals_): - local_variable_names, local_variable_values = zip(*locals_.items()) - shortened_local_variable_values = unittest.util._common_shorten_repr(*local_variable_values) - new_locals_items_representation = zip(local_variable_names, shortened_local_variable_values) - return '\n'.join(cls._format_local(k, v) for k, v in new_locals_items_representation) + return '\n'.join(cls._format_local(k, v) for k, v in locals_.items()) @staticmethod def _find_assert_stmt(filename, linenumber, leading=1, following=2, From 3296f32e8fa62c19f5aefbd420a1738a88293e78 Mon Sep 17 00:00:00 2001 From: Vegard Stikbakke Date: Wed, 24 Apr 2019 22:52:23 +0200 Subject: [PATCH 3/4] Truncate ,ultiline values line by line as oneline --- marbles/core/marbles/core/marbles.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/marbles/core/marbles/core/marbles.py b/marbles/core/marbles/core/marbles.py index f49f33c..618cdde 100644 --- a/marbles/core/marbles/core/marbles.py +++ b/marbles/core/marbles/core/marbles.py @@ -330,13 +330,18 @@ def formattedMsg(self): # mimic unittest's name for standardMsg @classmethod def _format_local(cls, name, value): + def truncate(s): + return unittest.util.safe_repr(s, short=True) value_str = repr(value) if '\n' in value_str: - value_str = textwrap.indent(value_str, '\t\t') + value_str_lines = value_str.split('\n') + value_str_lines = [truncate(line) for line in value_str_lines] + truncated_value_str = '\n'.join(value_str_lines) + value_str = textwrap.indent(truncated_value_str, '\t\t') return '\t{0} =\n{1}'.format(name, value_str) else: - short_value_str = unittest.util.safe_repr(value_str, short=True) - return '\t{0} = {1}'.format(name, short_value_str) + value_str = truncate(value_str) + return '\t{0} = {1}'.format(name, value_str) @classmethod def _format_locals(cls, locals_): From 691f28c03056fa62cab23a92b83dd7ce1031b6a1 Mon Sep 17 00:00:00 2001 From: Vegard Stikbakke Date: Thu, 25 Apr 2019 09:45:46 +0200 Subject: [PATCH 4/4] Use better variable names and remove nested function --- marbles/core/marbles/core/marbles.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/marbles/core/marbles/core/marbles.py b/marbles/core/marbles/core/marbles.py index 618cdde..1109ee3 100644 --- a/marbles/core/marbles/core/marbles.py +++ b/marbles/core/marbles/core/marbles.py @@ -61,6 +61,7 @@ import sys import textwrap import unittest +from unittest import util from . import log from . import _stack @@ -330,18 +331,17 @@ def formattedMsg(self): # mimic unittest's name for standardMsg @classmethod def _format_local(cls, name, value): - def truncate(s): - return unittest.util.safe_repr(s, short=True) value_str = repr(value) if '\n' in value_str: value_str_lines = value_str.split('\n') - value_str_lines = [truncate(line) for line in value_str_lines] - truncated_value_str = '\n'.join(value_str_lines) - value_str = textwrap.indent(truncated_value_str, '\t\t') + short_lines = [util.safe_repr(line, short=True) + for line in value_str_lines] + short_value_str = '\n'.join(short_lines) + value_str = textwrap.indent(short_value_str, '\t\t') return '\t{0} =\n{1}'.format(name, value_str) else: - value_str = truncate(value_str) - return '\t{0} = {1}'.format(name, value_str) + short_value_str = util.safe_repr(value_str, short=True) + return '\t{0} = {1}'.format(name, short_value_str) @classmethod def _format_locals(cls, locals_):