Handle logging of sets

This commit is contained in:
Itamar Turner-Trauring 2023-11-15 13:03:43 -05:00
parent 16aa5fece2
commit 0bd402e680
2 changed files with 17 additions and 0 deletions

View File

@ -88,6 +88,21 @@ def passes():
return AfterPreprocessing(run, Equals(True))
class BytesEliotJSONEncoderTests(TestCase):
"""Tests for ``BytesEliotJSONEncoder``."""
def test_encoding_bytes(self):
"""``BytesEliotJSONEncoder`` can encode bytes."""
encoder = BytesEliotJSONEncoder()
self.assertEqual(encoder.default(b"xxx"), "xxx")
self.assertEqual(encoder.default(bytes([12])), "\x0c")
def test_encoding_sets(self):
"""``BytesEliotJSONEncoder`` can encode sets."""
encoder = BytesEliotJSONEncoder()
self.assertIn(encoder.default({1, 2}), ([1, 2], [2, 1]))
class EliotLoggedTestTests(TestCase):
"""
Tests for the automatic log-related provided by ``AsyncTestCase``.

View File

@ -85,6 +85,8 @@ class BytesEliotJSONEncoder(EliotJSONEncoder):
def default(self, o):
if isinstance(o, bytes):
return o.decode("utf-8", "backslashreplace")
if isinstance(o, set):
return list(o)
return EliotJSONEncoder.default(self, o)