Utils.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { assert } from 'chai';
  2. import { Utils } from '../../../src/utils/Utils';
  3. import { JSFuck } from '../../../src/enums/JSFuck';
  4. describe('Utils', () => {
  5. describe('arrayRotate <T> (array: T[], times: number): T[]', () => {
  6. let array: number[];
  7. beforeEach(() => {
  8. array = [1, 2, 3, 4, 5, 6];
  9. });
  10. it('should rotate (shift) array by a given value', () => {
  11. assert.deepEqual(Utils.arrayRotate(array, 2), [5, 6, 1, 2, 3, 4]);
  12. });
  13. it('should do nothing if value <= 0', () => {
  14. assert.deepEqual(Utils.arrayRotate(array, 0), [1, 2, 3, 4, 5, 6]);
  15. assert.deepEqual(Utils.arrayRotate(array, -1), [1, 2, 3, 4, 5, 6]);
  16. });
  17. it('should throw exception if array is empty', () => {
  18. assert.throws(() => Utils.arrayRotate([], 5), ReferenceError);
  19. });
  20. });
  21. describe('decToHex (dec: number): string', () => {
  22. it('should creates a string with hexadecimal value from a given decimal number', () => {
  23. assert.equal(Utils.decToHex(0), '0');
  24. assert.equal(Utils.decToHex(10), 'a');
  25. assert.equal(Utils.decToHex(17), '11');
  26. assert.equal(Utils.decToHex(536870912), '20000000');
  27. });
  28. });
  29. describe('extractDomainFromUrl (url: string): string', () => {
  30. it('should extract domain from the given URL', () => {
  31. assert.equal(Utils.extractDomainFromUrl('http://google.ru'), 'google.ru');
  32. assert.equal(Utils.extractDomainFromUrl('http://www.google.ru'), 'www.google.ru');
  33. assert.equal(Utils.extractDomainFromUrl('https://www.google.ru:9000'), 'www.google.ru');
  34. assert.equal(Utils.extractDomainFromUrl('//google.ru/abc'), 'google.ru');
  35. assert.equal(Utils.extractDomainFromUrl('//localhost:9000'), 'localhost');
  36. });
  37. });
  38. describe('isCeilNumber (number: number): boolean', () => {
  39. it('should return true if given number is a ceil', () => {
  40. assert.equal(Utils.isCeilNumber(4), true);
  41. assert.equal(Utils.isCeilNumber(4.5), false);
  42. });
  43. });
  44. describe('mapGetFirstKeyOf(map: Map <any, any>, value: any): any', () => {
  45. it('should returns key of map item', () => {
  46. const map: Map <any, any> = new Map();
  47. map.set('number1', 1);
  48. map.set('number2', 2);
  49. map.set('number3', 2);
  50. map.set('string1', 'foo');
  51. map.set('string2', 'bar');
  52. map.set('object1', {item: 'value'});
  53. map.set('object2', {item: 'value'});
  54. map.set({key: 'object'}, [1, 2, 3]);
  55. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 1), 'number1');
  56. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 2), 'number2');
  57. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 'foo'), 'string1');
  58. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 'bar'), 'string2');
  59. assert.deepEqual(Utils.mapGetFirstKeyOf(map, {item: 'value'}), 'object1');
  60. assert.deepEqual(Utils.mapGetFirstKeyOf(map, [1, 2, 3]), {key: 'object'});
  61. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 3), null);
  62. });
  63. });
  64. describe('stringRotate (string: string, times: number): string', () => {
  65. let string: string;
  66. beforeEach(() => {
  67. string = 'abcdefg';
  68. });
  69. it('should rotate string by a given value', () => {
  70. assert.deepEqual(Utils.stringRotate(string, 2), 'fgabcde');
  71. });
  72. it('should do nothing if value <= 0', () => {
  73. assert.deepEqual(Utils.stringRotate(string, 0), 'abcdefg');
  74. assert.deepEqual(Utils.stringRotate(string, -1), 'abcdefg');
  75. });
  76. it('should throw exception if string is empty', () => {
  77. assert.throws(() => Utils.stringRotate('', 5), ReferenceError);
  78. });
  79. });
  80. describe('stringToJSFuck (string: string): string', () => {
  81. let expected: string = `${JSFuck.s} + ${JSFuck.t} + ${JSFuck.r} + ${JSFuck.i} + ${JSFuck.n} + ${JSFuck.g}`;
  82. it('should creates a JSFuck encoded string from a given string', () => {
  83. assert.equal(Utils.stringToJSFuck('string'), expected);
  84. });
  85. });
  86. describe('stringToUnicodeEscapeSequence (string: string): string', () => {
  87. let expected: string = '\\x73\\x74\\x72\\x69\\x6e\\x67';
  88. it('should return a unicode escape sequence based on a given string', () => {
  89. assert.equal(Utils.stringToUnicodeEscapeSequence('string'), expected);
  90. });
  91. });
  92. });