[pypy-svn] r75743 - pypy/trunk/pypy/interpreter/pyparser/test

afa at codespeak.net afa at codespeak.net
Thu Jul 1 23:40:53 CEST 2010


Author: afa
Date: Thu Jul  1 23:40:51 2010
New Revision: 75743

Modified:
   pypy/trunk/pypy/interpreter/pyparser/test/test_parsestring.py
Log:
Factor common code in tests


Modified: pypy/trunk/pypy/interpreter/pyparser/test/test_parsestring.py
==============================================================================
--- pypy/trunk/pypy/interpreter/pyparser/test/test_parsestring.py	(original)
+++ pypy/trunk/pypy/interpreter/pyparser/test/test_parsestring.py	Thu Jul  1 23:40:51 2010
@@ -2,81 +2,56 @@
 import py
 
 class TestParsetring:
+    def parse_and_compare(self, literal, value):
+        space = self.space
+        w_ret = parsestring.parsestr(space, None, literal)
+        if isinstance(value, str):
+            assert space.type(w_ret) == space.w_str
+            assert space.str_w(w_ret) == value
+        elif isinstance(value, unicode):
+            assert space.type(w_ret) == space.w_unicode
+            assert space.unicode_w(w_ret) == value
+        else:
+            assert False
+
     def test_simple(self):
         space = self.space
-        s = 'hello world'
-        w_ret = parsestring.parsestr(space, None, repr(s))
-        assert space.str_w(w_ret) == s
-        s = 'hello\n world'
-        w_ret = parsestring.parsestr(space, None, repr(s))
-        assert space.str_w(w_ret) == s
-        s = "'''hello\\x42 world'''"
-        w_ret = parsestring.parsestr(space, None, s)
-        assert space.str_w(w_ret) == 'hello\x42 world'
-        s = r'"\0"'
-        w_ret = parsestring.parsestr(space, None, s)
-        assert space.str_w(w_ret) == chr(0)
-        s = r'"\07"'
-        w_ret = parsestring.parsestr(space, None, s)
-        assert space.str_w(w_ret) == chr(7)
-        s = r'"\123"'
-        w_ret = parsestring.parsestr(space, None, s)
-        assert space.str_w(w_ret) == chr(0123)
-        s = r'"\400"'
-        w_ret = parsestring.parsestr(space, None, s)
-        assert space.str_w(w_ret) == chr(0)
-        s = r'"\9"'
-        w_ret = parsestring.parsestr(space, None, s)
-        assert space.str_w(w_ret) == '\\9'
-        s = r'"\08"'
-        w_ret = parsestring.parsestr(space, None, s)
-        assert space.str_w(w_ret) == chr(0) + '8'
-        s = r'"\x"'
-        space.raises_w(space.w_ValueError, parsestring.parsestr, space, None, s)
-        s = r'"\x7"'
-        space.raises_w(space.w_ValueError, parsestring.parsestr, space, None, s)
-        s = r'"\x7g"'
-        space.raises_w(space.w_ValueError, parsestring.parsestr, space, None, s)
-        s = r'"\xfF"'
-        w_ret = parsestring.parsestr(space, None, s)
-        assert space.str_w(w_ret) == chr(0xFF)
+        for s in ['hello world', 'hello\n world']:
+            self.parse_and_compare(repr(s), s)
+
+        self.parse_and_compare("'''hello\\x42 world'''", 'hello\x42 world')
+
+        # octal
+        self.parse_and_compare(r'"\0"', chr(0))
+        self.parse_and_compare(r'"\07"', chr(7))
+        self.parse_and_compare(r'"\123"', chr(0123))
+        self.parse_and_compare(r'"\400"', chr(0))
+        self.parse_and_compare(r'"\9"', '\\' + '9')
+        self.parse_and_compare(r'"\08"', chr(0) + '8')
+
+        # hexadecimal
+        self.parse_and_compare(r'"\xfF"', chr(0xFF))
+        self.parse_and_compare(r'"\""', '"')
+        self.parse_and_compare(r"'\''", "'")
+        for s in (r'"\x"', r'"\x7"', r'"\x7g"'):
+            space.raises_w(space.w_ValueError,
+                           parsestring.parsestr, space, None, s)
 
-        s = r'"\""'
-        w_ret = parsestring.parsestr(space, None, s)
-        assert space.str_w(w_ret) == '"'
-        
-        s = r"'\''"
-        w_ret = parsestring.parsestr(space, None, s)
-        assert space.str_w(w_ret) == "'"
-        
-        
     def test_unicode(self):
         space = self.space
-        s = u'hello world'
-        w_ret = parsestring.parsestr(space, None, repr(s))
-        ret = space.unwrap(w_ret)
-        assert isinstance(ret, unicode)
-        assert ret == s
-        s = u'hello\n world'
-        w_ret = parsestring.parsestr(self.space, None, repr(s))
-        ret = space.unwrap(w_ret)
-        assert isinstance(ret, unicode)
-        assert ret == s
-        s = "u'''hello\\x42 world'''"
-        w_ret = parsestring.parsestr(self.space, None, s)
-        ret = space.unwrap(w_ret)
-        assert isinstance(ret, unicode)
-        assert ret == u'hello\x42 world'
-        s = "u'''hello\\u0842 world'''"
-        w_ret = parsestring.parsestr(self.space, None, s)
-        ret = space.unwrap(w_ret)
-        assert isinstance(ret, unicode)
-        assert ret == u'hello\u0842 world'
+        for s in [u'hello world', u'hello\n world']:
+            self.parse_and_compare(repr(s), s)
+
+        self.parse_and_compare("u'''hello\\x42 world'''",
+                               u'hello\x42 world')
+        self.parse_and_compare("u'''hello\\u0842 world'''",
+                               u'hello\u0842 world')
+
         s = "u'\x81'"
         s = s.decode("koi8-u").encode("utf8")
         w_ret = parsestring.parsestr(self.space, 'koi8-u', s)
         ret = space.unwrap(w_ret)
-        assert ret == eval("# -*- coding: koi8-u -*-\nu'\x81'") 
+        assert ret == eval("# -*- coding: koi8-u -*-\nu'\x81'")
 
     def test_simple_enc_roundtrip(self):
         #py.test.skip("crashes in app_codecs, but when cheating using .encode at interp-level passes?!")



More information about the Pypy-commit mailing list