Permalink
Browse files

testing bson support.

  • Loading branch information...
1 parent b7eac84 commit a107772724a2aa3b30a8f709b235938836f63e1e Tim Burks committed Sep 3, 2010
Showing with 74 additions and 1 deletion.
  1. +10 −0 objc/NuBSON.h
  2. +30 −1 objc/NuBSON.m
  3. +34 −0 test/test_bson.nu
View
@@ -38,6 +38,8 @@ limitations under the License.
- (NuBSON *) initWithDictionary:(NSDictionary *) dict;
/*! Return a dictionary equivalent of a BSON object. */
- (NSMutableDictionary *) dictionaryValue;
+/*! Return an NSData representation of the BSON object. */
+- (NSData *) data;
@end
@interface NuBSONObjectID : NSObject
@@ -50,3 +52,11 @@ limitations under the License.
bson *bson_for_object(id object);
+@interface NSData (NuBSON)
+- (NSMutableDictionary *) BSONValue;
+@end
+
+@interface NSDictionary (NuBSON)
+- (NSData *) BSONRepresentation;
+@end
+
View
@@ -1,4 +1,5 @@
-#import "NuMongoDB.h"
+#import "NuBSON.h"
+#include "bson.h"
@interface NuBSON (Private)
- (NuBSON *) initWithBSON:(bson) b;
@@ -66,6 +67,11 @@ - (NuBSON *) initWithBSON:(bson) b
return self;
}
+- (NSData *) data
+{
+ return [[[NSData alloc] initWithBytes:(bsonValue.data) length:bson_size(&(bsonValue.data))] autorelease];
+}
+
void add_object_to_bson_buffer(bson_buffer *bb, id key, id object)
{
const char *name = [key cStringUsingEncoding:NSUTF8StringEncoding];
@@ -314,3 +320,26 @@ - (NSMutableDictionary *) dictionaryValue
}
return b;
}
+
+@implementation NSData (NuBSON)
+
+- (NSMutableDictionary *) BSONValue
+{
+ bson bsonValue;
+ bsonValue.data = [self bytes];
+ bsonValue.owned = NO;
+ NuBSON *bsonObject = [[[NuBSON alloc] initWithBSON:bsonValue] autorelease];
+ return [bsonObject dictionaryValue];
+}
+
+@end
+
+@implementation NSDictionary (NuBSON)
+
+- (NSData *) BSONRepresentation
+{
+ NuBSON *bsonObject = [[[NuBSON alloc] initWithDictionary:self] autorelease];
+ return [bsonObject data];
+}
+
+@end
View
@@ -0,0 +1,34 @@
+;; test_bson.nu
+;; tests for NuBSON representation.
+;;
+;; Copyright (c) 2010 Tim Burks, Neon Design Technology, Inc.
+
+(load "NuMongoDB")
+
+(class TestBSON is NuTestCase
+
+ (- testRoundtrip is
+ (set x (dict a:"a" b:"bee" c:123 d:(NSDate date) e:123.456 f:(dict a:(array 1 2 3))))
+ (set data (x BSONRepresentation))
+ (set y (data BSONValue))
+ (assert_equal (x a:) (y a:))
+ (assert_equal (x b:) (y b:))
+ (assert_equal (x c:) (y c:))
+ ;; bson only stores times with millisecond precision
+ (assert_in_delta ((x d:) timeIntervalSinceReferenceDate)
+ ((y d:) timeIntervalSinceReferenceDate)
+ 0.001)
+ (assert_equal (x e:) (y e:))
+ (assert_equal (x f:) (y f:))
+
+ (set data (y BSONRepresentation))
+ (set z (data BSONValue))
+ (assert_equal (y a:) (z a:))
+ (assert_equal (y b:) (z b:))
+ (assert_equal (y c:) (z c:))
+ (assert_equal (y d:) (z d:))
+ (assert_equal (y e:) (z e:))
+ (assert_equal (y f:) (z f:))
+
+
+ ))

0 comments on commit a107772

Please sign in to comment.