[pypy-svn] r8174 - pypy/branch/src-pytest/pypy/tool

pedronis at codespeak.net pedronis at codespeak.net
Sat Jan 8 18:36:07 CET 2005


Author: pedronis
Date: Sat Jan  8 18:36:06 2005
New Revision: 8174

Modified:
   pypy/branch/src-pytest/pypy/tool/fiximport.py
   pypy/branch/src-pytest/pypy/tool/utestconvert.py
Log:
- improved fiximport, should be able to deal with most of ours setUp, 
tearDown

- added more PyPy stuff to utestconvert: assertEqual_w

 --This line, and those below, will be ignored--

M    tool/utestconvert.py
M    tool/fiximport.py


Modified: pypy/branch/src-pytest/pypy/tool/fiximport.py
==============================================================================
--- pypy/branch/src-pytest/pypy/tool/fiximport.py	(original)
+++ pypy/branch/src-pytest/pypy/tool/fiximport.py	Sat Jan  8 18:36:06 2005
@@ -1,12 +1,92 @@
 import sys
+import re
+import parser
 
+blank_re=re.compile(r"\s*(#.*)?")
 
+def get_indent(line):
+    indent = re.match(blank_re,line).group()
+    return indent, indent==line
+
+def read_whole_suite(intro_line):
+    base_indent, dummy = get_indent(intro_line)
+    lines = []
+    cont = []
+    parsing_prefix = ""
+    if len(base_indent) > 0:
+	parsing_prefix = "if 0:\n"
+    while True:
+	line = f.readline()
+	if not line:
+	    break
+   	indent, isblank = get_indent(line)
+        if isblank:
+	    pass
+        elif cont:
+	    cont.append(line)
+	    try:
+		parser.suite(parsing_prefix+''.join(cont))
+	    except SyntaxError:
+		pass 
+	    else:
+		cont = []
+        else:
+	    if len(indent) <= len(base_indent):
+		pushback.append(line)
+		break
+	    try:
+		parser.suite(parsing_prefix+line)
+	    except SyntaxError:
+		cont = [line]
+	lines.append(line)
+
+    return base_indent,lines
+
+pass_re = re.compile(r"^\s*pass\s*$")
+getobjspace_re = r"testit\.objspace\((.*)\)"
+setspace_re = r"self\.space\s*=\s*"
+
+def up_down_port(lines):
+    npass = 0
+    nblank = 0
+    objspace = None
+    n = -1
+    for line in lines:
+        n += 1
+	dummy, isblank = get_indent(line)
+	if isblank:
+	    nblank += 1
+	    continue
+	if re.match(pass_re, line):
+	    npass += 1
+	    continue
+	m = re.search(getobjspace_re, line)
+	if m:
+	    objspace = m.group(1)
+	    line = line[:m.start()]+"self.space"+line[m.end():]
+	    line = re.sub(setspace_re,"",line)
+	    if line.strip() == "self.space":
+		line = ""
+		nblank += 1
+	    lines[n] = line
+
+    skip = npass+nblank == len(lines)
+
+    return objspace,skip
+	    
 for fn in sys.argv[1:]:
     print fn
     lines = []
+    pushback = []
+    kind = None
     f = file(fn, 'r')
+
+    confused = False
     while True:
-        line = f.readline()
+        if pushback:
+	    line = pushback.pop()
+	else:
+	    line = f.readline()
         if not line:
             break
         rline = line.rstrip()
@@ -17,11 +97,30 @@
             tail = f.read()
             if tail.strip() != 'testit.main()':
                 print ' * uncommon __main__ lines at the end'
+		confused = True
             break
         if line.strip() == 'def setUp(self):':
-            print ' * setUp() ignored'
+            base_indent,suite = read_whole_suite(line)
+	    objspace,skip = up_down_port(suite)
+	    #print suite
+	    if objspace:
+		lines.append(base_indent+"objspacename = %s\n" % objspace)
+		lines.append("\n")
+	    if not skip:
+		lines.append(base_indent+"def setup_method(self,method):\n")
+		lines.extend(suite)
+	    continue
         if line.strip() == 'def tearDown(self):':
-            print ' * tearDown() ignored'
+            base_indent, suite = read_whole_suite(line)
+            unexpected,skip = up_down_port(suite)
+	    if unexpected is not None:
+		print "* testit.objspace(<name>) in tearDown"
+		confused = True
+	    #print suite
+	    if not skip:
+		lines.append(base_indent+"def teardown_method(self,method):\n")
+		lines.extend(suite)
+	    continue
         if line.startswith('class '):
             rest = line[6:].strip()
             if rest.endswith('(testit.AppTestCase):'):
@@ -30,14 +129,17 @@
                     if not rest.startswith('Test'):
                         rest = 'Test'+rest
                     rest = 'App'+rest
+		kind = 'app-test'
             elif rest.endswith('(testit.IntTestCase):'):
                 rest = rest[:-21].strip() + ':'
                 if not rest.startswith('Test'):
                     rest = 'Test'+rest
+                kind = 'test'
             elif rest.endswith('(testit.TestCase):'):
                 rest = rest[:-18].strip() + ':'
                 if not rest.startswith('Test'):
                     rest = 'Test'+rest
+                kind = 'test'
             else:
                 print ' * ignored class', rest
             line = 'class ' + rest + '\n'
@@ -46,7 +148,11 @@
 
     while lines and not lines[-1].strip():
         del lines[-1]
-
-    f = file(fn, 'w')
-    f.writelines(lines)
-    f.close()
+    
+    if confused: 
+	print "** confused: file not changed"
+    else:
+	#sys.stdout.writelines(lines)
+	f = file(fn, 'w')
+	f.writelines(lines)
+	f.close()

Modified: pypy/branch/src-pytest/pypy/tool/utestconvert.py
==============================================================================
--- pypy/branch/src-pytest/pypy/tool/utestconvert.py	(original)
+++ pypy/branch/src-pytest/pypy/tool/utestconvert.py	Sat Jan  8 18:36:06 2005
@@ -23,7 +23,9 @@
 d['failIfAlmostEqual']      = ('assert not round',  ' ==', [2,3,4])
 d['assertNotAlmostEqual']   = ('assert round',      ' !=', [2,3,4])
 d['failUnlessAlmostEquals'] = ('assert not round',  ' !=', [2,3,4])
+# PyPy specific
 d['assertRaises_w']         = ('self.raises_w',        '', ['Any'])
+d['assertEqual_w']          = ('assert self.space.eq_w','',['Any'])
 
 #  the list of synonyms
 d['failUnlessRaises']      = d['assertRaises']



More information about the Pypy-commit mailing list