def test_simple_case_from_start_without_whitespace():
    """
    Make sure that we test a simple extraction from the start of the string without whitespace.
    """

    # Arrange
    input_string = "  this is a test"
    start_index = 0
    character_to_match = " "
    expected_output = (0, 0)

    # Act
    actual_output = ParserHelper.collect_backwards_while_one_of_characters(
        input_string, start_index, character_to_match)

    # Assert
    assert expected_output == actual_output
def test_empty_string_with_good_index():
    """
    Make sure that an empty string is handled properly with a good index
    """

    # Arrange
    input_string = ""
    start_index = 0
    character_to_match = " "
    expected_output = (0, 0)

    # Act
    actual_output = ParserHelper.collect_backwards_while_one_of_characters(
        input_string, start_index, character_to_match)

    # Assert
    assert expected_output == actual_output
def test_simple_case_from_end_with_eol_index():
    """
    Make sure that we test a simple extraction from the end of the string with whitespace
    """

    # Arrange
    input_string = "this is a test  "
    start_index = -1
    character_to_match = " "
    expected_output = (2, len(input_string) - 2)

    # Act
    actual_output = ParserHelper.collect_backwards_while_one_of_characters(
        input_string, start_index, character_to_match)

    # Assert
    assert expected_output == actual_output
def test_empty_string_with_bad_left_index():
    """
    Make sure that an empty string is handled properly with an index that is too far to the left.
    """

    # Arrange
    input_string = ""
    start_index = -2
    character_to_match = " "
    expected_output = (None, None)

    # Act
    actual_output = ParserHelper.collect_backwards_while_one_of_characters(
        input_string, start_index, character_to_match)

    # Assert
    assert expected_output == actual_output
Beispiel #5
0
    def remove_final_whitespace(self):
        """
        Remove any final whitespace.  Used by paragraph blocks so that they do not
        end with a hard break.
        """

        removed_whitespace = ""
        (
            collected_whitespace_length,
            first_non_whitespace_index,
        ) = ParserHelper.collect_backwards_while_one_of_characters(
            self.token_text, -1, Constants.whitespace)
        if collected_whitespace_length:
            removed_whitespace = self.token_text[
                first_non_whitespace_index:first_non_whitespace_index +
                collected_whitespace_length]
            self.token_text = self.token_text[0:first_non_whitespace_index]
        return removed_whitespace