Ejemplo n.º 1
0
    def compare_versus_expected(
        cls,
        stream_name,
        actual_stream,
        expected_text,
        additional_text=None,
        log_extra=None,
    ):
        """
        Do a thorough comparison of the actual stream against the expected text.
        """

        if additional_text:
            assert actual_stream.getvalue().strip().startswith(expected_text.strip()), (
                f"Block\n---\n{expected_text}\n---\nwas not found at the start of"
                + "\n---\n{actual_stream.getvalue()}\nExtra:{log_extra}"
            )

            for next_text_block in additional_text:
                was_found = next_text_block.strip() in actual_stream.getvalue().strip()
                diff = difflib.ndiff(
                    next_text_block.strip().splitlines(),
                    actual_stream.getvalue().strip().splitlines(),
                )

                diff_values = ParserHelper.newline_character.join(list(diff))
                print(diff_values, file=sys.stderr)
                if not was_found:
                    raise AssertionError(
                        f"Block\n---\n{next_text_block}\n---\nwas not found in\n---\n{actual_stream.getvalue()}"
                    )
        elif actual_stream.getvalue().strip() != expected_text.strip():
            diff = difflib.ndiff(
                expected_text.splitlines(), actual_stream.getvalue().splitlines()
            )

            diff_values = f"{ParserHelper.newline_character.join(list(diff))}\n---\n"

            LOGGER.warning(
                "actual>>%s",
                ParserHelper.make_value_visible(actual_stream.getvalue()),
            )
            print(
                f"WARN>actual>>{ParserHelper.make_value_visible(actual_stream.getvalue())}"
            )
            LOGGER.warning("expect>>%s", ParserHelper.make_value_visible(expected_text))
            print(f"WARN>expect>>{ParserHelper.make_value_visible(expected_text)}")
            if log_extra:
                print(f"log_extra:{log_extra}")
            raise AssertionError(f"{stream_name} not as expected:\n{diff_values}")
Ejemplo n.º 2
0
    def __munge(cls, show_whitespace: bool, log_format: str, args: List[Any]) -> str:
        split_log_format = log_format.split("$")
        split_log_format_length = len(split_log_format)
        args_length = len(args)
        if split_log_format_length != args_length + 1:
            raise Exception(
                "The number of $ substitution characters does not equal the number of arguments in the list."
            )

        recipient_array: List[str] = [""] * (split_log_format_length + args_length)
        for next_array_index, _ in enumerate(recipient_array):
            if next_array_index % 2 == 0:
                recipient_array[next_array_index] = split_log_format[
                    int(next_array_index / 2)
                ]
            elif show_whitespace:
                recipient_array[
                    next_array_index
                ] = ParserHelper.make_whitespace_visible(
                    args[int(next_array_index / 2)]
                )
            else:
                recipient_array[next_array_index] = ParserHelper.make_value_visible(
                    args[int(next_array_index / 2)]
                )
        return "".join(recipient_array)
Ejemplo n.º 3
0
def verify_markdown_roundtrip(source_markdown, actual_tokens):
    """
    Verify that we can use the information in the tokens to do a round trip back
    to the original Markdown that created the token.
    """

    if "\t" in source_markdown:
        new_source = []
        split_source = source_markdown.split(ParserHelper.newline_character)
        for next_line in split_source:
            if "\t" in next_line:
                next_line = ParserHelper.detabify_string(next_line)
            new_source.append(next_line)
        source_markdown = ParserHelper.newline_character.join(new_source)

    transformer = TransformToMarkdown()
    original_markdown = transformer.transform(actual_tokens)

    print(
        "".join(
            [
                "\n-=-=-\nExpected\n-=-=-\n-->",
                ParserHelper.make_value_visible(source_markdown),
                "<--\n-=-=-\nActual\n-=-=-\n-->",
                ParserHelper.make_value_visible(original_markdown),
                "<--\n-=-=-\n",
            ]
        )
    )
    diff = difflib.ndiff(source_markdown, original_markdown)
    diff_values = "".join(
        [
            "\n-=-=-n",
            ParserHelper.newline_character.join(list(diff)),
            "\n-=-=-expected\n-->",
            ParserHelper.make_value_visible(source_markdown),
            "<--\n-=-=-actual\n-->",
            ParserHelper.make_value_visible(original_markdown),
            "<--\n-=-=-\n",
        ]
    )

    assert (
        source_markdown == original_markdown
    ), f"Markdown strings are not equal.{diff_values}"