|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 | 3 | var test = require('tape')
|
| 4 | +var fc = require('fast-check') |
| 5 | +var _ = require('lodash') |
4 | 6 | var is = require('.')
|
5 | 7 |
|
6 | 8 | test('unist-util-is', function (t) {
|
@@ -131,3 +133,66 @@ test('unist-util-is', function (t) {
|
131 | 133 |
|
132 | 134 | t.end()
|
133 | 135 | })
|
| 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