import tests from perl code

This commit is contained in:
Doug
2016-08-01 01:16:22 -04:00
parent f8390507ad
commit 7f7c72b4bb
4 changed files with 159 additions and 8 deletions

3
.babelrc Normal file
View File

@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
node_modules/
lib/

View File

@@ -2,19 +2,18 @@
"name": "update-immutable",
"version": "1.0.0",
"description": "React-compatible data-structure update utility",
"main": "update.js",
"main": "lib/update",
"scripts": {
"test": "npm run build && mocha --ui tdd",
"build": "node node_modules/babel-cli/bin/babel.js update.js --out-dir lib",
"test": "nodeunit test.js"
},
"author": "Doug Hoyte",
"license": "BSD-2-Clause",
"devDependencies": {
"mocha": "2.1.0",
"better-assert": "1.0.1",
"deep-equal": "1.0.0",
"babel-cli": "6.4.5",
"babel-preset-es2015": "^6.9.0",
"nodeunit": "^0.9.1"
},
"repository": {
"type": "git",
"url": "git://github.com/hoytech/update-immutable.git"
@@ -22,7 +21,7 @@
"keywords": [
"update",
"immutable",
"react",
"react"
],
"homepage": "https://github.com/hoytech/update-immutable"
}

147
test.js Normal file
View File

@@ -0,0 +1,147 @@
var update = require("./lib/update").default;
function clone(a) {
return JSON.parse(JSON.stringify(a));
}
function apply_update(test, desc, input, update_v, expected) {
var orig = clone(input);
var output = update(input, update_v);
test.deepEqual(output, expected, "update applied correctly: " + desc);
test.ok(input != output, "new structure created: " + desc);
test.deepEqual(input, orig, "original not modified: " + desc);
}
exports.testUpdate = function(test){
// set
apply_update(test,
"simple set",
{},
{ a: { $set: 1} },
{ a: 1 }
);
apply_update(test,
"nested set",
{ a: { b: 1 }, c: 2 },
{ a: { b: { '$set': 5 } } },
{ a: { b: 5 }, c: 2 }
);
apply_update(test,
"set, auto-vivify",
{ c: 2 },
{ a: { b: { '$set': 5 } } },
{ a: { b: 5 }, c: 2 }
);
// unset
apply_update(test,
"unset",
{ a: { b: 1, z: 2 }, c: 2 },
{ a: { '$unset': 'b' } },
{ a: { z: 2 }, c: 2 }
);
apply_update(test,
"unset auto-vivify",
{},
{ a: { '$unset': 'b' } },
{ a: {} }
);
// merge
apply_update(test,
"merge",
{ a: 1, b: 2 },
{ '$merge': { c: 3, d: { e: 4 } } },
{ a: 1, b: 2, c: 3, d: { e: 4 } }
);
apply_update(test,
"merge overwrites",
{ a: 1, b: 2, c: 9 },
{ '$merge': { c: 3, d: { e: 4 } } },
{ a: 1, b: 2, c: 3, d: { e: 4 } }
);
apply_update(test,
"merge auto-vivify",
{},
{ a: { b: { '$merge': { c: 1 } } } },
{ a: { b: { c: 1 } } }
);
// push
apply_update(test,
"push",
{ a: [ 0, ], },
{ a: { '$push': [ 1, 2 ] } },
{ a: [ 0, 1, 2 ] }
);
apply_update(test,
"push auto-vivify",
{},
{ a: { '$push': [ 1, 2 ] } },
{ a: [ 1, 2 ] }
);
// unshift
apply_update(test,
"unshift",
{ a: [ 0 ] },
{ a: { '$unshift': [ 1, 2 ] } },
{ a: [ 1, 2, 0 ] }
);
apply_update(test,
"unshift auto-vivify",
{},
{ a: { '$unshift': [ 1, 2 ] } },
{ a: [ 1, 2 ] }
);
// splice
apply_update(test,
"splice add",
{ a: [ 0, 1 ], },
{ a: { '$splice': [ [ 1, 0, 8, 9 ] ] } },
{ a: [ 0, 8, 9, 1 ] }
);
apply_update(test,
"splice del",
{ a: [ 0, 1, 2 ] },
{ a: { '$splice': [ [ 1, 1, 8, 9 ] ] } },
{ a: [ 0, 8, 9, 2 ] }
);
apply_update(test,
"splice multi",
{ a: [ 0, 1, 2 ] },
{ a: { '$splice': [ [ 1, 1, 8, 9 ], [ 0, 2, 6, {a: 1} ] ] } },
{ a: [ 6, { a: 1 }, 9, 2 ] }
);
apply_update(test,
"splice auto-vivify",
{},
{ a: { '$splice': [ [ 0, 0, 8, 9 ] ] } },
{ a: [ 8, 9 ] }
);
test.done();
};