import { assert } from 'chai'; import { create, assign } from '../../../../../../lib/core/object'; import { MetaContext } from '../../../../../../lib/experimental/meta/MetaContext'; import { $COMPARE, withCompare } from '../../../../../../lib/experimental/meta/operations/base/$compare'; import builtins from '../../../../../../lib/experimental/meta/operations/builtins/$compare'; const compare = (function () { const context = new (withCompare(MetaContext))(); context.registerAll(builtins); return function compare(lhs: T, rhs: T) { return context[$COMPARE](lhs, rhs); }; })(); describe('experimental.meta.operations.builtins.$compare', () => { describe('string', () => { test.each([ ['', '', '==='], ['', 'abc', '<'], ['abc', '', '>'], ['abc', 'abc', '==='], ['abc', 'abd', '<'], ['abd', 'abc', '>'], ['abc', 'abe', '<'], ['abca', 'abc', '>'], ['abc', 'abca', '<'], ])('string#%#', (lhs: string, rhs: string, result: string) => { assert.operator(compare(lhs, rhs), result, 0); }); }); describe('String', () => { test.each([ ['', '', '==='], ['', 'abc', '<'], ['abc', '', '>'], ['abc', 'abc', '==='], ['abc', 'abd', '<'], ['abd', 'abc', '>'], ['abc', 'abe', '<'], ['abca', 'abc', '>'], ['abc', 'abca', '<'], ])('String#%#', (lhsValue: string, rhsValue: string, result: string) => { const lhs = new String(lhsValue); const rhs = new String(rhsValue); assert.operator(compare(lhs, rhs), result, 0); }); }); describe('number', () => { test.each([ [0, 0, '==='], [1, 3, '<'], [3, 1, '>'], [3, 3, '==='] ])('number#%#', (lhs: number, rhs: number, result: string) => { assert.operator(compare(lhs, rhs), result, 0); }); }); describe('Number', () => { test.each([ [0, 0, '==='], [1, 3, '<'], [3, 1, '>'], [3, 3, '==='] ])('Number#%#', (lhsValue: number, rhsValue: number, result: string) => { const lhs = new Number(lhsValue); const rhs = new Number(rhsValue); assert.operator(compare(lhs, rhs), result, 0); }); }); describe('boolean', () => { test.each([ [false, false, '==='], [false, true, '<'], [true, false, '>'], [true, true, '==='] ])('boolean#%#', (lhs: boolean, rhs: boolean, result: string) => { assert.operator(compare(lhs, rhs), result, 0); }); }); describe('Boolean', () => { test.each([ [false, false, '==='], [false, true, '<'], [true, false, '>'], [true, true, '==='], [{}, [], '==='], [undefined, '', '==='], [undefined, 'abc', '<'], ])('Boolean#%#', (lhsValue: boolean, rhsValue: boolean, result: string) => { const lhs = new Boolean(lhsValue); const rhs = new Boolean(rhsValue); assert.operator(compare(lhs, rhs), result, 0); }); }); describe('undefined', () => { it('undefined', () => { assert.throws(() => { compare(undefined, undefined); }); }); }); describe('null', () => { it('null', () => { assert.throws(() => { compare(null, null); }); }); }); describe('Object', () => { test.each([ [{}, {}, '==='], [{ x: 1, y: 2 }, {}, '>'], [{}, { x: 1, y: 2 }, '<'], [{ x: 1, y: 2 }, { x: 1, y: 2 }, '==='], [{ x: 2, y: 2 }, { x: 1, y: 2 }, '>'], [{ x: 1, y: 3 }, { x: 1, y: 2 }, '>'], [{ x: 1, y: 2 }, { x: 2, y: 2 }, '<'], [{ x: 1, y: 2 }, { x: 1, y: 3 }, '<'], [{ x: 1, y: 2, z: -1 }, { x: 1, y: 2 }, '>'], [{ x: 1, y: 2, z: -1 }, { x: 1, y: 2, z: 0 }, '<'], [{ x: 1, y: 2 }, { x: 1, y: 2, z: -1 }, '<'], [{ d: 1, b: 2, c: 3 }, { d: 1, b: 2, c: 3 }, '==='], [{ d: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 }, '>'], [{ a: 1, b: 2, c: 3 }, { d: 1, b: 2, c: 3 }, '<'], ])('Object#%#', (lhs: any, rhs: any, result: string) => { assert.operator(compare(lhs, rhs), result, 0); }); }); describe('object', () => { test.each([ [{}, {}, '==='], [{ x: 1, y: 2 }, {}, '>'], [{}, { x: 1, y: 2 }, '<'], [{ x: 1, y: 2 }, { x: 1, y: 2 }, '==='], [{ x: 2, y: 2 }, { x: 1, y: 2 }, '>'], [{ x: 1, y: 3 }, { x: 1, y: 2 }, '>'], [{ x: 1, y: 2 }, { x: 2, y: 2 }, '<'], [{ x: 1, y: 2 }, { x: 1, y: 3 }, '<'], [{ x: 1, y: 2, z: -1 }, { x: 1, y: 2 }, '>'], [{ x: 1, y: 2, z: -1 }, { x: 1, y: 2, z: 0 }, '<'], [{ x: 1, y: 2 }, { x: 1, y: 2, z: -1 }, '<'], [{ d: 1, b: 2, c: 3 }, { d: 1, b: 2, c: 3 }, '==='], [{ d: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 }, '>'], [{ a: 1, b: 2, c: 3 }, { d: 1, b: 2, c: 3 }, '<'], ])('object#%#', (lhsValue: any, rhsValue: any, result: string) => { const lhs = assign(create(), lhsValue); const rhs = assign(create(), rhsValue); assert.operator(compare(lhs, rhs), result, 0); }); }); describe('Date', () => { const now = Date.now(); test.each([ [new Date(now), new Date(now), '==='], [new Date(now + 1000), new Date(now), '>'], [new Date(now), new Date(now + 1000), '<'], [new Date(2014, 1, 1), new Date(2014, 1, 1), '==='], ])('Date#%#', (lhs: Date, rhs: Date, result: string) => { assert.operator(compare(lhs, rhs), result, 0); }); }); describe('Array', () => { test.each([ [[], [], '==='], [[1, 2, 3], [], '>'], [[], [1, 2, 3], '<'], [[1, 2, 3], [1, 2, 3], '==='], [[1, 2, 3], [1, 2, 3, 4], '<'], [[1, 2, 3, 4], [1, 2, 3], '>'], [[2, 2, 3], [1, 2, 3], '>'], [[1, 2, 3], [2, 2, 3], '<'], [[''], [''], '==='], [['a', 'b', 'c'], [''], '>'], [[''], ['a', 'b', 'c'], '<'], [['a', 'b', 'c'], ['a', 'b', 'c'], '==='], [['a', 'b', 'd'], ['a', 'b', 'c'], '>'], [['a', 'b', 'c'], ['a', 'b', 'd'], '<'], [['a', 'b', 'c', 'a'], ['a', 'b', 'c'], '>'], [['a', 'b', 'c'], ['a', 'b', 'c', 'a'], '<'], [[{ x: 1, y: 2 }], [{ x: 1, y: 2 }], '==='], [[{ x: 2, y: 2 }], [{ x: 1, y: 2 }], '>'], [[{ x: 1, y: 2 }], [{ x: 2, y: 2 }], '<'], [[{ x: 1, y: 2 }, { x: 1, y: 2 }], [{ x: 1, y: 2 }], '>'], [[{ x: 1, y: 2 }], [{ x: 1, y: 2 }, { x: 1, y: 2 }], '<'], [[{ x: 1, y: 2 }, { x: 1, y: 2 }], [{ x: 1, y: 2 }, { x: 1, y: 2 }], '==='], ])('Array#%#', (lhs: any[], rhs: any[], result: string) => { assert.operator(compare(lhs, rhs), result, 0); }); }); describe('Map', () => { test.each([ [[], [], '==='], [[['a', 1], ['b', 2]], [], '>'], [[], [['a', 1], ['b', 2]], '<'], [[['a', 1], ['b', 2]], [['a', 1], ['b', 2]], '==='], [[['a', 1], ['b', 2], ['c', 3]], [['a', 1], ['b', 2]], '>'], [[['a', 1], ['b', 2]], [['a', 1], ['b', 2], ['c', 3]], '<'], [[['a', 2], ['b', 2]], [['a', 1], ['b', 2]], '>'], [[['a', 1], ['b', 2]], [['a', 2], ['b', 2]], '<'], [[['a', 1], ['c', 2]], [['a', 1], ['b', 2]], '>'], [[['a', 1], ['b', 2]], [['a', 1], ['c', 2]], '<'], [[[{ x: 1, y: 2 }, 3]], [[{ x: 1, y: 2 }, 3]], '==='], [[[{ x: 1, y: 3 }, 3]], [[{ x: 1, y: 2 }, 3]], '>'], [[[{ x: 1, y: 2 }, 3]], [[{ x: 1, y: 3 }, 3]], '<'], ])('Map#%#', (lhsValue: [any, any][], rhsValue: [any, any][], result: string) => { const lhs = new Map(lhsValue); const rhs = new Map(rhsValue); assert.operator(compare(lhs, rhs), result, 0); }); }); describe('Set', () => { test.each([ [[], [], '==='], [[1, 2, 3], [], '>'], [[], [1, 2, 3], '<'], [[1, 2, 3], [1, 2, 3], '==='], [[4, 2, 3], [1, 2, 3], '>'], [[1, 2, 3], [4, 2, 3], '<'], [['a', 'b', 'c'], ['a', 'b', 'c'], '==='], [['d', 'b', 'c'], ['a', 'b', 'c'], '>'], [['a', 'b', 'c'], ['d', 'b', 'c'], '<'], [[{ x: 1, y: 2 }], [{ x: 1, y: 2 }], '==='], [[{ x: 2, y: 2 }], [{ x: 1, y: 2 }], '>'], [[{ x: 1, y: 2 }], [{ x: 2, y: 2 }], '<'], [[{ x: 1, y: 2 }, { x: 1, y: 2 }], [{ x: 1, y: 2 }], '>'], [[{ x: 1, y: 2 }], [{ x: 1, y: 2 }, { x: 1, y: 2 }], '<'], [[{ x: 1, y: 2 }, { x: 1, y: 2 }], [{ x: 1, y: 2 }, { x: 1, y: 2 }], '==='], ])('Set#%#', (lhsValue: any[], rhsValue: any[], result: string) => { const lhs = new Set(lhsValue); const rhs = new Set(rhsValue); assert.operator(compare(lhs, rhs), result, 0); }); }); describe('TypedArrays', () => { describe('Uint8Array', () => { test.each([ [[], [], '==='], [[21, 31], [21, 31], '==='], [[21, 31], [21, 31, 41], '<'], [[21, 31, 41], [21, 31], '>'], [[21, 31], [21, 11], '>'], [[21, 31], [21, 41], '<'], ])('Uint8Array#%#', (lhsValue: number[], rhsValue: number[], result: string) => { const lhs = new Uint8Array(lhsValue); const rhs = new Uint8Array(rhsValue); assert.operator(compare(lhs, rhs), result, 0); }); }); }); });