|
1 | 1 | import { range } from '../../src'; |
2 | 2 |
|
3 | | -describe('Array', () => { |
4 | | - test('range', () => { |
5 | | - expect(range()).toEqual([]); |
6 | | - expect(range(0)).toEqual([]); |
7 | | - expect(range(1)).toEqual([]); |
8 | | - expect(range(1, 2)).toEqual([1, 2]); |
9 | | - expect(range(10, 50, 10)).toEqual([10, 20, 30, 40, 50]); |
10 | | - expect(range(10, 12, 0.5)).toEqual([10, 10.5, 11, 11.5, 12]); |
11 | | - expect(range(-10, -5)).toEqual([-10, -9, -8, -7, -6, -5]); |
12 | | - expect(range(0, -4, -1)).toEqual([0, -1, -2, -3, -4]); |
13 | | - expect(range(-4)).toEqual([]); |
14 | | - expect(range(1, 2, 0)).toEqual([]); |
15 | | - expect(range(-1, -2, 1)).toEqual([]); |
16 | | - expect(range(-1, -2, -1)).toEqual([-1, -2]); |
17 | | - expect(range(1, 2, 1)).toEqual([1, 2]); |
18 | | - expect(range(0, 5)).toEqual([0, 1, 2, 3, 4, 5]); |
19 | | - expect(range(3, 9, 3)).toEqual([3, 6, 9]); |
| 3 | +describe('Array > range', () => { |
| 4 | + test('Error handling: Type Mismatch', () => { |
| 5 | + const sharedMatrix = [ |
| 6 | + [], |
| 7 | + [0], |
| 8 | + [0n], |
| 9 | + [0, 1, function () {}], |
| 10 | + [0n, 1n, function () {}], |
| 11 | + [0, function () {}, 2], |
| 12 | + [function () {}, 2, 2], |
| 13 | + [0n, 1], |
| 14 | + [0n, 1, 1], |
| 15 | + [0n, 1, { step: 1 }], |
| 16 | + [0, 1n], |
| 17 | + [0, 1n, 1], |
| 18 | + [0, 1n, { step: 1 }], |
| 19 | + [0, 1, 1n], |
| 20 | + [0, 1, { step: 1n }], |
| 21 | + ]; |
| 22 | + for (const each of sharedMatrix) { |
| 23 | + expect(() => range(...each)).toThrowError(); |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + test('empty', () => { |
| 28 | + expect(range(0, 0)).toEqual([]); |
| 29 | + expect(range(0, -5, 1)).toEqual([]); |
| 30 | + }); |
| 31 | + |
| 32 | + test('positive integers', () => { |
| 33 | + expect(range(0, 5)).toEqual([0, 1, 2, 3, 4]); |
| 34 | + expect(range(1, 2)).toEqual([1]); |
| 35 | + expect(range(1, 2, { step: 1 })).toEqual([1]); |
| 36 | + expect(range(10, 50, { step: 10 })).toEqual([10, 20, 30, 40]); |
| 37 | + expect(range(10, 50, 10)).toEqual([10, 20, 30, 40]); |
| 38 | + expect(range(3, 9, { step: 3 })).toEqual([3, 6]); |
| 39 | + }); |
| 40 | + |
| 41 | + test('negative integers', () => { |
| 42 | + expect(range(0, -5)).toEqual([0, -1, -2, -3, -4]); |
| 43 | + expect(range(0, -4, { step: -1 })).toEqual([0, -1, -2, -3]); |
| 44 | + expect(range(-1, 5)).toEqual([-1, 0, 1, 2, 3, 4]); |
| 45 | + expect(range(0, -5, { step: -1 })).toEqual([0, -1, -2, -3, -4]); |
| 46 | + expect(range(-1, -2)).toEqual([-1]); |
| 47 | + expect(range(-1, -2, { step: -1 })).toEqual([-1]); |
| 48 | + expect(range(-10, -5)).toEqual([-10, -9, -8, -7, -6]); |
| 49 | + }); |
| 50 | + |
| 51 | + test('decimal', () => { |
| 52 | + expect(range(10, 12, { step: 0.5 })).toEqual([10, 10.5, 11, 11.5]); |
| 53 | + expect(range(0, 1, { step: 0.1 })).toEqual([ |
| 54 | + 0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, |
| 55 | + 0.7000000000000001, 0.8, 0.9, |
| 56 | + ]); |
| 57 | + }); |
| 58 | + |
| 59 | + test('inclusive true', () => { |
| 60 | + expect(range(0, 5, { inclusive: true })).toEqual([0, 1, 2, 3, 4, 5]); |
| 61 | + expect(range(-1, -2, { step: -1, inclusive: true })).toEqual([-1, -2]); |
| 62 | + }); |
| 63 | + |
| 64 | + test('Use with for of loop', () => { |
| 65 | + const res = []; |
| 66 | + for (const item of range(1, 5)) { |
| 67 | + res.push(item); |
| 68 | + } |
| 69 | + expect(res).toEqual([1, 2, 3, 4]); |
| 70 | + }); |
| 71 | + |
| 72 | + test('NaN', () => { |
| 73 | + expect(() => range(NaN, 0)).toThrowError(); |
| 74 | + expect(() => range(0, NaN)).toThrowError(); |
| 75 | + expect(() => range(NaN, NaN)).toThrowError(); |
| 76 | + |
| 77 | + expect(() => range(0, 0, { step: NaN })).toThrowError(); |
| 78 | + expect(() => range(0, 5, NaN)).toThrowError(); |
| 79 | + }); |
| 80 | + |
| 81 | + test('Step infer', () => { |
| 82 | + expect(range(0, -2)).toEqual([0, -1]); |
| 83 | + expect(range(0, -2, { inclusive: true })).toEqual([0, -1, -2]); |
| 84 | + }); |
| 85 | + |
| 86 | + test('Error: Zero as step', () => { |
| 87 | + expect(() => range(0, 10, 0)).toThrowError(); |
| 88 | + expect(() => range(0, 10, { step: 0 })).toThrowError(); |
| 89 | + expect(() => range(0n, 10n, 0n)).toThrowError(); |
| 90 | + expect(() => range(0n, 10n, { step: 0n })).toThrowError(); |
| 91 | + }); |
| 92 | + |
| 93 | + test('Error: Infinity as start / step', () => { |
| 94 | + expect(() => range(Infinity, 10, 0)).toThrowError(); |
| 95 | + expect(() => range(-Infinity, 10, 0)).toThrowError(); |
| 96 | + expect(() => range(0, 10, Infinity)).toThrowError(); |
| 97 | + expect(() => range(0, 10, { step: Infinity })).toThrowError(); |
| 98 | + }); |
| 99 | + |
| 100 | + test('Inclusive on same start-end', () => { |
| 101 | + expect(range(0, 0, { inclusive: false })).toEqual([]); |
| 102 | + expect(range(0, 0, { inclusive: true })).toEqual([0]); |
20 | 103 | }); |
21 | 104 | }); |
0 commit comments