StrUtils.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * StrUtils tests.
  3. *
  4. * @author n1474335 [n1474335@gmail.com]
  5. * @copyright Crown Copyright 2017
  6. * @license Apache-2.0
  7. */
  8. import TestRegister from "../../TestRegister.js";
  9. TestRegister.addTests([
  10. {
  11. name: "Diff, basic usage",
  12. input: "testing23\n\ntesting123",
  13. expectedOutput: "testing<span class='hl5'>1</span>23",
  14. recipeConfig: [
  15. {
  16. "op": "Diff",
  17. "args": ["\\n\\n", "Character", true, true, false]
  18. }
  19. ],
  20. },
  21. {
  22. name: "Head 0",
  23. input: [1, 2, 3, 4, 5, 6].join("\n"),
  24. expectedOutput: [].join("\n"),
  25. recipeConfig: [
  26. {
  27. "op": "Head",
  28. "args": ["Line feed", 0]
  29. }
  30. ],
  31. },
  32. {
  33. name: "Head 1",
  34. input: [1, 2, 3, 4, 5, 6].join("\n"),
  35. expectedOutput: [1].join("\n"),
  36. recipeConfig: [
  37. {
  38. "op": "Head",
  39. "args": ["Line feed", 1]
  40. }
  41. ],
  42. },
  43. {
  44. name: "Head 2",
  45. input: [1, 2, 3, 4, 5, 6].join("\n"),
  46. expectedOutput: [1, 2].join("\n"),
  47. recipeConfig: [
  48. {
  49. "op": "Head",
  50. "args": ["Line feed", 2]
  51. }
  52. ],
  53. },
  54. {
  55. name: "Head 6",
  56. input: [1, 2, 3, 4, 5, 6].join("\n"),
  57. expectedOutput: [1, 2, 3, 4, 5, 6].join("\n"),
  58. recipeConfig: [
  59. {
  60. "op": "Head",
  61. "args": ["Line feed", 6]
  62. }
  63. ],
  64. },
  65. {
  66. name: "Head big",
  67. input: [1, 2, 3, 4, 5, 6].join("\n"),
  68. expectedOutput: [1, 2, 3, 4, 5, 6].join("\n"),
  69. recipeConfig: [
  70. {
  71. "op": "Head",
  72. "args": ["Line feed", 100]
  73. }
  74. ],
  75. },
  76. {
  77. name: "Head all but 1",
  78. input: [1, 2, 3, 4, 5, 6].join("\n"),
  79. expectedOutput: [1, 2, 3, 4, 5].join("\n"),
  80. recipeConfig: [
  81. {
  82. "op": "Head",
  83. "args": ["Line feed", -1]
  84. }
  85. ],
  86. },
  87. {
  88. name: "Head all but 2",
  89. input: [1, 2, 3, 4, 5, 6].join("\n"),
  90. expectedOutput: [1, 2, 3, 4].join("\n"),
  91. recipeConfig: [
  92. {
  93. "op": "Head",
  94. "args": ["Line feed", -2]
  95. }
  96. ],
  97. },
  98. {
  99. name: "Head all but 6",
  100. input: [1, 2, 3, 4, 5, 6].join("\n"),
  101. expectedOutput: [].join("\n"),
  102. recipeConfig: [
  103. {
  104. "op": "Head",
  105. "args": ["Line feed", -6]
  106. }
  107. ],
  108. },
  109. {
  110. name: "Head all but big",
  111. input: [1, 2, 3, 4, 5, 6].join("\n"),
  112. expectedOutput: [].join("\n"),
  113. recipeConfig: [
  114. {
  115. "op": "Head",
  116. "args": ["Line feed", -100]
  117. }
  118. ],
  119. },
  120. {
  121. name: "Tail 0",
  122. input: [1, 2, 3, 4, 5, 6].join("\n"),
  123. expectedOutput: [].join("\n"),
  124. recipeConfig: [
  125. {
  126. "op": "Tail",
  127. "args": ["Line feed", 0]
  128. }
  129. ],
  130. },
  131. {
  132. name: "Tail 1",
  133. input: [1, 2, 3, 4, 5, 6].join("\n"),
  134. expectedOutput: [6].join("\n"),
  135. recipeConfig: [
  136. {
  137. "op": "Tail",
  138. "args": ["Line feed", 1]
  139. }
  140. ],
  141. },
  142. {
  143. name: "Tail 2",
  144. input: [1, 2, 3, 4, 5, 6].join("\n"),
  145. expectedOutput: [5, 6].join("\n"),
  146. recipeConfig: [
  147. {
  148. "op": "Tail",
  149. "args": ["Line feed", 2]
  150. }
  151. ],
  152. },
  153. {
  154. name: "Tail 6",
  155. input: [1, 2, 3, 4, 5, 6].join("\n"),
  156. expectedOutput: [1, 2, 3, 4, 5, 6].join("\n"),
  157. recipeConfig: [
  158. {
  159. "op": "Tail",
  160. "args": ["Line feed", 6]
  161. }
  162. ],
  163. },
  164. {
  165. name: "Tail big",
  166. input: [1, 2, 3, 4, 5, 6].join("\n"),
  167. expectedOutput: [1, 2, 3, 4, 5, 6].join("\n"),
  168. recipeConfig: [
  169. {
  170. "op": "Tail",
  171. "args": ["Line feed", 100]
  172. }
  173. ],
  174. },
  175. {
  176. name: "Tail all but 1",
  177. input: [1, 2, 3, 4, 5, 6].join("\n"),
  178. expectedOutput: [2, 3, 4, 5, 6].join("\n"),
  179. recipeConfig: [
  180. {
  181. "op": "Tail",
  182. "args": ["Line feed", -1]
  183. }
  184. ],
  185. },
  186. {
  187. name: "Tail all but 2",
  188. input: [1, 2, 3, 4, 5, 6].join("\n"),
  189. expectedOutput: [3, 4, 5, 6].join("\n"),
  190. recipeConfig: [
  191. {
  192. "op": "Tail",
  193. "args": ["Line feed", -2]
  194. }
  195. ],
  196. },
  197. {
  198. name: "Tail all but 6",
  199. input: [1, 2, 3, 4, 5, 6].join("\n"),
  200. expectedOutput: [].join("\n"),
  201. recipeConfig: [
  202. {
  203. "op": "Tail",
  204. "args": ["Line feed", -6]
  205. }
  206. ],
  207. },
  208. {
  209. name: "Tail all but big",
  210. input: [1, 2, 3, 4, 5, 6].join("\n"),
  211. expectedOutput: [].join("\n"),
  212. recipeConfig: [
  213. {
  214. "op": "Tail",
  215. "args": ["Line feed", -100]
  216. }
  217. ],
  218. },
  219. {
  220. name: "Escape String: single quotes",
  221. input: "Escape 'these' quotes.",
  222. expectedOutput: "Escape \\'these\\' quotes.",
  223. recipeConfig: [
  224. {
  225. "op": "Escape string",
  226. "args": ["Special chars", "Single", false, true, false]
  227. }
  228. ],
  229. },
  230. {
  231. name: "Escape String: double quotes",
  232. input: "Hello \"World\"!",
  233. expectedOutput: "Hello \\\"World\\\"!",
  234. recipeConfig: [
  235. {
  236. "op": "Escape string",
  237. "args": ["Special chars", "Double", false, true, false]
  238. }
  239. ],
  240. },
  241. {
  242. name: "Escape String: special characters",
  243. input: "Fizz & buzz\n\ttabbed newline\rcarriage returned line\nbackspace character: \"\" form feed character: \" \"",
  244. expectedOutput: "Fizz & buzz\\n\\ttabbed newline\\rcarriage returned line\\nbackspace character: \\\"\\b\\\" form feed character: \\\"\\f\\\"",
  245. recipeConfig: [
  246. {
  247. "op": "Escape string",
  248. "args": ["Special chars", "Double", false, true, false]
  249. }
  250. ],
  251. },
  252. {
  253. name: "Unescape String: quotes",
  254. input: "Hello \\\"World\\\"! Escape \\'these\\' quotes.",
  255. expectedOutput: "Hello \"World\"! Escape 'these' quotes.",
  256. recipeConfig: [
  257. {
  258. "op": "Unescape string",
  259. "args": []
  260. }
  261. ],
  262. },
  263. {
  264. name: "Unescape String: special characters",
  265. input: "Fizz \x26 buzz\\n\\ttabbed newline\\rcarriage returned line\\nbackspace character: \\\"\\b\\\" form feed character: \\\"\\f\\\"",
  266. expectedOutput: "Fizz & buzz\n\ttabbed newline\rcarriage returned line\nbackspace character: \"\" form feed character: \" \"",
  267. recipeConfig: [
  268. {
  269. "op": "Unescape string",
  270. "args": []
  271. }
  272. ],
  273. },
  274. {
  275. name: "Escape String: complex",
  276. input: "null\0backspace\btab\tnewline\nverticaltab\vformfeed\fcarriagereturn\rdoublequote\"singlequote'hex\xa9unicode\u2665codepoint\u{1D306}",
  277. expectedOutput: "null\\0backspace\\btab\\tnewline\\nverticaltab\\x0bformfeed\\fcarriagereturn\\rdoublequote\"singlequote\\'hex\\xa9unicode\\u2665codepoint\\u{1d306}",
  278. recipeConfig: [
  279. {
  280. "op": "Escape string",
  281. "args": ["Special chars", "Single", false, true, false]
  282. }
  283. ],
  284. },
  285. {
  286. name: "Unescape String: complex",
  287. input: "null\\0backspace\\btab\\tnewline\\nverticaltab\\vformfeed\\fcarriagereturn\\rdoublequote\\\"singlequote\\'hex\\xa9unicode\\u2665codepoint\\u{1D306}",
  288. expectedOutput: "null\0backspace\btab\tnewline\nverticaltab\vformfeed\fcarriagereturn\rdoublequote\"singlequote'hex\xa9unicode\u2665codepoint\u{1D306}",
  289. recipeConfig: [
  290. {
  291. "op": "Unescape string",
  292. "args": []
  293. }
  294. ],
  295. },
  296. ]);