-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
bpo-30308: Code coverage for argument in random.shuffle #1504
Conversation
Lib/test/test_random.py
Outdated
self.assertRaises(TypeError, shuffle, (1, 2, 3)) | ||
self.assertRaises(TypeError, shuffle, {1, 2, 3}) | ||
self.assertRaises(TypeError, shuffle, 'shuffle') | ||
self.assertRaises(KeyError, shuffle, dict(a=1, b=2)) |
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.
Let's only keep the first of these four tests. It is reasonable to check to see if an immutable argument raises a TypeError. The rest are either repeats of the same concept or an over-specification (what the current code happens to do rather than what is guaranteed).
Lib/test/test_random.py
Outdated
def test_shuffle_dict_with_numeric_keys(self): | ||
shuffle = self.gen.shuffle | ||
seq = dict(zip(range(9), 'abcdefghi')) | ||
self.assertRaises(KeyError, shuffle, seq) |
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.
I think this isn't a valid test and should be dropped. It is an effect incidental to the implementation (due to duck-typing).
Lib/test/test_random.py
Outdated
mock_random = unittest.mock.Mock(return_value=0.5) | ||
seq = bytearray(b'abcdefghijk') | ||
shuffle(seq, mock_random) | ||
self.assertEqual(seq, b'ajbhcgdiekf') |
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.
Let's drop line 169. We're not guaranteeing a particular shuffle order. Instead, we're just checking the shuffle worked at all when given a shuffle argument.
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.
Very little needs to be added to the shuffle() tests. We need to check to make sure it doesn't fail when given a random argument and that it raises a TypeError for an immutable input. Everything else are mostly incidental implementation details (nothing that we want to guarantee or even care about). We try to write tests that would matter if they failed at some point in the future if shuffle() gets rewritten.
Thank you for taking the time to explain the reasons for what to include and not include. The things I was wondering about make much more sense now. I really have to keep in mind that simple is better. :-) |
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.
Looks great. Thank you.
Add code coverage tests for optional parameter
random
in random.shuffle.