forked from wooorm/dictionaries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
89 lines (72 loc) · 1.86 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import fs from 'node:fs'
import path from 'node:path'
import assert from 'node:assert'
import test from 'tape'
import {toVFile} from 'to-vfile'
import {isHidden} from 'is-hidden'
import isUtf8 from 'utf-8-validate'
import {bcp47Normalize} from 'bcp-47-normalize'
const own = {}.hasOwnProperty
const root = 'dictionaries'
const checks = {
'Should have a canonical BCP-47 tag': bcp47,
'All required files should exist': requiredFiles,
'All files should be in UTF-8': utf8
}
function bcp47(name) {
assert.strictEqual(
name,
bcp47Normalize(name, {warning: warn}),
name + ' should be a canonical, normal BCP-47 tag'
)
function warn(reason) {
console.log('warning:%s: %s', name, reason)
}
}
function utf8(name) {
const dirname = path.join(root, name)
const files = fs.readdirSync(dirname)
let index = -1
while (++index < files.length) {
const d = files[index]
if (isHidden(d)) continue
const file = toVFile.readSync(path.join(dirname, d))
assert.ok(isUtf8(file.value), file.basename + ' should be utf8')
}
}
function requiredFiles(name) {
const dirname = path.join(root, name)
const files = fs.readdirSync(dirname).filter((d) => !isHidden(d))
const paths = [
'index.dic',
'index.aff',
'readme.md',
'index.js',
'index.d.ts',
'package.json'
]
let index = -1
while (++index < paths.length) {
const d = paths[index]
assert.notStrictEqual(files.indexOf(d), -1, 'should have `' + d + '`')
}
}
test('dictionaries', (t) => {
const files = fs.readdirSync(root)
let index = -1
while (++index < files.length) {
const d = files[index]
if (isHidden(d)) continue
t.test(d, (st) => {
let key
for (key in checks) {
if (!own.call(checks, key)) continue
st.doesNotThrow(() => {
checks[key](d)
}, key)
}
st.end()
})
}
t.end()
})