Skip to content

Commit

Permalink
quite a few changes here (sorry!) which sort-of blend together in a
Browse files Browse the repository at this point in the history
bit of a tidyup, making the compiler more strictly like python,
thanks to LibTest.

here's what's in CHANGELOG:

 * String constants are now returned as instances of String()
   which results in them having the __iter__ method that has
   been added to the proto of the javascript base String type.

 * made TypeError inherit from BaseException rather than being a
   javascript proto from Error().  TypeError can now be raised
   as an Exception, with a message.

 * made == comparison use __eq__, which conditionally uses __cmp__
   if it is available: this fixes bugs in comparison of lists in the
   LibTest.  replaced a large number of numerical "==" comparisons
   with "is", so that __eq__ doesn't get called (efficiency).

 * added support for calling *args on functions: previously, a call
   function(*args) would result in the args being thrown away!

 * made Tuple its own class, rather than inheriting from List, in order
   that it can be distinguished from List.  __cmp__ of a List with a
   Tuple now correctly fails.

git-svn-id: https://pyjamas.svn.sourceforge.net/svnroot/pyjamas/trunk@455 7a2bd370-bda8-463c-979e-2900ccfb811e
  • Loading branch information
lkcl committed Mar 20, 2009
1 parent d24002d commit 48c5393
Show file tree
Hide file tree
Showing 12 changed files with 401 additions and 94 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG
@@ -1,6 +1,26 @@
Changes made to Pyjamas since 0.5
---------------------------------

* String constants are now returned as instances of String()
which results in them having the __iter__ method that has
been added to the proto of the javascript base String type.

* made TypeError inherit from BaseException rather than being a
javascript proto from Error(). TypeError can now be raised
as an Exception, with a message.

* made == comparison use __eq__, which conditionally uses __cmp__
if it is available: this fixes bugs in comparison of lists in the
LibTest. replaced a large number of numerical "==" comparisons
with "is", so that __eq__ doesn't get called (efficiency).

* added support for calling *args on functions: previously, a call
function(*args) would result in the args being thrown away!

* made Tuple its own class, rather than inheriting from List, in order
that it can be distinguished from List. __cmp__ of a List with a
Tuple now correctly fails.

* rewrote the examples/libtest so that they can be run under standard
python (python ./LibTest.py). this allows for a comparison of pyjs
against standard python.
Expand Down
55 changes: 54 additions & 1 deletion examples/libtest/ArgsTest.py
@@ -1,5 +1,9 @@
from UnitTest import UnitTest

def aArgs(*args):
return args


class ArgsTest(UnitTest):
def __init__(self):
UnitTest.__init__(self)
Expand All @@ -18,27 +22,76 @@ def testSimpleCall(self):
self.assertEquals(values[1], 2)
self.assertEquals(values[2], 3)

def testKeywordCall(self):
def testKeywordCall1(self):
values = foo2(c=3, b=2, a=1)
self.assertEquals(values[0], 1)
self.assertEquals(values[1], 2)
self.assertEquals(values[2], 3)

def testKeywordCall2(self):
values = foo2(b=2, a=1, c=3)
self.assertEquals(values[0], 1)
self.assertEquals(values[1], 2)
self.assertEquals(values[2], 3)

def testKeywordCall3(self):
values = foo2(1, c=3)
self.assertEquals(values[0], 1)
self.assertEquals(values[1], None)
self.assertEquals(values[2], 3)

def testKeywordCall4(self):
values = foo2()
self.assertEquals(values[0], None)
self.assertEquals(values[1], None)
self.assertEquals(values[2], None)

def testKeywordCall5(self):
values = foo2(c=True)
self.assertEquals(values[0], None)
self.assertEquals(values[1], None)
self.assertEquals(values[2], True)

def testStarArgs(self):
args = (1,2)
res = aArgs(*args)
self.assertEquals(args, res)

args = "123"
try:
res = aArgs(*args)
called = True
exc = None
except TypeError, e:
called = False
exc = e

# weird one: a string is a sequence, so it gets away with being
# called on its own as *args! eeugh.
self.assertTrue(called,
"exception not expected but function called:" + repr(res) + repr(exc))
self.assertEquals(res, ("1", "2", "3"))


args = 1
try:
res = aArgs(*args)
called = True
except TypeError:
called = False

self.assertFalse(called,
"exception expected but not raised - TypeError: aArgs() argument after * must be a sequence")


args = (1,)
res = aArgs(*args)
self.assertEquals(args, res)

args = (1,)
res = aArgs(args)
self.assertEquals((args,), res)


def testDefaultValuesCall(self):
values = foo3(b=7)
Expand Down
3 changes: 2 additions & 1 deletion examples/libtest/ClassTest.py
Expand Up @@ -100,7 +100,8 @@ def testInheritedConstructors(self):
self.assertEquals(obj1.z, expected_result3, "Did not inherit property from grandparent")
self.assertEquals(obj2.z, expected_result3, "Did not inherit property from grandparent")

self.assertNotEqual(getattr(obj1, "r", None), expected_result4, "ExampleGrandParentConstructor.__init__() was called")
res = getattr(obj1, "r", None)
self.assertNotEqual(res, expected_result4, "ExampleGrandParentConstructor.__init__() was called (%s)" % res)
self.assertNotEqual(getattr(obj2, "r", None), expected_result4, "ExampleGrandParentConstructor.__init__() was called")

# check inherited class vars (from parent)
Expand Down
12 changes: 10 additions & 2 deletions examples/libtest/ExceptionTest.py
Expand Up @@ -17,6 +17,14 @@ def __init__(self):
def getName(self):
return "Exception"

def testTypeError(self):
try:
raise TypeError("fred")
except:
self.assertTrue(True, "the exception should have happened")
return
self.assertTrue(False, "the exception should have happened")

def testExceptionOrdTrigger(self):
try:
x = ord(5) # shouldn't be a number
Expand Down Expand Up @@ -46,7 +54,7 @@ def testCatchClassException(self):
raise MyException()
except MyException, e:
self.assertEqual(e.toString(), 'MyException',
"Catched exception does not match")
"Caught exception does not match")
return
self.fail('MyException was not caught or raised')

Expand All @@ -55,7 +63,7 @@ def testCatchMultiClassException(self):
raise MyException()
except (MyException, MyException2), e:
self.assertEqual(e.toString(), 'MyException',
"Catched exception does not match")
"Caught exception does not match")
return
self.fail('MyException was not caught or raised')

Expand Down
32 changes: 2 additions & 30 deletions examples/libtest/FunctionTest.py
Expand Up @@ -6,18 +6,15 @@ def __init__(self, x):
self._x = x

def handle(self, y):
return self._x == y
return self._x is y

def aProcedure():
x = 1
if x == 2:
if x is 2:
# a return statement which is not reached
return "something"
#this is a comment

def aArgs(*args):
return args

def aFunctionReturningNone():
return None

Expand Down Expand Up @@ -53,28 +50,3 @@ def testProcedure(self):
self.assertTrue(aProcedure() is None,
"Procedures should always return None")

def testArgs(self):

args = (1,2)
res = aArgs(*args)
self.assertEqual(args, res)

args = 1
try:
res = aArgs(*args)
called = True
except TypeError:
called = False

self.assertFalse(called,
"exception expected but not raised - TypeError: aArgs() argument after * must be a sequence")


args = (1,)
res = aArgs(*args)
self.assertEqual(args, res)

args = (1,)
res = aArgs(args)
self.assertEqual(args, (res,))

82 changes: 52 additions & 30 deletions examples/libtest/ListTest.py
Expand Up @@ -10,47 +10,47 @@ def getName(self):
def testSliceGet(self):
value = [0, 1, 2, 3, 4]

self.assertTrue(value[-1]==4)
self.assertTrue(value[1]==1)
self.assertTrue(value[4]==4)
self.assertTrue(value[-3]==2)
self.assertTrue(value[-1] is 4)
self.assertTrue(value[1] is 1)
self.assertTrue(value[4] is 4)
self.assertTrue(value[-3] is 2)

def testSliceRange(self):
value = [0, 1, 2, 3, 4]

self.assertTrue(value[1:3][0]==1)
self.assertTrue(value[1:3][1]==2)
self.assertTrue(len(value[1:2])==1)
self.assertTrue(len(value[1:3])==2)
self.assertTrue(value[1:3][0] is 1)
self.assertTrue(value[1:3][1] is 2)
self.assertTrue(len(value[1:2]) is 1)
self.assertTrue(len(value[1:3]) is 2)

self.assertTrue(value[:2][0]==0)
self.assertTrue(value[:2][1]==1)
self.assertTrue(len(value[:2])==2)
self.assertTrue(len(value[:1])==1)
self.assertTrue(value[:2][0] is 0)
self.assertTrue(value[:2][1] is 1)
self.assertTrue(len(value[:2]) is 2)
self.assertTrue(len(value[:1]) is 1)

self.assertTrue(value[:-1][0]==0)
self.assertTrue(value[:-1][3]==3)
self.assertTrue(len(value[:-1])==4)
self.assertTrue(value[:-1][0] is 0)
self.assertTrue(value[:-1][3] is 3)
self.assertTrue(len(value[:-1]) is 4)

self.assertTrue(value[:][3]==3)
self.assertTrue(len(value[:])==5)
self.assertTrue(value[:][3] is 3)
self.assertTrue(len(value[:]) is 5)

self.assertTrue(value[0:][3]==3)
self.assertTrue(value[1:][0]==1)
self.assertTrue(len(value[1:])==4)
self.assertTrue(value[0:][3] is 3)
self.assertTrue(value[1:][0] is 1)
self.assertTrue(len(value[1:]) is 4)

self.assertTrue(value[-1:][0]==4)
self.assertTrue(len(value[-1:3])==0)
self.assertTrue(value[-1:][0] is 4)
self.assertTrue(len(value[-1:3]) is 0)

def testDelete(self):
value = [0, 1, 2, 3, 4]
del value[4]
self.assertTrue(len(value)==4)
self.assertTrue(value[3]==3)
self.assertTrue(len(value) is 4)
self.assertTrue(value[3] is 3)

del value[-1]
self.assertTrue(len(value)==3)
self.assertTrue(value[2]==2)
self.assertTrue(len(value) is 3)
self.assertTrue(value[2] is 2)

def testPop(self):
a = ['a']
Expand All @@ -63,19 +63,19 @@ def testPop(self):

x = value.pop(4)
self.assertTrue(x==e)
self.assertTrue(len(value)==4)
self.assertTrue(len(value) is 4)

x = value.pop(-1)
self.assertTrue(x==d)
self.assertTrue(len(value)==3)
self.assertTrue(len(value) is 3)

x = value.pop()
self.assertTrue(x==c)
self.assertTrue(len(value)==2)
self.assertTrue(len(value) is 2)

x = value.pop(0)
self.assertTrue(x==a)
self.assertTrue(len(value)==1)
self.assertTrue(len(value) is 1)

def testSort(self):
l1 = ['c', 'd', 'a', 'b']
Expand Down Expand Up @@ -108,6 +108,28 @@ def toLower(x):
self.assertTrue(l4[2] == 'b')
self.assertTrue(l4[3] == 'a')

def testCmp(self):

l1 = [1,2,3]
l2 = [1,2]
l3 = [1,2,3]
l4 = [1,2,4]

t1 = (1,2,3)

self.assertTrue(cmp(l1, l2) == 1)
self.assertTrue(cmp(l2, l1) == -1)
self.assertTrue(cmp(l3, l4) == -1)
self.assertTrue(cmp(l4, l3) == 1)

def testCmpListTuple(self):

t1 = (1,2,3)
l1 = [1,2,3]

self.assertFalse(l1 == t1)
self.assertTrue(cmp(l1, t1) == -1)

def testSortCmp(self):
a = A()
l1 = [a, 1]
Expand Down
6 changes: 3 additions & 3 deletions examples/libtest/SetTest.py
Expand Up @@ -22,7 +22,7 @@ def testAdd(self):

self.assertTrue('a' in value)
self.assertTrue('c' not in value)
self.assertTrue(len(value)==2)
self.assertTrue(len(value) is 2)

def testRemove(self):
value = Set(['a', 'b', 'c'])
Expand All @@ -38,7 +38,7 @@ def testIter(self):
for i in value:
items.remove(i)

self.assertTrue(len(items)==0)
self.assertTrue(len(items) is 0)

def testAddObject(self):
v1 = DummyClass('a')
Expand All @@ -57,7 +57,7 @@ def testAddObject(self):
self.assertTrue(v2 in value)
self.assertTrue(v3 in value)
self.assertTrue(v4 not in value)
self.assertTrue(len(value)==3)
self.assertTrue(len(value) is 3)

i = 0
for v in value:
Expand Down
4 changes: 2 additions & 2 deletions examples/libtest/UnitTest.py
Expand Up @@ -54,7 +54,7 @@ def getTestMethods(self):

def isTestMethod(self, method):
if callable(getattr(self, method)):
if method.find("test")==0:
if method.find("test") is 0:
return True
return False

Expand Down Expand Up @@ -109,7 +109,7 @@ def failUnlessAlmostEqual(self, first, second, places=7, msg=None):

def failIfAlmostEqual(self, first, second, places=7, msg=None):
self.startTest()
if round(second-first, places) == 0:
if round(second-first, places) is 0:
if not msg:
msg=repr(first) + " == " + repr(second) + " within " + repr(places) + " places"
return self.fail(msg)
Expand Down
4 changes: 2 additions & 2 deletions examples/libtest/VarsTest.py
Expand Up @@ -14,9 +14,9 @@ def getName(self):

def testChangeVarInInnerScope(self):
x = 5
if x == 1:
if x is 1:
x = 2
elif x == 5:
elif x is 5:
x = 3
self.assertEqual(x, 3, "the value of x should be 3")

Expand Down

0 comments on commit 48c5393

Please sign in to comment.