From 1f8ba5e46ed91212b316fb259f66569665463883 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sat, 13 Feb 2021 17:16:08 -0700 Subject: [PATCH 01/12] test: add property tests --- package.json | 3 ++- test.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 6d2c16d..23bc91f 100644 --- a/package.json +++ b/package.json @@ -34,11 +34,12 @@ "convert.d.ts" ], "types": "index.d.ts", - "dependencies": {}, "devDependencies": { "@types/mdast": "^3.0.0", "browserify": "^17.0.0", "dtslint": "^4.0.0", + "fast-check": "^2.0.0", + "lodash": "^4.0.0", "nyc": "^15.0.0", "prettier": "^2.0.0", "remark-cli": "^9.0.0", diff --git a/test.js b/test.js index ab6b275..ff90adc 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,8 @@ 'use strict' var test = require('tape') +var fc = require('fast-check') +var _ = require('lodash') var is = require('.') test('unist-util-is', function (t) { @@ -131,3 +133,66 @@ test('unist-util-is', function (t) { t.end() }) + +test('unist-util-is properties', function (t) { + t.plan(4) + t.doesNotThrow(function () { + fc.assert( + fc.property( + fc.record({type: fc.string({minLength: 1})}), + function (node) { + return is(node) + } + ) + ) + }, 'should check if given node (#1)') + + t.doesNotThrow(function () { + fc.assert( + fc.property( + fc.dictionary(fc.string(), fc.string()).filter(function (node) { + return typeof node.type === 'undefined' + }), + function (node) { + return !is(node) + } + ) + ) + }, 'should check if given node (#2)') + + t.doesNotThrow(function () { + fc.assert( + fc.property( + fc.record({type: fc.string({minLength: 1})}), + function (node) { + return is(node, node.type) + } + ) + ) + }, 'should match types') + + t.doesNotThrow(function () { + fc.assert( + fc.property( + fc + // generate an object with primitive values + .dictionary( + fc.string(), + fc.oneof(fc.string(), fc.integer(), fc.bigInt(), fc.boolean()) + ) + // object must have some keys + .filter(function (node) { return _.keys(node).length > 1}) + // return node and a list with a random subset of it's keys + .chain(function (node) { + return fc.tuple(fc.constant(node), fc.subarray(_.keys(node), {minLength: 1})) + }), + fc.string({minLength: 1}), + function (nodeAndKeys, type) { + var node = nodeAndKeys[0] + var keys = nodeAndKeys[1] + return is(_.assign(node, {type: type}), _.pick(_.cloneDeep(node), keys)) + } + ) + ) + }, 'should match partially') +}) From a9a7ee14d3f9b76d1d66fa88a297f006fce280b5 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sun, 14 Feb 2021 07:12:57 -0700 Subject: [PATCH 02/12] test: split example and property tests --- package.json | 2 +- test.js => test/example.js | 69 ++------------------------------------ test/index.js | 2 ++ test/property.js | 69 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 68 deletions(-) rename test.js => test/example.js (61%) create mode 100644 test/index.js create mode 100644 test/property.js diff --git a/package.json b/package.json index 23bc91f..87d1eae 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "build-mangle": "browserify . -s unistUtilIs -o unist-util-is.min.js -p tinyify", "build": "npm run build-bundle && npm run build-mangle", "test-api": "node test", - "test-coverage": "nyc --reporter lcov tape test.js", + "test-coverage": "nyc --reporter lcov tape test", "test-types": "dtslint .", "test": "npm run format && npm run build && npm run test-coverage && npm run test-types" }, diff --git a/test.js b/test/example.js similarity index 61% rename from test.js rename to test/example.js index ff90adc..3fec9a7 100644 --- a/test.js +++ b/test/example.js @@ -1,11 +1,9 @@ 'use strict' var test = require('tape') -var fc = require('fast-check') -var _ = require('lodash') -var is = require('.') +var is = require('..') -test('unist-util-is', function (t) { +test('unist-util-is example', function (t) { var node = {type: 'strong'} var parent = {type: 'paragraph', children: []} @@ -133,66 +131,3 @@ test('unist-util-is', function (t) { t.end() }) - -test('unist-util-is properties', function (t) { - t.plan(4) - t.doesNotThrow(function () { - fc.assert( - fc.property( - fc.record({type: fc.string({minLength: 1})}), - function (node) { - return is(node) - } - ) - ) - }, 'should check if given node (#1)') - - t.doesNotThrow(function () { - fc.assert( - fc.property( - fc.dictionary(fc.string(), fc.string()).filter(function (node) { - return typeof node.type === 'undefined' - }), - function (node) { - return !is(node) - } - ) - ) - }, 'should check if given node (#2)') - - t.doesNotThrow(function () { - fc.assert( - fc.property( - fc.record({type: fc.string({minLength: 1})}), - function (node) { - return is(node, node.type) - } - ) - ) - }, 'should match types') - - t.doesNotThrow(function () { - fc.assert( - fc.property( - fc - // generate an object with primitive values - .dictionary( - fc.string(), - fc.oneof(fc.string(), fc.integer(), fc.bigInt(), fc.boolean()) - ) - // object must have some keys - .filter(function (node) { return _.keys(node).length > 1}) - // return node and a list with a random subset of it's keys - .chain(function (node) { - return fc.tuple(fc.constant(node), fc.subarray(_.keys(node), {minLength: 1})) - }), - fc.string({minLength: 1}), - function (nodeAndKeys, type) { - var node = nodeAndKeys[0] - var keys = nodeAndKeys[1] - return is(_.assign(node, {type: type}), _.pick(_.cloneDeep(node), keys)) - } - ) - ) - }, 'should match partially') -}) diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..eecff3f --- /dev/null +++ b/test/index.js @@ -0,0 +1,2 @@ +require("./example") +require("./property") \ No newline at end of file diff --git a/test/property.js b/test/property.js new file mode 100644 index 0000000..1fe8345 --- /dev/null +++ b/test/property.js @@ -0,0 +1,69 @@ +'use strict' + +var test = require('tape') +var fc = require('fast-check') +var _ = require('lodash') +var is = require('..') + +test('unist-util-is properties', function (t) { + t.plan(4) + t.doesNotThrow(function () { + fc.assert( + fc.property( + fc.record({type: fc.string({minLength: 1})}), + function (node) { + return is(node) + } + ) + ) + }, 'should check if given node (#1)') + + t.doesNotThrow(function () { + fc.assert( + fc.property( + fc.dictionary(fc.string(), fc.string()).filter(function (node) { + return typeof node.type === 'undefined' + }), + function (node) { + return !is(node) + } + ) + ) + }, 'should check if given node (#2)') + + t.doesNotThrow(function () { + fc.assert( + fc.property( + fc.record({type: fc.string({minLength: 1})}), + function (node) { + return is(node, node.type) + } + ) + ) + }, 'should match types') + + t.doesNotThrow(function () { + fc.assert( + fc.property( + fc + // generate an object with primitive values + .dictionary( + fc.string(), + fc.oneof(fc.string(), fc.integer(), fc.bigInt(), fc.boolean()) + ) + // object must have some keys + .filter(function (node) { return _.keys(node).length > 1}) + // return node and a list with a random subset of it's keys + .chain(function (node) { + return fc.tuple(fc.constant(node), fc.subarray(_.keys(node), {minLength: 1})) + }), + fc.string({minLength: 1}), + function (nodeAndKeys, type) { + var node = nodeAndKeys[0] + var keys = nodeAndKeys[1] + return is(_.assign(node, {type: type}), _.pick(_.cloneDeep(node), keys)) + } + ) + ) + }, 'should match partially') +}) From 7ae6376c41f4b4ccd95b477280f8727391c2e39b Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sun, 14 Feb 2021 07:17:40 -0700 Subject: [PATCH 03/12] test: remove big int, expand subset test for clarity, add eslint ignore --- test/index.js | 5 +++-- test/property.js | 24 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/test/index.js b/test/index.js index eecff3f..c09dcfc 100644 --- a/test/index.js +++ b/test/index.js @@ -1,2 +1,3 @@ -require("./example") -require("./property") \ No newline at end of file +/* eslint-disable import/no-unassigned-import */ +require('./example') +require('./property') diff --git a/test/property.js b/test/property.js index 1fe8345..8ba0152 100644 --- a/test/property.js +++ b/test/property.js @@ -46,22 +46,30 @@ test('unist-util-is properties', function (t) { fc.assert( fc.property( fc - // generate an object with primitive values + // Generate an object with primitive values .dictionary( fc.string(), - fc.oneof(fc.string(), fc.integer(), fc.bigInt(), fc.boolean()) + fc.oneof(fc.string(), fc.integer(), fc.boolean()) ) - // object must have some keys - .filter(function (node) { return _.keys(node).length > 1}) - // return node and a list with a random subset of it's keys + // Object must have some keys + .filter(function (node) { + return _.keys(node).length > 1 + }) + // Return node and a list with a random subset of it's keys .chain(function (node) { - return fc.tuple(fc.constant(node), fc.subarray(_.keys(node), {minLength: 1})) + return fc.tuple( + fc.constant(node), + fc.subarray(_.keys(node), {minLength: 1}) + ) }), fc.string({minLength: 1}), function (nodeAndKeys, type) { - var node = nodeAndKeys[0] + var nodeProperties = nodeAndKeys[0] var keys = nodeAndKeys[1] - return is(_.assign(node, {type: type}), _.pick(_.cloneDeep(node), keys)) + + var node = _.assign(nodeProperties, {type: type}) + var subsetOfNode = _.pick(_.cloneDeep(node), keys) + return is(node, subsetOfNode) } ) ) From 62b6329fd3669a7c9ba2bd170e264fdb9e26d47f Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sun, 14 Feb 2021 07:31:52 -0700 Subject: [PATCH 04/12] test: use unicode JSON arbitrary for clarity --- test/property.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/test/property.js b/test/property.js index 8ba0152..108c5aa 100644 --- a/test/property.js +++ b/test/property.js @@ -46,20 +46,28 @@ test('unist-util-is properties', function (t) { fc.assert( fc.property( fc - // Generate an object with primitive values - .dictionary( - fc.string(), - fc.oneof(fc.string(), fc.integer(), fc.boolean()) - ) - // Object must have some keys + .unicodeJsonObject() + // Filter for JSON objects which unist can work with .filter(function (node) { - return _.keys(node).length > 1 + return ( + // json needs to be a plain object + _.isPlainObject(node) && + // also needs to have some keys with primitive values + _.some(_.keys(node), function (key) { + return !_.isObject(node[key]) + }) + ) }) - // Return node and a list with a random subset of it's keys + // Return node and a list with a random subset of it's primitive value keys .chain(function (node) { return fc.tuple( fc.constant(node), - fc.subarray(_.keys(node), {minLength: 1}) + fc.subarray( + _.keys(node).filter(function (key) { + return !_.isObject(node[key]) + }), + {minLength: 1} + ) ) }), fc.string({minLength: 1}), From 98dc49e6412aac7730eacc6a9902561f05ef5481 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sun, 14 Feb 2021 07:38:53 -0700 Subject: [PATCH 05/12] test: refactor to remove .filter and use lodash equivalent --- test/property.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/property.js b/test/property.js index 108c5aa..54d863b 100644 --- a/test/property.js +++ b/test/property.js @@ -50,9 +50,9 @@ test('unist-util-is properties', function (t) { // Filter for JSON objects which unist can work with .filter(function (node) { return ( - // json needs to be a plain object + // JSON needs to be a plain object _.isPlainObject(node) && - // also needs to have some keys with primitive values + // Also needs to have some keys with primitive values _.some(_.keys(node), function (key) { return !_.isObject(node[key]) }) @@ -63,7 +63,7 @@ test('unist-util-is properties', function (t) { return fc.tuple( fc.constant(node), fc.subarray( - _.keys(node).filter(function (key) { + _.filter(_.keys(node), function (key) { return !_.isObject(node[key]) }), {minLength: 1} From 47988946be8c7bb7de79773d2a9975ef3e0540a5 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sun, 14 Feb 2021 10:00:14 -0700 Subject: [PATCH 06/12] test: use es2017 in property tests --- test/example.js | 2 +- test/property.js | 141 ++++++++++++++++++++++------------------------- 2 files changed, 68 insertions(+), 75 deletions(-) diff --git a/test/example.js b/test/example.js index 3fec9a7..8795464 100644 --- a/test/example.js +++ b/test/example.js @@ -3,7 +3,7 @@ var test = require('tape') var is = require('..') -test('unist-util-is example', function (t) { +test('unist-util-is examples', function (t) { var node = {type: 'strong'} var parent = {type: 'paragraph', children: []} diff --git a/test/property.js b/test/property.js index 54d863b..360914e 100644 --- a/test/property.js +++ b/test/property.js @@ -1,85 +1,78 @@ 'use strict' -var test = require('tape') -var fc = require('fast-check') -var _ = require('lodash') -var is = require('..') +const test = require('tape') +const fc = require('fast-check') +const {isObject, isPlainObject, pick, cloneDeep} = require('lodash') +const is = require('..') -test('unist-util-is properties', function (t) { +test('unist-util-is properties', (t) => { t.plan(4) - t.doesNotThrow(function () { - fc.assert( - fc.property( - fc.record({type: fc.string({minLength: 1})}), - function (node) { - return is(node) - } - ) - ) - }, 'should check if given node (#1)') + t.doesNotThrow( + () => + fc.assert( + fc.property(fc.record({type: fc.string({minLength: 1})}), (node) => + is(node) + ) + ), + 'should check if given node (#1)' + ) - t.doesNotThrow(function () { - fc.assert( - fc.property( - fc.dictionary(fc.string(), fc.string()).filter(function (node) { - return typeof node.type === 'undefined' - }), - function (node) { - return !is(node) - } - ) - ) - }, 'should check if given node (#2)') + t.doesNotThrow( + () => + fc.assert( + fc.property( + fc + .dictionary(fc.string(), fc.string()) + .filter((node) => typeof node.type === 'undefined'), + (node) => !is(node) + ) + ), + 'should check if given node (#2)' + ) - t.doesNotThrow(function () { - fc.assert( - fc.property( - fc.record({type: fc.string({minLength: 1})}), - function (node) { - return is(node, node.type) - } - ) - ) - }, 'should match types') + t.doesNotThrow( + () => + fc.assert( + fc.property(fc.record({type: fc.string({minLength: 1})}), (node) => + is(node, node.type) + ) + ), + 'should match types' + ) - t.doesNotThrow(function () { - fc.assert( - fc.property( - fc - .unicodeJsonObject() - // Filter for JSON objects which unist can work with - .filter(function (node) { - return ( - // JSON needs to be a plain object - _.isPlainObject(node) && - // Also needs to have some keys with primitive values - _.some(_.keys(node), function (key) { - return !_.isObject(node[key]) - }) + t.doesNotThrow( + () => + fc.assert( + fc.property( + fc + .unicodeJsonObject() + // Filter for JSON objects which unist can work with + .filter( + (node) => + isPlainObject(node) && + Object.keys(node).some((key) => !isObject(node[key])) ) - }) - // Return node and a list with a random subset of it's primitive value keys - .chain(function (node) { - return fc.tuple( - fc.constant(node), - fc.subarray( - _.filter(_.keys(node), function (key) { - return !_.isObject(node[key]) - }), - {minLength: 1} + // Return node and a list with a random subset of it's primitive value keys + .chain((node) => + fc.tuple( + fc.constant(node), + fc.subarray( + Object.keys(node).filter((key) => !isObject(node[key])), + {minLength: 1} + ) ) - ) - }), - fc.string({minLength: 1}), - function (nodeAndKeys, type) { - var nodeProperties = nodeAndKeys[0] - var keys = nodeAndKeys[1] + ), + fc.string({minLength: 1}), + (nodeAndKeys, type) => { + const nodeProperties = nodeAndKeys[0] + const keys = nodeAndKeys[1] - var node = _.assign(nodeProperties, {type: type}) - var subsetOfNode = _.pick(_.cloneDeep(node), keys) - return is(node, subsetOfNode) - } - ) - ) - }, 'should match partially') + const node = {...nodeProperties, type: type} + const subsetOfNode = pick(cloneDeep(node), keys) + return is(node, subsetOfNode) + } + ) + ), + 'should match partially' + ) }) From 7170eb9d35e8b08dbaae297eae9c320665b7fa05 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sun, 14 Feb 2021 10:15:51 -0700 Subject: [PATCH 07/12] docs: update test comment Co-authored-by: Titus --- test/property.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/property.js b/test/property.js index 360914e..42c7683 100644 --- a/test/property.js +++ b/test/property.js @@ -52,7 +52,7 @@ test('unist-util-is properties', (t) => { isPlainObject(node) && Object.keys(node).some((key) => !isObject(node[key])) ) - // Return node and a list with a random subset of it's primitive value keys + // Return node and a list with a random subset of its primitive value keys .chain((node) => fc.tuple( fc.constant(node), From f64f8dec8b022e8885c8d3059fbb236114a4209e Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sun, 14 Feb 2021 10:27:43 -0700 Subject: [PATCH 08/12] docs: update test description Co-authored-by: Titus --- test/property.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/property.js b/test/property.js index 42c7683..c6ec888 100644 --- a/test/property.js +++ b/test/property.js @@ -27,7 +27,7 @@ test('unist-util-is properties', (t) => { (node) => !is(node) ) ), - 'should check if given node (#2)' + 'should see any object w/o a `type` as a non-node' ) t.doesNotThrow( From 80bd714242547041d331d8a18976c1fc87177043 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sun, 14 Feb 2021 10:27:51 -0700 Subject: [PATCH 09/12] docs: update test description Co-authored-by: Titus --- test/property.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/property.js b/test/property.js index c6ec888..8ac9ae3 100644 --- a/test/property.js +++ b/test/property.js @@ -14,7 +14,7 @@ test('unist-util-is properties', (t) => { is(node) ) ), - 'should check if given node (#1)' + 'should see any object w/ a non-empty `type` as a node' ) t.doesNotThrow( From b525b9ecaeb6df1dbea4e6d9523ae64dcafe0c98 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sun, 14 Feb 2021 10:39:18 -0700 Subject: [PATCH 10/12] test: generate unicode json objects for not a node test --- test/property.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/property.js b/test/property.js index 8ac9ae3..c1bc69a 100644 --- a/test/property.js +++ b/test/property.js @@ -22,8 +22,10 @@ test('unist-util-is properties', (t) => { fc.assert( fc.property( fc - .dictionary(fc.string(), fc.string()) - .filter((node) => typeof node.type === 'undefined'), + .unicodeJsonObject() + .filter( + (node) => !(isPlainObject(node) && typeof node.type === 'string') + ), (node) => !is(node) ) ), From e10c23cddc0a4b6899c9137e862ad87d3a7a1ed9 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sun, 14 Feb 2021 10:43:55 -0700 Subject: [PATCH 11/12] test: rename example tests to main --- test/index.js | 2 +- test/{example.js => main.js} | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename test/{example.js => main.js} (97%) diff --git a/test/index.js b/test/index.js index c09dcfc..e68ff32 100644 --- a/test/index.js +++ b/test/index.js @@ -1,3 +1,3 @@ /* eslint-disable import/no-unassigned-import */ -require('./example') +require('./main') require('./property') diff --git a/test/example.js b/test/main.js similarity index 97% rename from test/example.js rename to test/main.js index 8795464..b6c8025 100644 --- a/test/example.js +++ b/test/main.js @@ -1,9 +1,9 @@ 'use strict' var test = require('tape') -var is = require('..') +var is = require('unist-util-is') -test('unist-util-is examples', function (t) { +test('unist-util-is', function (t) { var node = {type: 'strong'} var parent = {type: 'paragraph', children: []} From e471480109b8c93103d60a049a2bb99b1c66aad0 Mon Sep 17 00:00:00 2001 From: Christian Murphy Date: Sun, 14 Feb 2021 10:53:12 -0700 Subject: [PATCH 12/12] test: fix auto adjusted import path --- test/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/main.js b/test/main.js index b6c8025..d23baf4 100644 --- a/test/main.js +++ b/test/main.js @@ -1,7 +1,7 @@ 'use strict' var test = require('tape') -var is = require('unist-util-is') +var is = require('..') test('unist-util-is', function (t) { var node = {type: 'strong'}