forked from lipro/sphinxcontrib-ansi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ansi.py
86 lines (62 loc) · 2.31 KB
/
test_ansi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from docutils import nodes
from mock import Mock
from sphinxcontrib import ansi
RAWSOURCE = '''\
\x1b[1mfoo\x1b[33;1mbar\x1b[1;34mhello\x1b[0mworld\x1b[1m'''
def pytest_funcarg__paragraph(request):
paragraph = nodes.paragraph()
paragraph.append(ansi.ansi_literal_block(RAWSOURCE, RAWSOURCE))
return paragraph
def pytest_funcarg__app(request):
return Mock()
def pytest_funcarg__parser(request):
return ansi.ANSIColorParser()
def _assert_colors(node, *colors):
assert isinstance(node, nodes.inline)
for color in colors:
assert ('ansi-%s' % color) in node['classes']
def _assert_text(node, text):
assert isinstance(node, nodes.Text)
assert node.astext() == text
def test_parser_strip_colors(app, parser, paragraph):
app.builder.name = 'foo'
parser(app, paragraph, 'foo')
assert isinstance(paragraph[0], nodes.literal_block)
_assert_text(paragraph[0][0], 'foobarhelloworld')
assert not paragraph[0][0].children
assert paragraph.astext() == 'foobarhelloworld'
def test_parser_colors_parsed(app, parser, paragraph):
app.builder.name = 'html'
parser(app, paragraph, 'foo')
block = paragraph[0]
assert isinstance(block, nodes.literal_block)
_assert_colors(block[0], 'bold')
_assert_text(block[0][0], 'foo')
_assert_colors(block[1], 'bold', 'yellow')
_assert_text(block[1][0], 'bar')
_assert_colors(block[2], 'bold', 'blue')
_assert_text(block[2][0], 'hello')
_assert_text(block[3], 'world')
_assert_colors(block[4], 'bold')
assert not block[4].children
assert paragraph.astext() == 'foobarhelloworld'
def test_setup(app):
ansi.setup(app)
app.require_sphinx.assert_called_with('1.0')
app.add_config_value.assert_called_with(
'html_ansi_stylesheet', None, 'env')
app.add_directive.assert_called_with(
'ansi-block', ansi.ANSIBlockDirective)
assert app.connect.call_args_list[:2] == [
(('builder-inited', ansi.add_stylesheet),),
(('build-finished', ansi.copy_stylesheet),)]
assert app.connect.call_args_list[2][0][0] == 'doctree-resolved'
assert isinstance(app.connect.call_args_list[2][0][1],
ansi.ANSIColorParser)
def main():
import py
py.cmdline.pytest()
if __name__ == '__main__':
main()