[py-svn] r33448 - in py/dist/py/test/tracer: . testing

guido at codespeak.net guido at codespeak.net
Thu Oct 19 12:41:11 CEST 2006


Author: guido
Date: Thu Oct 19 12:41:09 2006
New Revision: 33448

Modified:
   py/dist/py/test/tracer/genrest.py
   py/dist/py/test/tracer/testing/test_rest.py
Log:
Made that the sections now are produced in a sensible order, fixed internal
link rewriting (FileWriter), had to change rewrite_link so it gets more 
arguments (for building internal vs. external links, docutils builds links
using the text of the titles, so that has to be known to the function to be
able to generate internal links). Both changes are to allow producing a full
single ReST document rather than a dir structure. Also removed commented
chunks of code.


Modified: py/dist/py/test/tracer/genrest.py
==============================================================================
--- py/dist/py/test/tracer/genrest.py	(original)
+++ py/dist/py/test/tracer/genrest.py	Thu Oct 19 12:41:09 2006
@@ -65,8 +65,8 @@
         self.output.write("\n")
         self.output.write(data + "\n")
 
-    def rewrite_link(self, target):
-        return '%s.txt' % (target,)
+    def rewrite_link(self, type, targetname, targetfilename):
+        return '%s.txt' % (targetfilename,)
 
 class DirWriter(object):
     def __init__(self, directory=None):
@@ -82,9 +82,9 @@
     def write_index(self, data):
         self.write_section('index', data)
 
-    def rewrite_link(self, target):
+    def rewrite_link(self, type, targetname, targetfilename):
         # we assume the result will get converted to HTML...
-        return '%s.html' % (target,)
+        return '%s.html' % (targetfilename,)
 
 class FileWriter(object):
     def __init__(self, fpath):
@@ -104,8 +104,13 @@
         self.fp.write(data)
         self.fp.flush()
 
-    def rewrite_link(self, target):
-        return '#%s' % (target,)
+    _defined_targets = []
+    def rewrite_link(self, type, targetname, targetbasename):
+        if targetname in self._defined_targets:
+            return None
+        self._defined_targets.append(targetname)
+        targetname = targetname.lower().replace('.', '-').replace('_', '')
+        return '#%s-%s' % (type, targetname)
 
 class RestGen(object):
     def __init__(self, ds, linkgen, writer=PipeWriter()):
@@ -119,52 +124,71 @@
         self.write_interface()
     
     def write_interface(self):
-        lst = [Title("Module: %s" % self.dsa.get_module_name(), belowchar="="),
-               Paragraph(self.dsa.get_module_info()),
-               Title("Exported functions:", belowchar="-")]
-        self.write_function_list(lst)
-        lst.append(Title("Exported classes:", belowchar="-"))
-        self.write_class_list(lst)
-        self.writer.write_index(Rest(*lst).text())
+        indexlst = [Title("Module: %s" % self.dsa.get_module_name(),
+                          belowchar="="),
+                    Paragraph(self.dsa.get_module_info()),
+                    Title("Exported functions:", belowchar="-")]
+        funclst = self.write_function_list(indexlst)
+        indexlst.append(Title("Exported classes:", belowchar="-"))
+        classlst = self.write_class_list(indexlst)
+        self.writer.write_index(Rest(*indexlst).text())
+        for sectionname, restitems in funclst:
+            self.writer.write_section(sectionname, Rest(*restitems).text())
+        for sectionname, classdata in classlst:
+            restitems, classfunclst = classdata
+            self.writer.write_section(sectionname, Rest(*restitems).text())
+            for fsectionname, frestitems in classfunclst:
+                self.writer.write_section(fsectionname,
+                                          Rest(*frestitems).text())
     
-    def write_function_list(self, lst):
+    def write_function_list(self, indexlst):
+        retlst = []
         for name in self.dsa.get_function_names():
-            # XXX: should be .html here?
             sectionname = 'function_%s' % (name,)
-            linktarget = self.writer.rewrite_link(sectionname)
-            lst.append(ListItem(Text("Function: "),
-                       Link(name, linktarget)))
-            self.write_function(sectionname, name)
+            linktarget = self.writer.rewrite_link('function', name,
+                                                  sectionname)
+            indexlst.append(ListItem(Text("Function: "),
+                                Link(name, linktarget)))
+            retlst.append((sectionname,
+                           self.write_function(sectionname, name)))
+        return retlst
     
-    def write_class_list(self, lst):
+    def write_class_list(self, indexlst):
+        retlst = []
         for name in self.dsa.get_class_names():
             sectionname = 'class_%s' % (name,)
-            linktarget = self.writer.rewrite_link(sectionname)
-            lst.append(ListItem(Text("Class: "),
-                       Link(name, linktarget)))
-            self.write_class(sectionname, name)
+            linktarget = self.writer.rewrite_link('class', name, sectionname)
+            indexlst.append(ListItem(Text("Class: "), Link(name, linktarget)))
+            retlst.append((sectionname, self.write_class(sectionname, name)))
+        return retlst
     
     def write_class(self, section_name, class_name):
-        # XXX no docstring?
-        lst = [Title("Class: %s" % class_name, belowchar='-')]
+        classlst = [Title("Class: %s" % class_name, belowchar='-'),
+                    BlockQuote(self.dsa.get_function_doc(class_name))]
         
         # write down exported methods
-        lst.append(Title("Exported methods:", belowchar="^"))
+        classlst.append(Title("Exported methods:", belowchar="^"))
+        funclist = []
         for method in self.dsa.get_class_methods(class_name):
             sectionname = 'method_%s_%s' % (class_name, method)
-            linktarget = self.writer.rewrite_link(sectionname)
-            lst.append(ListItem(Link(method, linktarget)))
-            self.write_function(sectionname, class_name + "." + method)
-        
-        self.writer.write_section(section_name, Rest(*lst).text())
+            linktext = '%s.%s' % (class_name, method)
+            linktarget = self.writer.rewrite_link('function', linktext,
+                                                  sectionname)
+            classlst.append(ListItem(Link(linktext, linktarget)))
+            # XXX assuming a function is always part of a class section
+            funclist.append((sectionname,
+                             self.write_function(sectionname,
+                                                 class_name + "." + method,
+                                                 '+')))
+        return classlst, funclist
     
-    def write_function(self, section_name, fun_name):
+    def write_function(self, section_name, fun_name, belowchar='-'):
         # XXX I think the docstring should either be split on \n\n and cleaned
         # from indentation, or treated as ReST too (although this is obviously
         # dangerous for non-ReST docstrings)...
-        lst = [Title("Function: %s" % fun_name, belowchar="-"),
-            BlockQuote(self.dsa.get_function_doc(fun_name)),
-            BlockQuote(self.dsa.get_function_definition(fun_name))]
+        lst = [Title("Function: %s" % fun_name, belowchar=belowchar),
+               BlockQuote(self.dsa.get_function_doc(fun_name)),
+               BlockQuote(self.dsa.get_function_definition(fun_name))]
         
         lst.append(Paragraph("Function source: XXX (to write a link here)"))
         
@@ -179,15 +203,15 @@
         lst.append(arg_quote)
         
         # call sites..
-        call_site_title = Title("Call sites:", belowchar="-")
+        call_site_title = Title("Call sites:", belowchar=belowchar)
         lst.append(call_site_title)
         call_sites = lst
         
         for call_site, frame in self.dsa.get_function_callpoints(fun_name):
             link_str = "File %s:%s" % (call_site.filename,
-                call_site.lineno)
-            #_, link_target = self.linkgen.getlink(call_site.filename,
-            #                                      call_site.lineno)
+                                       call_site.lineno)
+            #link_str, link_target = self.linkgen.getlink(call_site.filename,
+            #                                             call_site.lineno)
             #if link_target: # otherwise it's just inline text
             #    call_sites.append(Paragraph(Link(link_str, link_target)))
             #else:
@@ -200,59 +224,11 @@
             for num, line in enumerate(source):
                 if num == call_site.lineno - frame.code.firstlineno - 1:
                     m = re.match("^( *)(.*)", line)
-                    lines.append("%s >" + "-"*len(m.group(1)) + m.group(2))
+                    lines.append("%s >%s%s" % (num, "-" * len(m.group(1)),
+                                               m.group(2)))
                 else:
                     lines.append(" " + line)
             call_sites.append(BlockQuote("\n".join(lines)))
         
-        self.writer.write_section(section_name, Rest(*lst).text())
-##class RestGen(object):
-##    def __init__(self, ds, linkgen, output=sys.stdout):
-##        self.dsa = DocStorageAccessor(ds)
-##        self.linkgen = linkgen
-##        self.output = output
-##        
-##    def add(self, item):
-##        self.list.append(item)
-##    
-##    def write(self):
-##        self.list = []
-##        self.add(Title("Module: %s" % self.dsa.get_module_name(), belowchar="="))
-##        self.add(Paragraph(Text(self.dsa.get_module_info())))
-##        self.write_functions()
-##        self.write_classes()
-##    
-##    def write_functions(self):
-##        self.add(Title("Exported functions:", belowchar="-"))
-##        for key in self.dsa.get_function_names():
-##            title = Title("%s description:" % key, belowchar="^")
-##            docstr = Paragraph(self.dsa.get_function_doc(key))
-##            args = self.dsa.get_function_args(key)
-##            arg_str = "(%s)" % (",".join([str(i) for i in args]))
-##            arg_quote = BlockQuote("Function type: %s" % arg_str)
-##            
-##            call_site_title = Title("Call sites:", belowchar="-")
-##            call_sites = []
-##            
-##            for call_site in self.dsa.get_function_callpoints(key):
-##                link_str = "File %s:%s" % (call_site.filename,
-##                    call_site.lineno)
-##                link_target = self.linkgen.getlink(call_site.filename,
-##                                                   call_site.lineno)
-##                if link_target: # otherwise it's just inline text
-##                    call_sites.append(Paragraph(Link(link_str, link_target)))
-##                else:
-##                    call_sites.append(Paragraph(link_str))
-##                call_sites.append(BlockQuote(call_site.source))
-##            
-##            for item in [title, docstr, arg_quote,
-##                         call_site_title] + call_sites:
-##                self.add(item)
-##            
-##            #for call_site in self.dsa.get_function_callpoints(key):
-##            #    self.writeline("File %s:%s\n%s" % call_site.get_tuple())
-##        self.output.write(Rest(*self.list).text())
-##
-##    def write_classes(self):
-##        self.add(Title("Exported classes:", belowchar="-"))
-        
+        return lst
+

Modified: py/dist/py/test/tracer/testing/test_rest.py
==============================================================================
--- py/dist/py/test/tracer/testing/test_rest.py	(original)
+++ py/dist/py/test/tracer/testing/test_rest.py	Thu Oct 19 12:41:09 2006
@@ -24,12 +24,18 @@
                         'testing/test_rest.py?view=markup')
 
 class SomeClass(object):
+    """Some class definition"""
+    
     def __init__(self, a):
         self.a = a
     
     def method(self, a, b, c):
+        """method docstring"""
         return a + b + c
 
+class SomeSubClass(SomeClass):
+    """Some subclass definition"""
+
 def fun(a, b, c):
     """Some docstring
     
@@ -61,7 +67,11 @@
     # C-c C-v ....
 
 class TestRest(object):
-    def get_some_trace(self, ds):
+    def get_filled_docstorage(self):
+        descs = {'SomeClass': SomeClass,
+                 'SomeSubClass': SomeSubClass,
+                 'fun':fun}
+        ds = DocStorage().from_dict(descs)
         t = Tracer(ds)
         t.start_tracing()
         s1 = SomeClass("a")
@@ -70,6 +80,7 @@
         s2.method(1,2,3)
         fun(1, 3, s2)
         t.end_tracing()
+        return ds
     
     def check_rest(self, tempdir):
         from py.__.misc import rest
@@ -77,49 +88,63 @@
             rest.process(path)
     
     def test_generation_simple_api(self):
-        descs = {'SomeClass':SomeClass, 'fun':fun}
-        ds = DocStorage().from_dict(descs)
-        t = self.get_some_trace(ds)
+        ds = self.get_filled_docstorage()
         lg = DirectPaste()
-        tempdir = temppath.ensure("test_generation_simple_api", dir=True)
+        tempdir = temppath.ensure("simple_api", dir=True)
         r = RestGen(ds, lg, DirWriter(tempdir))
         r.write()
         basenames = [p.basename for p in tempdir.listdir('*.txt')]
         assert sorted(basenames) == [
             'class_SomeClass.txt',
+            'class_SomeSubClass.txt',
             'function_fun.txt',
             'index.txt',
             'method_SomeClass___init__.txt',
             'method_SomeClass_method.txt',
+            'method_SomeSubClass___init__.txt',
+            'method_SomeSubClass_method.txt',
         ]
         # now we check out...
         self.check_rest(tempdir)
 
     def test_check_internal_links(self):
-        descs = {'SomeClass': SomeClass,
-                 'fun': fun}
-        ds = DocStorage().from_dict(descs)
-        t = self.get_some_trace(ds)
-
+        ds = self.get_filled_docstorage()
         lg = DirectPaste()
-        tempdir = temppath.ensure('test_generation_internal_links', dir=True)
+        tempdir = temppath.ensure('internal_links', dir=True)
         r = RestGen(ds, lg, DirWriter(tempdir))
         r.write()
         index = tempdir.join('index.txt')
         assert index.check(file=True)
         data = index.read()
         assert data.find('.. _`fun`: function_fun.html\n') > -1
-        assert data.find('.. _`fun`: #fun\n') == -1
+        assert data.find('.. _`fun`: #function-fun\n') == -1
 
-        tempfile = temppath.ensure('test_generation_internal_links.txt',
+        tempfile = temppath.ensure('internal_links.txt',
                                   file=True)
         r = RestGen(ds, lg, FileWriter(tempfile))
         r.write()
         data = tempfile.read()
-        print 'data:', data
-        assert data.find('.. _`fun`: #function_fun\n') > -1
+        assert data.find('.. _`fun`: #function-fun\n') > -1
         assert data.find('.. _`fun`: function_fun.html') == -1
 
+    def test_check_section_order(self):
+        # we use the previous method's data
+        tempfile = temppath.join('internal_links.txt')
+        data = tempfile.read()
+        # index should be above the rest
+        assert data.find('Exported classes:') > -1
+        assert data.find('Exported classes:') < data.find('Function: fun')
+        assert data.find('Exported classes:') < data.find('Class: SomeClass')
+        # function definitions should be above class ones
+        assert data.find('Function: fun') < data.find('Class: SomeClass')
+        # class method definitions should be below the class defs
+        assert data.find('Class: SomeClass') < data.find(
+                                            'Function: SomeClass.method')
+        # __init__ should be above other methods
+        assert data.find('Function: SomeClass.__init__') > -1
+        # XXX in the future... 
+        # assert data.find('Function: SomeClass.__init__') < data.find(
+        #                                     'Function: SomeClass.method')
 
 ##def test_generation():
 ##    py.test.skip("WIP")



More information about the pytest-commit mailing list