forked from pyimgui/pyimgui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
106 lines (79 loc) · 2.85 KB
/
conftest.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
import os
import sys
import pytest
from inspect import currentframe, getframeinfo
from sphinx.application import Sphinx
import imgui
PROJECT_ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
sphinx = None
def project_path(*paths):
return os.path.join(PROJECT_ROOT_DIR, *paths)
class SphinxDoc(pytest.File):
def __init__(self, path, parent):
# yuck!
global sphinx
if sphinx is None:
os.environ['SPHINX_DISABLE_RENDER'] = '1'
sphinx = Sphinx(
srcdir=project_path('doc', 'source'),
confdir=project_path('doc', 'source'),
outdir=project_path('doc', 'build', 'vistest'),
doctreedir=project_path('doc', 'build', 'doctree'),
buildername='vistest',
)
super(SphinxDoc, self).__init__(path, parent)
def collect(self):
# build only specified file
sphinx.build(filenames=[self.fspath.relto(project_path())])
return [
DocItem(name, self, code)
for (name, code) in sphinx.builder.snippets
]
class DocItem(pytest.Item):
def __init__(self, name, parent, code):
super(DocItem, self).__init__(name, parent)
self.code = code
def runtest(self):
self.exec_snippet(self.code)
def exec_snippet(self, source):
code = compile(source, '<str>', 'exec')
frameinfo = getframeinfo(currentframe())
io = imgui.get_io()
io.render_callback = lambda *args, **kwargs: None
io.delta_time = 1.0 / 60.0
io.display_size = 300, 300
# setup default font
io.fonts.get_tex_data_as_rgba32()
io.fonts.add_font_default()
io.fonts.texture_id = 0 # set any texture ID to avoid segfaults
imgui.new_frame()
try:
exec(code, locals(), globals())
except Exception as err:
# note: quick and dirty way to annotate sources with error marker
lines = source.split('\n')
lines.insert(sys.exc_info()[2].tb_next.tb_lineno, "^^^")
self.code = "\n".join(lines)
raise
imgui.render()
@staticmethod
def indent(text, width=4):
return "\n".join(
">" + " " * width + line
for line in text.split('\n')
)
def repr_failure(self, excinfo):
""" called when self.runtest() raises an exception. """
return "Visual example fail: {}\n\n{}\n\n{}".format(
excinfo,
self.indent(self.code),
excinfo.getrepr(funcargs=True, style='short')
)
def reportinfo(self):
return self.fspath, 0, "testcase: %s" % self.name
class ExecException(Exception):
pass
def pytest_collect_file(parent, path):
if path.ext == '.rst' and 'source' in path.dirname:
return SphinxDoc(path, parent)