forked from ipld/js-ipld-garbage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
52 lines (48 loc) · 1.25 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { garbage } from 'ipld-garbage'
import is from '@sindresorhus/is'
const expectedTypes = ['null', 'boolean', 'int', 'float', 'string', 'Uint8Array', 'Array', 'Object', 'CID']
/** @type {Record<string, number>} */
const types = {}
/**
* @param {any} obj
* @returns {string}
*/
const account = (obj) => {
/** @type {string} */
let type = is(obj)
if (type === 'Object' && obj.asCID === obj) {
type = 'CID'
} else if (type === 'number') {
type = Number.isInteger(obj) ? 'int' : 'float'
}
types[type] = (types[type] || 0) + 1
return type
}
for (let i = 0; i < 1000; i++) {
const obj = garbage()
const type = account(obj)
// just look one level deep
if (type === 'Object') {
for (const value of Object.values(obj)) {
account(value)
}
} else if (type === 'Array') {
for (const value of obj) {
account(value)
}
}
}
for (const type of expectedTypes) {
if (!types[type]) {
throw new Error(`Did not generate any ${type} values`)
}
if (types[type] < 20) {
throw new Error(`Did not generate enough ${type} values`)
}
}
for (const type of Object.keys(types)) {
if (!expectedTypes.includes(type)) {
throw new Error(`Got unexpected type ${type}`)
}
}
console.log('\u001b[32m✔\u001b[39m yep')