Skip to content

Commit 66b3a5e

Browse files
authored
Compare output (#41)
* Do a test which compares the output with a file. * Always order the methods in the same order. * Don't need debug set on this app. * whitespace. * Add the task to run check_example. * Refactor this test class for generalization. * Import the app which depends on modules correctly. * Check examples for factory and custom examples. * Add more __init__.py * Use a safe tag for the docstring in template. * Check the private group of the simple example This includes endpoints with multiline docstrings. * Use Markup in the <br> replacement string.
1 parent 63e4c38 commit 66b3a5e

File tree

14 files changed

+845
-7
lines changed

14 files changed

+845
-7
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ install:
2626
script:
2727
- python -m unittest discover
2828
- flake8 --exclude=examples --max-line-length=100
29+
- python -m tests.check_example
2930
notifications:
3031
email: false
3132

examples/__init__.py

Whitespace-only changes.

examples/custom/__init__.py

Whitespace-only changes.

examples/custom/templates/autodoc_custom.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ <h1>
6363
{% for doc in autodoc %}
6464
<div class="mapping">
6565
<a id="rule-{{doc.rule|urlencode}}" class="rule"><h2>{{doc.rule|escape}}</h2></a>
66-
<p class="location">{{doc.location.filename}}:{{doc.location.line}}</p>
6766
<ul class="methods">
6867
{% for method in doc.methods -%}
6968
<li class="method">{{method}}</li>

examples/factory/__init__.py

Whitespace-only changes.

examples/simple/__init__.py

Whitespace-only changes.

examples/simple/blog.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66

77
app = Flask(__name__)
8-
app.debug = True
98
auto = Autodoc(app)
109

1110
users = []

flask_selfdoc/autodoc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import inspect
77

88
from flask import current_app, render_template, render_template_string, jsonify
9-
from jinja2 import evalcontextfilter
9+
from jinja2 import evalcontextfilter, Markup
1010
from jinja2.exceptions import TemplateAssertionError
1111

1212

@@ -59,7 +59,7 @@ def add_custom_nl2br_filters(self, app):
5959
@app.template_filter()
6060
@evalcontextfilter
6161
def nl2br(eval_ctx, value):
62-
result = '\n\n'.join('%s' % p.replace('\n', '<br>\n')
62+
result = '\n\n'.join('%s' % p.replace('\n', Markup('<br>\n'))
6363
for p in _paragraph_re.split(value))
6464
return result
6565

@@ -150,7 +150,7 @@ def generate(self, groups='all', sort=None):
150150

151151
if func_groups.intersection(groups_to_generate):
152152
props = dict(
153-
methods=rule.methods,
153+
methods=sorted(list(rule.methods)),
154154
rule="%s" % rule,
155155
endpoint=rule.endpoint,
156156
docstring=func.__doc__,
@@ -215,7 +215,7 @@ def endpoint_info(doc):
215215
return {
216216
"args": [(arg, doc['defaults'][arg]) for arg in args],
217217
"docstring": doc['docstring'],
218-
"methods": sorted(list(doc['methods'])),
218+
"methods": doc['methods'],
219219
"rule": doc['rule']
220220
}
221221
data = {

flask_selfdoc/templates/autodoc_default.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ <h1>
7575
</li>
7676
{% endfor %}
7777
</ul>
78-
<p class="docstring">{{doc.docstring|urlize|nl2br}}</p>
78+
<p class="docstring">{{doc.docstring|urlize|nl2br|safe}}</p>
7979
</div>
8080
{% endfor %}
8181
</body>

tests/check_example.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import sys
2+
import unittest
3+
4+
from examples.custom.blog import app as custom_app
5+
from examples.simple.blog import app as simple_app
6+
7+
8+
class TestApp(object):
9+
maxDiff = None
10+
11+
def setUp(self):
12+
self.client = self.app.test_client()
13+
14+
def test_output(self):
15+
r = self.client.get(self.path)
16+
self.assertEqual(r.status_code, 200)
17+
data = r.data.decode('utf-8')
18+
with open(self.filename) as f:
19+
expected = f.read()
20+
21+
self.assertEqual(data, expected)
22+
23+
24+
class TestSimpleApp(TestApp, unittest.TestCase):
25+
app = simple_app
26+
filename = "tests/files/simple.html"
27+
path = "/doc"
28+
29+
30+
class TestSimpleAppPrivateGroup(TestApp, unittest.TestCase):
31+
app = simple_app
32+
filename = "tests/files/simple_private.html"
33+
path = "/doc/private"
34+
35+
36+
class TestFactoryApp(TestApp, unittest.TestCase):
37+
filename = "tests/files/factory.html"
38+
path = "/doc/"
39+
40+
@classmethod
41+
def setUpClass(cls):
42+
sys.path.append('examples/factory')
43+
from app import create_app as factory_create_app
44+
cls.app = factory_create_app()
45+
46+
47+
class TestCustomApp(TestApp, unittest.TestCase):
48+
app = custom_app
49+
filename = "tests/files/custom.html"
50+
path = "/doc/"
51+
52+
53+
if __name__ == "__main__":
54+
unittest.main()

0 commit comments

Comments
 (0)