-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.test.ts
61 lines (57 loc) · 1.96 KB
/
utils.test.ts
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
import { expect, test } from 'vitest'
import { extractPath, toArgs, toFlagCase } from './utils.js'
test.each([
['', { id: undefined, path: '/' }],
['/', { id: undefined, path: '/' }],
['/1', { id: 1, path: '/' }],
['/1/', { id: 1, path: '/' }],
['/-3', { id: undefined, path: '/-3' }],
['/123/foo', { id: 123, path: '/foo' }],
['/123456/bar', { id: 123456, path: '/bar' }],
['/-5/baz', { id: undefined, path: '/-5/baz' }],
['/321/foo/bar', { id: 321, path: '/foo/bar' }],
['/567/foo/bar/baz', { id: 567, path: '/foo/bar/baz' }],
['/321/foo/bar/', { id: 321, path: '/foo/bar' }],
['/foo', { id: undefined, path: '/foo' }],
['/foo/bar', { id: undefined, path: '/foo/bar' }],
])('extractPath("%s") -> %o', (input, expected) => {
expect(extractPath(input)).toEqual(expected)
})
test.each([
[[{}], []],
[[{ foo: undefined }], []],
[[{ foo: false }], ['--foo', 'false']],
[[{ foo: true }], ['--foo']],
[[{ foo: '' }], ['--foo']],
[[{ foo: 'bar' }], ['--foo', 'bar']],
[[{ foo: 0 }], ['--foo', '0']],
[[{ foo: 1 }], ['--foo', '1']],
[[{ foo: 1n }], ['--foo', '1']],
[[{ foo: 'bar', baz: 1 }], ['--foo', 'bar', '--baz', '1']],
[[{ fooBar: 'test' }], ['--foo-bar', 'test']],
[[{ foo: ['bar', 'baz'] }], ['--foo', 'bar,baz']],
[[{ foo: { barBaz: 'test' } }], ['--foo.bar-baz', 'test']],
[[{ foo: { barBaz: ['test', 'test2'] } }], ['--foo.bar-baz', 'test,test2']],
[
[{ fooBar: 'test' }, { casing: 'snake' }],
['--foo_bar', 'test'],
],
[
[{ foo: { barBaz: 'test' } }, { casing: 'snake' }],
['--foo.bar_baz', 'test'],
],
] as [Parameters<typeof toArgs>, string[]][])(
'toArgs(%o) -> %o',
([input, options], expected) => {
expect(toArgs(input, options)).toEqual(expected)
},
)
test.each([
['foo', '--foo'],
['Foo', '--foo'],
['fooBar', '--foo-bar'],
['fooBarBaz', '--foo-bar-baz'],
['foo.BarBaz', '--foo.bar-baz'],
])('toFlagCase(%s) -> %s', (input, expected) => {
expect(toFlagCase(input)).toBe(expected)
})