1+ import inspect
2+ import os .path
13import unittest
24import sys
35import os
@@ -13,6 +15,14 @@ def setUp(self):
1315 self .app .debug = True
1416 self .autodoc = Autodoc (self .app )
1517
18+ @staticmethod
19+ def thisFile ():
20+ """Returns the basename of __file__ without a trailing 'c'"""
21+ filename = os .path .basename (__file__ )
22+ if filename .endswith ('.pyc' ):
23+ filename = filename [:- 1 ]
24+ return filename
25+
1626 def testGet (self ):
1727 @self .app .route ('/' )
1828 @self .autodoc .doc ()
@@ -30,7 +40,7 @@ def index():
3040 self .assertEqual (d ['endpoint' ], 'index' )
3141 self .assertEqual (d ['docstring' ], 'Returns a hello world message' )
3242 self .assertIsInstance (d ['location' ]['line' ], int )
33- self .assertIn (__file__ , d ['location' ]['filename' ])
43+ self .assertIn (self . thisFile () , d ['location' ]['filename' ])
3444 self .assertFalse (d ['defaults' ])
3545
3646 def testPost (self ):
@@ -50,7 +60,7 @@ def index():
5060 self .assertEqual (d ['endpoint' ], 'index' )
5161 self .assertEqual (d ['docstring' ], 'Returns a hello world message' )
5262 self .assertIsInstance (d ['location' ]['line' ], int )
53- self .assertIn (__file__ , d ['location' ]['filename' ])
63+ self .assertIn (self . thisFile () , d ['location' ]['filename' ])
5464 self .assertFalse (d ['defaults' ])
5565
5666 def testParams (self ):
@@ -248,3 +258,44 @@ def ab(param1, param2):
248258 )
249259 self .assertIn ('Returns arguments' , doc )
250260
261+ def testLocation (self ):
262+ line_no = inspect .stack ()[0 ][2 ] + 2 # the doc() line
263+ @self .app .route ('/location' )
264+ @self .autodoc .doc ()
265+ def location ():
266+ return 'location'
267+
268+ with self .app .app_context ():
269+ doc = self .autodoc .generate ()
270+ d = doc [0 ]
271+ self .assertIsInstance (d ['location' ]['line' ], int )
272+ self .assertEqual (d ['location' ]['line' ], line_no )
273+ self .assertIn (self .thisFile (), d ['location' ]['filename' ])
274+
275+ def testNoLocation (self ):
276+ @self .app .route ('/location' )
277+ @self .autodoc .doc (set_location = False )
278+ def location ():
279+ return 'location'
280+
281+ with self .app .app_context ():
282+ doc = self .autodoc .generate ()
283+ d = doc [0 ]
284+ self .assertIsNone (d ['location' ])
285+
286+ def testRedecorate (self ):
287+ @self .app .route ('/redecorate' )
288+ # add to "all" and "group1"
289+ @self .autodoc .doc ('group1' )
290+ def redecorate ():
291+ return 'redecorate'
292+
293+ # add to "group2"
294+ self .app .view_functions ['redecorate' ] = self .autodoc .doc ('group2' )(redecorate )
295+
296+ with self .app .app_context ():
297+ self .assertTrue (1 == len (self .autodoc .generate ('all' )))
298+ self .assertTrue (1 == len (self .autodoc .generate ('group1' )))
299+ self .assertTrue (1 == len (self .autodoc .generate ('group2' )))
300+ self .assertFalse (1 == len (self .autodoc .generate ('group3' )))
301+
0 commit comments