Utils.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Utils } from '../src/Utils';
  2. import { JSFuck } from '../src/enums/JSFuck';
  3. const assert: Chai.AssertStatic = 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. it('should do nothing if value <= 0', () => {
  24. assert.deepEqual(Utils.arrayRotate(array, 0, true), [1, 2, 3, 4, 5, 6]);
  25. assert.deepEqual(Utils.arrayRotate(array, -1, true), [1, 2, 3, 4, 5, 6]);
  26. });
  27. });
  28. describe('btoa (string: string): string', () => {
  29. it('should creates a base-64 encoded string from a given string', () => {
  30. assert.equal(Utils.btoa('string'), 'c3RyaW5n');
  31. });
  32. });
  33. describe('decToHex (dec: number): string', () => {
  34. it('should creates a string with hexadecimal value from a given decimal number', () => {
  35. assert.equal(Utils.decToHex(0), '0');
  36. assert.equal(Utils.decToHex(10), 'a');
  37. assert.equal(Utils.decToHex(17), '11');
  38. });
  39. });
  40. describe('getRandomVariableName (length: number = 6): string', () => {
  41. it('should return a string of given length with random variable name', () => {
  42. assert.match(Utils.getRandomVariableName(4), /^_0x(\w){4}$/);
  43. assert.match(Utils.getRandomVariableName(6), /^_0x(\w){4,6}$/);
  44. });
  45. });
  46. describe('isInteger (number: number): boolean', () => {
  47. it('should return true if given number or string is integer', () => {
  48. assert.equal(Utils.isInteger(4), true);
  49. assert.equal(Utils.isInteger(<any>'4'), true);
  50. assert.equal(Utils.isInteger(<any>'a'), false);
  51. });
  52. });
  53. describe('stringToJSFuck (string: string): string', () => {
  54. let expected: string = `${JSFuck.s} + ${JSFuck.t} + ${JSFuck.r} + ${JSFuck.i} + ${JSFuck.n} + ${JSFuck.g}`;
  55. it('should creates a JSFuck encoded string from a given string', () => {
  56. assert.equal(Utils.stringToJSFuck('string'), expected);
  57. });
  58. });
  59. describe('stringToUnicode (string: string): string', () => {
  60. let expected: string = `'\\x73\\x74\\x72\\x69\\x6e\\x67'`;
  61. it('should return a unicode encoded string from a given string', () => {
  62. assert.equal(Utils.stringToUnicode('string'), expected);
  63. });
  64. });
  65. });