|
1
|
|
|
import {expect} from 'chai'; |
|
2
|
|
|
|
|
3
|
|
|
import {autoCast, castString, isColor, isFloat, isInt} from '../../src/lib/utils'; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
describe('autoCast utility', () => { |
|
7
|
|
|
it('should cast to an integer', () => { |
|
8
|
|
|
expect(autoCast('42')).to.equal(42); |
|
9
|
|
|
}); |
|
10
|
|
|
|
|
11
|
|
|
it('should cast to a float', () => { |
|
12
|
|
|
expect(autoCast('42.0')).to.equal(42.0); |
|
13
|
|
|
expect(autoCast('42.42')).to.equal(42.42); |
|
14
|
|
|
}); |
|
15
|
|
|
|
|
16
|
|
|
it('should cast to a boolean', () => { |
|
17
|
|
|
expect(autoCast('true')).to.be.true; |
|
|
|
|
|
|
18
|
|
|
expect(autoCast('True')).to.be.true; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
expect(autoCast('false')).to.be.false; |
|
|
|
|
|
|
21
|
|
|
expect(autoCast('False')).to.be.false; |
|
|
|
|
|
|
22
|
|
|
}); |
|
23
|
|
|
|
|
24
|
|
|
it('should return a string if no other type matches', () => { |
|
25
|
|
|
expect(autoCast('foo')).to.equal('foo'); |
|
26
|
|
|
}); |
|
27
|
|
|
}); |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
describe('castString utility', () => { |
|
31
|
|
|
it('should return a lowercase string', () => { |
|
32
|
|
|
expect(castString('FOO')).to.equal('foo'); |
|
33
|
|
|
expect(castString('foo')).to.equal('foo'); |
|
34
|
|
|
expect(castString('fOo')).to.equal('foo'); |
|
35
|
|
|
}); |
|
36
|
|
|
|
|
37
|
|
|
it('should return a string', () => { |
|
38
|
|
|
expect(castString(42)).to.equal('42'); |
|
39
|
|
|
expect(castString(42.42)).to.equal('42.42'); |
|
40
|
|
|
expect(castString(true)).to.equal('true'); |
|
41
|
|
|
expect(castString(false)).to.equal('false'); |
|
42
|
|
|
}); |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
describe('isInt utility', () => { |
|
46
|
|
|
it('should work as expected', () => { |
|
47
|
|
|
expect(isInt('187')).to.be.true; |
|
|
|
|
|
|
48
|
|
|
expect(isInt(187)).to.be.true; |
|
|
|
|
|
|
49
|
|
|
expect(isInt('-2')).to.be.true; |
|
|
|
|
|
|
50
|
|
|
expect(isInt(-'1')).to.be.true; |
|
|
|
|
|
|
51
|
|
|
expect(isInt('10e1')).to.be.true; |
|
|
|
|
|
|
52
|
|
|
expect(isInt(10e1)).to.be.true; |
|
|
|
|
|
|
53
|
|
|
expect(isInt('1.2')).to.be.false; |
|
|
|
|
|
|
54
|
|
|
expect(isInt('foo')).to.be.false; |
|
|
|
|
|
|
55
|
|
|
}); |
|
56
|
|
|
}); |
|
57
|
|
|
|
|
58
|
|
|
describe('isFloat utility', () => { |
|
59
|
|
|
it('should work as expected', () => { |
|
60
|
|
|
expect(isFloat('0.2')).to.be.true; |
|
|
|
|
|
|
61
|
|
|
expect(isFloat(0.2)).to.be.true; |
|
|
|
|
|
|
62
|
|
|
expect(isFloat('.2')).to.be.true; |
|
|
|
|
|
|
63
|
|
|
expect(isFloat('-.2')).to.be.true; |
|
|
|
|
|
|
64
|
|
|
expect(isFloat(-'.2')).to.be.true; |
|
|
|
|
|
|
65
|
|
|
expect(isFloat(-.2)).to.be.true; |
|
|
|
|
|
|
66
|
|
|
expect(isFloat('u.2')).to.be.false; |
|
|
|
|
|
|
67
|
|
|
expect(isFloat('2')).to.be.false; |
|
|
|
|
|
|
68
|
|
|
expect(isFloat('0.2u')).to.be.false; |
|
|
|
|
|
|
69
|
|
|
expect(isFloat('foo')).to.be.false; |
|
|
|
|
|
|
70
|
|
|
}); |
|
71
|
|
|
}); |
|
72
|
|
|
|
|
73
|
|
|
describe('isColor utility', () => { |
|
74
|
|
|
it('should work as expected', () => { |
|
75
|
|
|
expect(isColor('#ffffff')).to.be.true; |
|
|
|
|
|
|
76
|
|
|
expect(isColor('ffffff')).to.be.true; |
|
|
|
|
|
|
77
|
|
|
expect(isColor('#fFfFfF')).to.be.true; |
|
|
|
|
|
|
78
|
|
|
expect(isColor('fFfFfF')).to.be.true; |
|
|
|
|
|
|
79
|
|
|
expect(isColor('#012344')).to.be.true; |
|
|
|
|
|
|
80
|
|
|
expect(isColor('012344')).to.be.true; |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
expect(isColor('#01244')).to.be.false; |
|
|
|
|
|
|
83
|
|
|
expect(isColor('01244')).to.be.false; |
|
|
|
|
|
|
84
|
|
|
expect(isColor('#01244G')).to.be.false; |
|
|
|
|
|
|
85
|
|
|
expect(isColor('01244G')).to.be.false; |
|
|
|
|
|
|
86
|
|
|
}); |
|
87
|
|
|
}); |
|
88
|
|
|
|