forked from jestjs/jest
/
stringToBytes.test.ts
127 lines (100 loc) 路 3.06 KB
/
stringToBytes.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import stringToBytes from '../stringToBytes';
describe('numeric input', () => {
test('> 1 represents bytes', () => {
expect(stringToBytes(50.8)).toEqual(50);
});
test('1.1 should be a 1', () => {
expect(stringToBytes(1.1, 54)).toEqual(1);
});
test('< 1 represents a %', () => {
expect(stringToBytes(0.3, 51)).toEqual(15);
});
test('should throw when no reference supplied', () => {
expect(() => stringToBytes(0.3)).toThrowError();
});
test('should throw on a bad input', () => {
expect(() => stringToBytes(-0.3, 51)).toThrowError();
});
});
describe('string input', () => {
describe('numeric passthrough', () => {
test('> 1 represents bytes', () => {
expect(stringToBytes('50.8')).toEqual(50);
});
test('< 1 represents a %', () => {
expect(stringToBytes('0.3', 51)).toEqual(15);
});
test('should throw when no reference supplied', () => {
expect(() => stringToBytes('0.3')).toThrowError();
});
test('should throw on a bad input', () => {
expect(() => stringToBytes('-0.3', 51)).toThrowError();
});
});
describe('parsing', () => {
test('0% should throw an error', () => {
expect(() => stringToBytes('0%', 51)).toThrowError();
});
test('30%', () => {
expect(stringToBytes('30%', 51)).toEqual(15);
});
test('80%', () => {
expect(stringToBytes('80%', 51)).toEqual(40);
});
test('100%', () => {
expect(stringToBytes('100%', 51)).toEqual(51);
});
// The units caps is intentionally janky to test for forgiving string parsing.
describe('k', () => {
test('30k', () => {
expect(stringToBytes('30K')).toEqual(30000);
});
test('30KB', () => {
expect(stringToBytes('30kB')).toEqual(30000);
});
test('30KiB', () => {
expect(stringToBytes('30kIb')).toEqual(30720);
});
});
describe('m', () => {
test('30M', () => {
expect(stringToBytes('30M')).toEqual(30000000);
});
test('30MB', () => {
expect(stringToBytes('30MB')).toEqual(30000000);
});
test('30MiB', () => {
expect(stringToBytes('30MiB')).toEqual(31457280);
});
});
describe('g', () => {
test('30G', () => {
expect(stringToBytes('30G')).toEqual(30000000000);
});
test('30GB', () => {
expect(stringToBytes('30gB')).toEqual(30000000000);
});
test('30GiB', () => {
expect(stringToBytes('30GIB')).toEqual(32212254720);
});
});
test('unknown unit', () => {
expect(() => stringToBytes('50XX')).toThrowError();
});
});
});
test('nesting', () => {
expect(stringToBytes(stringToBytes(stringToBytes('30%', 51)))).toEqual(15);
});
test('null', () => {
expect(stringToBytes(null)).toEqual(null);
});
test('undefined', () => {
expect(stringToBytes(undefined)).toEqual(undefined);
});