test_base.py :  » Development » Psyco » psyco-dist » test » 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 » Psyco 
Psyco » psyco dist » test » test_base.py
#! /usr/bin/env python

"""  Run base Psyco tests.
"""

import sys, os, random, doctest, cStringIO
True,False = 1==1,1==0


SEPARATOR = """
========== %r ==========
"""
LASTLINE = "Tests completed."
BUFFERFILE = "buffer-basetests.txt"
EXPECTEDFILE = "expected-basetests.txt"
INPUTSCRIPT = "input-basetexts.py"

TESTS = open('btrun.py', 'r').read()


if hasattr(doctest, '_extract_examples'):
    tests = doctest._extract_examples(TESTS)
else:
    examples = doctest.DocTestParser().get_examples(TESTS)
    tests = [(example.source, example.want, example.lineno)
             for example in examples]

# ---

def main(quiet=False):
    PRELUDE = ''
    for inp, outp, line in tests[:]:
        if not outp:
            PRELUDE += inp + '\n'
            tests.remove((inp, outp, line))
    random.shuffle(tests)             # first run all tests in any order
    tests_again = tests[:]
    random.shuffle(tests_again)
    all_tests = tests + tests_again   # then run them all again in any other order

    childin = open(INPUTSCRIPT, 'w')
    expected = open(EXPECTEDFILE, 'w')

    print >> childin, 'import sys'
    print >> childin, PRELUDE

    def filterline(line):
        if line.startswith('${') and line.endswith('}'):
            line = str(eval(line[2:-1]))
        return line

    for inp, outp, line in all_tests:
        sep = SEPARATOR % inp
        print >> childin, 'print %r' % sep
        if not quiet:
            print >> childin, 'print >> sys.stderr, %r' % inp.strip()
        print >> expected, sep
        print >> childin, inp
        outplines = [filterline(line) for line in outp.split('\n')]
        expected.write('\n'.join(outplines))

    print >> childin, 'print %r' % LASTLINE
    print >> expected, LASTLINE
    expected.close()
    childin.close()

    # run in a child process
    err = os.system('"%s" %s > %s' % (sys.executable, INPUTSCRIPT, BUFFERFILE))
    print >> sys.stderr
    if err:
        print >> sys.stderr, 'FAIL: child process returned %d, %d' % (err>>8, err&255)
        sys.exit(1)
    else:
        data1 = open(EXPECTEDFILE, 'r').read()
        data2 = open(BUFFERFILE, 'r').read()
        if data1 != data2:
            print >> sys.stderr, 'FAIL: different output'
            if sys.argv[1:2] != ['-q']:
                cmd = 'diff -c %s %s' % (EXPECTEDFILE, BUFFERFILE)
                os.system(cmd)
            return False
        else:
            print >> sys.stderr, 'Passed.'
            return True

def test_main():
    ok = main(quiet=True)
    assert ok

if __name__ == '__main__':
    if not main():
        sys.exit(1)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.