123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- import 'reflect-metadata';
- import { assert } from 'chai';
- import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
- import { IArrayStorage } from '../../../src/interfaces/storages/IArrayStorage';
- import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
- import { IOptions } from '../../../src/interfaces/options/IOptions';
- import { IRandomGenerator } from '../../../src/interfaces/utils/IRandomGenerator';
- import { ArrayStorage } from '../../../src/storages/ArrayStorage';
- import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
- class ConcreteStorage <V> extends ArrayStorage <V> {
- constructor () {
- const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
- inversifyContainerFacade.load('', '', {});
- super(
- inversifyContainerFacade.get<IRandomGenerator>(ServiceIdentifiers.IRandomGenerator),
- inversifyContainerFacade.get<IOptions>(ServiceIdentifiers.IOptions)
- );
- }
- }
- /**
- * @returns {IArrayStorage<V>}
- */
- const getStorageInstance = <V> (): IArrayStorage <V> => {
- const storage: IArrayStorage <V> = new ConcreteStorage <V> ();
- storage.initialize();
- return storage;
- };
- describe('ArrayStorage', () => {
- const storageKey: number = 0;
- const storageValue: string = 'foo';
- let storage: IArrayStorage <any>;
- describe('initialize', () => {
- const expectedError: ErrorConstructor = Error;
- let testFunc: () => void;
- before(() => {
- storage = new ConcreteStorage<string>();
- testFunc = () => storage.set(storageKey, storageValue);
- });
- it('should throws an error when storage isn\'t initialized', () => {
- assert.throws(testFunc, expectedError);
- });
- });
- describe('getStorage', () => {
- const expectedInstanceOf: ArrayConstructor = Array;
- let arrayStorage: string[];
- before(() => {
- storage = getStorageInstance<string>();
- arrayStorage = storage.getStorage();
- });
- it('should return storage', () => {
- assert.instanceOf(arrayStorage, expectedInstanceOf);
- });
- });
- describe('getStorageId', () => {
- const storageIdRegExp: RegExp = /^[a-zA-Z0-9]{6}$/;
- let storageId: string;
- before(() => {
- storage = getStorageInstance<string>();
- storage.set(storageKey, storageValue);
- storageId = storage.getStorageId();
- });
- it('should return storage id', () => {
- assert.match(storageId, storageIdRegExp);
- });
- });
- describe('get', () => {
- describe('Variant #1: value exist', () => {
- const expectedValue: string = storageValue;
- let value: string;
- before(() => {
- storage = getStorageInstance<string>();
- storage.set(storageKey, storageValue);
- value = storage.get(storageKey);
- });
- it('should return value from storage by key', () => {
- assert.equal(value, expectedValue);
- });
- });
- describe('Variant #2: value isn\'t exist', () => {
- const expectedValue: undefined = undefined;
- let value: string;
- before(() => {
- storage = getStorageInstance<string>();
- value = storage.get(storageKey);
- });
- it('should return undefined if value does not exist in the storage', () => {
- assert.equal(value, expectedValue);
- });
- });
- });
- describe('getOrThrow', () => {
- describe('Variant #1: value exist', () => {
- const expectedValue: string = storageValue;
- let value: string;
- before(() => {
- storage = getStorageInstance<string>();
- storage.set(storageKey, storageValue);
- value = storage.getOrThrow(storageKey);
- });
- it('should return value from storage by key', () => {
- assert.equal(value, expectedValue);
- });
- });
- describe('Variant #2: value isn\'t exist', () => {
- const expectedError: ErrorConstructor = Error;
- let testFunc: () => void;
- before(() => {
- storage = getStorageInstance<string>();
- testFunc = () => storage.getOrThrow(storageKey);
- });
- it('should throw an error', () => {
- assert.throws(testFunc, expectedError);
- });
- });
- });
- describe('getLength', () => {
- const expectedStorageLength: number = 1;
- let storageLength: number;
- before(() => {
- storage = getStorageInstance<string>();
- storage.set(storageKey, storageValue);
- storageLength = storage.getLength();
- });
- it('should return length of storage', () => {
- assert.equal(storageLength, expectedStorageLength);
- });
- });
- describe('getKeyOf', () => {
- let key: string | number | null;
- describe('Variant #1', () => {
- before(() => {
- storage = getStorageInstance<string>();
- storage.set(storageKey, storageValue);
- key = storage.getKeyOf(storageValue);
- });
- it('should return key of string value', () => {
- assert.equal(key, storageKey);
- });
- });
- describe('Variant #2', () => {
- const object: Object = {
- foo: 'bar'
- };
- before(() => {
- storage = getStorageInstance<string>();
- storage.set(storageKey, object);
- key = storage.getKeyOf(object);
- });
- it('should return key of object if objects in `set` and `get` are two same objects', () => {
- assert.equal(key, storageKey);
- });
- });
- describe('Variant #3', () => {
- const expectedKey: null = null;
- const object: Object = {
- foo: 'bar'
- };
- before(() => {
- storage = getStorageInstance<string>();
- storage.set(storageKey, object);
- key = storage.getKeyOf({...object});
- });
- it('should return `null` if objects in `set` and `get` are two different objects', () => {
- assert.equal(key, expectedKey);
- });
- });
- });
- describe('set', () => {
- let value: string;
- before(() => {
- storage = getStorageInstance<string>();
- storage.set(storageKey, storageValue);
- value = storage.get(storageKey);
- });
- it('should set value to the storage', () => {
- assert.equal(value, storageValue);
- });
- });
- describe('delete', () => {
- describe('Variant #1: value exist', () => {
- const expectedUpdatedStorage: string[] = [
- 'foo',
- 'baz'
- ];
- const expectedUpdatedStorageLength: number = 2;
- const expectedDeletedValue: string = 'bar';
- let updatedStorage: string[];
- let updatedStorageLength: number;
- let deletedValue: string;
- before(() => {
- storage = getStorageInstance<string>();
- storage.set(0, 'foo');
- storage.set(1, 'bar');
- storage.set(2, 'baz');
- deletedValue = storage.delete(1);
- updatedStorage = storage.getStorage();
- updatedStorageLength = storage.getLength();
- });
- it('should delete value from the storage by index', () => {
- assert.deepEqual(updatedStorage, expectedUpdatedStorage);
- });
- it('should update storage length', () => {
- assert.deepEqual(updatedStorageLength, expectedUpdatedStorageLength);
- });
- it('should return deleted value', () => {
- assert.equal(deletedValue, expectedDeletedValue);
- });
- });
- describe('Variant #2: value isn\'t exist', () => {
- const expectedUpdatedStorage: string[] = [
- 'foo',
- 'bar',
- 'baz'
- ];
- const expectedUpdatedStorageLength: number = 3;
- const expectedDeletedValue: undefined = undefined;
- let updatedStorage: string[];
- let updatedStorageLength: number;
- let deletedValue: string;
- before(() => {
- storage = getStorageInstance<string>();
- storage.set(0, 'foo');
- storage.set(1, 'bar');
- storage.set(2, 'baz');
- deletedValue = storage.delete(3);
- updatedStorage = storage.getStorage();
- updatedStorageLength = storage.getLength();
- });
- it('should keep storage the same', () => {
- assert.deepEqual(updatedStorage, expectedUpdatedStorage);
- });
- it('should keep storage length', () => {
- assert.deepEqual(updatedStorageLength, expectedUpdatedStorageLength);
- });
- it('should return undefined', () => {
- assert.equal(deletedValue, expectedDeletedValue);
- });
- });
- });
- describe('mergeWith', () => {
- describe('Base merge', () => {
- const secondStorageKey: number = 1;
- const secondStorageValue: string = 'bar';
- const expectedArray: string[] = [storageValue, secondStorageValue];
- let array: string[];
- before(() => {
- storage = getStorageInstance<string>();
- storage.set(storageKey, storageValue);
- const secondStorage: IArrayStorage <string> = getStorageInstance<string>();
- secondStorage.set(secondStorageKey, secondStorageValue);
- storage.mergeWith(secondStorage, false);
- array = storage.getStorage();
- });
- it('should merge two storages', () => {
- assert.deepEqual(array, expectedArray);
- });
- });
- describe('Merge with storage id', () => {
- const secondStorageKey: number = 1;
- const secondStorageValue: string = 'bar';
- const expectedArray: string[] = [storageValue, secondStorageValue];
- let array: string[];
- let storageId: string;
- let expectedStorageId: string;
- before(() => {
- storage = getStorageInstance<string>();
- storage.set(storageKey, storageValue);
- const secondStorage: IArrayStorage <string> = getStorageInstance<string>();
- expectedStorageId = secondStorage.getStorageId();
- secondStorage.set(secondStorageKey, secondStorageValue);
- storage.mergeWith(secondStorage, true);
- storageId = storage.getStorageId();
- array = storage.getStorage();
- });
- it('should update storage id', () => {
- assert.deepEqual(storageId, expectedStorageId);
- });
- it('should merge two storages', () => {
- assert.deepEqual(array, expectedArray);
- });
- });
- });
- });
|