|
2 | 2 |
|
3 | 3 | const BSON = require('../register-bson');
|
4 | 4 | const Int32 = BSON.Int32;
|
| 5 | +const BSONError = BSON.BSONError; |
5 | 6 |
|
6 | 7 | describe('Int32', function () {
|
7 | 8 | context('Constructor', function () {
|
@@ -97,4 +98,50 @@ describe('Int32', function () {
|
97 | 98 | });
|
98 | 99 | }
|
99 | 100 | });
|
| 101 | + |
| 102 | + describe('fromString', () => { |
| 103 | + const acceptedInputs = [ |
| 104 | + ['Int32.max', '2147483647', 2147483647], |
| 105 | + ['Int32.min', '-2147483648', -2147483648], |
| 106 | + ['zero', '0', 0], |
| 107 | + ['a string with non-leading consecutive zeros', '45000000', 45000000], |
| 108 | + ['a string with zero with leading zeros', '000000', 0], |
| 109 | + ['a string with positive leading zeros', '000000867', 867], |
| 110 | + ['a string with explicity positive leading zeros', '+000000867', 867], |
| 111 | + ['a string with negative leading zeros', '-00007', -7] |
| 112 | + ]; |
| 113 | + const errorInputs = [ |
| 114 | + ['Int32.max + 1', '2147483648', 'larger than the maximum value for Int32'], |
| 115 | + ['Int32.min - 1', '-2147483649', 'smaller than the minimum value for Int32'], |
| 116 | + ['positive integer with decimal', '2.0', 'not a valid Int32 string'], |
| 117 | + ['zero with decimals', '0.0', 'not a valid Int32 string'], |
| 118 | + ['negative zero', '-0', 'not a valid Int32 string'], |
| 119 | + ['Infinity', 'Infinity', 'larger than the maximum value for Int32'], |
| 120 | + ['-Infinity', '-Infinity', 'smaller than the minimum value for Int32'], |
| 121 | + ['NaN', 'NaN', 'not a safe integer'], |
| 122 | + ['a fraction', '2/3', 'not a safe integer'], |
| 123 | + ['a string containing commas', '34,450', 'not a safe integer'], |
| 124 | + ['a string in exponentiation notation', '1e1', 'not a valid Int32 string'], |
| 125 | + ['a octal string', '0o1', 'not a valid Int32 string'], |
| 126 | + ['a binary string', '0b1', 'not a valid Int32 string'], |
| 127 | + ['a hexadecimal string', '0x1', 'not a valid Int32 string'], |
| 128 | + ['a empty string', '', 'not a valid Int32 string'], |
| 129 | + ['a leading and trailing whitespace', ' 89 ', 'not a valid Int32 string'] |
| 130 | + ]; |
| 131 | + |
| 132 | + for (const [testName, value, expectedInt32] of acceptedInputs) { |
| 133 | + context(`when the input is ${testName}`, () => { |
| 134 | + it(`should successfully return an Int32 representation`, () => { |
| 135 | + expect(Int32.fromString(value).value).to.equal(expectedInt32); |
| 136 | + }); |
| 137 | + }); |
| 138 | + } |
| 139 | + for (const [testName, value, expectedErrMsg] of errorInputs) { |
| 140 | + context(`when the input is ${testName}`, () => { |
| 141 | + it(`should throw an error containing '${expectedErrMsg}'`, () => { |
| 142 | + expect(() => Int32.fromString(value)).to.throw(BSONError, expectedErrMsg); |
| 143 | + }); |
| 144 | + }); |
| 145 | + } |
| 146 | + }); |
100 | 147 | });
|
0 commit comments