-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
string.test.js
78 lines (72 loc) · 2.35 KB
/
string.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
import * as prng from './prng.js'
import * as string from './string.js'
import * as t from './testing.js'
/**
* @param {t.TestCase} tc
*/
export const testUtilities = tc => {
t.assert(string.repeat('1', 3) === '111')
t.assert(string.repeat('1', 0) === '')
t.assert(string.repeat('1', 1) === '1')
}
/**
* @param {t.TestCase} tc
*/
export const testLowercaseTransformation = tc => {
t.compareStrings(string.fromCamelCase('ThisIsATest', ' '), 'this is a test')
t.compareStrings(string.fromCamelCase('Testing', ' '), 'testing')
t.compareStrings(string.fromCamelCase('testingThis', ' '), 'testing this')
t.compareStrings(string.fromCamelCase('testYAY', ' '), 'test y a y')
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatStringUtf8Encoding = tc => {
t.skip(!string.utf8TextDecoder)
const str = prng.utf16String(tc.prng, 1000000)
let nativeResult, polyfilledResult
t.measureTime('TextEncoder utf8 encoding', () => {
nativeResult = string._encodeUtf8Native(str)
})
t.measureTime('Polyfilled utf8 encoding', () => {
polyfilledResult = string._encodeUtf8Polyfill(str)
})
t.compare(nativeResult, polyfilledResult, 'Encoded utf8 buffers match')
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatStringUtf8Decoding = tc => {
t.skip(!string.utf8TextDecoder)
const buf = string.encodeUtf8(prng.utf16String(tc.prng, 1000000))
let nativeResult, polyfilledResult
t.measureTime('TextEncoder utf8 decoding', () => {
nativeResult = string._decodeUtf8Native(buf)
})
t.measureTime('Polyfilled utf8 decoding', () => {
polyfilledResult = string._decodeUtf8Polyfill(buf)
})
t.compare(nativeResult, polyfilledResult, 'Decoded utf8 buffers match')
}
/**
* @param {t.TestCase} tc
*/
export const testBomEncodingDecoding = tc => {
const bomStr = 'bom'
t.assert(bomStr.length === 4)
const polyfilledResult = string._decodeUtf8Polyfill(string._encodeUtf8Polyfill(bomStr))
t.assert(polyfilledResult.length === 4)
t.assert(polyfilledResult === bomStr)
if (string.utf8TextDecoder) {
const nativeResult = string._decodeUtf8Native(string._encodeUtf8Native(bomStr))
t.assert(nativeResult === polyfilledResult)
}
}
/**
* @param {t.TestCase} tc
*/
export const testSplice = tc => {
const initial = 'xyz'
t.compareStrings(string.splice(initial, 0, 2), 'z')
t.compareStrings(string.splice(initial, 0, 2, 'u'), 'uz')
}