예제 #1
0
def test_config_highlight_overlap_partial_type_same(pcre):
    '''Two rules, partially 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. The behavior
    should be consistent regardless of the order of the rules.
    1: ----------
    2:     ----------'''
    rule1 = chromaterm.Rule('hell',
                            chromaterm.Color('b#123123 bold'),
                            pcre=pcre)
    rule2 = chromaterm.Rule('llo',
                            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, b'he', rule2.color.color_code, b'll',
        rule2.color.color_types[1][1], rule2.color.color_types[0][1], b'o',
        rule2.color.color_reset
    ]

    assert config.highlight(data) == b''.join(expected)
    config.rules.reverse()
    assert config.highlight(data) == b''.join(expected)
예제 #2
0
def test_config_highlight_overlap_partial_type_same():
    """Two rules, partially 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. The behavior
    should be consistent regardless of the order of the rules.
    1: ----------
    2:     ----------"""
    config = chromaterm.Config()

    rule1 = chromaterm.Rule('hell', 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, 'll',
        rule2.color.color_code, 'o', rule2.color.color_reset
    ]

    assert repr(config.highlight(data)) == repr(''.join(expected))

    config.remove_rule(rule1)
    config.add_rule(rule1)

    assert repr(config.highlight(data)) == repr(''.join(expected))
예제 #3
0
def test_config_highlight_adjoin_type_mixed(pcre):
    '''Two rules with mixed color types, 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 blink'),
                            pcre=pcre)
    rule2 = chromaterm.Rule('llo',
                            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, 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)
예제 #4
0
def test_config_highlight_encapsulate_type_different(pcre):
    '''Three rules, with matches that encapsulate each other, different color
    types. None of them should affect the rest as they are of different types. The
    order in which the rules are applied should not matter.
    1:     --
    2:   ------
    3: ----------'''
    rule1 = chromaterm.Rule('lo wo', chromaterm.Color('b#123123'), pcre=pcre)
    rule2 = chromaterm.Rule('llo wor', chromaterm.Color('f#456456'), pcre=pcre)
    rule3 = chromaterm.Rule('hello world', chromaterm.Color('bold'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule1)
    config.rules.append(rule2)
    config.rules.append(rule3)

    data = b'hello world'
    expected = [
        rule3.color.color_code, b'he', rule2.color.color_code, b'l',
        rule1.color.color_code, b'lo wo', rule1.color.color_reset, b'r',
        rule2.color.color_reset, b'ld', rule3.color.color_reset
    ]

    assert config.highlight(data) == b''.join(expected)
    config.rules.reverse()
    assert config.highlight(data) == b''.join(expected)
예제 #5
0
def test_config_highlight_overlap_partial_type_mixed(pcre):
    '''Two rules, partially overlapping matches, mixed color types. The different
    color types should not affect each other, but those that are the same should
    correctly update the reset that is in the middle of the other match to that
    of the match's color. Order of rules should not matter.
    1: ----------
    2:     ----------'''
    rule1 = chromaterm.Rule('hell',
                            chromaterm.Color('b#123123 bold'),
                            pcre=pcre)
    rule2 = chromaterm.Rule('llo',
                            chromaterm.Color('b#456456 italic'),
                            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',
        RESET_BOLD, rule2.color.color_types[0][1], b'o',
        rule2.color.color_reset
    ]

    assert config.highlight(data) == b''.join(expected)
    config.rules.reverse()
    assert config.highlight(data) == b''.join(expected)
예제 #6
0
def test_config_highlight_encapsulate_type_same():
    """Three rules, with matches that encapsulate each other, same color type.
    The reset of a match would be updated to the color of the encapsulating one.
    Once the second match updates the reset of the first one to a color, it is no
    longer considered a reset, and therefore the third match would not update it
    to its own color. The order in which the rules are applied should not matter.
    1:     --
    2:   ------
    3: ----------"""
    config = chromaterm.Config()

    rule1 = chromaterm.Rule('lo wo', color=chromaterm.Color('b#123123'))
    rule2 = chromaterm.Rule('llo wor', color=chromaterm.Color('b#321321'))
    rule3 = chromaterm.Rule('hello world', color=chromaterm.Color('b#abcabc'))
    config.add_rule(rule1)
    config.add_rule(rule2)
    config.add_rule(rule3)

    data = 'hello world'
    expected = [
        rule3.color.color_code, 'he', rule2.color.color_code, 'l',
        rule1.color.color_code, 'lo wo', rule2.color.color_code, 'r',
        rule3.color.color_code, 'ld', rule3.color.color_reset
    ]

    assert repr(config.highlight(data)) == repr(''.join(expected))

    config.remove_rule(rule1)
    config.remove_rule(rule2)
    config.add_rule(rule2)
    config.add_rule(rule1)

    assert repr(config.highlight(data)) == repr(''.join(expected))
예제 #7
0
def test_config_highlight_overlap_partial_type_mixed():
    """Two rules, partially overlapping matches, mixed color types. The different
    color types should not affect each other, but those that are the same should
    correctly update the reset that is in the middle of the other match to that
    of the match's color. Order of rules should not matter.
    1: ----------
    2:     ----------"""
    config = chromaterm.Config()

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

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

    assert repr(config.highlight(data)) == repr(''.join(expected))

    config.remove_rule(rule1)
    config.add_rule(rule1)

    assert repr(config.highlight(data)) == repr(''.join(expected))
예제 #8
0
def test_config_highlight_encapsulate_type_different():
    """Three rules, with matches that encapsulate each other, different color
    types. None of them should affect the rest as they are of different types. The
    order in which the rules are applied should not matter.
    1:     --
    2:   ------
    3: ----------"""
    config = chromaterm.Config()

    rule1 = chromaterm.Rule('lo wo', color=chromaterm.Color('b#123123'))
    rule2 = chromaterm.Rule('llo wor', color=chromaterm.Color('f#321321'))
    rule3 = chromaterm.Rule('hello world', color=chromaterm.Color('bold'))
    config.add_rule(rule1)
    config.add_rule(rule2)
    config.add_rule(rule3)

    data = 'hello world'
    expected = [
        rule3.color.color_code, 'he', rule2.color.color_code, 'l',
        rule1.color.color_code, 'lo wo', rule1.color.color_reset, 'r',
        rule2.color.color_reset, 'ld', rule3.color.color_reset
    ]

    assert repr(config.highlight(data)) == repr(''.join(expected))

    config.remove_rule(rule1)
    config.remove_rule(rule2)
    config.add_rule(rule2)
    config.add_rule(rule1)

    assert repr(config.highlight(data)) == repr(''.join(expected))
예제 #9
0
def test_config_highlight_tracking_mixed_full_reset(pcre):
    '''Track multiple color types and ensure a full reset only defaults the types
    that were not updated by other colors in the data.'''
    rule1 = chromaterm.Rule('hello', chromaterm.Color('f#456456'), pcre=pcre)
    rule2 = chromaterm.Rule('world', chromaterm.Color('b#123123'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule1)
    config.rules.append(rule2)

    data = b'\x1b[33mhello\x1b[0m there \x1b[43mworld'
    expected = [
        b'\x1b[33m', rule1.color.color_code, b'hello', b'\x1b[33m', b'\x1b[0m',
        b' there ', b'\x1b[43m', rule2.color.color_code, b'world', b'\x1b[43m'
    ]

    assert config.highlight(data) == b''.join(expected)

    # The color of rule1 was reset to its default because a full reset came after
    # it, but the color of rule2 was already updated so it wasn't affected by the
    # full reset
    data = b'hello there world'
    expected = [
        rule1.color.color_code, b'hello', rule1.color.color_reset, b' there ',
        rule2.color.color_code, b'world', b'\x1b[43m'
    ]

    assert config.highlight(data) == b''.join(expected)
예제 #10
0
def test_config_highlight_adjoin_type_mixed():
    """Two rules with mixed color types, with one ending where the other starts.
    Both are applied without any overlap in the codes, independent of the
    order.
    1: -------
    2:        -------"""
    config = chromaterm.Config()

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

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

    assert repr(config.highlight(data)) == repr(''.join(expected))

    config.remove_rule(rule1)
    config.add_rule(rule1)

    assert repr(config.highlight(data)) == repr(''.join(expected))
예제 #11
0
def test_config_highlight_common_end_type_different():
    """Two rules with different color types, 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('f#321321'))
    config.add_rule(rule1)
    config.add_rule(rule2)

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

    assert repr(config.highlight(data)) == repr(''.join(expected))

    config.remove_rule(rule1)
    config.add_rule(rule1)

    # Flip end color
    expected[-1], expected[-2] = expected[-2], expected[-1]

    assert repr(config.highlight(data)) == repr(''.join(expected))
예제 #12
0
def test_config_highlight_encapsulate_type_mixed(pcre):
    '''Three rules, with matches that encapsulate each other, mixed color types.
    The colors that are different should not affect each other, but those that
    are the same should correctly track each others colors and update their
    resets appropriately.
    1:     --
    2:   ------
    3: ----------'''
    rule1 = chromaterm.Rule('lo wo',
                            chromaterm.Color('b#123123 italic'),
                            pcre=pcre)
    rule2 = chromaterm.Rule('llo wor',
                            chromaterm.Color('b#456456 bold'),
                            pcre=pcre)
    rule3 = chromaterm.Rule('hello world', chromaterm.Color('bold'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule1)
    config.rules.append(rule2)
    config.rules.append(rule3)

    data = b'hello world'
    expected = [
        rule3.color.color_code, b'he', rule2.color.color_code, b'l',
        rule1.color.color_code, b'lo wo', RESET_ITALIC,
        rule2.color.color_types[0][1], b'r', rule2.color.color_types[1][1],
        chromaterm.COLOR_TYPES[rule2.color.color_types[0][0]]['reset'], b'ld',
        rule3.color.color_reset
    ]

    assert config.highlight(data) == b''.join(expected)
    config.rules.reverse()
    assert config.highlight(data) == b''.join(expected)
예제 #13
0
def test_config_highlight_encapsulate_type_same(pcre):
    '''Three rules, with matches that encapsulate each other, same color type.
    The reset of a match would be updated to the color of the encapsulating one.
    Once the second match updates the reset of the first one to a color, it is no
    longer considered a reset, and therefore the third match would not update it
    to its own color. The order in which the rules are applied should not matter.
    1:     --
    2:   ------
    3: ----------'''
    rule1 = chromaterm.Rule('lo wo', chromaterm.Color('b#123123'), pcre=pcre)
    rule2 = chromaterm.Rule('llo wor', chromaterm.Color('b#456456'), pcre=pcre)
    rule3 = chromaterm.Rule('hello world',
                            chromaterm.Color('b#abcabc'),
                            pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule1)
    config.rules.append(rule2)
    config.rules.append(rule3)

    data = b'hello world'
    expected = [
        rule3.color.color_code, b'he', rule2.color.color_code, b'l',
        rule1.color.color_code, b'lo wo', rule2.color.color_code, b'r',
        rule3.color.color_code, b'ld', rule3.color.color_reset
    ]

    assert config.highlight(data) == b''.join(expected)
    config.rules.reverse()
    assert config.highlight(data) == b''.join(expected)
예제 #14
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
예제 #15
0
def test_config_highlight_encapsulate_type_mixed():
    """Three rules, with matches that encapsulate each other, mixed color types.
    The colors that are different should not affect each other, but those that
    are the same should correctly track each others colors and update their
    resets appropriately.
    1:     --
    2:   ------
    3: ----------"""
    config = chromaterm.Config()

    rule1 = chromaterm.Rule('lo wo', color=chromaterm.Color('b#123123 italic'))
    rule2 = chromaterm.Rule('llo wor', color=chromaterm.Color('b#321321 bold'))
    rule3 = chromaterm.Rule('hello world', color=chromaterm.Color('bold'))
    config.add_rule(rule1)
    config.add_rule(rule2)
    config.add_rule(rule3)

    data = 'hello world'
    expected = [
        rule3.color.color_code, 'he', rule2.color.color_code, 'l',
        rule1.color.color_code, 'lo wo', RESET_ITALIC,
        rule2.color.color_types[0][1], 'r', rule2.color.color_types[1][1],
        chromaterm.COLOR_TYPES[rule2.color.color_types[0][0]]['reset'], 'ld',
        rule3.color.color_reset
    ]

    assert repr(config.highlight(data)) == repr(''.join(expected))

    config.remove_rule(rule1)
    config.remove_rule(rule2)
    config.add_rule(rule2)
    config.add_rule(rule1)

    assert repr(config.highlight(data)) == repr(''.join(expected))
예제 #16
0
def test_config___str__():
    """Confim Config's __str__ format."""
    config = chromaterm.Config()
    assert str(config) == 'Config: 0 rules'

    config.add_rule(chromaterm.Rule('hello'))
    assert str(config) == 'Config: 1 rule'

    config.add_rule(chromaterm.Rule('hello'))
    assert str(config) == 'Config: 2 rules'
예제 #17
0
def test_config___call__():
    """Confim Config's decorator (__call__)."""
    config = chromaterm.Config()
    config.add_rule(chromaterm.Rule('hello', color=chromaterm.Color('bold')))
    config.add_rule(chromaterm.Rule('world', color=chromaterm.Color('italic')))

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

    assert repr(config.highlight('hello world')) == repr(echo('hello world'))
예제 #18
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)
예제 #19
0
def test_config_print(capsys):
    """Test the print wrapper for config."""
    config = chromaterm.Config()

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

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

    assert config.highlight(data) in capsys.readouterr().out
예제 #20
0
def test_config_highlight_tracking_common_beginning_type_same(pcre):
    '''A rule with a match that has a color of the same type just before its
    start. The rule's color is closer to the match and the reset used is the
    existing color.
    1: x-------------'''
    rule = chromaterm.Rule('hello', chromaterm.Color('f#456456'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule)

    data = b'\x1b[33mhello'
    expected = [b'\x1b[33m', rule.color.color_code, b'hello', b'\x1b[33m']

    assert config.highlight(data) == b''.join(expected)
예제 #21
0
def test_config_highlight_tracking_multiline_type_same(pcre):
    '''Ensure that data with an existing color is tracked across highlights and
    affects the reset of a color of the same type.'''
    rule = chromaterm.Rule('hello', chromaterm.Color('f#456456'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule)

    # Inject a foreground color to have it tracked
    assert config.highlight(b'\x1b[33m') == b'\x1b[33m'

    data = b'hello'
    expected = [rule.color.color_code, b'hello', b'\x1b[33m']

    assert config.highlight(data) == b''.join(expected)
예제 #22
0
def test_config_remove_rule():
    """Remove a rule from config."""
    config = chromaterm.Config()
    rule1 = chromaterm.Rule('hello')
    rule2 = chromaterm.Rule('hello')

    config.add_rule(rule1)
    config.add_rule(rule2)
    assert rule1 in config.rules
    assert rule2 in config.rules

    config.remove_rule(rule2)
    assert rule1 in config.rules
    assert rule2 not in config.rules
예제 #23
0
def test_config_highlight_tracking_common_end_type_same(pcre):
    '''A rule with a match that has a color of the same type just after its end.
    The rule's reset is closer to the match and is unaffected by the existing
    color.
    1: -------------x'''
    rule = chromaterm.Rule('hello', chromaterm.Color('f#456456'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule)

    data = b'hello\x1b[33m'
    expected = [
        rule.color.color_code, b'hello', rule.color.color_reset, b'\x1b[33m'
    ]

    assert config.highlight(data) == b''.join(expected)
예제 #24
0
def test_config_highlight_tracking_full_reset_middle(pcre):
    '''A rule with a match that has a full reset in the middle of it. The full
    reset is changed to the color code of the match and the reset of the match
    is changed to a full reset.
    1: ------R-------'''
    rule = chromaterm.Rule('hello', chromaterm.Color('f#456456'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule)

    data = b'hel\x1b[0mlo'
    expected = [
        rule.color.color_code, b'hel', rule.color.color_code, b'lo', b'\x1b[0m'
    ]

    assert config.highlight(data) == b''.join(expected)
예제 #25
0
def test_config_highlight_tracking_full_reset_beginning(pcre):
    '''A rule with a match that has a full reset just before the start of the
    match. The rule's color is closer to the match and the reset used is the
    default for that color type.
    1: R-------------'''
    rule = chromaterm.Rule('hello', chromaterm.Color('f#456456'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule)

    data = b'\x1b[0mhello'
    expected = [
        b'\x1b[0m', rule.color.color_code, b'hello',
        chromaterm.COLOR_TYPES[rule.color.color_types[0][0]]['reset']
    ]

    assert config.highlight(data) == b''.join(expected)
예제 #26
0
def test_config_highlight_tracking_malformed(pcre):
    '''A rule with a match that has a malformed SGR in the middle. It should be
    ignored and inserted back into the match. Highlighting from the rule should
    still go through.
    1: x-------------'''
    rule = chromaterm.Rule('hello', chromaterm.Color('b#123123'), pcre=pcre)
    config = chromaterm.Config()
    config.rules.append(rule)

    data = b'he\x1b[38;5mllo'
    expected = [
        rule.color.color_code, b'he', b'\x1b[38;5m', b'llo',
        rule.color.color_reset
    ]

    assert config.highlight(data) == b''.join(expected)
예제 #27
0
def test_config_highlight():
    """Highlight with one rule."""
    config = chromaterm.Config()

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

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

    assert repr(config.highlight(data)) == repr(''.join(expected))

    # Ensure that forcing a tty mode changes the highlighting effect
    assert repr(config.highlight(data, force=False)) != repr(
        config.highlight(data, force=True))
예제 #28
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)
예제 #29
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)
예제 #30
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)