File size: 11,404 Bytes
bc20498 |
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
'use strict'
const { test } = require('tap')
const rfdc = require('..')
const cloneDefault = require('../default')
const clone = rfdc()
const cloneProto = rfdc({ proto: true })
const cloneCircles = rfdc({ circles: true })
const cloneCirclesProto = rfdc({ circles: true, proto: true })
const rnd = (max) => Math.round(Math.random() * max)
types(clone, 'default')
types(cloneProto, 'proto option')
types(cloneCircles, 'circles option')
types(cloneCirclesProto, 'circles and proto option')
test('default β does not copy proto properties', async ({ is }) => {
is(clone(Object.create({ a: 1 })).a, undefined, 'value not copied')
})
test('default β shorthand import', async ({ same }) => {
same(
clone(Object.create({ a: 1 })),
cloneDefault(Object.create({ a: 1 })),
'import equals clone with default options'
)
})
test('proto option β copies enumerable proto properties', async ({ is }) => {
is(cloneProto(Object.create({ a: 1 })).a, 1, 'value copied')
})
test('circles option - circular object', async ({ same, is, isNot }) => {
const o = { nest: { a: 1, b: 2 } }
o.circular = o
same(cloneCircles(o), o, 'same values')
isNot(cloneCircles(o), o, 'different objects')
isNot(cloneCircles(o).nest, o.nest, 'different nested objects')
const c = cloneCircles(o)
is(c.circular, c, 'circular references point to copied parent')
isNot(c.circular, o, 'circular references do not point to original parent')
})
test('circles option β deep circular object', async ({ same, is, isNot }) => {
const o = { nest: { a: 1, b: 2 } }
o.nest.circular = o
same(cloneCircles(o), o, 'same values')
isNot(cloneCircles(o), o, 'different objects')
isNot(cloneCircles(o).nest, o.nest, 'different nested objects')
const c = cloneCircles(o)
is(c.nest.circular, c, 'circular references point to copied parent')
isNot(
c.nest.circular,
o,
'circular references do not point to original parent'
)
})
test('circles option alone β does not copy proto properties', async ({
is
}) => {
is(cloneCircles(Object.create({ a: 1 })).a, undefined, 'value not copied')
})
test('circles and proto option β copies enumerable proto properties', async ({
is
}) => {
is(cloneCirclesProto(Object.create({ a: 1 })).a, 1, 'value copied')
})
test('circles and proto option - circular object', async ({
same,
is,
isNot
}) => {
const o = { nest: { a: 1, b: 2 } }
o.circular = o
same(cloneCirclesProto(o), o, 'same values')
isNot(cloneCirclesProto(o), o, 'different objects')
isNot(cloneCirclesProto(o).nest, o.nest, 'different nested objects')
const c = cloneCirclesProto(o)
is(c.circular, c, 'circular references point to copied parent')
isNot(c.circular, o, 'circular references do not point to original parent')
})
test('circles and proto option β deep circular object', async ({
same,
is,
isNot
}) => {
const o = { nest: { a: 1, b: 2 } }
o.nest.circular = o
same(cloneCirclesProto(o), o, 'same values')
isNot(cloneCirclesProto(o), o, 'different objects')
isNot(cloneCirclesProto(o).nest, o.nest, 'different nested objects')
const c = cloneCirclesProto(o)
is(c.nest.circular, c, 'circular references point to copied parent')
isNot(
c.nest.circular,
o,
'circular references do not point to original parent'
)
})
test('circles and proto option β deep circular array', async ({
same,
is,
isNot
}) => {
const o = { nest: [1, 2] }
o.nest.push(o)
same(cloneCirclesProto(o), o, 'same values')
isNot(cloneCirclesProto(o), o, 'different objects')
isNot(cloneCirclesProto(o).nest, o.nest, 'different nested objects')
const c = cloneCirclesProto(o)
is(c.nest[2], c, 'circular references point to copied parent')
isNot(c.nest[2], o, 'circular references do not point to original parent')
})
function types (clone, label) {
test(label + ' β number', async ({ is }) => {
is(clone(42), 42, 'same value')
})
test(label + ' β string', async ({ is }) => {
is(clone('str'), 'str', 'same value')
})
test(label + ' β boolean', async ({ is }) => {
is(clone(true), true, 'same value')
})
test(label + ' β function', async ({ is }) => {
const fn = () => {}
is(clone(fn), fn, 'same function')
})
test(label + ' β async function', async ({ is }) => {
const fn = async () => {}
is(clone(fn), fn, 'same function')
})
test(label + ' β generator function', async ({ is }) => {
const fn = function * () {}
is(clone(fn), fn, 'same function')
})
test(label + ' β date', async ({ is, isNot }) => {
const date = new Date()
is(+clone(date), +date, 'same value')
isNot(clone(date), date, 'different object')
})
test(label + ' β null', async ({ is }) => {
is(clone(null), null, 'same value')
})
test(label + ' β shallow object', async ({ same, isNot }) => {
const o = { a: 1, b: 2 }
same(clone(o), o, 'same values')
isNot(clone(o), o, 'different object')
})
test(label + ' β shallow array', async ({ same, isNot }) => {
const o = [1, 2]
same(clone(o), o, 'same values')
isNot(clone(o), o, 'different arrays')
})
test(label + ' β deep object', async ({ same, isNot }) => {
const o = { nest: { a: 1, b: 2 } }
same(clone(o), o, 'same values')
isNot(clone(o), o, 'different objects')
isNot(clone(o).nest, o.nest, 'different nested objects')
})
test(label + ' β deep array', async ({ same, isNot }) => {
const o = [{ a: 1, b: 2 }, [3]]
same(clone(o), o, 'same values')
isNot(clone(o), o, 'different arrays')
isNot(clone(o)[0], o[0], 'different array elements')
isNot(clone(o)[1], o[1], 'different array elements')
})
test(label + ' β nested number', async ({ is }) => {
is(clone({ a: 1 }).a, 1, 'same value')
})
test(label + ' β nested string', async ({ is }) => {
is(clone({ s: 'str' }).s, 'str', 'same value')
})
test(label + ' β nested boolean', async ({ is }) => {
is(clone({ b: true }).b, true, 'same value')
})
test(label + ' β nested function', async ({ is }) => {
const fn = () => {}
is(clone({ fn }).fn, fn, 'same function')
})
test(label + ' β nested async function', async ({ is }) => {
const fn = async () => {}
is(clone({ fn }).fn, fn, 'same function')
})
test(label + ' β nested generator function', async ({ is }) => {
const fn = function * () {}
is(clone({ fn }).fn, fn, 'same function')
})
test(label + ' β nested date', async ({ is, isNot }) => {
const date = new Date()
is(+clone({ d: date }).d, +date, 'same value')
isNot(clone({ d: date }).d, date, 'different object')
})
test(label + ' β nested date in array', async ({ is, isNot }) => {
const date = new Date()
is(+clone({ d: [date] }).d[0], +date, 'same value')
isNot(clone({ d: [date] }).d[0], date, 'different object')
is(+cloneCircles({ d: [date] }).d[0], +date, 'same value')
isNot(cloneCircles({ d: [date] }).d, date, 'different object')
})
test(label + ' β nested null', async ({ is }) => {
is(clone({ n: null }).n, null, 'same value')
})
test(label + ' β arguments', async ({ isNot, same }) => {
function fn (...args) {
same(clone(arguments), args, 'same values')
isNot(clone(arguments), arguments, 'different object')
}
fn(1, 2, 3)
})
test(`${label} copies buffers from object correctly`, async ({ ok, is, isNot }) => {
const input = Date.now().toString(36)
const inputBuffer = Buffer.from(input)
const clonedBuffer = clone({ a: inputBuffer }).a
ok(Buffer.isBuffer(clonedBuffer), 'cloned value is buffer')
isNot(clonedBuffer, inputBuffer, 'cloned buffer is not same as input buffer')
is(clonedBuffer.toString(), input, 'cloned buffer content is correct')
})
test(`${label} copies buffers from arrays correctly`, async ({ ok, is, isNot }) => {
const input = Date.now().toString(36)
const inputBuffer = Buffer.from(input)
const [clonedBuffer] = clone([inputBuffer])
ok(Buffer.isBuffer(clonedBuffer), 'cloned value is buffer')
isNot(clonedBuffer, inputBuffer, 'cloned buffer is not same as input buffer')
is(clonedBuffer.toString(), input, 'cloned buffer content is correct')
})
test(`${label} copies TypedArrays from object correctly`, async ({ ok, is, isNot }) => {
const [input1, input2] = [rnd(10), rnd(10)]
var buffer = new ArrayBuffer(8)
const int32View = new Int32Array(buffer)
int32View[0] = input1
int32View[1] = input2
const cloned = clone({ a: int32View }).a
ok(cloned instanceof Int32Array, 'cloned value is instance of class')
isNot(cloned, int32View, 'cloned value is not same as input value')
is(cloned[0], input1, 'cloned value content is correct')
is(cloned[1], input2, 'cloned value content is correct')
})
test(`${label} copies TypedArrays from array correctly`, async ({ ok, is, isNot }) => {
const [input1, input2] = [rnd(10), rnd(10)]
var buffer = new ArrayBuffer(16)
const int32View = new Int32Array(buffer)
int32View[0] = input1
int32View[1] = input2
const [cloned] = clone([int32View])
ok(cloned instanceof Int32Array, 'cloned value is instance of class')
isNot(cloned, int32View, 'cloned value is not same as input value')
is(cloned[0], input1, 'cloned value content is correct')
is(cloned[1], input2, 'cloned value content is correct')
})
test(`${label} copies complex TypedArrays`, async ({ ok, deepEqual, is, isNot }) => {
const [input1, input2, input3] = [rnd(10), rnd(10), rnd(10)]
var buffer = new ArrayBuffer(4)
const view1 = new Int8Array(buffer, 0, 2)
const view2 = new Int8Array(buffer, 2, 2)
const view3 = new Int8Array(buffer)
view1[0] = input1
view2[0] = input2
view3[3] = input3
const cloned = clone({ view1, view2, view3 })
ok(cloned.view1 instanceof Int8Array, 'cloned value is instance of class')
ok(cloned.view2 instanceof Int8Array, 'cloned value is instance of class')
ok(cloned.view3 instanceof Int8Array, 'cloned value is instance of class')
isNot(cloned.view1, view1, 'cloned value is not same as input value')
isNot(cloned.view2, view2, 'cloned value is not same as input value')
isNot(cloned.view3, view3, 'cloned value is not same as input value')
deepEqual(Array.from(cloned.view1), [input1, 0], 'cloned value content is correct')
deepEqual(Array.from(cloned.view2), [input2, input3], 'cloned value content is correct')
deepEqual(Array.from(cloned.view3), [input1, 0, input2, input3], 'cloned value content is correct')
})
test(`${label} - maps`, async ({ same, isNot }) => {
const map = new Map([['a', 1]])
same(Array.from(clone(map)), [['a', 1]], 'same value')
isNot(clone(map), map, 'different object')
})
test(`${label} - sets`, async ({ same, isNot }) => {
const set = new Set([1])
same(Array.from(clone(set)), [1])
isNot(clone(set), set, 'different object')
})
test(`${label} - nested maps`, async ({ same, isNot }) => {
const data = { m: new Map([['a', 1]]) }
same(Array.from(clone(data).m), [['a', 1]], 'same value')
isNot(clone(data).m, data.m, 'different object')
})
test(`${label} - nested sets`, async ({ same, isNot }) => {
const data = { s: new Set([1]) }
same(Array.from(clone(data).s), [1], 'same value')
isNot(clone(data).s, data.s, 'different object')
})
}
|