Skip to content

Commit

Permalink
chore: swap test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Jan 27, 2021
1 parent e3a48de commit 17476b9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
.DS_Store
*-lock.*
*.lock
*.log
dist
3 changes: 0 additions & 3 deletions .travis.yml

This file was deleted.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"author": {
"name": "Luke Edwards",
"email": "[email protected]",
"url": "lukeed.com"
"url": "https://lukeed.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"build": "bundt",
"pretest": "npm run build",
"test": "tape test/*.js | tap-spec"
"test": "uvu -r esm test"
},
"files": [
"*.d.ts",
Expand All @@ -36,8 +36,8 @@
"set"
],
"devDependencies": {
"bundt": "^0.3.0",
"tap-spec": "^4.1.1",
"tape": "^4.8.0"
"bundt": "1.1.2",
"esm": "3.2.25",
"uvu": "0.5.1"
}
}
86 changes: 44 additions & 42 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,74 @@
const test = require('tape');
const fn = require('../dist/dset');
// @ts-check
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import dset from '../dist/dset';

test('dset', t => {
t.is(typeof fn, 'function', 'exports a function');
test('dset', () => {
assert.type(dset, 'function', 'exports a function');

let foo = { a:1, b:2 };
let out = fn(foo, 'c', 3); // add c
t.is(out, undefined, 'does not return output');
t.same(foo, { a:1, b:2, c:3 }, 'mutates; adds simple key:val');
let out = dset(foo, 'c', 3); // add c
assert.is(out, undefined, 'does not return output');
assert.equal(foo, { a:1, b:2, c:3 }, 'mutates; adds simple key:val');

foo = {};
fn(foo, 'a.b.c', 999); // add deep
t.same(foo, { a:{ b:{ c:999 } } }, 'mutates; adds deeply nested key:val');
dset(foo, 'a.b.c', 999); // add deep
assert.equal(foo, { a:{ b:{ c:999 } } }, 'mutates; adds deeply nested key:val');

foo = {};
fn(foo, ['a', 'b', 'c'], 123); // change via array
t.same(foo, { a:{ b:{ c:123 } } }, 'mutates; changes the value via array-type keys');
dset(foo, ['a', 'b', 'c'], 123); // change via array
assert.equal(foo, { a:{ b:{ c:123 } } }, 'mutates; changes the value via array-type keys');

foo = { a:1 };
fn(foo, 'e.0.0', 2); // create arrays instead of objects
t.is(foo.e[0][0], 2, 'mutates; can create arrays when key is numeric');
t.same(foo, { a: 1, e:[[2]] });
t.true(Array.isArray(foo.e));
dset(foo, 'e.0.0', 2); // create arrays instead of objects
assert.is(foo.e[0][0], 2, 'mutates; can create arrays when key is numeric');
assert.equal(foo, { a: 1, e:[[2]] });
assert.instance(foo.e, Array);

foo = { a:{ b:{ c:123 } } };
fn(foo, 'a.b.x.y', 456); // preserve existing structure
t.same(foo, { a:{ b:{ c:123, x:{ y:456 } }} }, 'mutates; writes into/preserves existing object');
dset(foo, 'a.b.x.y', 456); // preserve existing structure
assert.equal(foo, { a:{ b:{ c:123, x:{ y:456 } }} }, 'mutates; writes into/preserves existing object');

foo = { a: { b:123 } };
fn(foo, 'a.b.c', 'hello'); // preserve non-object value, won't alter
t.is(foo.a.b, 123, 'refuses to convert existing non-object value into object');
t.same(foo, { a: { b:123 }});
dset(foo, 'a.b.c', 'hello'); // preserve non-object value, won't alter
assert.is(foo.a.b, 123, 'refuses to convert existing non-object value into object');
assert.equal(foo, { a: { b:123 }});

foo = { a:{ b:{ c:123, d:{ e:5 } } } };
fn(foo, 'a.b.d.z', [1,2,3,4]); // preserve object tree, with array value
t.same(foo.a.b.d, { e:5, z:[1,2,3,4] }, 'mutates; writes into existing object w/ array value');
dset(foo, 'a.b.d.z', [1,2,3,4]); // preserve object tree, with array value
assert.equal(foo.a.b.d, { e:5, z:[1,2,3,4] }, 'mutates; writes into existing object w/ array value');

foo = { b:123 };
t.doesNotThrow(_ => fn(foo, 'b.c.d.e', 123), undefined, 'silently preserves existing non-null value');
t.is(foo.b, 123, 'preserves existing value');
assert.not.throws(_ => dset(foo, 'b.c.d.e', 123), 'silently preserves existing non-null value');
assert.is(foo.b, 123, 'preserves existing value');

foo = { b:0 };
t.doesNotThrow(_ => fn(foo, 'b.a.s.d', 123), undefined, 'silently preserves `0` as existing non-null value');
t.same(foo, { b:0 }, 'preserves existing object values');
assert.not.throws(_ => dset(foo, 'b.a.s.d', 123), 'silently preserves `0` as existing non-null value');
assert.equal(foo, { b:0 }, 'preserves existing object values');

foo = {};
fn(foo, ['x', 'y', 'z'], 123);
t.same(foo, { x:{ y:{ z:123 } } });
dset(foo, ['x', 'y', 'z'], 123);
assert.equal(foo, { x:{ y:{ z:123 } } });

foo = {};
fn(foo, ['x', '0', 'z'], 123);
t.same(foo, { x:[{ z:123 }] });
t.true(Array.isArray(foo.x));
dset(foo, ['x', '0', 'z'], 123);
assert.equal(foo, { x:[{ z:123 }] });
assert.instance(foo.x, Array);

foo = {};
fn(foo, ['x', '1', 'z'], 123);
t.same(foo, { x:[,{ z:123 }] });
t.true(Array.isArray(foo.x));
dset(foo, ['x', '1', 'z'], 123);
assert.equal(foo, { x:[,{ z:123 }] });
assert.instance(foo.x, Array);

foo = {};
fn(foo, ['x', '10.0', 'z'], 123);
t.same(foo, { x:{ '10.0':{ z:123 } } });
t.false(Array.isArray(foo.x));
dset(foo, ['x', '10.0', 'z'], 123);
assert.equal(foo, { x:{ '10.0':{ z:123 } } });
assert.not.instance(foo.x, Array);

foo = {};
fn(foo, ['x', '10.2', 'z'], 123);
t.same(foo, { x:{ '10.2':{ z:123 } } });
t.false(Array.isArray(foo.x));

t.end();
dset(foo, ['x', '10.2', 'z'], 123);
assert.equal(foo, { x:{ '10.2':{ z:123 } } });
assert.not.instance(foo.x, Array);
});

test.run();

0 comments on commit 17476b9

Please sign in to comment.