-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
178 lines (157 loc) · 5.05 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import assert from 'node:assert'
import util from 'node:util'
import cp from 'node:child_process'
import fs from 'node:fs'
import {URL} from 'node:url'
import {PassThrough} from 'node:stream'
import test from 'node:test'
import {levenshteinEditDistance as levenshtein} from './index.js'
const exec = util.promisify(cp.exec)
/** @type {Object.<string, unknown>} */
const pack = JSON.parse(
String(fs.readFileSync(new URL('package.json', import.meta.url)))
)
test('api', async function (t) {
await t.test('should work', function () {
assert.equal(levenshtein('', 'a'), 1)
assert.equal(levenshtein('a', ''), 1)
assert.equal(levenshtein('', ''), 0)
assert.equal(levenshtein('levenshtein', 'levenshtein'), 0)
assert.equal(levenshtein('sitting', 'kitten'), 3)
assert.equal(levenshtein('gumbo', 'gambol'), 2)
assert.equal(levenshtein('saturday', 'sunday'), 3)
assert.notEqual(
levenshtein('DwAyNE', 'DUANE'),
levenshtein('dwayne', 'DuAnE'),
'should not match case insensitive'
)
assert.equal(
levenshtein('DwAyNE', 'DUANE', true),
levenshtein('dwayne', 'DuAnE', true),
'should match case if `insensitive` is given'
)
assert.equal(
levenshtein('aarrgh', 'aargh'),
levenshtein('aargh', 'aarrgh'),
'should not care about parameter order'
)
})
await t.test('Compatibility with `fast-levenshtein`', function () {
assert.equal(levenshtein('a', 'b'), 1)
assert.equal(levenshtein('ab', 'ac'), 1)
assert.equal(levenshtein('ac', 'bc'), 1)
assert.equal(levenshtein('abc', 'axc'), 1)
assert.equal(levenshtein('xabxcdxxefxgx', '1ab2cd34ef5g6'), 6)
assert.equal(levenshtein('xabxcdxxefxgx', 'abcdefg'), 6)
assert.equal(levenshtein('javawasneat', 'scalaisgreat'), 7)
assert.equal(levenshtein('example', 'samples'), 3)
assert.equal(levenshtein('sturgeon', 'urgently'), 6)
assert.equal(levenshtein('levenshtein', 'frankenstein'), 6)
assert.equal(levenshtein('distance', 'difference'), 5)
assert.equal(
levenshtein(
'因為我是中國人所以我會說中文',
'因為我是英國人所以我會說英文'
),
2
)
assert.equal(
levenshtein(
'Morbi interdum ultricies neque varius condimentum. Donec ' +
'volutpat turpis interdum metus ultricies vulputate. Duis ' +
'ultricies rhoncus sapien, sit amet fermentum risus ' +
'imperdiet vitae. Ut et lectus',
'Duis erat dolor, cursus in tincidunt a, lobortis in odio. ' +
'Cras magna sem, pharetra et iaculis quis, faucibus quis ' +
'tellus. Suspendisse dapibus sapien in justo cursus'
),
143
)
})
})
test('cli', async function () {
try {
await exec('./cli.js sitting')
assert.fail('should not pass')
} catch (error) {
assert.ok(
/Usage: levenshtein-edit-distance/.test(String(error)),
'not enough arguments'
)
}
assert.deepEqual(
await exec('./cli.js sitting kitten'),
{stdout: '3\n', stderr: ''},
'spaces'
)
assert.deepEqual(
await exec('./cli.js sitting,kitten'),
{stdout: '3\n', stderr: ''},
'commas'
)
assert.deepEqual(
await exec('./cli.js sitting, kitten'),
{stdout: '3\n', stderr: ''},
'commas and spaces'
)
assert.deepEqual(
await exec('./cli.js a A'),
{stdout: '1\n', stderr: ''},
'case-sensitive: default'
)
assert.deepEqual(
await exec('./cli.js a A -i'),
{stdout: '0\n', stderr: ''},
'case-sensitive: -i'
)
assert.deepEqual(
await exec('./cli.js a A --insensitive'),
{stdout: '0\n', stderr: ''},
'case-sensitive: --insensitive'
)
await new Promise(function (resolve) {
const input = new PassThrough()
const subprocess = cp.exec('./cli.js', function (error, stdout, stderr) {
assert.deepEqual([error, stdout, stderr], [null, '6\n', ''], 'stdin')
setImmediate(resolve)
})
assert(subprocess.stdin, 'expected stdin on `subprocess`')
input.pipe(subprocess.stdin)
input.write('sturgeon')
setImmediate(function () {
input.end(' urgently')
})
})
await new Promise(function (resolve) {
const input = new PassThrough()
const subprocess = cp.exec('./cli.js', function (error, stdout, stderr) {
assert.deepEqual(
[
Boolean(error),
stdout,
/Usage: levenshtein-edit-distance/.test(stderr)
],
[true, '', true],
'stdin (not enough arguments)'
)
setImmediate(resolve)
})
assert(subprocess.stdin, 'expected stdin on `subprocess`')
input.pipe(subprocess.stdin)
input.end('sturgeon')
})
const h = await exec('./cli.js -h')
assert.ok(/\sUsage: levenshtein-edit-distance/.test(h.stdout), '-h')
const help = await exec('./cli.js --help')
assert.ok(/\sUsage: levenshtein-edit-distance/.test(help.stdout), '-h')
assert.deepEqual(
await exec('./cli.js -v'),
{stdout: pack.version + '\n', stderr: ''},
'-v'
)
assert.deepEqual(
await exec('./cli.js --version'),
{stdout: pack.version + '\n', stderr: ''},
'--version'
)
})