Skip to content

Commit 1f8ba5e

Browse files
test: add property tests
1 parent 18bbf90 commit 1f8ba5e

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
"convert.d.ts"
3535
],
3636
"types": "index.d.ts",
37-
"dependencies": {},
3837
"devDependencies": {
3938
"@types/mdast": "^3.0.0",
4039
"browserify": "^17.0.0",
4140
"dtslint": "^4.0.0",
41+
"fast-check": "^2.0.0",
42+
"lodash": "^4.0.0",
4243
"nyc": "^15.0.0",
4344
"prettier": "^2.0.0",
4445
"remark-cli": "^9.0.0",

test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
22

33
var test = require('tape')
4+
var fc = require('fast-check')
5+
var _ = require('lodash')
46
var is = require('.')
57

68
test('unist-util-is', function (t) {
@@ -131,3 +133,66 @@ test('unist-util-is', function (t) {
131133

132134
t.end()
133135
})
136+
137+
test('unist-util-is properties', function (t) {
138+
t.plan(4)
139+
t.doesNotThrow(function () {
140+
fc.assert(
141+
fc.property(
142+
fc.record({type: fc.string({minLength: 1})}),
143+
function (node) {
144+
return is(node)
145+
}
146+
)
147+
)
148+
}, 'should check if given node (#1)')
149+
150+
t.doesNotThrow(function () {
151+
fc.assert(
152+
fc.property(
153+
fc.dictionary(fc.string(), fc.string()).filter(function (node) {
154+
return typeof node.type === 'undefined'
155+
}),
156+
function (node) {
157+
return !is(node)
158+
}
159+
)
160+
)
161+
}, 'should check if given node (#2)')
162+
163+
t.doesNotThrow(function () {
164+
fc.assert(
165+
fc.property(
166+
fc.record({type: fc.string({minLength: 1})}),
167+
function (node) {
168+
return is(node, node.type)
169+
}
170+
)
171+
)
172+
}, 'should match types')
173+
174+
t.doesNotThrow(function () {
175+
fc.assert(
176+
fc.property(
177+
fc
178+
// generate an object with primitive values
179+
.dictionary(
180+
fc.string(),
181+
fc.oneof(fc.string(), fc.integer(), fc.bigInt(), fc.boolean())
182+
)
183+
// object must have some keys
184+
.filter(function (node) { return _.keys(node).length > 1})
185+
// return node and a list with a random subset of it's keys
186+
.chain(function (node) {
187+
return fc.tuple(fc.constant(node), fc.subarray(_.keys(node), {minLength: 1}))
188+
}),
189+
fc.string({minLength: 1}),
190+
function (nodeAndKeys, type) {
191+
var node = nodeAndKeys[0]
192+
var keys = nodeAndKeys[1]
193+
return is(_.assign(node, {type: type}), _.pick(_.cloneDeep(node), keys))
194+
}
195+
)
196+
)
197+
}, 'should match partially')
198+
})

0 commit comments

Comments
 (0)