|
| 1 | +import Comparator from '../Comparator'; |
| 2 | + |
| 3 | +describe('Comparator', () => { |
| 4 | + it('should compare with default comparator function', () => { |
| 5 | + const comparator = new Comparator(); |
| 6 | + |
| 7 | + expect(comparator.equal(0, 0)).toBeTruthy(); |
| 8 | + expect(comparator.equal(0, 1)).toBeFalsy(); |
| 9 | + expect(comparator.equal('a', 'a')).toBeTruthy(); |
| 10 | + expect(comparator.lessThen(1, 2)).toBeTruthy(); |
| 11 | + expect(comparator.lessThen(-1, 2)).toBeTruthy(); |
| 12 | + expect(comparator.lessThen('a', 'b')).toBeTruthy(); |
| 13 | + expect(comparator.lessThen('a', 'ab')).toBeTruthy(); |
| 14 | + expect(comparator.lessThen(10, 2)).toBeFalsy(); |
| 15 | + expect(comparator.lessThenOrEqual(10, 2)).toBeFalsy(); |
| 16 | + expect(comparator.lessThenOrEqual(1, 1)).toBeTruthy(); |
| 17 | + expect(comparator.lessThenOrEqual(0, 0)).toBeTruthy(); |
| 18 | + expect(comparator.greaterThen(0, 0)).toBeFalsy(); |
| 19 | + expect(comparator.greaterThen(10, 0)).toBeTruthy(); |
| 20 | + expect(comparator.greaterThenOrEqual(10, 0)).toBeTruthy(); |
| 21 | + expect(comparator.greaterThenOrEqual(10, 10)).toBeTruthy(); |
| 22 | + expect(comparator.greaterThenOrEqual(0, 10)).toBeFalsy(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should compare with custom comparator function', () => { |
| 26 | + const comparator = new Comparator((a, b) => { |
| 27 | + if (a.length === b.length) { |
| 28 | + return 0; |
| 29 | + } |
| 30 | + |
| 31 | + return a.length < b.length ? -1 : 1; |
| 32 | + }); |
| 33 | + |
| 34 | + expect(comparator.equal('a', 'b')).toBeTruthy(); |
| 35 | + expect(comparator.equal('a', '')).toBeFalsy(); |
| 36 | + expect(comparator.lessThen('b', 'aa')).toBeTruthy(); |
| 37 | + expect(comparator.greaterThenOrEqual('a', 'aa')).toBeFalsy(); |
| 38 | + expect(comparator.greaterThenOrEqual('aa', 'a')).toBeTruthy(); |
| 39 | + expect(comparator.greaterThenOrEqual('a', 'a')).toBeTruthy(); |
| 40 | + |
| 41 | + comparator.reverse(); |
| 42 | + |
| 43 | + expect(comparator.equal('a', 'b')).toBeTruthy(); |
| 44 | + expect(comparator.equal('a', '')).toBeFalsy(); |
| 45 | + expect(comparator.lessThen('b', 'aa')).toBeFalsy(); |
| 46 | + expect(comparator.greaterThenOrEqual('a', 'aa')).toBeTruthy(); |
| 47 | + expect(comparator.greaterThenOrEqual('aa', 'a')).toBeFalsy(); |
| 48 | + expect(comparator.greaterThenOrEqual('a', 'a')).toBeTruthy(); |
| 49 | + }); |
| 50 | +}); |
0 commit comments