Utils.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { Utils } from '../src/Utils';
  2. import { JSFuck } from '../src/enums/JSFuck';
  3. let assert: any = require('chai').assert;
  4. describe('Utils', () => {
  5. describe('arrayContains (array: any[], searchElement: any): boolean', () => {
  6. it('should return boolean depends on condition if array is contains given value or not', () => {
  7. assert.equal(Utils.arrayContains(['1', '2', '3'], '2'), true);
  8. assert.equal(Utils.arrayContains([1, 2, 3], 2), true);
  9. assert.equal(Utils.arrayContains([1, 2, 3], 4), false);
  10. });
  11. });
  12. describe('arrayRotate <T> (array: T[], times: number, reverse: boolean = false): T[]', () => {
  13. let array: number[];
  14. beforeEach(() => {
  15. array = [1, 2, 3, 4, 5, 6];
  16. });
  17. it('should rotate (shift) array by a given value', () => {
  18. assert.deepEqual(Utils.arrayRotate(array, 2), [5, 6, 1, 2, 3, 4]);
  19. });
  20. it('should rotate (shift) array by a given value in reverse directions', () => {
  21. assert.deepEqual(Utils.arrayRotate(array, 2, true), [3, 4, 5, 6, 1, 2]);
  22. });
  23. });
  24. describe('btoa (string: string): string', () => {
  25. it('should creates a base-64 encoded string from a given string', () => {
  26. assert.equal(Utils.btoa('string'), 'c3RyaW5n');
  27. });
  28. });
  29. describe('decToHex (dec: number): string', () => {
  30. it('should creates a string with hexadecimal value from a given decimal number', () => {
  31. assert.equal(Utils.decToHex(0), '0');
  32. assert.equal(Utils.decToHex(10), 'a');
  33. assert.equal(Utils.decToHex(17), '11');
  34. });
  35. });
  36. describe('getRandomInteger (min: number, max: number): number', () => {
  37. let values: number[] = [],
  38. randomValue: number;
  39. beforeEach(() => {
  40. for (let i = 0; i < 200; i++) {
  41. randomValue = Utils.getRandomInteger(0, 100);
  42. if (values.indexOf(randomValue) === -1) {
  43. values.push(randomValue);
  44. }
  45. }
  46. values.sort((a, b) => {
  47. return a - b;
  48. });
  49. });
  50. it('should return a random integer between two ranges', () => {
  51. assert.isAtLeast(values[0], 0);
  52. assert.isAtMost(values[values.length - 1], 100);
  53. });
  54. });
  55. describe('getRandomVariableName (length: number = 6): string', () => {
  56. it('should return a string of given length with random variable name', () => {
  57. assert.match(Utils.getRandomVariableName(4), /^_0x(\w){4}$/);
  58. assert.match(Utils.getRandomVariableName(6), /^_0x(\w){6}$/);
  59. });
  60. });
  61. describe('isInteger (number: number): boolean', () => {
  62. it('should return true if given number or string is integer', () => {
  63. assert.equal(Utils.isInteger(4), true);
  64. assert.equal(Utils.isInteger(<any>'4'), true);
  65. assert.equal(Utils.isInteger(<any>'a'), false);
  66. });
  67. });
  68. describe('stringToJSFuck (string: string): string', () => {
  69. let expected: string = `${JSFuck.s} + ${JSFuck.t} + ${JSFuck.r} + ${JSFuck.i} + ${JSFuck.n} + ${JSFuck.g}`;
  70. it('should creates a JSFuck encoded string from a given string', () => {
  71. assert.equal(Utils.stringToJSFuck('string'), expected);
  72. });
  73. });
  74. describe('stringToUnicode (string: string): string', () => {
  75. let expected: string = `'\\x73\\x74\\x72\\x69\\x6e\\x67'`;
  76. it('should return a unicode encoded string from a given string', () => {
  77. assert.equal(Utils.stringToUnicode('string'), expected);
  78. });
  79. });
  80. });