-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutil.test.tsx
More file actions
190 lines (163 loc) · 5.81 KB
/
Copy pathutil.test.tsx
File metadata and controls
190 lines (163 loc) · 5.81 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import getMiniDecimal, {
BigIntDecimal,
NumberDecimal,
toFixed,
} from '../src/MiniDecimal';
import type { DecimalClass, ValueType } from '../src/MiniDecimal';
import { num2str } from '../src/numberUtil';
jest.mock('../src/supportUtil');
const { supportBigInt } = require('../src/supportUtil');
describe('InputNumber.Util', () => {
beforeEach(() => {
supportBigInt.mockImplementation(() => {
return true;
});
});
afterEach(() => {
supportBigInt.mockRestore();
});
const classList: {
name: string;
getDecimal: (value: ValueType) => DecimalClass;
mockSupportBigInt?: boolean;
}[] = [
{ name: 'Default', getDecimal: getMiniDecimal },
{
name: 'BigInt',
getDecimal: (value: ValueType) => new BigIntDecimal(value),
},
{
name: 'Number',
getDecimal: (value: ValueType) => new NumberDecimal(value),
mockSupportBigInt: false,
},
];
classList.forEach(({ name, getDecimal, mockSupportBigInt }) => {
describe(name, () => {
const tinyScientificValue = `0.${'0'.repeat(306)}1`;
beforeEach(() => {
supportBigInt.mockImplementation(() => {
return mockSupportBigInt !== false;
});
});
afterEach(() => {
supportBigInt.mockRestore();
});
it('parse', () => {
expect(getDecimal(100).toString()).toEqual('100');
expect(getDecimal(11).toString()).toEqual('11');
expect(getDecimal(-9).toString()).toEqual('-9');
expect(getDecimal('11.28').toString()).toEqual('11.28');
expect(getDecimal('-9.3').toString()).toEqual('-9.3');
expect(getDecimal(1e-19).toString()).toEqual('0.0000000000000000001');
expect(getDecimal(-1e-19).toString()).toEqual('-0.0000000000000000001');
expect(getDecimal('-0').toString()).toEqual('0');
expect(getDecimal('.1').toString()).toEqual('0.1');
expect(getDecimal('1.').toString()).toEqual('1');
});
it('parses very small scientific notation', () => {
expect(getDecimal('1e-307').toString()).toEqual(
mockSupportBigInt === false ? '1e-307' : tinyScientificValue,
);
});
it('invalidate', () => {
expect(getDecimal('abc').toString()).toEqual('');
});
it('add', () => {
expect(getDecimal('1128').add('9.3').toString()).toEqual('1137.3');
expect(getDecimal(1128).add(93).toString()).toEqual('1221');
expect(getDecimal('1.35').add('2.65').toString()).toEqual('4');
expect(getDecimal('0.1').add('1.1').toString()).toEqual('1.2');
// Negative
expect(getDecimal('-1128').add('-9.3').toString()).toEqual('-1137.3');
expect(getDecimal('11.28').add('-9.3').toString()).toEqual('1.98');
expect(getDecimal('1128').add('-0.93').toString()).toEqual('1127.07');
expect(getDecimal('11.28').add('-93').toString()).toEqual('-81.72');
expect(getDecimal('-11.28').add('9.3').toString()).toEqual('-1.98');
// Continue update
let number = getDecimal('2.3');
number = number.add('-1');
expect(number.toString()).toEqual('1.3');
number = number.add('-1');
expect(number.toString()).toEqual('0.3');
// mini value
expect(getDecimal(0).add(-0.000000001).toString()).toEqual(
'-0.000000001',
);
});
it('toString !safe', () => {
const invalidate = getDecimal('Invalidate');
expect(invalidate.toString()).toEqual('');
expect(invalidate.toString(false)).toEqual('Invalidate');
});
});
});
describe('NumberDecimal', () => {
beforeEach(() => {
supportBigInt.mockImplementation(() => false);
});
afterEach(() => {
supportBigInt.mockRestore();
});
it('parse', () => {
expect(new NumberDecimal('1e24').toString()).toEqual(
String(Number.MAX_SAFE_INTEGER),
);
expect(new NumberDecimal('-1e+25').toString()).toEqual(
String(Number.MIN_SAFE_INTEGER),
);
});
});
describe('BigIntDecimal', () => {
it('parse', () => {
expect(new BigIntDecimal('1e24').toString()).toEqual(
'999999999999999983222784',
);
expect(new BigIntDecimal('1e+25').toString()).toEqual(
'10000000000000000905969664',
);
});
it('add', () => {
expect(new BigIntDecimal('11.28').add('0.0903').toString()).toEqual(
'11.3703',
);
});
});
describe('scientific notation', () => {
it('keeps num2str behavior correct', () => {
expect(num2str(0.123e-1)).toEqual('0.0123');
expect(num2str(-0.123e-1)).toEqual('-0.0123');
expect(num2str(0.00123e2)).toEqual('0.123');
expect(num2str(0e5)).toEqual('0');
expect(num2str(1.23e-19)).toEqual(`0.${'0'.repeat(18)}123`);
expect(num2str(-1.23e-20)).toEqual(`-0.${'0'.repeat(19)}123`);
expect(num2str(1.23e1)).toEqual('12.3');
expect(num2str(1.23e5)).toEqual('123000');
});
});
describe('toFixed', () => {
it('less than precision', () => {
expect(toFixed('1.1', ',', 2)).toEqual('1,10');
expect(toFixed('1.23', '.', 5)).toEqual('1.23000');
});
it('large than precision', () => {
expect(toFixed('1.234', '.', 2)).toEqual('1.23');
expect(toFixed('1.235', '.', 2)).toEqual('1.24');
expect(toFixed('1.238', '.', 2)).toEqual('1.24');
// Integer
expect(toFixed('1.238', '.', 0)).toEqual('1');
expect(toFixed('1.5', '.', 0)).toEqual('2');
});
it('empty precision', () => {
expect(toFixed('1.2', '.')).toEqual('1.2');
expect(toFixed('1.000', '.')).toEqual('1');
});
it('should return "" when input is ""', () => {
expect(toFixed('', '.')).toEqual('');
});
it('negative', () => {
// expect(toFixed('77.88', '.', 1)).toEqual('77.9');
expect(toFixed('-77.88', '.', 1)).toEqual('-77.9');
});
});
});