def check(fn='test.txt'): f = File(fn) if not f.exists(): raise support.TestError('"%s" should exist' % fn) if not f.length(): raise support.TestError('"%s" should have contents' % fn) os.remove(fn)
def finalTestReport(): global totalTestFailures global totalTests if totalTestFailures > 0: raise support.TestError('%d of %d test(s) failed in this module' % (totalTestFailures, totalTests)) else: print 'All %d test(s) passed in this module.' % totalTests
""" [ 562943 ] os.path.getmtime misbehaves on nonfile """ import support import os.path try: print os.path.getmtime('nonfile') except OSError: pass else: raise support.TestError('Should raise an OSError')
""" Exceptions raised unpacking tuple args have wrong line number """ import support import sys def foo( (x,y) ): pass try: foo(10) except TypeError: tb = sys.exc_info()[2] if tb.tb_lineno == 0: raise support.TestError("Traceback lineno was zero")
""" """ import support msg = 'This is a test.' try: msg.startsWith('This') except AttributeError, e: pass else: raise support.TestError("startsWith should not be defined") if not msg.startswith('This'): raise support.TestError("startswith error")
""" test for patch [ 1153003 ] """ import support import jarray import java from org.python.core import ArgParser, PyObject try: # test(1, arg1=2) args = jarray.array([1, 2], PyObject) kwds = jarray.array(['arg1'], java.lang.String) ArgParser('test', args, kwds, 'arg1', 'arg2') except TypeError: pass else: raise support.TestError('Should raise a TypeError')
""" """ import support import test200p1 s = test200p1.cls().toString() if s[-10:] != "overridden": raise support.TestError("overridden method not working")
""" Check that a list based on a tuple can not change the tuple """ import support t1 = (1, 2, 3) t2 = (1, 2, 3) list(t1).reverse() if t1 != t2: raise support.TestError('tuple was modified.')
""" """ import support support.compileJava("test157j.java") import test157j from jarray import * f = zeros(2, 'd') support.compare(test157j.test(f), "double") if f[0] == 0: raise support.TestError("array not changed") f = zeros(2, 'f') support.compare(test157j.test(f), "float") if f[0] == 0: raise support.TestError("array not changed")
""" Check if id() returns unique values for different objects. """ import support d = {} cnt = 0 for i in xrange(100000): s = "test" + ` i ` j = id(s) if d.has_key(j): cnt = cnt + 1 d[j] = s if cnt != 0: raise support.TestError("%d id() value conflicts" % cnt)
def check(filename, result): f = open(filename) l = f.readlines() f.close() if l != result: raise support.TestError("Result was wrong: %s" % l)
def check(filename, result): f = open(filename) l = f.readlines() f.close() if l != result: raise support.TestError("Result was wrong: %s" % l) # Different exit situations in the interpreter. support.runJython("test333s1.py", output="test333s1.out") check("test333s1.out", ["myfunc\n"]) ret = support.runJython("test333s2.py", output="test333s2.out", expectError=1) if ret != 42: raise support.TestError("Return code was wrong: %d" % ret) check("test333s2.out", ["myfunc\n"]) support.runJython("test333s3.py", output="test333s3.out", error="test333s3.err", expectError=1) check("test333s3.out", ["myfunc\n"]) check("test333s3.err", [ 'Traceback (innermost last):\n', ' File "test333s3.py", line 8, in ?\n', 'Exc\n', ]) # Different exit situations in compiled applications.
import BaseHTTPServer def test(HandlerClass = SimpleHTTPServer.SimpleHTTPRequestHandler, ServerClass = BaseHTTPServer.HTTPServer): server_address = ('', 8000) # Silense the server HandlerClass.log_message = lambda x, b, *arg: None httpd = ServerClass(server_address, HandlerClass) # do just one request. httpd.handle_request() import thread thread.start_new_thread(test, ()) import httplib import time time.sleep(5) h = httplib.HTTP() h.connect("localhost", 8000) h.putrequest('GET', "/") h.endheaders() status, reason, headers = h.getreply() if status != 200: raise support.TestError("Wrong status: %d" % status) if reason != "OK": raise support.TestError("Wrong status: %d" % status) h.getfile().read()
""" """ import support support.compileJPythonc("test190c.py", output="test190.err") if support.grep("jpywork/test190c.java", "SubDate extends .*Date", count=1) != 1: raise support.TestError("SubDate should extends java.util.Date")
def test(self): if self.attr is not SINGL: raise support.TestError("Singleton not unique") if self.attr != SINGL: raise support.TestError("Singleton not unique")
""" [ #490157 ] string.splitlines() - incorrectly splits """ import support r = 'This is a\n multiline string\n'.splitlines() if r != ['This is a', ' multiline string']: raise support.TestError("Wrong splitlines(): %s" % r)
""" Comparing ints and strings """ import support if -1 > 'a': raise support.TestError("-1 > 'a'") if not -1 < 'a': raise support.TestError("-1 < 'a'") if 4 > 'a': raise support.TestError("4 > 'a'") if not 4 < 'a': raise support.TestError("4 < 'a'") if -2 > 'a': raise support.TestError("-2 > 'a'") if not -2 < 'a': raise support.TestError("-2 < 'a'") if -1 == 'a': raise support.TestError("-1 == 'a'")
""" """ import support alfa = {} alfa["test"] = "test1" try: del alfa["beta"] except KeyError, e: pass else: raise support.TestError("Should raise")
""" Bug #222847 - Can't access public member of package private base class """ import support support.compileJava("classes/test232p/Foo.java") from test232p import Foo try: Foo().hi() except IllegalAccessException: raise support.TestError( 'Should be able to call public method on package protected superclass')
''' Confirms the correct bevahior of Python int to Java byte coercion. Even though a Python int can be expressed in a byte like format such as 0xFF, Java bytes only range from -128 to 127 so any int greater than 0x80 should throw a TypeError if it's passed to something expecting a Java byte. ''' import support support.compileJava("classes/test399j.java") import test399j test399j.takesByteArray([0x7E, 0x12]) try: test399j.takesByteArray([0xFF, 0x00]) raise support.TestError('0xFF should not be acceptable as a Java byte') except TypeError: pass#expected since 0xFF can't be coerced to a Java byte
""" Check that UEE also matches IOError. """ import support import java try: x = java.io.OutputStreamWriter(java.lang.System.out, "garbage") except java.io.UnsupportedEncodingException, e: pass else: raise support.TestError("Should raise an exception") try: x = java.io.OutputStreamWriter(java.lang.System.out, "garbage") except IOError, e: pass else: raise support.TestError("Should raise an exception")
""" [ #458945 ] Missing 'lastindex' on match objects """ import support import re m = re.match(r"(\w*) (\w*) (\w*)", "word1 word2 word3") if m.lastindex != 3: raise support.TestError('Wrong lastindex value#1 : %d' % m.lastindex) m = re.match(r"((\w*) )+", "word1 word2 word3 ") if m.lastindex != 2: raise support.TestError('Wrong lastindex value#2 : %d' % m.lastindex) m = re.match(r"abc", "abc") if m.lastindex != None: raise support.TestError('Wrong lastindex value#3 : %d' % m.lastindex)
""" Test multiple inheritance of 2 java classes """ import support try: import test087m except TypeError, e: support.compare(e, "multiple inheritance") else: raise support.TestError("multiple inheritance should fail")
import support try: from test233s import foobar except ImportError: pass else: raise support.TestError("Should raise ImportError")
""" "%#.0f", "%e" and "%+f" w/ negative numbers don't print correctly. """ import support if "%#.0f" % 5 != "5.": raise support.TestWarning("Format Error #1 %#.0f" % 5) if "%.1f" % 5 != "5.0": raise support.TestError("Format Error #2 %.1f" % 5) if "%e" % -1e-6 != "-1.000000e-006": raise support.TestError("Format Error #3 %e" % -1e-6) if "%e" % 0 != "0.000000e+000": raise support.TestError("Format Error #4 %e" % 0) if "%e" % 1e-6 != "1.000000e-006": raise support.TestError("Format Error #5 %e" % 1e-6) if "%+f" % -5 != "-5.000000": raise support.TestError("Format Error #6 %+f" % -5) if "%+f" % 5 != "+5.000000": raise support.TestError("Format Error #7 %+f" % 5) import java java.util.Locale.setDefault(java.util.Locale("us", "")) if "%#.0f" % 5 != "5.": raise support.TestError("Format Error #8") if "%.1f" % 5 != "5.0": raise support.TestError("Format Error #9")
""" [ 515894 ] Behaviour of "+=" stm. is different from """ import support a = [1, 2, 3] a += "456" if a != [1, 2, 3, '4', '5', '6']: raise support.TestError('list += not working')
""" Check for lists and dicts as key in a dict. """ import support try: a = {[1]: 2} a = {{1: 2}: 3} except TypeError, e: pass else: raise support.TestError("Should fail, lists and dicts not allowed as key")
""" """ import support class A: def __getitem__(self, key): raise IndexError, "hello" try: A()['b'] except IndexError: pass else: raise support.TestError("Should raise IndexError")
""" Test that open append position filepointer at the end """ import support s1 = "abcdefghijklmnopqrstuvwxyz" s2 = "0123456789" f = open("test299.out", "wb") f.write(s1) f.close() f = open("test299.out", "ab") f.write(s2) f.close() f = open("test299.out", "rb") res = f.read() f.close() if res != s1 + s2: raise support.TestError('File did not append correctly')
""" Test the re.M flag. Fails when using re. Success with sre. """ import support import re var ="T\nO" m = re.search("^O",var,re.M) if m == None: raise support.TestError("Should match") m = re.search("^O",var) if m != None: raise support.TestError("Should not match")