Skip to main content
2 of 2
minor clarification
stefan
  • 3k
  • 8
  • 17

Let's start by removing print from the loop and add a return value

def censor(text,word):
    #[...]
        else:
            censored_text += x
    return censored_text

now we have a testable function. we add some tests

import unittest

class TestSome(unittest.TestCase):

    def test_not_found(self):
        self.assertEqual(censor("", "bar"), "")
        self.assertEqual(censor("f", "bar"), "f")
        self.assertEqual(censor("foo", "bar"), "foo")
        self.assertEqual(censor("fooo", "bar"), "fooo")

    def test_found(self):
        self.assertEqual(censor("bar", "bar"), "***")
        self.assertEqual(censor("bar!", "bar"), "***!")
        self.assertEqual(censor("cow bar", "bar"), "cow ***")

    def test_parts(self):
        self.assertEqual(censor("foobar", "bar"), "foobar")
        self.assertEqual(censor("bare", "bar"), "bare")

    def test_capital(self):
        self.assertEqual(censor("Bar", "bar"), "***")

    def test_multiple(self):
        self.assertEqual(censor("foo bar bar foo", "bar"), "foo *** *** foo")

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

Running the tests show that the functions are not meeting my expectations.

  • detect_word_start() does not return a value if nothing found
  • you miss words at the beginning of a sentence becaue of capitalization
  • you censor different words containing the character sequence
  • you miss multiple appearences
stefan
  • 3k
  • 8
  • 17