#! /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]

# --- some tests can't run with Python 2.1

if sys.version_info < (2,2):
    tests = [example for example in tests
             if example[0].find('compactobject') < 0]

# ---

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
    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))
if err:
    print >> sys.stderr, 'child process returned %d, %d' % (err>>8, err&255)
else:
    if sys.argv[1:2] == ['-q']:
        data1 = open(EXPECTEDFILE, 'r').read()
        data2 = open(BUFFERFILE, 'r').read()
        err = (data1 != data2) << 8
        if err:
            print >> sys.stderr, 'there are differences'
    else:
        err = os.system('diff -c %s %s' % (EXPECTEDFILE, BUFFERFILE))
sys.exit(err>>8)

@@ -1883,16 +1883,36 @@
 RFILE))%0A
+print %3E%3E sys.stderr%0A
 if err:%0A
@@ -1937,16 +1937,22 @@
 tderr, '
+FAIL: 
 child pr
@@ -1998,50 +1998,30 @@
 55)%0A
-else:%0A
     
-if 
 sys.
-argv%5B1:2%5D == %5B'-q'%5D:%0A    
+exit(1)%0Aelse:%0A
     
@@ -2055,28 +2055,24 @@
 'r').read()%0A
-    
     data2 = 
@@ -2108,19 +2108,11 @@
     
-    err = (
+if 
 data
@@ -2125,35 +2125,10 @@
 ata2
-) %3C%3C 8%0A        if err:%0A    
+:%0A
     
@@ -2157,124 +2157,223 @@
 r, '
-there are differences'%0A    else:%0A        err = os.system('diff -c %25s %25s' %25 (EXPECTEDFILE, BUFFERFILE))%0Asys.exit(err%3E%3E8)
+FAIL: different output'%0A        if sys.argv%5B1:2%5D != %5B'-q'%5D:%0A            cmd = 'diff -c %25s %25s' %25 (EXPECTEDFILE, BUFFERFILE)%0A            os.system(cmd)%0A        sys.exit(1)%0A    else:%0A        print %3E%3E sys.stderr, 'Passed.'
 %0A

Check the output in-process first, before relying on a possibly absent external 'diff' executable.