def test_basic_success_yaml():
    """Ensure the yaml string is returned as 2 lines, with 1 and 3 parts
    respectively, ensure the parts of the second line can be reaseembled to
    the second line of the yaml string
    """
    sample = human_dump({"test": "data"})
    result = Colorize(grammar_dir=GRAMMAR_DIR, theme_path=THEME_PATH).render(
        doc=sample, scope="source.yaml"
    )
    assert len(result) == 2
    assert len(result[0]) == 1
    assert result[0][0]["chars"] == sample.splitlines()[0]
    assert len(result[1]) == 3
    assert "".join(line_part["chars"] for line_part in result[1]) == sample.splitlines()[1]
def test_graceful_failure(mocked_func, caplog):
    """Ensure a tokenization error returns the original one line json string
    w/o color and the log reflects the critical error
    """
    mocked_func.side_effect = ValueError()
    sample = json.dumps({"test": "data"})
    result = Colorize(grammar_dir=GRAMMAR_DIR, theme_path=THEME_PATH).render(
        doc=sample, scope="source.json"
    )
    assert len(result) == 1
    assert len(result[0]) == 1
    assert result[0][0]["chars"] == sample
    assert result[0][0]["color"] is None
    assert result[0][0]["column"] == 0
    assert "rendered without color" in caplog.text
def test_basic_success_log():
    """Ensure the log string is returned as 1 line, with 5 parts.

    Also ensure the parts can be reassembled to match the string.
    """
    sample = "1 ERROR text 42\n"

    result = Colorize(grammar_dir=GRAMMAR_DIR, theme_path=THEME_PATH).render(
        doc=sample,
        scope="text.log",
    )
    assert len(result) == 1
    first_line = result[0]
    line_parts = tuple(p.chars for p in first_line)
    assert line_parts == ("1", " ", "ERROR", " text ", "42", "\n")
    assert "".join(line_parts) == sample
def test_basic_success_json():
    """Ensure the json string is returned as 1 lines, 5 parts and can be reassembled
    to the json string"""
    sample = serialize(**SAMPLE_JSON) + "\n"
    colorized = Colorize(grammar_dir=GRAMMAR_DIR,
                         theme_path=THEME_PATH).render(
                             doc=sample,
                             scope="source.json",
                         )
    assert len(colorized) == 3
    serialized_lines = sample.splitlines(keepends=True)
    colorized_lines = [
        "".join(part.chars for part in line) for line in colorized
    ]
    assert serialized_lines == colorized_lines
    assert "".join(serialized_lines) == sample
def test_styled_markdown():
    """Ensure style is captured from markdown correctly."""
    content_format = ContentFormat.MARKDOWN

    result = Colorize(grammar_dir=GRAMMAR_DIR, theme_path=THEME_PATH).render(
        doc=STYLED_MARKDOWN,
        scope=content_format.value.scope,
    )
    assert result == [
        [
            SimpleLinePart(chars="This is a header\n",
                           column=0,
                           color=(86, 156, 214),
                           style="bold")
        ],
    ]
def test_basic_success_yaml():
    """Ensure the yaml string is returned as 2 lines, with 1 and 3 parts
    respectively, ensure the parts of the second line can be reassembled to
    the second line of the yaml string
    """
    sample = serialize(**SAMPLE_YAML)
    result = Colorize(grammar_dir=GRAMMAR_DIR, theme_path=THEME_PATH).render(
        doc=sample,
        scope="source.yaml",
    )
    assert len(result) == 2
    assert len(result[0]) == 1
    assert result[0][0].chars == sample.splitlines(keepends=True)[0]
    assert len(result[1]) == 4
    assert ("".join(
        line_part.chars
        for line_part in result[1]) == sample.splitlines(keepends=True)[1])