ArrayUtils.spec.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { assert } from 'chai';
  2. import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
  3. import { IArrayUtils } from '../../../src/interfaces/utils/IArrayUtils';
  4. import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
  5. import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
  6. describe('ArrayUtils', () => {
  7. let arrayUtils: IArrayUtils;
  8. before(() => {
  9. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  10. inversifyContainerFacade.load('', {});
  11. arrayUtils = inversifyContainerFacade.get<IArrayUtils>(ServiceIdentifiers.IArrayUtils);
  12. });
  13. describe('createWithRange', () => {
  14. describe('range length more than 0', () => {
  15. const rangeLength: number = 5;
  16. const expectedArray: number[] = [0, 1, 2, 3, 4];
  17. let array: number[];
  18. before(() => {
  19. array = arrayUtils.createWithRange(rangeLength);
  20. });
  21. it('should return array with range of numbers', () => {
  22. assert.deepEqual(array, expectedArray);
  23. });
  24. });
  25. describe('range length is 0', () => {
  26. const rangeLength: number = 0;
  27. const expectedArray: number[] = [];
  28. let array: number[];
  29. before(() => {
  30. array = arrayUtils.createWithRange(rangeLength);
  31. });
  32. it('should return empty array', () => {
  33. assert.deepEqual(array, expectedArray);
  34. });
  35. });
  36. describe('range length less than 0', () => {
  37. const rangeLength: number = -5;
  38. const expectedArray: number[] = [];
  39. let array: number[];
  40. before(() => {
  41. array = arrayUtils.createWithRange(rangeLength);
  42. });
  43. it('should return empty array', () => {
  44. assert.deepEqual(array, expectedArray);
  45. });
  46. });
  47. });
  48. describe('rotate', () => {
  49. let array: number[],
  50. rotatedArray: number[];
  51. beforeEach(() => {
  52. array = [1, 2, 3, 4, 5, 6];
  53. });
  54. describe('value is not 0', () => {
  55. const rotateValue: number = 2;
  56. const expectedArray: number[] = [5, 6, 1, 2, 3, 4];
  57. beforeEach(() => {
  58. rotatedArray = arrayUtils.rotate(array, rotateValue);
  59. });
  60. it('should rotate (shift) array by a given value', () => {
  61. assert.deepEqual(rotatedArray, expectedArray);
  62. });
  63. });
  64. describe('value equals or less 0', () => {
  65. const rotateValue: number = 0;
  66. const expectedArray: number[] = [1, 2, 3, 4, 5, 6];
  67. beforeEach(() => {
  68. rotatedArray = arrayUtils.rotate(array, rotateValue);
  69. });
  70. it('shouldn\'t rotate array', () => {
  71. assert.deepEqual(rotatedArray, expectedArray);
  72. });
  73. });
  74. describe('empty array', () => {
  75. const emptyArray: number[] = [];
  76. const rotateValue: number = 5;
  77. const expectedError: ReferenceErrorConstructor = ReferenceError;
  78. let testFunc: () => void;
  79. beforeEach(() => {
  80. testFunc = () => arrayUtils.rotate(emptyArray, rotateValue);
  81. });
  82. it('should throw exception if array is empty', () => {
  83. assert.throws(testFunc, expectedError);
  84. });
  85. });
  86. });
  87. });