/
index.test.ts
104 lines (81 loc) · 2.49 KB
/
index.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
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
import parse from '.'
import commonJSModule = require('.')
const typeError = new TypeError('Expected a number.')
describe('parse-css-number', () => {
it('exports a CommonJS module for npm compatibility', () => {
expect(commonJSModule).toBe(parse)
})
it('returns a positive integer (number type) as-is', () => {
expect(parse(42 as any)).toBe(42)
})
it('throws parsing a string value of null', () => {
expect(() => parse('null')).toThrow(typeError)
})
it('throws parsing null value', () => {
expect(() => parse(null as any)).toThrow(typeError)
})
it('throws parsing NaN value', () => {
expect(() => parse(NaN as any)).toThrow(typeError)
})
it('throws parsing Infinity value', () => {
expect(() => parse(Infinity as any)).toThrow(typeError)
})
describe('number-exclusive scenarios', () => {
it('parses a positive fraction', () => {
expect(parse('4.01')).toBe(4.01)
})
it('parses a negative fraction', () => {
expect(parse('-456.8')).toBe(-456.8)
})
it('parses a fractional number w/o a leading zero', () => {
expect(parse('.60')).toBe(0.6)
})
it('parses scientific notation', () => {
expect(parse('10e3')).toBe(10e3)
})
it('parses complicated scientific notation', () => {
expect(parse('-3.4e-2')).toBe(-3.4e-2)
})
it('throws parsing an integer with a trailing decimal point', () => {
expect(() => parse('12.')).toThrow(
'Decimal points must be followed by at least one digit.',
)
})
it('throws parsing two leading signs', () => {
expect(() => parse('+-12.2')).toThrow(
'Only one leading +/- is allowed.',
)
})
it('throws parsing two decimal points', () => {
expect(() => parse('12.1.1')).toThrow(
'Only one decimal point is allowed.',
)
})
})
describe('number-safe integer scenarios', () => {
it('parses a positive integer (without a leading + sign)', () => {
expect(parse('12')).toBe(12)
})
it('parses a positive integer (with a leading + sign)', () => {
expect(parse('+123')).toBe(123)
})
it('parses a negative integer', () => {
expect(parse('-456')).toBe(-456)
})
it('parses zero', () => {
expect(parse('0.0')).toBe(0)
})
it('parses zero with a leading +', () => {
expect(parse('+0.0')).toBe(0)
})
it('parses zero with a leading -', () => {
expect(parse('-0.0')).toBe(-0)
})
it('throws parsing letters', () => {
expect(() => parse('ten')).toThrow(typeError)
})
it('throws parsing a special character', () => {
expect(() => parse('_5')).toThrow(typeError)
})
})
})