Killing mutants by refactoring if str != “”

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 3:Outcome.SURVIVED:hello
command: cosmic-ray worker hello replace_NotEq_with_NotIn 0 nose -- test_hello.py
--- mutation diff ---
--- a/example_03/hello.py
+++ b/example_03/hello.py
@@ -1,7 +1,7 @@


 def sayHello(name, greeting=''):
-    if (greeting != ''):
+    if (greeting not in ''):
         return ((greeting + ', ') + name)
     else:
         return ('Hello, ' + name)

job ID 8:Outcome.SURVIVED:hello
command: cosmic-ray worker hello replace_NotEq_with_Gt 0 nose -- test_hello.py
--- mutation diff ---
--- a/example_03/hello.py
+++ b/example_03/hello.py
@@ -1,7 +1,7 @@


 def sayHello(name, greeting=''):
-    if (greeting != ''):
+    if (greeting > ''):
         return ((greeting + ', ') + name)
     else:
         return ('Hello, ' + name)

total jobs: 8
complete: 8 (100.00%)
survival rate: 25.00%

Now compare the results with hello2.py and test_hello2.py where we’ve refactored the condition to if greeting::

$ cosmic-ray run --test-runner nose --baseline=10 example.json hello2.py -- test_hello2.py
$ cosmic-ray report example.json
total jobs: 0
no jobs completed

Source code

hello.py
def sayHello(name, greeting=""):
    if greeting != "":
        return greeting + ', ' + name
    else:
        return "Hello, " + name
hello2.py
def sayHello(name, greeting=""):
    if greeting:
        return greeting + ', ' + name
    else:
        return "Hello, " + name
test_hello.py
import hello
import unittest

class TestHello(unittest.TestCase):
    def test_sayHello_name(self):
        result = hello.sayHello("Alex")
        self.assertEqual(result, "Hello, Alex")

    def test_sayHello_name_with_greeting(self):
        result = hello.sayHello("Alex", "Happy testing")
        self.assertEqual(result, "Happy testing, Alex")

if __name__ == "__main__":
    unittest.main()
test_hello2.py
import hello2
import unittest

class TestHello(unittest.TestCase):
    def test_sayHello_name(self):
        result = hello2.sayHello("Alex")
        self.assertEqual(result, "Hello, Alex")

    def test_sayHello_name_with_greeting(self):
        result = hello2.sayHello("Alex", "Happy testing")
        self.assertEqual(result, "Happy testing, Alex")

if __name__ == "__main__":
    unittest.main()