def __justify(self, text: str, length: int): min_spaces_to_add = SpaceJustifyHelper.get_min_spaces_to_add( text, length) # Add all spaces with same size text = text.replace(" ", " " + " " * min_spaces_to_add) # Add spaces to reach the correct length if SpaceJustifyHelper.have_to_add_spaces(text, length): text = self.__add_missing_spaces(text, length) return text
def test_get_space_size_should_return_to_number_of_spaces_size_by_size(): original_text = "In the beginning God..." qtd = SpaceJustifyHelper.get_space_size(original_text) assert qtd is not None assert qtd == 2
def test_have_to_add_spaces_should_return_false_if_string_len_is_not_length(): original_text = "In the beginning God..." have_to_add = SpaceJustifyHelper.have_to_add_spaces(original_text, 24) assert have_to_add is not None assert have_to_add is True
def test_get_min_spaces_to_add_should_return_the_qtd_of_spaces_to_append_in_any_space( ): original_text = "In the beginning God..." qtd = SpaceJustifyHelper.get_min_spaces_to_add(original_text, 30) assert qtd is not None assert qtd is 2
def test_get_qtd_spaces_in_text_should_return_number_of_spaces_with_any_qtd_of_spaces( ): original_text = "In the beginning God..." qtd = SpaceJustifyHelper.get_qtd_spaces_in_text(original_text) assert qtd is not None assert qtd == 3
def test_get_qtd_spaces_to_add_should_return_difference_between_string_length_and_length( ): original_text = "In the beginning God..." qtd = SpaceJustifyHelper.get_qtd_spaces_to_add(original_text, 30) assert qtd is not None assert qtd == 7
def __add_missing_spaces(self, text: str, length: int): spaces_to_add = SpaceJustifyHelper.get_qtd_spaces_to_add(text, length) space_size = SpaceJustifyHelper.get_space_size(text) #print(space_size) #print(text) spaces_added = 0 try: text_list = SplitHelper.split_with_delimiter(text, " ", space_size) for i in range(0, len(text_list)): if text_list[i][-1] == " ": text_list[i] = text_list[i] + " " spaces_added = spaces_added + 1 if spaces_added >= spaces_to_add: break text = "".join(text_list) except: return text return text