Utils.spec.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { assert } from 'chai';
  2. import { JSFuck } from '../../../src/enums/JSFuck';
  3. import { Utils } from '../../../src/utils/Utils';
  4. describe('Utils', () => {
  5. describe('decToHex (dec: number): string', () => {
  6. describe('variant #1: number `0`', () => {
  7. const number: number = 0;
  8. const expectedHexString = '0';
  9. let hexString: string;
  10. before(() => {
  11. hexString = Utils.decToHex(number);
  12. });
  13. it('should create a string with hexadecimal value from a given decimal number', () => {
  14. assert.equal(hexString, expectedHexString);
  15. });
  16. });
  17. describe('variant #2: number `10`', () => {
  18. const number: number = 10;
  19. const expectedHexString = 'a';
  20. let hexString: string;
  21. before(() => {
  22. hexString = Utils.decToHex(number);
  23. });
  24. it('should create a string with hexadecimal value from a given decimal number', () => {
  25. assert.equal(hexString, expectedHexString);
  26. });
  27. });
  28. describe('variant #3: number `17`', () => {
  29. const number: number = 17;
  30. const expectedHexString = '11';
  31. let hexString: string;
  32. before(() => {
  33. hexString = Utils.decToHex(number);
  34. });
  35. it('should create a string with hexadecimal value from a given decimal number', () => {
  36. assert.equal(hexString, expectedHexString);
  37. });
  38. });
  39. describe('variant #4: number `536870912`', () => {
  40. const number: number = 536870912;
  41. const expectedHexString = '20000000';
  42. let hexString: string;
  43. before(() => {
  44. hexString = Utils.decToHex(number);
  45. });
  46. it('should create a string with hexadecimal value from a given decimal number', () => {
  47. assert.equal(hexString, expectedHexString);
  48. });
  49. });
  50. });
  51. describe('extractDomainFromUrl (url: string): string', () => {
  52. describe('variant #1: simple url', () => {
  53. const url: string = 'http://google.ru';
  54. const expectedDomain: string = 'google.ru';
  55. let domain: string;
  56. before(() => {
  57. domain = Utils.extractDomainFromUrl(url);
  58. });
  59. it('should extract domain from the given URL', () => {
  60. assert.equal(domain, expectedDomain);
  61. });
  62. });
  63. describe('variant #2: url with `www` part', () => {
  64. const url: string = 'http://www.google.ru';
  65. const expectedDomain: string = 'www.google.ru';
  66. let domain: string;
  67. before(() => {
  68. domain = Utils.extractDomainFromUrl(url);
  69. });
  70. it('should extract domain from the given URL', () => {
  71. assert.equal(domain, expectedDomain);
  72. });
  73. });
  74. describe('variant #3: url with `https` protocol and port', () => {
  75. const url: string = 'https://www.google.ru:9000';
  76. const expectedDomain: string = 'www.google.ru';
  77. let domain: string;
  78. before(() => {
  79. domain = Utils.extractDomainFromUrl(url);
  80. });
  81. it('should extract domain from the given URL', () => {
  82. assert.equal(domain, expectedDomain);
  83. });
  84. });
  85. describe('variant #4: protocol-wide url and route', () => {
  86. const url: string = '//google.ru/abc';
  87. const expectedDomain: string = 'google.ru';
  88. let domain: string;
  89. before(() => {
  90. domain = Utils.extractDomainFromUrl(url);
  91. });
  92. it('should extract domain from the given URL', () => {
  93. assert.equal(domain, expectedDomain);
  94. });
  95. });
  96. describe('variant #5: protocol-wide url, `localhost` and port', () => {
  97. const url: string = '//localhost:9000';
  98. const expectedDomain: string = 'localhost';
  99. let domain: string;
  100. before(() => {
  101. domain = Utils.extractDomainFromUrl(url);
  102. });
  103. it('should extract domain from the given URL', () => {
  104. assert.equal(domain, expectedDomain);
  105. });
  106. });
  107. });
  108. describe('isCeilNumber (number: number): boolean', () => {
  109. describe('given number is a ceil', () => {
  110. const number: number = 4;
  111. const expectedResult: boolean = true;
  112. let result: boolean;
  113. before(() => {
  114. result = Utils.isCeilNumber(number);
  115. });
  116. it('should return true', () => {
  117. assert.equal(result, expectedResult);
  118. });
  119. });
  120. describe('given number is a float', () => {
  121. const number: number = 4.5;
  122. const expectedResult: boolean = false;
  123. let result: boolean;
  124. before(() => {
  125. result = Utils.isCeilNumber(number);
  126. });
  127. it('should return false', () => {
  128. assert.equal(result, expectedResult);
  129. });
  130. });
  131. });
  132. describe('stringRotate (string: string, times: number): string', () => {
  133. const string: string = 'abcdefg';
  134. let rotatedString: string;
  135. describe('value is not 0', () => {
  136. const rotateValue: number = 2;
  137. const expectedString: string = 'fgabcde';
  138. before(() => {
  139. rotatedString = Utils.stringRotate(string, rotateValue);
  140. });
  141. it('should rotate string by a given value', () => {
  142. assert.deepEqual(rotatedString, expectedString);
  143. });
  144. });
  145. describe('value equals or less 0', () => {
  146. const rotateValue: number = 0;
  147. const expectedString: string = 'abcdefg';
  148. before(() => {
  149. rotatedString = Utils.stringRotate(string, rotateValue);
  150. });
  151. it('shouldn\'t rotate string', () => {
  152. assert.deepEqual(rotatedString, expectedString);
  153. });
  154. });
  155. describe('empty array', () => {
  156. const emptyString: string = '';
  157. const rotateValue: number = 5;
  158. const expectedError: ReferenceErrorConstructor = ReferenceError;
  159. let testFunc: () => void ;
  160. before(() => {
  161. testFunc = () => Utils.stringRotate(emptyString, rotateValue);
  162. });
  163. it('should throw exception if string is empty', () => {
  164. assert.throws(testFunc, expectedError);
  165. });
  166. });
  167. });
  168. describe('stringToJSFuck (string: string): string', () => {
  169. const string: string = 'string';
  170. const expectedString: string = `${JSFuck.s} + ${JSFuck.t} + ${JSFuck.r} + ${JSFuck.i} + ${JSFuck.n} + ${JSFuck.g}`;
  171. let actualString: string;
  172. before(() => {
  173. actualString = Utils.stringToJSFuck(string);
  174. });
  175. it('should create a JSFuck encoded string from a given string', () => {
  176. assert.equal(actualString, expectedString);
  177. });
  178. });
  179. });