def test_repo(self): repos = FileRepository('teste') s1 = Sentence(1, 'abc abc abc') repos.add(s1) self.assertEqual(str(repos.return_all()[0]), '1 - abc abc abc') x = repos.return_all() self.assertEqual(x, repos.return_all()) s2 = Sentence(1, 'abc abc abc') repos.add(s2) self.assertEqual(str(repos.return_all()[1]), '1 - abc abc abc') x = repos.return_all() self.assertEqual(x, repos.return_all()) s3 = Sentence(1, 'abc abc abc') repos.add(s3) self.assertEqual(str(repos.return_all()[2]), '1 - abc abc abc') x = repos.return_all() self.assertEqual(x, repos.return_all()) s4 = Sentence(1, 'abc abc abc') repos.add(s4) self.assertEqual(str(repos.return_all()[3]), '1 - abc abc abc') x = repos.return_all() self.assertEqual(x, repos.return_all())
def __init_from_file(self): file_handler = open(self.__file_name, 'r') input = file_handler.read() input = input.strip('\n') input = input.split('\n') selected_sentece = random.randint(0, len(input) - 1) selected_sentece = input[selected_sentece] selected_sentece = selected_sentece.split(' ') final_sentence = Sentence() for word in selected_sentece: new_word = Word(word) final_sentence.append(new_word) self.__sentence = final_sentence self.__initial_sentence = copy.deepcopy(final_sentence)
def ui_add_sentence_3(self): """ the function for adding a sentence in the repository :return: """ sent = str(input("introduce the sentence and press enter: ")) id = int(len(self._sentence_controller.return_all())) sentence = Sentence(id, sent) x = [] ok = 0 nr = 0 d = 0 okk = True for l in sent: x.append(l) for l in x: if l == " ": ok += 1 nr += 1 for l in self._sentence_controller.return_all(): i = 0 cuv = [] j = 0 for letter in l: cuv.append(letter) while i < len(cuv) and j < len(x): if cuv[i] == x[i]: okk = False if nr > 3 and ok >= 2 or okk == False: self._sentence_controller.add(sentence) else: print("the sentence is not valid! ")
def test_repo(self): repos = Repository() s1 = Sentence(1, 'abc abc abc') repos.add(s1) self.assertEqual(str(repos.return_all()[0]), '1 - abc abc abc') x = repos.return_all() self.assertEqual(x, repos.return_all())
def test_get_all(self): sentence_list = self.repo.get_all() self.assertEqual(3, len(sentence_list), "Error at getting sentence list.") cmp_sentence = Sentence("anna has apples") self.assertEqual(cmp_sentence.hidden, sentence_list[0].hidden, "Matching error.") self.assertEqual(cmp_sentence.string, sentence_list[0].string, "Matching error.")
def add(self, new_sentence): """ Adds e new sentence to the repo :param new_sentence: a string :return: Controller Exception if the sentence is already in the repo. """ new_sentence = new_sentence.strip() sentence_list = self.__repo.get_all_string() if new_sentence in sentence_list: raise ControllerException("Sentence already exists.") sentence_to_add = Sentence(new_sentence) self.__repo.store(sentence_to_add)
class TestSentence(TestCase): def setUp(self): super().setUp() self.sentence = Sentence("anna has apples") def test_find_letter(self): self.assertEqual(True, self.sentence.find_letter('n'), "error at finding letter") self.assertEqual(False, self.sentence.find_letter('z'), "error at finding letter") def test_did_win(self): sent = Sentence("anna") self.assertEqual(False, sent.did_win(), "error at game end check") sent.find_letter("n") self.assertEqual(True, sent.did_win(), "error at game check") def test__str__(self): sent = Sentence("anna") self.assertEqual("a__a", str(sent), "Error at string") def test_hide_letters(self): self.assertEqual("a__a has a____s", str(self.sentence), "Error at hiding")
def ui_add_sentence_2(self): """ the function for adding a sentence in the repository :return: """ sent = str(input("introduce the sentence and press enter: ")) id = int(len(self._sentence_controller.return_all())) sentence = Sentence(id, sent) x = [] ok = 0 nr = 0 for l in sent: x.append(l) for l in x: if l == " ": ok += 1 nr += 1 if nr > 3 and ok >=2: self._sentence_controller.add(sentence) else: print("the sentence is not valid! ")
def read_from_file(self): """ function used for reading from the file and for adding those lines to the repo as sentence instances :return: """ # try: # f = open(self.__fName, 'r') # line = f.readline().strip() # while line != "": # Repository.add(self, line) # line = f.readline().strip() # except IOError as ie: # print("there was a problem while reading from the file! ") # finally: # f.close() f = open(self.__fName, 'r') line = f.readline().strip() while line != "": id = len(self.return_all()) sent = Sentence(id, line) Repository.add(self, sent) line = f.readline().strip() f.close()
def test_store(self): self.repo.store(Sentence("ana are mere")) self.assertEqual(4, len(self.repo.get_all()), "Error after adding entity") self.assertRaises(RepositoryException, self.repo.store, Sentence("eu"))
def test1(self): s1 = Sentence(1, 'abc abc') s2 = Sentence(1, 'abc abc') self.assertEqual(s1, s2)
def test4(self): s1 = Sentence(1, 'abcd abcd') self.assertEqual(s1, Sentence(1, 'abcd abcd'))
def test__str__(self): sent = Sentence("anna") self.assertEqual("a__a", str(sent), "Error at string")
def test_did_win(self): sent = Sentence("anna") self.assertEqual(False, sent.did_win(), "error at game end check") sent.find_letter("n") self.assertEqual(True, sent.did_win(), "error at game check")
def setUp(self): super().setUp() self.sentence = Sentence("anna has apples")
from controller.sentence_controller import Controller from domain.entities import Sentence from repo.repository import Repository from ui.console import Console repo = Repository() controller = Controller(repo) console = Console(controller) controller.add(Sentence(1, 'the fox is ok')) controller.add(Sentence(2, 'the kid is ok')) while __name__ == '__main__': console.RunMenu()
def test_get_available_letters(self): sentence = Sentence("anna has apples") char_list = self.controller.get_available_letters(sentence) self.assertEqual(True, 'a' in char_list, "fail at getting letter") self.assertEqual(True, 'h' in char_list, "fail at getting letter") self.assertEqual(True, 's' in char_list, "fail at getting letter")
def test3(self): s1 = Sentence(1, 'abc abc') s2 = Sentence(2, 'abc abc') self.assertNotEqual(s1, s2)