1from unittest.mock import Mock, patch
2
3from ..terminal import clip, render
4
5def get_mock_terminal(**overrides):
6    attrs = dict(
7        move='move({},{})'.format,
8        height=11,
9        width=22,
10    )
11    attrs.update(overrides)
12    return Mock(**attrs)
13
14@patch('cbeams.terminal.terminal', get_mock_terminal())
15def test_render():
16    strips = [(1, 2, 3), (4, 5, 6)]
17    assert render(strips) == \
18        'move(1,2)' + ' ' * 3 + \
19        'move(4,5)' + ' ' * 6
20
21@patch('cbeams.terminal.terminal', get_mock_terminal())
22def test_render_should_clip():
23    strips = [(1, -2, 26)]
24    assert render(strips) == 'move(1,0)' + ' ' * 22
25
26
27@patch('cbeams.terminal.terminal', Mock(height=99, width=10))
28def test_clip_left():
29    strips = [
30        (1, 0, 10), # not clipped
31        (2, -2, 6), # clipped to length 4
32        (3, -4, 5), # clipped to length 1
33        (4, -6, 6), # clipped to length 0, i.e. removed entirely
34    ]
35    assert list(clip(strips)) == [
36        (1, 0, 10),
37        (2, 0, 4),
38        (3, 0, 1),
39    ]
40
41@patch('cbeams.terminal.terminal', Mock(height=99, width=10))
42def test_clip_right():
43    strips = [
44        (1, 0, 10), # not clipped
45        (2, 0, 11), # clipped to length 10
46        (3, 9, 99), # clipped to length 1
47        (4, 10, 99), # clipped to length 0, i.e. removed entirely
48    ]
49    assert list(clip(strips)) == [
50        (1, 0, 10),
51        (2, 0, 10),
52        (3, 9, 1),
53    ]
54
55@patch('cbeams.terminal.terminal', Mock(height=10, width=99))
56def test_clip_top_and_bottom():
57    strips = [
58        (-1, 0, 10), # removed entirely
59        (0, 0, 10), # not clipped
60        (9, 0, 10), # not clipped
61        (10, 0, 10), # removed entirely
62    ]
63    assert list(clip(strips)) == [
64        (0, 0, 10),
65        (9, 0, 10),
66    ]
67
68