Commits

Ronald Oussoren committed f1a453b

- Rework the unittests for NSNumber.
- Add testcase for NSDecimal(floatValue)

Comments (0)

Files changed (2)

Lib/Foundation/test/test_nsdecimal.py

 
         o = NSDecimal(1L << 64 - 1)
 
-        # This is wrong, explict conversion should be allowed!
-        #self.assertRaises(TypeError, NSDecimal, 1.2)
+        # Explicit conversion is supported, but might not do
+        # what a naive user expects...
+        o = NSDecimal(1.1)
+        self.assert_(isinstance(o, NSDecimal))
+        self.assertEquals(str(o), repr(1.1))
+
         self.assertRaises(OverflowError, NSDecimal, 1L << 128)
         self.assertRaises(OverflowError, NSDecimal, -1L << 128)
 

Lib/Foundation/test/test_nsnumber.py

 import unittest
 import objc
 import re
+import sys
 
 from Foundation import *
 
     return r.replace(u'version="0.9"', u'version="1.0"')
 
 
-if not isinstance(NSNumber.numberWithInt_(10), int):
-    # NSNumber is proxied into python
+# NSNumber instances are converted to Python numbers
 
-    class TestNSNumber( unittest.TestCase ):
-        def testNSNumber( self ):
-            x = NSMutableArray.arrayWithArray_( range(1, 10) )
-            y = range( 1, 10 )
+class TestNSNumber( unittest.TestCase ):
+    def testSimple(self):
+        self.assertEquals(NSNumber.numberWithFloat_(1.0), 1,0)
+        self.assertEquals(NSNumber.numberWithInt_(1), 1)
+        self.assertEquals(NSNumber.numberWithFloat_(-0.5), -0.5)
+        self.assertEquals(NSNumber.numberWithInt_(-4), -4)
+        self.assertEquals(NSNumber.numberWithInt_(0), 0)
+        self.assertEquals(NSNumber.numberWithFloat_(0.0), 0,0)
 
-            self.assert_( isinstance( x[4], NSNumber ) )
-            self.assertEquals( x[3], y[3] )
-            self.assertEquals( x[3] + x[5], y[3] + y[5] )
-            self.assertEquals( x[2] + y[3], y[2] + x[3] )
-            self.assertEquals( x[8] * x[7], y[7] * y[8] )
-            self.assertEquals( y[8] * x[7], x[7] * y[8] )
+    def testUseAsBasicType(self):
+        lstValue = list(range(0, 20, 2))
+        for idx, v in enumerate(lstValue):
+            self.assertEquals(v, lstValue[NSNumber.numberWithInt_(idx)])
 
-        def testAsBool(self):
-            trues = (
-                NSNumber.numberWithFloat_(1.0),
-                NSNumber.numberWithInt_(1),
-                NSNumber.numberWithFloat_(-0.01),
-                NSNumber.numberWithInt_(-4),
-            )
-            falses = (
-                NSNumber.numberWithFloat_(0.0),
-                NSNumber.numberWithInt_(0),
-            )
+    def testUnsignedIssues(self):
+        # NSNumber stores unsigned numbers as signed numbers
+        # This is a bug in Cocoa...
 
-            for a in trues:
-                if a:
-                    pass
-                else:
-                    raise AssertionError, "%s is not true"%(a.description())
+        self.assertEquals(NSNumber.numberWithUnsignedInt_(sys.maxint+1),
+                    -sys.maxint-1)
 
-            for a in falses:
-                if a:
-                    raise AssertionError, "%s is true"%(a.description())
+    def testMethods(self):
+        v = NSNumber.numberWithUnsignedInt_(sys.maxint+1)
 
-        def testStr(self):
-            x = NSNumber.numberWithInt_(4)
-            self.assertEquals(4, int(str(x)))
+        self.assertEquals(v.unsignedIntValue(), sys.maxint+1)
+        self.assertEquals(v.intValue(), -sys.maxint-1)
 
-else:
-    # NSNumber instances are converted to Python numbers
-
-    class TestNSNumber( unittest.TestCase ):
-        def testNSNumber( self ):
-            self.assertEquals(NSNumber.numberWithFloat_(1.0), 1,0)
-            self.assertEquals(NSNumber.numberWithInt_(1), 1)
-            self.assertEquals(NSNumber.numberWithFloat_(-0.5), -0.5)
-            self.assertEquals(NSNumber.numberWithInt_(-4), -4)
-            self.assertEquals(NSNumber.numberWithInt_(0), 0)
-            self.assertEquals(NSNumber.numberWithFloat_(0.0), 0,0)
-
+        v = NSNumber.numberWithInt_(10)
+        self.assertEquals(v.doubleValue(), float(10))
 
 if objc.platform == 'MACOSX':
     class TestPropList (unittest.TestCase):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.