2 var utils = require('../lib/utils');
3 var assert = require('assert');
7 mongo = new require('mongodb');
10 describe('lib/utils', function() {
11 describe('clone', function() {
12 it('clones constructors named ObjectId', function(done) {
13 function ObjectId (id) {
17 var o1 = new ObjectId('1234');
18 var o2 = utils.clone(o1);
19 assert.ok(o2 instanceof ObjectId);
24 it('clones constructors named ObjectID', function(done) {
25 function ObjectID (id) {
29 var o1 = new ObjectID('1234');
30 var o2 = utils.clone(o1);
32 assert.ok(o2 instanceof ObjectID);
36 it('does not clone constructors named ObjectIdd', function(done) {
37 function ObjectIdd (id) {
41 var o1 = new ObjectIdd('1234');
42 var o2 = utils.clone(o1);
43 assert.ok(!(o2 instanceof ObjectIdd));
48 it('optionally clones ObjectId constructors using its clone method', function(done) {
49 function ObjectID (id) {
54 ObjectID.prototype.clone = function () {
55 var ret = new ObjectID(this.id);
61 var o1 = new ObjectID(id);
62 assert.equal(id, o1.id);
63 assert.equal(false, o1.cloned);
65 var o2 = utils.clone(o1);
66 assert.ok(o2 instanceof ObjectID);
67 assert.equal(id, o2.id);
72 it('clones mongodb.ReadPreferences', function (done) {
73 if (!mongo) return done();
79 new mongo.ReadPreference("primary"),
80 new mongo.ReadPreference(mongo.ReadPreference.PRIMARY_PREFERRED),
81 new mongo.ReadPreference("primary", tags),
82 mongo.ReadPreference("primary", tags)
85 var prefsCloned = utils.clone(prefs);
87 for (var i = 0; i < prefsCloned.length; i++) {
88 assert.notEqual(prefs[i], prefsCloned[i]);
89 assert.ok(prefsCloned[i] instanceof mongo.ReadPreference);
90 assert.ok(prefsCloned[i].isValid());
92 assert.ok(prefsCloned[i].tags);
93 assert.notEqual(prefs[i].tags, prefsCloned[i].tags);
94 assert.notEqual(prefs[i].tags[0], prefsCloned[i].tags[0]);
96 assert.equal(prefsCloned[i].tags, null);
103 it('clones mongodb.Binary', function(done){
104 if (!mongo) return done();
106 var buf = new Buffer('hi');
107 var binary= new mongo.Binary(buf, 2);
108 var clone = utils.clone(binary);
109 assert.equal(binary.sub_type, clone.sub_type);
110 assert.equal(String(binary.buffer), String(buf));
111 assert.ok(binary !== clone);
115 it('handles objects with no constructor', function(done) {
118 var o = Object.create(null);
122 assert.doesNotThrow(function() {
123 clone = utils.clone(o);
126 assert.equal(name, clone.name);
127 assert.ok(o != clone);
131 it('handles buffers', function(done){
132 var buff = new Buffer(10);
134 var clone = utils.clone(buff);
136 for (var i = 0; i < buff.length; i++) {
137 assert.equal(buff[i], clone[i]);