forked from SBoudrias/Inquirer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.js
116 lines (91 loc) · 3 KB
/
number.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
const { expect } = require('chai');
const ReadlineStub = require('../../helpers/readline');
const fixtures = require('../../helpers/fixtures');
const NumberPrompt = require('../../../lib/prompts/number');
const ACCEPTABLE_ERROR = 0.001;
describe('`number` prompt', () => {
beforeEach(function () {
this.fixture = { ...fixtures.number };
this.rl = new ReadlineStub();
this.number = new NumberPrompt(this.fixture, this.rl);
});
it('should parse the largest number', function (done) {
this.number.run().then((answer) => {
expect(answer).to.equal(Number.MAX_SAFE_INTEGER);
done();
});
this.rl.emit('line', String(Number.MAX_SAFE_INTEGER));
});
it('should parse the smallest number', function (done) {
this.number.run().then((answer) => {
expect(answer).to.equal(Number.MIN_SAFE_INTEGER);
done();
});
this.rl.emit('line', String(Number.MIN_SAFE_INTEGER));
});
it('should parse an integer', function (done) {
this.number.run().then((answer) => {
expect(answer).to.equal(42);
done();
});
this.rl.emit('line', '42');
});
it('should parse negative numbers', function (done) {
this.number.run().then((answer) => {
expect(answer).to.equal(-363);
done();
});
this.rl.emit('line', '-363');
});
it('should parse a regular float', function (done) {
this.number.run().then((answer) => {
expect(answer).to.be.closeTo(4353.43, ACCEPTABLE_ERROR);
done();
});
this.rl.emit('line', '4353.43');
});
it('should parse a float with no digits before the decimal', function (done) {
this.number.run().then((answer) => {
expect(answer).to.be.closeTo(0.01264, ACCEPTABLE_ERROR);
done();
});
this.rl.emit('line', '.01264');
});
it('should parse a float with no digits after the decimal', function (done) {
this.number.run().then((answer) => {
expect(answer).to.be.closeTo(1234.0, ACCEPTABLE_ERROR);
done();
});
this.rl.emit('line', '1234.');
});
it('should parse a float with exponents', function (done) {
this.number.run().then((answer) => {
expect(answer).to.be.closeTo(534e12, ACCEPTABLE_ERROR);
done();
});
this.rl.emit('line', '534e12');
});
it('should parse any other string as NaN', function (done) {
this.number.run().then((answer) => {
expect(answer).to.be.NaN; // eslint-disable-line no-unused-expressions
done();
});
this.rl.emit('line', 'The cat');
});
it('should parse the empty string as NaN', function (done) {
this.number.run().then((answer) => {
expect(answer).to.be.NaN; // eslint-disable-line no-unused-expressions
done();
});
this.rl.emit('line', '');
});
it('should return default value if it is set on a bad input', function (done) {
this.number.opt.default = 11;
this.number.run().then((answer) => {
expect(answer).to.equal(11);
done();
});
this.rl.input.emit('keypress', 'a', { name: 'a' });
this.rl.emit('line');
});
});