Refactor if X is None¶
Reproducer¶
$ pip install nose
$ pip install https://github.com/sixty-north/cosmic-ray/zipball/master
$ cosmic-ray run --test-runner nose --baseline=10 example.json hello.py -- test_hello.py
$ cosmic-ray report example.json
job ID 4:Outcome.SURVIVED:hello
command: cosmic-ray worker hello replace_Is_with_Eq 0 nose -- -v test_hello.py
--- mutation diff ---
--- a/example_06/hello.py
+++ b/example_06/hello.py
@@ -1,7 +1,7 @@
def sayHello(name, title=None):
- if (title is None):
+ if (title == None):
title = 'Mr.'
return ('Hello %s %s' % (title, name))
total jobs: 9
complete: 9 (100.00%)
survival rate: 11.11%
Note
Since PR #162
Cosmic-Ray skips mutations of the kind == -> is
but doesn’t skip
the opposite of is -> ==
!
Now compare the results after refactoring
$ cosmic-ray run --test-runner nose --baseline=10 example.json hello2.py -- test_hello2.py
$ cosmic-ray report example.json --full-report
total jobs: 0
no jobs completed
Functionality is the same but we have reduced the number of possible mutations!
Source code¶
def sayHello(name, title=None):
if title is None:
title = "Mr."
return "Hello %s %s" % (title, name)
import hello
import unittest
class TestHello(unittest.TestCase):
def test_sayHello_without_title(self):
result = hello.sayHello("Senko")
self.assertEqual(result, "Hello Mr. Senko")
def test_sayHello_with_title(self):
result = hello.sayHello("Senko", title="The Misterious")
self.assertEqual(result, "Hello The Misterious Senko")
if __name__ == "__main__":
unittest.main()
def sayHello(name, title=None):
if not title:
title = "Mr."
return "Hello %s %s" % (title, name)
import hello
import unittest
class TestHello(unittest.TestCase):
def test_sayHello_without_title(self):
result = hello.sayHello("Senko")
self.assertEqual(result, "Hello Mr. Senko")
def test_sayHello_with_title(self):
result = hello.sayHello("Senko", title="The Misterious")
self.assertEqual(result, "Hello The Misterious Senko")
if __name__ == "__main__":
unittest.main()