123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- import 'reflect-metadata';
- import { assert } from 'chai';
- import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
- import { IArrayUtils } from '../../../src/interfaces/utils/IArrayUtils';
- import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
- import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
- describe('ArrayUtils', () => {
- let arrayUtils: IArrayUtils;
- before(() => {
- const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
- inversifyContainerFacade.load('', '', {});
- arrayUtils = inversifyContainerFacade.get<IArrayUtils>(ServiceIdentifiers.IArrayUtils);
- });
- describe('createWithRange', () => {
- describe('range length more than 0', () => {
- const rangeLength: number = 5;
- const expectedArray: number[] = [0, 1, 2, 3, 4];
- let array: number[];
- before(() => {
- array = arrayUtils.createWithRange(rangeLength);
- });
- it('should return array with range of numbers', () => {
- assert.deepEqual(array, expectedArray);
- });
- });
- describe('range length is 0', () => {
- const rangeLength: number = 0;
- const expectedArray: number[] = [];
- let array: number[];
- before(() => {
- array = arrayUtils.createWithRange(rangeLength);
- });
- it('should return empty array', () => {
- assert.deepEqual(array, expectedArray);
- });
- });
- describe('range length less than 0', () => {
- const rangeLength: number = -5;
- const expectedArray: number[] = [];
- let array: number[];
- before(() => {
- array = arrayUtils.createWithRange(rangeLength);
- });
- it('should return empty array', () => {
- assert.deepEqual(array, expectedArray);
- });
- });
- });
- describe('fillWithRange', () => {
- const valueFunction: (index: number) => string = (index: number) => `foo${index}`;
- describe('range length more than 0', () => {
- const rangeLength: number = 5;
- const expectedArray: string[] = [
- 'foo0',
- 'foo1',
- 'foo2',
- 'foo3',
- 'foo4',
- ];
- let array: string[];
- before(() => {
- array = arrayUtils.fillWithRange(rangeLength, valueFunction);
- });
- it('should return array with range of strings', () => {
- assert.deepEqual(array, expectedArray);
- });
- });
- describe('range length is 0', () => {
- const rangeLength: number = 0;
- const expectedArray: string[] = [];
- let array: string[];
- before(() => {
- array = arrayUtils.fillWithRange(rangeLength, valueFunction);
- });
- it('should return empty array', () => {
- assert.deepEqual(array, expectedArray);
- });
- });
- describe('range length less than 0', () => {
- const rangeLength: number = -5;
- const expectedArray: string[] = [];
- let array: string[];
- before(() => {
- array = arrayUtils.fillWithRange(rangeLength, valueFunction);
- });
- it('should return empty array', () => {
- assert.deepEqual(array, expectedArray);
- });
- });
- });
- describe('findMostOccurringElement', () => {
- describe('empty array', () => {
- const array: string[] = [];
- const expectedMostOccurringElement: null = null;
- let mostOccurringElement: string | null;
- before(() => {
- mostOccurringElement = arrayUtils.findMostOccurringElement(array);
- });
- it('should return null if array is empty', () => {
- assert.equal(mostOccurringElement, expectedMostOccurringElement);
- });
- });
- describe('one elements is most occurring', () => {
- const array: string[] = ['foo', 'bar', 'bar', 'baz', 'bar', 'foo'];
- const expectedMostOccurringElement: string = 'bar';
- let mostOccurringElement: string | null;
- before(() => {
- mostOccurringElement = arrayUtils.findMostOccurringElement(array);
- });
- it('should return most occurring element', () => {
- assert.equal(mostOccurringElement, expectedMostOccurringElement);
- });
- });
- describe('few elements are most occurring', () => {
- const array: string[] = ['foo', 'bar', 'bar', 'baz', 'bar'];
- const expectedMostOccurringElement: string = 'bar';
- let mostOccurringElement: string | null;
- before(() => {
- mostOccurringElement = arrayUtils.findMostOccurringElement(array);
- });
- it('should return first most occurring element', () => {
- assert.equal(mostOccurringElement, expectedMostOccurringElement);
- });
- });
- });
- describe('getLastElement', () => {
- describe('empty array', () => {
- const array: string[] = [];
- const expectedLastElement: undefined = undefined;
- let lastElement: string | undefined;
- before(() => {
- lastElement = arrayUtils.getLastElement(array);
- });
- it('should return undefined if array is empty', () => {
- assert.equal(lastElement, expectedLastElement);
- });
- });
- describe('array length: `1`', () => {
- const array: string[] = ['foo'];
- const expectedLastElement: string = 'foo';
- let lastElement: string | undefined;
- before(() => {
- lastElement = arrayUtils.getLastElement(array);
- });
- it('should return first element for array with length: `1`', () => {
- assert.equal(lastElement, expectedLastElement);
- });
- });
- describe('array length: `3`', () => {
- const array: string[] = ['foo', 'bar', 'baz'];
- const expectedLastElement: string = 'baz';
- let lastElement: string | undefined;
- before(() => {
- lastElement = arrayUtils.getLastElement(array);
- });
- it('should return last element for array with length: `3`', () => {
- assert.equal(lastElement, expectedLastElement);
- });
- });
- });
- describe('getLastElementByIndex', () => {
- describe('empty array', () => {
- const array: string[] = [];
- const expectedLastElement: undefined = undefined;
- let lastElement: string | undefined;
- before(() => {
- lastElement = arrayUtils.getLastElementByIndex(array, 1);
- });
- it('should return undefined if array is empty', () => {
- assert.equal(lastElement, expectedLastElement);
- });
- });
- describe('array length: `1` and index is out of array boundary', () => {
- const array: string[] = ['foo'];
- const expectedLastElement: undefined = undefined;
- let lastElement: string | undefined;
- before(() => {
- lastElement = arrayUtils.getLastElementByIndex(array, 2);
- });
- it('should return undefined', () => {
- assert.equal(lastElement, expectedLastElement);
- });
- });
- describe('array length: `3` and index is `0`', () => {
- const array: string[] = ['foo', 'bar', 'baz'];
- const expectedLastElement: string = 'baz';
- let lastElement: string | undefined;
- before(() => {
- lastElement = arrayUtils.getLastElementByIndex(array, 0);
- });
- it('should return element with a correct index', () => {
- assert.equal(lastElement, expectedLastElement);
- });
- });
- describe('array length: `3` and index is `1`', () => {
- const array: string[] = ['foo', 'bar', 'baz'];
- const expectedLastElement: string = 'bar';
- let lastElement: string | undefined;
- before(() => {
- lastElement = arrayUtils.getLastElementByIndex(array, 1);
- });
- it('should return element with a correct index', () => {
- assert.equal(lastElement, expectedLastElement);
- });
- });
- });
- describe('rotate', () => {
- let array: number[],
- rotatedArray: number[];
- beforeEach(() => {
- array = [1, 2, 3, 4, 5, 6];
- });
- describe('value is not 0', () => {
- const rotateValue: number = 2;
- const expectedArray: number[] = [5, 6, 1, 2, 3, 4];
- beforeEach(() => {
- rotatedArray = arrayUtils.rotate(array, rotateValue);
- });
- it('should rotate (shift) array by a given value', () => {
- assert.deepEqual(rotatedArray, expectedArray);
- });
- });
- describe('value equals or less 0', () => {
- const rotateValue: number = 0;
- const expectedArray: number[] = [1, 2, 3, 4, 5, 6];
- beforeEach(() => {
- rotatedArray = arrayUtils.rotate(array, rotateValue);
- });
- it('shouldn\'t rotate array', () => {
- assert.deepEqual(rotatedArray, expectedArray);
- });
- });
- describe('empty array', () => {
- const emptyArray: number[] = [];
- const rotateValue: number = 5;
- const expectedError: ReferenceErrorConstructor = ReferenceError;
- let testFunc: () => void;
- beforeEach(() => {
- testFunc = () => arrayUtils.rotate(emptyArray, rotateValue);
- });
- it('should throw exception if array is empty', () => {
- assert.throws(testFunc, expectedError);
- });
- });
- });
- });
|