FlowControl.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * Flow Control tests.
  3. *
  4. * @author tlwr [toby@toby.codes]
  5. *
  6. * @copyright Crown Copyright 2017
  7. * @license Apache-2.0
  8. */
  9. import TestRegister from "../../TestRegister.js";
  10. TestRegister.addTests([
  11. {
  12. name: "Fork: nothing",
  13. input: "",
  14. expectedOutput: "",
  15. recipeConfig: [
  16. {
  17. op: "Fork",
  18. args: ["\n", "\n", false],
  19. },
  20. ],
  21. },
  22. {
  23. name: "Fork, Merge: nothing",
  24. input: "",
  25. expectedOutput: "",
  26. recipeConfig: [
  27. {
  28. op: "Fork",
  29. args: ["\n", "\n", false],
  30. },
  31. {
  32. op: "Merge",
  33. args: [],
  34. },
  35. ],
  36. },
  37. {
  38. name: "Fork, (expect) Error, Merge",
  39. input: "1.1\n2.5\na\n3.4",
  40. expectedError: true,
  41. recipeConfig: [
  42. {
  43. op: "Fork",
  44. args: ["\n", "\n", false],
  45. },
  46. {
  47. op: "Object Identifier to Hex",
  48. args: [],
  49. },
  50. {
  51. op: "Merge",
  52. args: [],
  53. },
  54. ],
  55. },
  56. {
  57. name: "Fork, Conditional Jump, Encodings",
  58. input: "Some data with a 1 in it\nSome data with a 2 in it",
  59. expectedOutput: "U29tZSBkYXRhIHdpdGggYSAxIGluIGl0\n53 6f 6d 65 20 64 61 74 61 20 77 69 74 68 20 61 20 32 20 69 6e 20 69 74\n",
  60. recipeConfig: [
  61. {"op": "Fork", "args": ["\\n", "\\n", false]},
  62. {"op": "Conditional Jump", "args": ["1", false, "skipReturn", "10"]},
  63. {"op": "To Hex", "args": ["Space"]},
  64. {"op": "Return", "args": []},
  65. {"op": "Label", "args": ["skipReturn"]},
  66. {"op": "To Base64", "args": ["A-Za-z0-9+/="]}
  67. ]
  68. },
  69. {
  70. name: "Jump: Empty Label",
  71. input: [
  72. "should be changed",
  73. ].join("\n"),
  74. expectedOutput: [
  75. "should be changed was changed",
  76. ].join("\n"),
  77. recipeConfig: [
  78. {
  79. op: "Jump",
  80. args: ["", 10],
  81. },
  82. {
  83. op: "Find / Replace",
  84. args: [
  85. {
  86. "option": "Regex",
  87. "string": "should be changed"
  88. },
  89. "should be changed was changed",
  90. true,
  91. true,
  92. true,
  93. ],
  94. },
  95. ],
  96. },
  97. {
  98. name: "Jump: skips 1",
  99. input: [
  100. "shouldnt be changed",
  101. ].join("\n"),
  102. expectedOutput: [
  103. "shouldnt be changed",
  104. ].join("\n"),
  105. recipeConfig: [
  106. {
  107. op: "Jump",
  108. args: ["skipReplace", 10],
  109. },
  110. {
  111. op: "Find / Replace",
  112. args: [
  113. {
  114. "option": "Regex",
  115. "string": "shouldnt be changed"
  116. },
  117. "shouldnt be changed was changed",
  118. true,
  119. true,
  120. true,
  121. ],
  122. },
  123. {
  124. op: "Label",
  125. args: ["skipReplace"]
  126. },
  127. ],
  128. },
  129. {
  130. name: "Conditional Jump: Skips 0",
  131. input: [
  132. "match",
  133. "should be changed 1",
  134. "should be changed 2",
  135. ].join("\n"),
  136. expectedOutput: [
  137. "match",
  138. "should be changed 1 was changed",
  139. "should be changed 2 was changed"
  140. ].join("\n"),
  141. recipeConfig: [
  142. {
  143. op: "Conditional Jump",
  144. args: ["match", false, "", 0],
  145. },
  146. {
  147. op: "Find / Replace",
  148. args: [
  149. {
  150. "option": "Regex",
  151. "string": "should be changed 1"
  152. },
  153. "should be changed 1 was changed",
  154. true,
  155. true,
  156. true,
  157. ],
  158. },
  159. {
  160. op: "Find / Replace",
  161. args: [
  162. {
  163. "option": "Regex",
  164. "string": "should be changed 2"
  165. },
  166. "should be changed 2 was changed",
  167. true,
  168. true,
  169. true,
  170. ],
  171. },
  172. ],
  173. },
  174. {
  175. name: "Comment: nothing",
  176. input: "",
  177. expectedOutput: "",
  178. recipeConfig: [
  179. {
  180. "op": "Comment",
  181. "args": [""]
  182. }
  183. ]
  184. },
  185. {
  186. name: "Fork, Comment, Base64",
  187. input: "cat\nsat\nmat",
  188. expectedOutput: "Y2F0\nc2F0\nbWF0\n",
  189. recipeConfig: [
  190. {
  191. "op": "Fork",
  192. "args": ["\\n", "\\n", false]
  193. },
  194. {
  195. "op": "Comment",
  196. "args": ["Testing 123"]
  197. },
  198. {
  199. "op": "To Base64",
  200. "args": ["A-Za-z0-9+/="]
  201. }
  202. ]
  203. },
  204. {
  205. name: "Conditional Jump: Skips 1",
  206. input: [
  207. "match",
  208. "should not be changed",
  209. "should be changed",
  210. ].join("\n"),
  211. expectedOutput: [
  212. "match",
  213. "should not be changed",
  214. "should be changed was changed"
  215. ].join("\n"),
  216. recipeConfig: [
  217. {
  218. op: "Conditional Jump",
  219. args: ["match", false, "skip match", 10],
  220. },
  221. {
  222. op: "Find / Replace",
  223. args: [
  224. {
  225. "option": "Regex",
  226. "string": "should not be changed"
  227. },
  228. "should not be changed was changed",
  229. true,
  230. true,
  231. true,
  232. ],
  233. },
  234. {
  235. op: "Label", args: ["skip match"],
  236. },
  237. {
  238. op: "Find / Replace",
  239. args: [
  240. {
  241. "option": "Regex",
  242. "string": "should be changed"
  243. },
  244. "should be changed was changed",
  245. true,
  246. true,
  247. true,
  248. ],
  249. },
  250. ],
  251. },
  252. {
  253. name: "Conditional Jump: Skips negatively",
  254. input: [
  255. "match",
  256. ].join("\n"),
  257. expectedOutput: [
  258. "replaced",
  259. ].join("\n"),
  260. recipeConfig: [
  261. {
  262. op: "Label",
  263. args: ["back to the beginning"],
  264. },
  265. {
  266. op: "Jump",
  267. args: ["skip replace"],
  268. },
  269. {
  270. op: "Find / Replace",
  271. args: [
  272. {
  273. "option": "Regex",
  274. "string": "match"
  275. },
  276. "replaced",
  277. true,
  278. true,
  279. true,
  280. ],
  281. },
  282. {
  283. op: "Label",
  284. args: ["skip replace"],
  285. },
  286. {
  287. op: "Conditional Jump",
  288. args: ["match", false, "back to the beginning", 10],
  289. },
  290. ],
  291. },
  292. ]);