diff options
author | Stephen Finucane <sfinucan@redhat.com> | 2022-09-20 16:59:09 +0100 |
---|---|---|
committer | Stephen Finucane <sfinucan@redhat.com> | 2022-10-03 14:23:47 +0100 |
commit | 67217b0e56cae8e88ebb861d3894551e3dedb1b7 (patch) | |
tree | c1fa6e72d65e8b8b49c7ebf172025adf74dc5f0e | |
parent | 9614d30450c642dd176ba00e6b6c2a893d742171 (diff) | |
download | cliff-67217b0e56cae8e88ebb861d3894551e3dedb1b7.tar.gz |
columns: Useful __str__, __repr__ implementation
The default implementations for __str__ and __repr__ are rubbish.
>>> from osc_lib.cli import format_columns
>>> str(format_columns.DictColumn({'foo': 'bar'}))
'<osc_lib.cli.format_columns.DictColumn object at 0x7f6e26771e40>'
>>> repr(format_columns.DictColumn({'foo': 'bar'}))
'<osc_lib.cli.format_columns.DictColumn object at 0x7f6e26b57ac0>'
Make it useful.
>>> from osc_lib.cli import format_columns
>>> str(format_columns.DictColumn({'foo': 'bar'}))
"foo='bar'"
>>> repr(format_columns.DictColumn({'foo': 'bar'}))
"DictColumn({'foo': 'bar'})"
This helps when testing as the reason for mismatches will be more
obvious.
Change-Id: I8b8598875f896cb3dbf417515d377e7758b3b98b
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
-rw-r--r-- | cliff/columns.py | 6 | ||||
-rw-r--r-- | cliff/tests/test_columns.py | 20 |
2 files changed, 23 insertions, 3 deletions
diff --git a/cliff/columns.py b/cliff/columns.py index b9cac5e..3462176 100644 --- a/cliff/columns.py +++ b/cliff/columns.py @@ -31,6 +31,12 @@ class FormattableColumn(object, metaclass=abc.ABCMeta): self.__class__ == other.__class__ and self._value < other._value ) + def __str__(self): + return self.human_readable() + + def __repr__(self): + return '%s(%r)' % (self.__class__.__name__, self.machine_readable()) + @abc.abstractmethod def human_readable(self): """Return a basic human readable version of the data.""" diff --git a/cliff/tests/test_columns.py b/cliff/tests/test_columns.py index 6bce767..364d258 100644 --- a/cliff/tests/test_columns.py +++ b/cliff/tests/test_columns.py @@ -23,17 +23,31 @@ class FauxColumn(columns.FormattableColumn): class TestColumns(unittest.TestCase): - def test_faux_column_machine(self): + def test_machine_readable(self): c = FauxColumn(['list', 'of', 'values']) self.assertEqual(['list', 'of', 'values'], c.machine_readable()) - def test_faux_column_human(self): + def test_human_readable(self): c = FauxColumn(['list', 'of', 'values']) self.assertEqual( - u"I made this string myself: ['list', 'of', 'values']", + "I made this string myself: ['list', 'of', 'values']", c.human_readable(), ) + def test_str(self): + c = FauxColumn(['list', 'of', 'values']) + self.assertEqual( + "I made this string myself: ['list', 'of', 'values']", + str(c), + ) + + def test_repr(self): + c = FauxColumn(['list', 'of', 'values']) + self.assertEqual( + "FauxColumn(['list', 'of', 'values'])", + repr(c), + ) + def test_sorting(self): cols = [ FauxColumn('foo'), |