示例#1
0
def test_styled_sequence_move_and_relative_move_work(moveto, rmoveto,
                                                     expected_pos, method):
    sc, sh, text_plane = styled_text()
    msg = "01234566789"
    if method == "merged":
        mark_seq = Mark(moveto=moveto, rmoveto=rmoveto)
    elif method == "sequence":
        mark_seq = [Mark(moveto=moveto), Mark(rmoveto=rmoveto)]

    # let's trhow in some color -
    r = random.Random(hash((moveto, rmoveto, method)))
    pos = r.randrange(0, 5)
    attr = r.choice(["color", "background"])
    color = r.choice(["yellow", "red", "green", "blue"])

    aa = StyledSequence(msg, {
        pos: Mark(attributes={attr: color}),
        5: mark_seq,
    }, text_plane)
    aa.render()
    sc.update()
    yield None
    # TODO: these should be aliased in TM.Context class.
    assert sc.data[5,
                   0].value == TM.values.EMPTY if expected_pos != (5,
                                                                   0) else True
    assert sc.data[expected_pos].value == '5'
示例#2
0
def test_styled_sequence_is_rendered():
    sc, sh, text_plane = styled_text()
    msg = "Hello World!"
    aa = StyledSequence(msg, {}, text_plane)
    aa.render()
    sc.update()
    yield None
    for i, char in enumerate(msg):
        assert sc.data[i, 0].value == char
示例#3
0
def test_styled_sequence_retrives_marks_from_text_plane():
    sc, sh, text_plane = styled_text()
    text_plane.marks[1, 0] = Mark(attributes={"color": TM.Color("yellow")})
    msg = "Hello World!"
    aa = StyledSequence(msg, {}, text_plane)
    aa.render()
    sc.update()
    yield None
    assert sc.data[0, 0].foreground == TM.DEFAULT_FG
    for i, letter in enumerate(msg[1:], 1):
        assert sc.data[i, 0].foreground == TM.Color("yellow")
示例#4
0
def test_styled_sequence_is_rendered_with_attribute(attrname, value):
    sc, sh, text_plane = styled_text()
    msg = "Hello World!"
    aa = StyledSequence(msg, {0: Mark(attributes={attrname: value})},
                        text_plane)
    aa.render()
    sc.update()
    yield None
    # TODO: these should be aliased in TM.Context class.
    if attrname == "color":
        attrname = "foreground"
    for i, char in enumerate(msg):
        assert getattr(sc.data[i, 0], attrname) == value
示例#5
0
def test_styled_sequence_can_handle_pretransformers():
    sc, sh, text_plane = styled_text()
    msg = "0123456789"
    tt = TM.Transformer(
        foreground=lambda pos: TM.Color((pos[0] * 25 % 256, 0, 0)),
        background=lambda pos: TM.Color((0, 0, (255 - pos[0] * 25) % 256)),
    )
    aa = StyledSequence(msg, {0: TM.Mark(attributes={"pretransformer": tt})},
                        text_plane)
    aa.render()
    sc.update()
    yield None
    assert sc.data[0, 0].foreground == TM.Color((0, 0, 0))
    assert sc.data[0, 0].background == TM.Color((0, 0, 255))
    assert sc.data[9, 0].foreground == TM.Color((225, 0, 0))
    assert sc.data[9, 0].background == TM.Color((0, 0, 30))
示例#6
0
def test_styled_sequence_pops_attributes(pos1, attrname, value, pos2, default):
    sc, sh, text_plane = styled_text()
    msg = "Hello World!"
    aa = StyledSequence(
        msg, {
            pos1: Mark(attributes={attrname: value}),
            pos2: Mark(pop_attributes={attrname: None})
        }, text_plane)
    aa.render()
    sc.update()
    yield None
    # TODO: these should be aliased in TM.Context class.
    if attrname == "color":
        attrname = "foreground"
    for i, char in enumerate(msg):
        if pos1 <= i < pos2:
            assert getattr(sc.data[i, 0], attrname) == value
        else:
            assert getattr(sc.data[i, 0], attrname) == default
示例#7
0
def test_styled_sequence_adds_attributes_at_mark(attrname, value, position,
                                                 attr2, value2):
    sc, sh, text_plane = styled_text()
    msg = "Hello World!"
    aa = StyledSequence(
        msg, {
            0: Mark(attributes={attrname: value}),
            position: Mark(attributes={attr2: value2})
        }, text_plane)
    aa.render()
    sc.update()
    yield None
    # TODO: these should be aliased in TM.Context class.
    if attrname == "color":
        attrname = "foreground"
    for i, char in enumerate(msg):
        assert getattr(sc.data[i, 0], attrname) == value
        if i >= position:
            assert getattr(sc.data[i, 0], attr2) == value2
        else:
            assert getattr(sc.data[i, 0], attr2) != value2
示例#8
0
def test_styled_text_anotates_writtings():
    sc, sh, text_plane = styled_text()
    msg = "Hello World!"
    # Special mark callable index uses dependency injection, like TM.Transformers.
    m = SpecialMark(index=lambda tick, length: tick % length,
                    attributes={"color": TM.Color("red")})
    m1 = SpecialMark(index=lambda tick, length: (tick + 1) % length,
                     pop_attributes={"color": None})

    # text_plane.marks.special.update(m, m1)
    mm = {"special": [m, m1]}

    aa = StyledSequence(msg,
                        mark_sequence=mm,
                        text_plane=text_plane,
                        starting_point=(0, 5))
    text_plane.render_styled_sequence(aa)

    # text_plane[0, 5] = msg

    assert text_plane.writtings
    assert next(iter(text_plane.writtings)) is aa
示例#9
0
def test_styled_text_push_context_sequence_attribute():
    from copy import copy
    seq = StyledSequence("", {})
    seq._enter_iteration()
    tr1 = TM.Transformer(foreground="red")
    tr2 = TM.Transformer(background="yellow")

    assert tr1 not in seq.context.pretransformers
    original = copy(seq.context.pretransformers)
    seq._context_push({"pretransformer": tr1}, {}, "sequence", 0)
    assert seq.context.pretransformers[-1].foreground == tr1.foreground
    seq._context_push({"pretransformer": tr2}, {}, "sequence", 0)
    assert seq.context.pretransformers[-1].background == tr2.background
    assert seq.context.pretransformers[-2].foreground == tr1.foreground
    seq._context_push({}, {"pretransformer": None}, "sequence", 0)
    assert seq.context.pretransformers[-1].foreground == tr1.foreground
    seq._context_push({}, {"pretransformer": None}, "sequence", 0)
    assert seq.context.pretransformers == original
示例#10
0
def test_styled_text_push_context_attribute():
    seq = StyledSequence("", {})
    seq._enter_iteration()
    color = TM.Color("red")
    color2 = TM.Color("yellow")

    assert seq.context.color != color
    original = seq.context.color
    seq._context_push({"color": color}, {}, "sequence", 0)
    assert seq.context.color == color
    seq._context_push({"color": color2}, {}, "sequence", 0)
    assert seq.context.color == color2
    seq._context_push({}, {"color": None}, "sequence", 0)
    assert seq.context.color == color
    seq._context_push({}, {"color": None}, "sequence", 0)
    assert seq.context.color == original