Skip to content
Browse files

Tests for NSNumber bridging

  • Loading branch information...
1 parent d425271 commit d2a6efdc05b54c9f60b71ace8a83528e056b00a0 @jspahrsummers committed Jul 23, 2012
Showing with 21 additions and 0 deletions.
  1. +11 −0 ObjectiveHaskellTests/BridgedObjectTest.hs
  2. +10 −0 ObjectiveHaskellTests/ObjectiveHaskellTests.m
View
11 ObjectiveHaskellTests/BridgedObjectTest.hs
@@ -12,6 +12,7 @@ import Foreign.Ptr
import ObjectiveHaskell.NSArray
import ObjectiveHaskell.NSData
import ObjectiveHaskell.NSDictionary
+import ObjectiveHaskell.NSNumber
import ObjectiveHaskell.NSString
import ObjectiveHaskell.NSValue
import ObjectiveHaskell.ObjC
@@ -47,9 +48,19 @@ nullNSValue' = toNSValue nullPtr
ptrAddress' :: Id -> IO CUIntPtr
ptrAddress' obj = (fromIntegral . ptrToWordPtr) <$> fromNSValue obj
+plus :: (Real a, Read a) => Id -> a -> IO Id
+plus obj n2 = do
+ n1 <- fromRational <$> fromNSNumber obj
+
+ -- This juggling is really only necessary because the type of this function is vague.
+ let sum = n1 + (fromRational $ toRational n2)
+ toNSNumber $ toRational $ sum
+
exportFunc "appendFoobar" [t| UnsafeId -> IO UnsafeId |] 'appendFoobar'
exportFunc "addFoobarToArray" [t| UnsafeId -> IO UnsafeId |] 'addFoobarToArray'
exportFunc "setFooToBar" [t| UnsafeId -> IO UnsafeId |] 'setFooToBar'
exportFunc "appendByte" [t| UnsafeId -> CUChar -> IO UnsafeId |] 'appendByte'
exportFunc "nullNSValue" [t| IO UnsafeId |] 'nullNSValue'
exportFunc "ptrAddress" [t| UnsafeId -> IO CUIntPtr |] 'ptrAddress'
+exportFunc "plusInt" [t| UnsafeId -> CInt -> IO UnsafeId |] 'plus
+exportFunc "plusDouble" [t| UnsafeId -> CDouble -> IO UnsafeId |] 'plus
View
10 ObjectiveHaskellTests/ObjectiveHaskellTests.m
@@ -39,6 +39,16 @@ - (void)testNSDictionaryBridging {
STAssertEqualObjects(dict, expectedDict, @"");
}
+- (void)testNSNumberBridging {
+ NSNumber *inum = @5;
+ STAssertEqualObjects(@6, plusInt(inum, 1), @"");
+ STAssertEqualsWithAccuracy(6.5, [plusDouble(inum, 1.5) doubleValue], 0.001, @"");
+
+ NSNumber *dnum = @5.25;
+ STAssertEqualsWithAccuracy(6.25, [plusInt(dnum, 1) doubleValue], 0.001, @"");
+ STAssertEqualsWithAccuracy(6.75, [plusDouble(dnum, 1.5) doubleValue], 0.001, @"");
+}
+
- (void)testNSValueBridging {
NSValue *value = nullNSValue();
STAssertNotNil(value, @"");

0 comments on commit d2a6efd

Please sign in to comment.
Something went wrong with that request. Please try again.