Refactor if len(list) != 0

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


 def sayHello(name, friends):
-    if (len(friends) != 0):
+    if (len(friends) > 0):
         raise Exception("You can't say hello to other people's friends!")
     return ('Hello, ' + name)

total jobs: 9
complete: 9 (100.00%)
survival rate: 11.11%

Note

If we didn’t have the test_sayHello_with_friends() test then the != -> < mutation would have survived as well!

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

hello.py
def sayHello(name, friends):
    if len(friends) != 0:
        raise Exception("You can't say hello to other people's friends!")

    return "Hello, " + name
test_hello.py
import hello
import unittest

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

    def test_sayHello_with_friends(self):
        with self.assertRaises(Exception):
            hello.sayHello("Alex", ["Krasi"])


if __name__ == "__main__":
    unittest.main()
hello2.py
def sayHello(name, friends):
    if friends:
        raise Exception("You can't say hello to other people's friends!")

    return "Hello, " + name
test_hello2.py
import hello2
import unittest

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

    def test_sayHello_with_friends(self):
        with self.assertRaises(Exception):
            hello2.sayHello("Alex", ["Krasi"])


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