-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-94808: add tests covering PySequence_[InPlace]Concat
#99319
base: main
Are you sure you want to change the base?
Changes from 4 commits
3556649
359b007
6cee271
5893ba7
3f46389
db068d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -505,6 +505,163 @@ def __getitem__(self, other): return 5 # so that C is a sequence | |
self.assertEqual(operator.ixor (c, 5), "ixor") | ||
self.assertEqual(operator.iconcat (c, c), "iadd") | ||
|
||
def test_concat(self): | ||
operator = self.module | ||
|
||
# Simple cases: | ||
data1 = [1, 2] | ||
data2 = ['a', 'b'] | ||
self.assertEqual(operator.concat(data1, data2), [1, 2, 'a', 'b']) | ||
self.assertEqual(operator.concat(data1, data2), data1 + data2) | ||
self.assertEqual(data1, [1, 2]) # must not change | ||
self.assertEqual(data2, ['a', 'b']) # must not change | ||
|
||
data1 = (1, 2) | ||
data2 = ('a', 'b') | ||
self.assertEqual(operator.concat(data1, data2), (1, 2, 'a', 'b')) | ||
self.assertEqual(operator.concat(data1, data2), data1 + data2) | ||
self.assertEqual(data1, (1, 2)) # must not change | ||
self.assertEqual(data2, ('a', 'b')) # must not change | ||
|
||
data1 = '12' | ||
data2 = 'ab' | ||
self.assertEqual(operator.concat(data1, data2), '12ab') | ||
self.assertEqual(operator.concat(data1, data2), data1 + data2) | ||
self.assertEqual(data1, '12') # must not change | ||
self.assertEqual(data2, 'ab') # must not change | ||
|
||
# Subclasses: | ||
class ListSubclass(list): | ||
pass | ||
|
||
data1 = ListSubclass([1, 2]) | ||
data2 = ListSubclass(['a', 'b']) | ||
|
||
res = operator.concat(data1, data2) | ||
self.assertIsInstance(res, list) | ||
self.assertEqual(res, [1, 2, 'a', 'b']) | ||
self.assertIsInstance(data1, ListSubclass) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the point of this -- of course data1 and data2 are instances of ListSubclass, that's how they were created. |
||
self.assertEqual(data1, ListSubclass([1, 2])) # must not change | ||
self.assertIsInstance(data2, ListSubclass) | ||
self.assertEqual(data2, ListSubclass(['a', 'b'])) # must not change | ||
|
||
# Custom type with `__add__`: | ||
class TupleSubclass(tuple): | ||
def __add__(self, other): | ||
return TupleSubclass(other + self) | ||
|
||
data1 = TupleSubclass([1, 2]) | ||
data2 = ('a', 'b') | ||
res = operator.concat(data1, data2) | ||
self.assertIsInstance(res, TupleSubclass) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is good. |
||
self.assertEqual(res, TupleSubclass(['a', 'b', 1, 2])) | ||
self.assertIsInstance(data1, TupleSubclass) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this is still questionable. I can't think of a scenario where this would fail. Or do you have coverage results showing this is needed? (Where?) Ditto for data2 and again in the following block of tests. |
||
self.assertEqual(data1, TupleSubclass([1, 2])) # must not change | ||
self.assertIsInstance(data2, tuple) | ||
self.assertEqual(data2, ('a', 'b')) # must not change | ||
|
||
# Corner cases: | ||
self.assertEqual(operator.concat([1, 2], []), [1, 2]) | ||
self.assertEqual(operator.concat([], [1, 2]), [1, 2]) | ||
self.assertEqual(operator.concat([], []), []) | ||
|
||
# Type errors: | ||
self.assertRaises(TypeError, operator.concat, [], 1) | ||
self.assertRaises(TypeError, operator.concat, 1, []) | ||
self.assertRaises(TypeError, operator.concat, 1, 1) | ||
self.assertRaises(TypeError, operator.concat, (), []) | ||
self.assertRaises(TypeError, operator.concat, [], ()) | ||
|
||
# Returns `NotImplemented`: | ||
class BrokenSeq(tuple): | ||
def __add__(self, other): | ||
return NotImplemented | ||
|
||
self.assertRaises(TypeError, operator.concat, BrokenSeq(), BrokenSeq()) | ||
|
||
def test_iconcat(self): | ||
operator = self.module | ||
|
||
# Simple cases: | ||
data1 = [1, 2, 3] | ||
data2 = ['a', 'b'] | ||
res = operator.iconcat(data1, data2) | ||
self.assertEqual(res, [1, 2, 3, 'a', 'b']) | ||
self.assertEqual(data1, [1, 2, 3, 'a', 'b']) # must change | ||
self.assertEqual(data2, ['a', 'b']) # must not change | ||
|
||
data1 = (1, 2) | ||
data2 = ('a', 'b') | ||
self.assertEqual(operator.iconcat(data1, data2), (1, 2, 'a', 'b')) | ||
self.assertEqual(operator.iconcat(data1, data2), data1 + data2) | ||
self.assertEqual(data1, (1, 2)) # must not change | ||
self.assertEqual(data2, ('a', 'b')) # must not change | ||
|
||
data1 = '12' | ||
data2 = 'ab' | ||
self.assertEqual(operator.iconcat(data1, data2), '12ab') | ||
self.assertEqual(operator.iconcat(data1, data2), data1 + data2) | ||
self.assertEqual(data1, '12') # must not change | ||
self.assertEqual(data2, 'ab') # must not change | ||
|
||
# Subclasses: | ||
class ListSubclass(list): | ||
pass | ||
|
||
data1 = ListSubclass([1, 2]) | ||
data2 = ListSubclass(['a', 'b']) | ||
res = operator.iconcat(data1, data2) | ||
self.assertIsInstance(res, ListSubclass) | ||
self.assertEqual(res, [1, 2, 'a', 'b']) | ||
self.assertIsInstance(data1, ListSubclass) | ||
self.assertEqual(data1, ListSubclass([1, 2, 'a', 'b'])) # must change | ||
self.assertIsInstance(data2, ListSubclass) | ||
self.assertEqual(data2, ListSubclass(['a', 'b'])) # must not change | ||
|
||
# Custom type with `__add__`: | ||
class TupleSubclass(tuple): | ||
def __add__(self, other): | ||
return TupleSubclass(other + self) | ||
|
||
data1 = TupleSubclass([1, 2]) | ||
data2 = ('a', 'b') | ||
res = operator.iconcat(data1, data2) | ||
self.assertIsInstance(res, TupleSubclass) | ||
self.assertEqual(res, TupleSubclass(['a', 'b', 1, 2])) | ||
self.assertIsInstance(data1, TupleSubclass) | ||
self.assertEqual(data1, TupleSubclass([1, 2])) # must not change | ||
self.assertIsInstance(data2, tuple) | ||
self.assertEqual(data2, ('a', 'b')) # must not change | ||
|
||
# Corner cases: | ||
self.assertEqual(operator.iconcat([1, 2], []), [1, 2]) | ||
self.assertEqual(operator.iconcat([], [1, 2]), [1, 2]) | ||
self.assertEqual(operator.iconcat([], []), []) | ||
self.assertEqual(operator.iconcat([], ()), []) | ||
|
||
# Type errors: | ||
self.assertRaises(TypeError, operator.iconcat, [], 1) | ||
self.assertRaises(TypeError, operator.iconcat, 1, []) | ||
self.assertRaises(TypeError, operator.iconcat, 1, 1) | ||
self.assertRaises(TypeError, operator.iconcat, (), []) | ||
|
||
# Returns `NotImplemented`: | ||
class BrokenSeq1(list): | ||
def __add__(self, other): | ||
return NotImplemented | ||
def __iadd__(self, other): | ||
return NotImplemented | ||
|
||
self.assertRaises(TypeError, | ||
operator.iconcat, BrokenSeq1(), BrokenSeq1()) | ||
|
||
class BrokenSeq2(tuple): | ||
def __add__(self, other): | ||
return NotImplemented | ||
|
||
self.assertRaises(TypeError, | ||
operator.iconcat, BrokenSeq2(), BrokenSeq2()) | ||
|
||
def test_length_hint(self): | ||
operator = self.module | ||
class X(object): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't prove anything, does it? Whether the result is a ListSubclass or a plain list, this will always be true. If you want to say something interesting here I'd assert that it isn't a ListSubclass instance.