Utils.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import { assert } from 'chai';
  2. import { Utils } from '../../../../src/utils/Utils';
  3. import { JSFuck } from '../../../../src/enums/JSFuck';
  4. describe('Utils', () => {
  5. describe('arrayRange (length: number): number[]', () => {
  6. describe('range length more than 0', () => {
  7. const rangeLength: number = 5;
  8. const expectedArray: number[] = [0, 1, 2, 3, 4];
  9. let array: number[];
  10. before(() => {
  11. array = Utils.arrayRange(rangeLength);
  12. });
  13. it('should return array with range of numbers', () => {
  14. assert.deepEqual(array, expectedArray);
  15. });
  16. });
  17. describe('range length is 0', () => {
  18. const rangeLength: number = 0;
  19. const expectedArray: number[] = [];
  20. let array: number[];
  21. before(() => {
  22. array = Utils.arrayRange(rangeLength);
  23. });
  24. it('should return empty array', () => {
  25. assert.deepEqual(array, expectedArray);
  26. });
  27. });
  28. describe('range length less than 0', () => {
  29. const rangeLength: number = -5;
  30. const expectedArray: number[] = [];
  31. let array: number[];
  32. before(() => {
  33. array = Utils.arrayRange(rangeLength);
  34. });
  35. it('should return empty array', () => {
  36. assert.deepEqual(array, expectedArray);
  37. });
  38. });
  39. });
  40. describe('arrayRotate <T> (array: T[], times: number): T[]', () => {
  41. let array: number[],
  42. rotatedArray: number[];
  43. beforeEach(() => {
  44. array = [1, 2, 3, 4, 5, 6];
  45. });
  46. describe('value is not 0', () => {
  47. const rotateValue: number = 2;
  48. const expectedArray: number[] = [5, 6, 1, 2, 3, 4];
  49. beforeEach(() => {
  50. rotatedArray = Utils.arrayRotate(array, rotateValue);
  51. });
  52. it('should rotate (shift) array by a given value', () => {
  53. assert.deepEqual(rotatedArray, expectedArray);
  54. });
  55. });
  56. describe('value equals or less 0', () => {
  57. const rotateValue: number = 0;
  58. const expectedArray: number[] = [1, 2, 3, 4, 5, 6];
  59. beforeEach(() => {
  60. rotatedArray = Utils.arrayRotate(array, rotateValue);
  61. });
  62. it('shouldn\'t rotate array', () => {
  63. assert.deepEqual(rotatedArray, expectedArray);
  64. });
  65. });
  66. describe('empty array', () => {
  67. const emptyArray: number[] = [];
  68. const rotateValue: number = 5;
  69. const expectedError: ReferenceErrorConstructor = ReferenceError;
  70. let testFunc: () => void;
  71. beforeEach(() => {
  72. testFunc = () => Utils.arrayRotate(emptyArray, rotateValue);
  73. });
  74. it('should throw exception if array is empty', () => {
  75. assert.throws(testFunc, expectedError);
  76. });
  77. });
  78. });
  79. describe('decToHex (dec: number): string', () => {
  80. describe('variant #1: number `0`', () => {
  81. const number: number = 0;
  82. const expectedHexString = '0';
  83. let hexString: string;
  84. before(() => {
  85. hexString = Utils.decToHex(number);
  86. });
  87. it('should create a string with hexadecimal value from a given decimal number', () => {
  88. assert.equal(hexString, expectedHexString);
  89. });
  90. });
  91. describe('variant #2: number `10`', () => {
  92. const number: number = 10;
  93. const expectedHexString = 'a';
  94. let hexString: string;
  95. before(() => {
  96. hexString = Utils.decToHex(number);
  97. });
  98. it('should create a string with hexadecimal value from a given decimal number', () => {
  99. assert.equal(hexString, expectedHexString);
  100. });
  101. });
  102. describe('variant #3: number `17`', () => {
  103. const number: number = 17;
  104. const expectedHexString = '11';
  105. let hexString: string;
  106. before(() => {
  107. hexString = Utils.decToHex(number);
  108. });
  109. it('should create a string with hexadecimal value from a given decimal number', () => {
  110. assert.equal(hexString, expectedHexString);
  111. });
  112. });
  113. describe('variant #4: number `536870912`', () => {
  114. const number: number = 536870912;
  115. const expectedHexString = '20000000';
  116. let hexString: string;
  117. before(() => {
  118. hexString = Utils.decToHex(number);
  119. });
  120. it('should create a string with hexadecimal value from a given decimal number', () => {
  121. assert.equal(hexString, expectedHexString);
  122. });
  123. });
  124. });
  125. describe('extractDomainFromUrl (url: string): string', () => {
  126. describe('variant #1: simple url', () => {
  127. const url: string = 'http://google.ru';
  128. const expectedDomain: string = 'google.ru';
  129. let domain: string;
  130. before(() => {
  131. domain = Utils.extractDomainFromUrl(url);
  132. });
  133. it('should extract domain from the given URL', () => {
  134. assert.equal(domain, expectedDomain);
  135. });
  136. });
  137. describe('variant #2: url with `www` part', () => {
  138. const url: string = 'http://www.google.ru';
  139. const expectedDomain: string = 'www.google.ru';
  140. let domain: string;
  141. before(() => {
  142. domain = Utils.extractDomainFromUrl(url);
  143. });
  144. it('should extract domain from the given URL', () => {
  145. assert.equal(domain, expectedDomain);
  146. });
  147. });
  148. describe('variant #3: url with `https` protocol and port', () => {
  149. const url: string = 'https://www.google.ru:9000';
  150. const expectedDomain: string = 'www.google.ru';
  151. let domain: string;
  152. before(() => {
  153. domain = Utils.extractDomainFromUrl(url);
  154. });
  155. it('should extract domain from the given URL', () => {
  156. assert.equal(domain, expectedDomain);
  157. });
  158. });
  159. describe('variant #4: protocol-wide url and route', () => {
  160. const url: string = '//google.ru/abc';
  161. const expectedDomain: string = 'google.ru';
  162. let domain: string;
  163. before(() => {
  164. domain = Utils.extractDomainFromUrl(url);
  165. });
  166. it('should extract domain from the given URL', () => {
  167. assert.equal(domain, expectedDomain);
  168. });
  169. });
  170. describe('variant #5: protocol-wide url, `localhost` and port', () => {
  171. const url: string = '//localhost:9000';
  172. const expectedDomain: string = 'localhost';
  173. let domain: string;
  174. before(() => {
  175. domain = Utils.extractDomainFromUrl(url);
  176. });
  177. it('should extract domain from the given URL', () => {
  178. assert.equal(domain, expectedDomain);
  179. });
  180. });
  181. });
  182. describe('isCeilNumber (number: number): boolean', () => {
  183. describe('given number is a ceil', () => {
  184. const number: number = 4;
  185. const expectedResult: boolean = true;
  186. let result: boolean;
  187. before(() => {
  188. result = Utils.isCeilNumber(number);
  189. });
  190. it('should return true', () => {
  191. assert.equal(result, expectedResult);
  192. });
  193. });
  194. describe('given number is a float', () => {
  195. const number: number = 4.5;
  196. const expectedResult: boolean = false;
  197. let result: boolean;
  198. before(() => {
  199. result = Utils.isCeilNumber(number);
  200. });
  201. it('should return false', () => {
  202. assert.equal(result, expectedResult);
  203. });
  204. });
  205. });
  206. describe('stringRotate (string: string, times: number): string', () => {
  207. const string: string = 'abcdefg';
  208. let rotatedString: string;
  209. describe('value is not 0', () => {
  210. const rotateValue: number = 2;
  211. const expectedString: string = 'fgabcde';
  212. before(() => {
  213. rotatedString = Utils.stringRotate(string, rotateValue);
  214. });
  215. it('should rotate string by a given value', () => {
  216. assert.deepEqual(rotatedString, expectedString);
  217. });
  218. });
  219. describe('value equals or less 0', () => {
  220. const rotateValue: number = 0;
  221. const expectedString: string = 'abcdefg';
  222. before(() => {
  223. rotatedString = Utils.stringRotate(string, rotateValue);
  224. });
  225. it('shouldn\'t rotate string', () => {
  226. assert.deepEqual(rotatedString, expectedString);
  227. });
  228. });
  229. describe('empty array', () => {
  230. const emptyString: string = '';
  231. const rotateValue: number = 5;
  232. const expectedError: ReferenceErrorConstructor = ReferenceError;
  233. let testFunc: () => void ;
  234. before(() => {
  235. testFunc = () => Utils.stringRotate(emptyString, rotateValue);
  236. });
  237. it('should throw exception if string is empty', () => {
  238. assert.throws(testFunc, expectedError);
  239. });
  240. });
  241. });
  242. describe('stringToJSFuck (string: string): string', () => {
  243. const string: string = 'string';
  244. const expectedString: string = `${JSFuck.s} + ${JSFuck.t} + ${JSFuck.r} + ${JSFuck.i} + ${JSFuck.n} + ${JSFuck.g}`;
  245. let actualString: string;
  246. before(() => {
  247. actualString = Utils.stringToJSFuck(string);
  248. });
  249. it('should create a JSFuck encoded string from a given string', () => {
  250. assert.equal(actualString, expectedString);
  251. });
  252. });
  253. describe('stringToUnicodeEscapeSequence (string: string, nonLatinAndNonDigitsOnly: boolean = false): string', () => {
  254. describe('variant #1: default', () => {
  255. const string: string = 'string';
  256. const expectedString: string = '\\x73\\x74\\x72\\x69\\x6e\\x67';
  257. let actualString: string;
  258. before(() => {
  259. actualString = Utils.stringToUnicodeEscapeSequence(string, true);
  260. });
  261. it('should return a unicode escape sequence based on a given string', () => {
  262. assert.equal(actualString, expectedString);
  263. });
  264. });
  265. describe('variant #2: escape `escape sequences`', () => {
  266. const string: string = 'abc\'\\r\\n';
  267. const expectedString: string = 'abc\\x27\\x5cr\\x5cn';
  268. let actualString: string;
  269. before(() => {
  270. actualString = Utils.stringToUnicodeEscapeSequence(string, false);
  271. });
  272. it('should return a string where all `escape sequences` are escaped', () => {
  273. assert.equal(actualString, expectedString);
  274. });
  275. });
  276. });
  277. });