Permalink
Browse files

Improve tests

  • Loading branch information...
simov committed Aug 22, 2017
1 parent 7791541 commit 14b63bd6b83ba4131962365071661b603019151c
Showing with 95 additions and 55 deletions.
  1. +95 −55 test/index.js
View
@@ -3,74 +3,114 @@ var t = require('assert')
var dcopy = require('../')
describe('deep-copy', function () {
var obj = {
a: 0,
b: [0,1,2],
c: {
a: 0,
b: 1,
},
d: function () {},
e: new Date(2017, 0, 1)
}
var arr = [
0,
1,
{
a: 0,
b: {
c: 1
}
},
new Date(2017, 0, 1)
]
it('deep copy object', function () {
var copy = dcopy(obj)
copy.c = {c: 15}
t.deepEqual(obj.c, {a: 0, b: 1})
t.deepEqual(copy.c, {c: 15})
describe('preserve', function () {
it('number', function () {
var copy = dcopy(42)
t.deepStrictEqual(copy, 42)
})
it('deep copy array', function () {
var copy = dcopy(arr)
copy[2].b = {d: 15}
t.deepEqual(arr[2].b, {c: 1})
t.deepEqual(copy[2].b, {d: 15})
it('string', function () {
var copy = dcopy('a')
t.deepStrictEqual(copy, 'a')
})
it('deep copy date', function () {
var copy = dcopy(obj)
t.deepEqual(copy.e.getTime(), new Date(2017, 0, 1).getTime())
it('boolean', function () {
var copy = dcopy(true)
t.deepStrictEqual(copy, true)
})
})
it('preserve numbers', function () {
var copy = dcopy(42)
t.deepEqual(copy, 42)
describe('copy', function () {
it('object', function () {
var target = {a: 0}
var copy = dcopy(target)
t.deepStrictEqual(target, copy)
copy.a = 1
t.notDeepStrictEqual(target, copy)
t.deepStrictEqual(target, {a: 0})
t.deepStrictEqual(copy, {a: 1})
})
it('preserve strings', function () {
var copy = dcopy('a')
t.deepEqual(copy, 'a')
it('array', function () {
var target = [0]
var copy = dcopy(target)
t.deepStrictEqual(target, copy)
copy[0] = 1
t.notDeepStrictEqual(target, copy)
t.deepStrictEqual(target, [0])
t.deepStrictEqual(copy, [1])
})
it('preserve booleans', function () {
var copy = dcopy(true)
t.deepEqual(copy, true)
it('date', function () {
var target = new Date(2017, 0, 1)
var copy = dcopy(target)
t.deepStrictEqual(target, copy)
copy.setMonth(5)
t.notDeepStrictEqual(target, copy)
t.deepStrictEqual(target.getMonth(), 0)
t.deepStrictEqual(copy.getMonth(), 5)
})
})
it('preserve dates', function () {
var date = new Date(2017, 5, 1)
var copy = dcopy(date)
date.setMonth(0)
t.notDeepEqual(date.getTime(), new Date(2017, 5, 1).getTime())
t.deepEqual(copy.getTime(), new Date(2017, 5, 1).getTime())
describe('deep copy', function () {
it('object', function () {
var target = {
object: {a: 0}
}
var copy = dcopy(target)
t.deepStrictEqual(target, copy)
copy.object.a = 1
t.notDeepStrictEqual(target, copy)
t.deepStrictEqual(target, {object: {a: 0}})
t.deepStrictEqual(copy, {object: {a: 1}})
})
it('array', function () {
var target = {
array: [0]
}
var copy = dcopy(target)
t.deepStrictEqual(target, copy)
copy.array[0] = 1
t.notDeepStrictEqual(target, copy)
t.deepStrictEqual(target, {array: [0]})
t.deepStrictEqual(copy, {array: [1]})
})
it('preserve functions', function () {
var copy = dcopy(obj)
t.ok(typeof obj.d === 'function')
t.ok(typeof copy.d === 'function')
it('date', function () {
var target = {
date: new Date(2017, 0, 1)
}
var copy = dcopy(target)
t.deepStrictEqual(target, copy)
copy.date.setMonth(5)
t.notDeepStrictEqual(target, copy)
t.deepStrictEqual(target.date.getMonth(), 0)
t.deepStrictEqual(copy.date.getMonth(), 5)
})
})
describe('deep preserve', function () {
it('function', function () {
var target = {
func: function () {}
}
var copy = dcopy(target)
t.deepStrictEqual(target, copy)
t.deepStrictEqual(typeof copy.func, 'function')
})
})

0 comments on commit 14b63bd

Please sign in to comment.