-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_spec.js
82 lines (68 loc) · 1.9 KB
/
test_spec.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
'use strict'
/* eslint-disable */
const safeTrim = require('../dist/safe-trim.common.js')
describe('safeTrim', () => {
it('trim 0', () => {
let str = 'a\u200Bb c \r\n\r\td e\u2003 f g'
let ret = safeTrim(str)
expect(ret).toEqual('ab c\n\ndef g')
})
it('trim 1', () => {
let str = ' "a":1 a \r\n\r\t b '
let ret = safeTrim(str)
expect(ret).toEqual('"a":1 a\n\nb')
})
it('trim 2', () => {
expect(safeTrim('throw new Error(";("')).toEqual('throw new Error(";("')
})
it('converted CR CR-LR into LR', () => {
// safeTrim('\r\n') === ''
expect(safeTrim(' a\r\n\r\nb ')).toEqual('a\n\nb')
expect(safeTrim(' \r\n\r\n ')).toEqual('')
expect(safeTrim(' a\r\rb ')).toEqual('a\n\nb')
expect(safeTrim(' \r\r\r ')).toEqual('')
expect(safeTrim(' a\r\r\nb ')).toEqual('a\n\nb')
expect(safeTrim(' \r\r\n ')).toEqual('')
})
it('trim BOM', () => {
let str = '{"a":1}'
let ret
try {
JSON.parse(str)
} catch (e) {
ret = 'error!!!'
}
expect(ret).toEqual('error!!!')
})
it('safe trim Bom', () => {
let str = '{"a":1}'
let ret = JSON.parse(safeTrim(str))
expect(ret).toEqual({a: 1})
})
})
describe('bad args', () => {
it('{}', () => {
expect(safeTrim({})).toEqual('[object Object]')
})
it('[]', () => {
expect(safeTrim([])).toEqual('')
})
it('NaN', () => {
expect(safeTrim(NaN)).toEqual('NaN')
})
it('undefined', () => {
expect(safeTrim(undefined)).toEqual('undefined')
})
it('null', () => {
expect(safeTrim(null)).toEqual('null')
})
it('0', () => {
expect(safeTrim(0)).toEqual('0')
})
it('function', () => {
let fun = () => {
}
let ret = safeTrim(fun)
expect(ret).toEqual(String(fun))
})
})