Utils.spec.ts 3.3 KB

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