Skip to content
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

Merged
merged 3 commits into from
May 11, 2017

Conversation

csabella
Copy link
Contributor

@csabella csabella commented May 8, 2017

Add code coverage tests for optional parameter random in random.shuffle.

@brettcannon brettcannon added the tests Tests in the Lib/test dir label May 8, 2017
@vstinner vstinner self-requested a review May 9, 2017 07:57
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))
Copy link
Contributor

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).

def test_shuffle_dict_with_numeric_keys(self):
shuffle = self.gen.shuffle
seq = dict(zip(range(9), 'abcdefghi'))
self.assertRaises(KeyError, shuffle, seq)
Copy link
Contributor

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).

mock_random = unittest.mock.Mock(return_value=0.5)
seq = bytearray(b'abcdefghijk')
shuffle(seq, mock_random)
self.assertEqual(seq, b'ajbhcgdiekf')
Copy link
Contributor

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.

Copy link
Contributor

@rhettinger rhettinger left a 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.

@csabella
Copy link
Contributor Author

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. :-)

Copy link
Contributor

@rhettinger rhettinger left a 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.

@rhettinger rhettinger merged commit f111fd2 into python:master May 11, 2017
@csabella csabella deleted the random_coverage branch May 11, 2017 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants