-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.js
110 lines (94 loc) · 2.75 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
import assert from 'node:assert'
import fs from 'node:fs'
import childProcess from 'node:child_process'
import {PassThrough} from 'node:stream'
import test from 'tape'
import {direction} from './index.js'
/** @type {{[key: string]: unknown, version: string}} */
const pkg = JSON.parse(String(fs.readFileSync('package.json')))
const fixtures = [
{input: '0', output: 'neutral'},
{input: '123', output: 'neutral'},
{input: 'a', output: 'ltr'},
{input: 'נ', output: 'rtl'},
{input: '\u0000', output: 'neutral'},
{input: ' ', output: 'neutral'},
{input: '!', output: 'neutral'},
{input: '@', output: 'neutral'},
{input: '[', output: 'neutral'},
{input: '`', output: 'neutral'},
{input: 'A', output: 'ltr'},
{input: 'english', output: 'ltr'},
{input: 'sentence', output: 'ltr'},
{input: 'Un', output: 'ltr'},
{input: 'simple', output: 'ltr'},
{input: 'anglais', output: 'ltr'},
{input: 'phrase', output: 'ltr'},
{input: 'أ', output: 'rtl'},
{input: 'الجملة', output: 'rtl'},
{input: 'الانجليزية', output: 'rtl'},
{input: 'بسيطة', output: 'rtl'}
]
test('api', function (t) {
let index = -1
// @ts-ignore
t.equal(direction(), 'neutral', 'should classify nullish as `neutral`')
while (++index < fixtures.length) {
t.equal(
direction(fixtures[index].input),
fixtures[index].output,
'should classify `' +
fixtures[index].input +
'` as `' +
fixtures[index].output +
'`'
)
}
t.end()
})
test('cli', function (t) {
const input = new PassThrough()
t.plan(7)
childProcess.exec('./cli.js abc', function (error, stdout, stderr) {
t.deepEqual([error, stderr, stdout], [null, '', 'ltr\n'], 'arguments')
})
childProcess.exec('./cli.js @', function (error, stdout, stderr) {
t.deepEqual([error, stderr, stdout], [null, '', 'neutral\n'], 'neutral')
})
const subprocess = childProcess.exec(
'./cli.js',
function (error, stdout, stderr) {
t.deepEqual([error, stderr, stdout], [null, '', 'rtl\n'], 'stdin')
}
)
assert(subprocess.stdin, 'expected stdin')
input.pipe(subprocess.stdin)
input.write('لة')
setImmediate(function () {
input.end('الجم')
})
help('-h')
help('--help')
/**
* @param {string} flag
*/
function help(flag) {
childProcess.exec('./cli.js ' + flag, function (error, stdout, stderr) {
t.deepEqual(
[error, stderr, /\s+Usage: direction/.test(stdout)],
[null, '', true],
flag
)
})
}
version('-v')
version('--version')
/**
* @param {string} flag
*/
function version(flag) {
childProcess.exec('./cli.js ' + flag, function (error, stdout, stderr) {
t.deepEqual([error, stderr, stdout], [null, '', pkg.version + '\n'], flag)
})
}
})