cfgparse_tex_doctest.py :  » Development » Configuration-Parser » cfgparse-v01_02 » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Configuration Parser 
Configuration Parser » cfgparse v01_02 » tests » cfgparse_tex_doctest.py
import cStringIO
import difflib
import os
import re
import shutil
import sys
import traceback

npass = 0
nfail = 0

print '-'*80
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Fake out sys.exit()
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

def exit(*args,**kwargs):
    global nfail
    #nfail += 1
    print "ERROR: got system exit"
    
sys.exit = exit

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Read special XML markup in TEX file, record tag, attributes and body
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

f = open('../docs/cfgparse.tex')
content = f.read()
f.close()

lowercontent = content.lower()
npass_exp = lowercontent.count('<stdout') + lowercontent.count('<cline')

tag_group = '(?P<tag>file|python|stdout|stderr|cline)'
attribs_group = '(?P<attribs>.*?)'
tag_line = '\n%% <%s%s>.*?' % (tag_group,attribs_group)
beg_verb = r'\n\\begin{verbatim}.*?\n'
body = '(?P<body>.*?\n)'
end_verb = r'\\end{verbatim}'
pattern = ''.join([tag_line,beg_verb,body,end_verb])
xml_sect_regexp = re.compile(pattern,re.DOTALL)

attrib_regexp = re.compile('''(?P<name>\w+)=['"](?P<value>[\w\.]*)['"]''')

sections = []

def xml_section_recorder(sr_mo):
    attribs = dict()
    def attrib_recorder(ar_mo):
        attribs[ar_mo.group('name')] = ar_mo.group('value')
    attrib_regexp.sub(attrib_recorder,sr_mo.group('attribs'))
    sections.append((sr_mo.group('tag'),attribs,sr_mo.group('body')))    

xml_sect_regexp.sub(xml_section_recorder,content)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Process special XML markup
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

subdir = '_cfgparse_tex_doctest'
if os.path.exists(subdir):
    shutil.rmtree(subdir)
os.mkdir(subdir)
os.chdir(subdir)

testdir = os.getcwd()

try:
    # for python 2.4 and later
    differ = difflib.HtmlDiff()
    diffext = 'diff.html'
except AttributeError:
    # for python 2.3 and earlier
    class Diff(object):
        def make_file(self,flines,tlines,fdesc,tdesc):
            flines = [fdesc] + flines
            tlines = [tdesc] + tlines
            return '\n'.join(difflib.ndiff(flines,tlines))
    differ = Diff()
    diffext = 'diff.txt'

for tag,attribs,body in sections:
    id = attribs.get('id')
    if tag == 'file':
        # print "creating file:",attribs['name']
        f = open(attribs['name'],'w')
        f.write(body)
        f.close()
    elif tag == 'python':
        if id:
            f = open(id,'w')
            f.write(body)
            f.close()
        print "executing python (id=%s)" % id
        stdout, sys.stdout = sys.stdout, cStringIO.StringIO()
        stderr, sys.stderr = sys.stderr, cStringIO.StringIO()
        try:
            exec body
            error = None
        except KeyboardInterrupt:
            stdout, sys.stdout = sys.stdout.getvalue(), stdout
            stderr, sys.stderr = sys.stderr.getvalue(), stderr
            raise
        except:
            stdout, sys.stdout = sys.stdout.getvalue(), stdout
            stderr, sys.stderr = sys.stderr.getvalue(), stderr
            nfail += 1
            type,value,tb = sys.exc_info()[0:3]
            tb = traceback.format_list(traceback.extract_tb(tb)[1:])
            offset = int(tb[0].replace(',',' ').split()[3])
            tb[1:1] = '    %s\n' % body.split('\n')[offset-1]
            e = ''.join(traceback.format_exception_only(type,value))
            print "ERROR: \n%s\n%s" % (''.join(tb),e)
            #type,value = sys.exc_info()[0:2]
            #exception = traceback.format_exception_only(type,value)
        else:
            stdout, sys.stdout = sys.stdout.getvalue(), stdout
            stderr, sys.stderr = sys.stderr.getvalue(), stderr
    elif tag == 'cline':
        result = []
        body = body.replace('[CWD]',testdir)
        for line in body.split('\n'):
            if line.strip() == '$':
                result.append(line)
            elif line.startswith('$ '):
                result.append(line)
                fi,fo,fe = os.popen3(line[2:])
                fi.close()
                result.append(fo.read()+fe.read())
        result = '\n'.join(result)
        if body == result:
            npass += 1
            print "pass: comparing command line (id=%s)" % id
        else:
            nfail += 1
            print "ERROR: comparing command line (id=%s)" % id
            flines = body.split('\n')
            tlines = result.split('\n')
            f = open('cline_%s.txt'%id,'w')
            f.write(result)
            f.close()
            f = open('cline_%s.%s'%(id,diffext),'w')
            f.write(differ.make_file(flines,tlines,'baseline','cline (id=%s)'%id))
            f.close()
    elif tag == 'stdout':
        if body == stdout:
            npass += 1
            print "pass: comparing stdout (id=%s)" % id
        else:
            nfail += 1
            print "ERROR: comparing stdout (id=%s)" % id
            flines = body.split('\n')
            tlines = stdout.split('\n')
            f = open('stdout_%s.txt'%id,'w')
            f.write(stdout)
            f.close()
            f = open('stdout_%s.%s'%(id,diffext),'w')
            f.write(differ.make_file(flines,tlines,'baseline','stdout (id=%s)'%id))
            f.close()
    elif tag == 'stderr':
        if body == stderr:
            npass += 1
            print "pass: comparing stderr (id=%s)" % id
        else:
            nfail += 1
            print "ERROR: comparing stderr (id=%s)" % id
            flines = body.split('\n')
            tlines = stderr.split('\n')
            f = open('stderr_%s.%s'%(id,diffext),'w')
            f.write(differ.make_file(flines,tlines,'baseline','stderr (id=%s)'%id))
            f.close()
    else:
        print "ERROR: unknown tag '%s'" % tag
    # print '-'*40

if nfail == 0 and npass == npass_exp:        
    print "PASS (%s)" % npass
else:
    print "nfail=%d" % nfail
    print "npass=%d (%d expected)" % (npass,npass_exp)
    print "FAIL" 

#import doctest
#print '='*90
#doctest.testfile("cfgparse.tex",optionflags=doctest.ELLIPSIS)
 
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.