-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
number.test.js
141 lines (113 loc) · 3.37 KB
/
number.test.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
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
import { beforeEach, describe, it, expect } from 'vitest';
import ReadlineStub from '../../helpers/readline.js';
import fixtures from '../../helpers/fixtures.js';
import NumberPrompt from '../../../lib/prompts/number.js';
const ACCEPTABLE_ERROR = 0.001;
describe('`number` prompt', () => {
let fixture;
let rl;
let number;
beforeEach(() => {
fixture = { ...fixtures.number };
rl = new ReadlineStub();
number = new NumberPrompt(fixture, rl);
});
it('should parse the largest number', () =>
new Promise((done) => {
number.run().then((answer) => {
expect(answer).toEqual(Number.MAX_SAFE_INTEGER);
done();
});
rl.emit('line', String(Number.MAX_SAFE_INTEGER));
}));
it('should parse the smallest number', () =>
new Promise((done) => {
number.run().then((answer) => {
expect(answer).toEqual(Number.MIN_SAFE_INTEGER);
done();
});
rl.emit('line', String(Number.MIN_SAFE_INTEGER));
}));
it('should parse an integer', () =>
new Promise((done) => {
number.run().then((answer) => {
expect(answer).toEqual(42);
done();
});
rl.emit('line', '42');
}));
it('should parse a negative integer', () =>
new Promise((done) => {
number.run().then((answer) => {
expect(answer).toEqual(-363);
done();
});
rl.emit('line', '-363');
}));
it('should parse a positive float', () =>
new Promise((done) => {
number.run().then((answer) => {
expect(answer).toBeCloseTo(4353.43, ACCEPTABLE_ERROR);
done();
});
rl.emit('line', '4353.43');
}));
it('should parse a negative float', () =>
new Promise((done) => {
number.run().then((answer) => {
expect(answer).toBeCloseTo(-4353.43, ACCEPTABLE_ERROR);
done();
});
rl.emit('line', '-4353.43');
}));
it('should parse a float with no digits before the decimal', () =>
new Promise((done) => {
number.run().then((answer) => {
expect(answer).toBeCloseTo(0.012_64, ACCEPTABLE_ERROR);
done();
});
rl.emit('line', '.01264');
}));
it('should parse a float with no digits after the decimal', () =>
new Promise((done) => {
number.run().then((answer) => {
expect(answer).toBeCloseTo(1234, ACCEPTABLE_ERROR);
done();
});
rl.emit('line', '1234.');
}));
it('should parse a float with exponents', () =>
new Promise((done) => {
number.run().then((answer) => {
expect(answer).toBeCloseTo(534e12, ACCEPTABLE_ERROR);
done();
});
rl.emit('line', '534e12');
}));
it('should parse any other string as NaN', () =>
new Promise((done) => {
number.run().then((answer) => {
expect(answer).toBeNaN();
done();
});
rl.emit('line', 'The cat');
}));
it('should parse the empty string as NaN', () =>
new Promise((done) => {
number.run().then((answer) => {
expect(answer).toBeNaN();
done();
});
rl.emit('line', '');
}));
it('should return default value if it is set on a bad input', () =>
new Promise((done) => {
number.opt.default = 11;
number.run().then((answer) => {
expect(answer).toEqual(11);
done();
});
rl.input.emit('keypress', 'a', { name: 'a' });
rl.emit('line');
}));
});