In: Computer Science
Write a python program using the following requirements:
Create a class called Sentence which
Include a unit test in an if __name__ block to test the class. Use assert to check for errors.
Unit Test Example:
class Sentence:
def __init__(self, sentence):
self.__sentence = sentence
def get_all_words(self):
return self.__sentence.split()
def get_word(self, index):
return self.__sentence.split()[index]
def replace(self, index, new_word):
l = self.__sentence.split()
l[index] = new_word
self.__sentence = ' '.join(l)
def __str__(self):
return str(self.__sentence)
if __name__ == "__main__":
s = "Hello, this is the sample test case."
sentence = Sentence(s)
# Testing get_all_words
assert ["Hello,", "this", "is", "the", "sample", "test", "case."] == sentence.get_all_words()
# Testing get_word(index)
assert "Hello," == sentence.get_word(0) and "is" == sentence.get_word(2) and "test" == sentence.get_word(5)
# Testing replace(index, new_word) along with print
print("Before Replace:", sentence)
sentence.replace(4, "new")
assert str(sentence) == "Hello, this is the new test case."
print("After Replace:", sentence)