Example #1
0
def test_rule_highlight_zero_length_match():
    """Highlight with rule that has a regex that matches zero-length data."""
    color = chromaterm.Color('bold')
    rule = chromaterm.Rule('hello() world')
    rule.add_color(color, group=1)

    assert repr(rule.highlight('hello world')) == repr('hello world')
Example #2
0
def test_rule_get_matches_new_line(pcre):
    '''Attempt to match a new line, which shouldn't be possible as it's considered
    as a separator (i.e. not passed to be highlighted). New lines are only used
    to replace data matched by exclusive rules.'''
    rule = chromaterm.Rule(r'hell\Wo', chromaterm.Color('bold'), pcre=pcre)

    assert not rule.get_matches(b'hell\no')
Example #3
0
def test_rule_change_color():
    """Confirm that a color change overrides the old one."""
    rule = chromaterm.Rule('hello', color=chromaterm.Color('bold'))

    old_color = rule.color
    rule.color = chromaterm.Color('b#123123')
    assert old_color is not rule.color
Example #4
0
def test_config_add_rule():
    """Add a rule to config."""
    config = chromaterm.Config()
    rule = chromaterm.Rule('hello')

    config.add_rule(rule)
    assert rule in config.rules
Example #5
0
def test_rule_print(capsys):
    """Test the print wrapper for rule."""
    rule = chromaterm.Rule('hello|world', color=chromaterm.Color('bold'))

    data = 'hello world'
    rule.print(data)

    assert rule.highlight(data) in capsys.readouterr().out
Example #6
0
def test_rule_remove_color_specific_group():
    """Remove from a rule the color of a specific regex group."""
    rule = chromaterm.Rule('he(llo)')
    rule.add_color(chromaterm.Color('bold'), group=1)
    assert rule.colors.get(1) is not None

    rule.remove_color(1)
    assert rule.colors.get(1) is None
Example #7
0
def test_rule_remove_color_default_group():
    """Remove from a rule the color of the default regex group (0)."""
    rule = chromaterm.Rule('hello')
    rule.add_color(chromaterm.Color('bold'))
    assert rule.colors.get(0) is not None

    rule.remove_color(0)
    assert rule.colors.get(0) is None
Example #8
0
def test_rule_change_pcre(pcre):
    '''Confirm that a change in the PCRE flag recompiles the regex.'''
    # pylint: disable=protected-access
    rule = chromaterm.Rule('hello', chromaterm.Color('bold'), pcre=pcre)

    old_regex = rule._regex
    rule.pcre = not rule.pcre
    assert old_regex is not rule._regex
Example #9
0
def test_rule___call__():
    """Confim Rule's decorator (__call__)."""
    rule = chromaterm.Rule('hello', color=chromaterm.Color('bold'))

    @rule
    def echo(*args):
        return ', '.join(args)

    assert repr(rule.highlight('hello world')) == repr(echo('hello world'))
Example #10
0
def test_rule_get_matches(pcre):
    '''Get matches of rule that only colors the default regex group.'''
    color = chromaterm.Color('bold')
    rule = chromaterm.Rule('hello|world', color, pcre=pcre)

    data = b'hello world'
    expected = [(0, 5, color), (6, 11, color)]

    assert rule.get_matches(data) == expected
Example #11
0
def test_rule_add_color_default_group():
    """Add to a rule a color to the default regex group (0)."""
    rule = chromaterm.Rule('hello')
    assert rule.color is None

    color = chromaterm.Color('bold')
    rule.add_color(color)
    assert rule.color is color
    assert rule.colors.get(0) is color
Example #12
0
def test_rule_set_color_default_group(pcre):
    '''Add to a rule a color to the default regex group (0).'''
    rule = chromaterm.Rule('hello', pcre=pcre)
    assert rule.color is None

    color = chromaterm.Color('bold')
    rule.set_color(color)
    assert rule.color is color
    assert rule.colors.get(0) is color
Example #13
0
def test_config_highlight_common_end_type_same(pcre):
    '''Two rules with same color type, and both sharing the same end of a
    match. The most recent rule will be closer to the match's end.
    1: --------------
    2:        -------'''
    rule1 = chromaterm.Rule('hello', chromaterm.Color('b#123123'), pcre=pcre)
    rule2 = chromaterm.Rule('llo', chromaterm.Color('b#456456'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule1)
    config.rules.append(rule2)

    data = b'hello'
    expected = [
        rule1.color.color_code, b'he', rule2.color.color_code, b'llo',
        rule1.color.color_code, rule1.color.color_reset
    ]

    assert config.highlight(data) == b''.join(expected)
Example #14
0
def test_rule_set_color_clear_existing(pcre):
    '''Clear the color of a rule by setting it to None.'''
    color = chromaterm.Color('bold')
    colors = {0: color, 1: color}
    rule = chromaterm.Rule('(hello|world)', colors, pcre=pcre)

    assert list(rule.colors) == [0, 1]
    rule.set_color(None, group=0)
    assert list(rule.colors) == [1]
Example #15
0
def test_config_highlight_overlap_full_type_same(pcre):
    '''Two rules, fully overlapping matches, same color type. The reset of the
    first rule should be replaced with the color code of the second (most recent)
    rule to prevent the reset from interrupting the color.
    1: ----------
    2: ----------'''
    rule1 = chromaterm.Rule('hello', chromaterm.Color('b#123123'), pcre=pcre)
    rule2 = chromaterm.Rule('hello', chromaterm.Color('b#456456'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule1)
    config.rules.append(rule2)

    data = b'hello'
    expected = [
        rule1.color.color_code, rule2.color.color_code, b'hello',
        rule1.color.color_code, rule1.color.color_reset
    ]

    assert config.highlight(data) == b''.join(expected)
Example #16
0
def test_config_highlight_common_beginning_type_mixed():
    """Two rules with different color types, and both sharing the same start of
    a match. The most recent rule will be closer to the match's start.
    1: --------------
    2: -------"""
    config = chromaterm.Config()

    rule1 = chromaterm.Rule('hello', color=chromaterm.Color('b#123123 italic'))
    rule2 = chromaterm.Rule('he', color=chromaterm.Color('b#321321 bold'))
    config.add_rule(rule1)
    config.add_rule(rule2)

    data = 'hello'
    expected = [
        rule1.color.color_code, rule2.color.color_code, 'he', RESET_BOLD,
        rule1.color.color_types[0][1], 'llo', rule1.color.color_reset
    ]

    assert repr(config.highlight(data)) == repr(''.join(expected))
Example #17
0
def test_config_highlight_overlap_full_type_different(pcre):
    '''Two rules, fully overlapping matches, different color types. Should not
    affect each other as they have different types. Most recent rule should be
    closest to the match.
    1: ----------
    2: ----------'''
    rule1 = chromaterm.Rule('hello', chromaterm.Color('b#123123'), pcre=pcre)
    rule2 = chromaterm.Rule('hello', chromaterm.Color('f#456456'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule1)
    config.rules.append(rule2)

    data = b'hello'
    expected = [
        rule1.color.color_code, rule2.color.color_code, b'hello',
        rule2.color.color_reset, rule1.color.color_reset
    ]

    assert config.highlight(data) == b''.join(expected)
Example #18
0
def test_config_highlight_common_end_type_same():
    """Two rules with same color type, and both sharing the same end of a
    match. The most recent rule will be closer to the match's end.
    1: --------------
    2:        -------"""
    config = chromaterm.Config()

    rule1 = chromaterm.Rule('hello', color=chromaterm.Color('b#123123'))
    rule2 = chromaterm.Rule('llo', color=chromaterm.Color('b#321321'))
    config.add_rule(rule1)
    config.add_rule(rule2)

    data = 'hello'
    expected = [
        rule1.color.color_code, 'he', rule2.color.color_code, 'llo',
        rule1.color.color_code, rule1.color.color_reset
    ]

    assert repr(config.highlight(data)) == repr(''.join(expected))
Example #19
0
def test_config_highlight_overlap_full_type_same():
    """Two rules, fully overlapping matches, same color type. The reset of the
    first rule should be replaced with the color code of the second (most recent)
    rule to prevent the reset from interrupting the color.
    1: ----------
    2: ----------"""
    config = chromaterm.Config()

    rule1 = chromaterm.Rule('hello', color=chromaterm.Color('b#123123'))
    rule2 = chromaterm.Rule('hello', color=chromaterm.Color('b#321321'))
    config.add_rule(rule1)
    config.add_rule(rule2)

    data = 'hello'
    expected = [
        rule1.color.color_code, rule2.color.color_code, 'hello',
        rule1.color.color_code, rule1.color.color_reset
    ]

    assert repr(config.highlight(data)) == repr(''.join(expected))
Example #20
0
def test_config_highlight_overlap_full_type_different():
    """Two rules, fully overlapping matches, different color types. Should not
    affect each other as they have different types. Most recent rule should be
    closest to the match.
    1: ----------
    2: ----------"""
    config = chromaterm.Config()

    rule1 = chromaterm.Rule('hello', color=chromaterm.Color('b#123123'))
    rule2 = chromaterm.Rule('hello', color=chromaterm.Color('f#321321'))
    config.add_rule(rule1)
    config.add_rule(rule2)

    data = 'hello'
    expected = [
        rule1.color.color_code, rule2.color.color_code, 'hello',
        rule2.color.color_reset, rule1.color.color_reset
    ]

    assert repr(config.highlight(data)) == repr(''.join(expected))
Example #21
0
def test_config_highlight_common_beginning_type_mixed(pcre):
    '''Two rules with different color types, and both sharing the same start of
    a match. The most recent rule will be closer to the match's start.
    1: --------------
    2: -------'''
    rule1 = chromaterm.Rule('hello',
                            chromaterm.Color('b#123123 italic'),
                            pcre=pcre)
    rule2 = chromaterm.Rule('he', chromaterm.Color('b#456456 bold'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule1)
    config.rules.append(rule2)

    data = b'hello'
    expected = [
        rule1.color.color_code, rule2.color.color_code, b'he', RESET_BOLD,
        rule1.color.color_types[0][1], b'llo', rule1.color.color_reset
    ]

    assert config.highlight(data) == b''.join(expected)
Example #22
0
def test_config_highlight_overlap_partial_type_different(pcre):
    '''Two rules, partially overlapping matches, different color types. The rules
    should not affect each other. Tested in reverse order, too.
    1: ----------
    2:     ----------'''
    rule1 = chromaterm.Rule('hell', chromaterm.Color('b#123123'), pcre=pcre)
    rule2 = chromaterm.Rule('llo', chromaterm.Color('f#456456'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule1)
    config.rules.append(rule2)

    data = b'hello'
    expected = [
        rule1.color.color_code, b'he', rule2.color.color_code, b'll',
        rule1.color.color_reset, b'o', rule2.color.color_reset
    ]

    assert config.highlight(data) == b''.join(expected)
    config.rules.reverse()
    assert config.highlight(data) == b''.join(expected)
Example #23
0
def test_config_highlight_adjoin_type_same(pcre):
    '''Two rules with same color type, with one ending where the other starts.
    Both are applied without any overlap in the codes, independent of the order.
    1: -------
    2:        -------'''
    rule1 = chromaterm.Rule('he', chromaterm.Color('b#123123'), pcre=pcre)
    rule2 = chromaterm.Rule('llo', chromaterm.Color('b#456456'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule1)
    config.rules.append(rule2)

    data = b'hello'
    expected = [
        rule1.color.color_code, b'he', rule1.color.color_reset,
        rule2.color.color_code, b'llo', rule2.color.color_reset
    ]

    assert config.highlight(data) == b''.join(expected)
    config.rules.reverse()
    assert config.highlight(data) == b''.join(expected)
Example #24
0
def test_config_highlight_overlap_full_type_mixed():
    """Two rules, fully overlapping matches, mixed color types. The different
    color types should not affect each other, but those that are the same should
    update the oldest reset with the most recent rule's color code.
    1: ----------
    2: ----------"""
    config = chromaterm.Config()

    rule1 = chromaterm.Rule('hello', color=chromaterm.Color('b#123123 bold'))
    rule2 = chromaterm.Rule('hello', color=chromaterm.Color('b#321321 italic'))
    config.add_rule(rule1)
    config.add_rule(rule2)

    data = 'hello'

    expected = [
        rule1.color.color_code, rule2.color.color_code, 'hello', RESET_ITALIC,
        rule1.color.color_types[0][1], rule1.color.color_reset
    ]

    assert repr(config.highlight(data)) == repr(''.join(expected))
Example #25
0
def test_config_highlight(pcre):
    '''Highlight with one rule.'''
    rule = chromaterm.Rule('hello', chromaterm.Color('b#123123'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule)

    data = b'hello world'
    expected = [
        rule.color.color_code, b'hello', rule.color.color_reset, b' world'
    ]

    assert config.highlight(data) == b''.join(expected)
Example #26
0
def test_rule_set_color_named_group(pcre):
    '''Reference a group by name. It should be converted to an index.'''
    rule = chromaterm.Rule('(?P<hi>hello)', pcre=pcre)
    assert rule.color is None

    color = chromaterm.Color('bold')
    rule.set_color(color, group='hi')
    assert rule.color is None
    assert rule.colors.get(1) is color

    rule.set_color(color)
    assert list(rule.colors) == [0, 1]
Example #27
0
def test_process_input_multiline(capsys, pcre):
    '''Input processing with multiple lines of data.'''
    pipe_r, pipe_w = os.pipe()
    config = chromaterm.__main__.Config()

    rule = chromaterm.Rule('hello world', chromaterm.Color('bold'), pcre=pcre)
    config.rules.append(rule)

    os.write(pipe_w, b'\nt hello world t\n' * 2)
    chromaterm.__main__.process_input(config, pipe_r, max_wait=0)

    assert capsys.readouterr().out == '\nt \x1b[1mhello world\x1b[22m t\n' * 2
Example #28
0
def test_process_input_multiline(capsys):
    """Input processing with multiple lines of data."""
    pipe_r, pipe_w = os.pipe()
    config = chromaterm.cli.Config()

    rule = chromaterm.Rule('hello world', color=chromaterm.Color('bold'))
    config.add_rule(rule)

    os.write(pipe_w, b'\nt hello world t\n' * 2)
    chromaterm.cli.process_input(config, pipe_r, max_wait=0)

    assert capsys.readouterr().out == '\nt \x1b[1mhello world\x1b[22m t\n' * 2
Example #29
0
def test_process_input_backspaces(capsys, pcre):
    '''Backspaces in the input should be accounted for when determining if typing
    is in progress.'''
    pipe_r, pipe_w = os.pipe()
    config = chromaterm.__main__.Config()

    rule = chromaterm.Rule('.', chromaterm.Color('bold'), pcre=pcre)
    config.rules.append(rule)

    os.write(pipe_w, b'\b\bxyz')
    chromaterm.__main__.process_input(config, pipe_r, max_wait=0)

    assert capsys.readouterr().out == '\b\bxyz'
Example #30
0
def test_rule_set_color_specific_group(pcre):
    '''Add to a rule a color to a specific regex group. Additionally, ensure
    that the dict keys are ordered according to the group number.'''
    rule = chromaterm.Rule(r'he(llo)\1', pcre=pcre)
    assert rule.color is None

    color = chromaterm.Color('bold')
    rule.set_color(color, group=1)
    assert rule.color is None
    assert rule.colors.get(1) is color

    rule.set_color(color)
    assert list(rule.colors) == [0, 1]