Utils.spec.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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('getRandomVariableName (length: number = 6): string', () => {
  37. it('should return a string of given length with random variable name', () => {
  38. assert.match(Utils.getRandomVariableName(4), /^_0x(\w){4}$/);
  39. assert.match(Utils.getRandomVariableName(6), /^_0x(\w){6}$/);
  40. });
  41. });
  42. describe('isInteger (number: number): boolean', () => {
  43. it('should return true if given number or string is integer', () => {
  44. assert.equal(Utils.isInteger(4), true);
  45. assert.equal(Utils.isInteger(<any>'4'), true);
  46. assert.equal(Utils.isInteger(<any>'a'), false);
  47. });
  48. });
  49. describe('stringToJSFuck (string: string): string', () => {
  50. let expected: string = `${JSFuck.s} + ${JSFuck.t} + ${JSFuck.r} + ${JSFuck.i} + ${JSFuck.n} + ${JSFuck.g}`;
  51. it('should creates a JSFuck encoded string from a given string', () => {
  52. assert.equal(Utils.stringToJSFuck('string'), expected);
  53. });
  54. });
  55. describe('stringToUnicode (string: string): string', () => {
  56. let expected: string = `'\\x73\\x74\\x72\\x69\\x6e\\x67'`;
  57. it('should return a unicode encoded string from a given string', () => {
  58. assert.equal(Utils.stringToUnicode('string'), expected);
  59. });
  60. });
  61. });