gh-121708: Improve test coverage for `unittest.util` (GH-121713)

This commit is contained in:
Tomas R 2024-07-14 10:08:47 +02:00 committed by GitHub
parent a2bec77d25
commit 901ea411bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,33 @@
import unittest
from unittest.util import safe_repr, sorted_list_difference, unorderable_list_difference
class TestUtil(unittest.TestCase):
def test_safe_repr(self):
class RaisingRepr:
def __repr__(self):
raise ValueError("Invalid repr()")
class LongRepr:
def __repr__(self):
return 'x' * 100
safe_repr(RaisingRepr())
self.assertEqual(safe_repr('foo'), "'foo'")
self.assertEqual(safe_repr(LongRepr(), short=True), 'x'*80 + ' [truncated]...')
def test_sorted_list_difference(self):
self.assertEqual(sorted_list_difference([], []), ([], []))
self.assertEqual(sorted_list_difference([1, 2], [2, 3]), ([1], [3]))
self.assertEqual(sorted_list_difference([1, 2], [1, 3]), ([2], [3]))
self.assertEqual(sorted_list_difference([1, 1, 1], [1, 2, 3]), ([], [2, 3]))
self.assertEqual(sorted_list_difference([4], [1, 2, 3, 4]), ([], [1, 2, 3]))
self.assertEqual(sorted_list_difference([1, 1], [2]), ([1], [2]))
self.assertEqual(sorted_list_difference([2], [1, 1]), ([2], [1]))
self.assertEqual(sorted_list_difference([1, 2], [1, 1]), ([2], []))
def test_unorderable_list_difference(self):
self.assertEqual(unorderable_list_difference([], []), ([], []))
self.assertEqual(unorderable_list_difference([1, 2], []), ([2, 1], []))
self.assertEqual(unorderable_list_difference([], [1, 2]), ([], [1, 2]))
self.assertEqual(unorderable_list_difference([1, 2], [1, 3]), ([2], [3]))