-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils_test.ts
More file actions
72 lines (67 loc) · 2.18 KB
/
utils_test.ts
File metadata and controls
72 lines (67 loc) · 2.18 KB
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
import {
delay,
lazyJSONParse,
makeArray,
makeEncryptor,
paramsEncoder,
pathsAreEqual,
} from './utils.ts'
import { assert, assertEquals } from '@std/assert'
Deno.test('makeArray', async (t) => {
await t.step('wraps a single value in an array', () => {
assertEquals(makeArray(1), [1])
})
await t.step('returns an array as-is', () => {
assertEquals(makeArray([1, 2]), [1, 2])
})
})
Deno.test('makeEncryptor', async (t) => {
await t.step('encrypt returns a hex string', () => {
const { encrypt } = makeEncryptor('key')
const result = encrypt('hi')
assertEquals(typeof result, 'string')
assert(result.length > 0)
})
await t.step('decrypt returns a string', () => {
const { decrypt } = makeEncryptor('key')
const result = decrypt('0a0b')
assertEquals(typeof result, 'string')
})
await t.step('decrypt handles empty match', () => {
const { decrypt } = makeEncryptor('key')
const result = decrypt('')
assertEquals(result, '')
})
await t.step('paramsEncoder encrypt and decrypt are callable', () => {
const encrypted = paramsEncoder.encrypt('test')
assertEquals(typeof encrypted, 'string')
const decrypted = paramsEncoder.decrypt(encrypted)
assertEquals(typeof decrypted, 'string')
})
})
Deno.test('lazyJSONParse', async (t) => {
await t.step('should parse JSON like JSON.parse', () => {
assertEquals(lazyJSONParse('{ "a": "b" }'), JSON.parse('{ "a": "b" }'))
})
await t.step('should return an empty object on failed parse', () => {
assertEquals(lazyJSONParse('{ "a": "b"'), {})
})
})
Deno.test('pathsAreEqual', async (t) => {
await t.step('if expected path is asterisk, return true', () => {
assertEquals(pathsAreEqual('/hello', '*'), true)
})
await t.step('should assert equal paths', () => {
assertEquals(pathsAreEqual('/hello', '/hello'), true)
})
await t.step('if nothing is expected, default to "/"', () => {
assertEquals(pathsAreEqual('/'), true)
})
})
Deno.test('delay', async (t) => {
await t.step('it delays a function for given time', async () => {
const then = performance.now()
await delay(10)
assert(performance.now() - then < 15) // there's extra run-time
})
})