HTMLify
utils.test.js
Views: 6 | Author: cody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 'use strict'; var Buffer = require('safe-buffer').Buffer; var utils = require('../lib/utils'); var assert = require('assert'); var debug = require('debug'); var mongo; try { mongo = new require('mongodb'); } catch (e) { debug('mongo', 'cannot construct mongodb instance'); } describe('lib/utils', function() { describe('clone', function() { it('clones constructors named ObjectId', function(done) { function ObjectId(id) { this.id = id; } var o1 = new ObjectId('1234'); var o2 = utils.clone(o1); assert.ok(o2 instanceof ObjectId); done(); }); it('clones constructors named ObjectID', function(done) { function ObjectID(id) { this.id = id; } var o1 = new ObjectID('1234'); var o2 = utils.clone(o1); assert.ok(o2 instanceof ObjectID); done(); }); it('does not clone constructors named ObjectIdd', function(done) { function ObjectIdd(id) { this.id = id; } var o1 = new ObjectIdd('1234'); var o2 = utils.clone(o1); assert.ok(!(o2 instanceof ObjectIdd)); done(); }); it('optionally clones ObjectId constructors using its clone method', function(done) { function ObjectID(id) { this.id = id; this.cloned = false; } ObjectID.prototype.clone = function() { var ret = new ObjectID(this.id); ret.cloned = true; return ret; }; var id = 1234; var o1 = new ObjectID(id); assert.equal(id, o1.id); assert.equal(false, o1.cloned); var o2 = utils.clone(o1); assert.ok(o2 instanceof ObjectID); assert.equal(id, o2.id); assert.ok(o2.cloned); done(); }); it('clones mongodb.ReadPreferences', function(done) { if (!mongo) return done(); var tags = [ {dc: 'tag1'} ]; var prefs = [ new mongo.ReadPreference('primary'), new mongo.ReadPreference(mongo.ReadPreference.PRIMARY_PREFERRED), new mongo.ReadPreference('secondary', tags) ]; var prefsCloned = utils.clone(prefs); for (var i = 0; i < prefsCloned.length; i++) { assert.notEqual(prefs[i], prefsCloned[i]); if (prefs[i].tags) { assert.ok(prefsCloned[i].tags); assert.notEqual(prefs[i].tags, prefsCloned[i].tags); assert.notEqual(prefs[i].tags[0], prefsCloned[i].tags[0]); } else { assert.equal(prefsCloned[i].tags, null); } } done(); }); it('clones mongodb.Binary', function(done) { if (!mongo) return done(); var buf = Buffer.from('hi'); var binary = new mongo.Binary(buf, 2); var clone = utils.clone(binary); assert.equal(binary.sub_type, clone.sub_type); assert.equal(String(binary.buffer), String(buf)); assert.ok(binary !== clone); done(); }); it('handles objects with no constructor', function(done) { var name = '335'; var o = Object.create(null); o.name = name; var clone; assert.doesNotThrow(function() { clone = utils.clone(o); }); assert.equal(name, clone.name); assert.ok(o != clone); done(); }); it('handles buffers', function(done) { var buff = Buffer.alloc(10); buff.fill(1); var clone = utils.clone(buff); for (var i = 0; i < buff.length; i++) { assert.equal(buff[i], clone[i]); } done(); }); }); }); |