FlowControl.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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\n2\na\n4",
  40. expectedError: true,
  41. recipeConfig: [
  42. {
  43. op: "Fork",
  44. args: ["\n", "\n", false],
  45. },
  46. {
  47. op: "To Base",
  48. args: [16],
  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", "2", "10"]},
  63. {"op":"To Hex", "args":["Space"]},
  64. {"op":"Return", "args":[]},
  65. {"op":"To Base64", "args":["A-Za-z0-9+/="]}
  66. ]
  67. },
  68. {
  69. name: "Jump: skips 0",
  70. input: [
  71. "should be changed",
  72. ].join("\n"),
  73. expectedOutput: [
  74. "should be changed was changed",
  75. ].join("\n"),
  76. recipeConfig: [
  77. {
  78. op: "Jump",
  79. args: [0, 10],
  80. },
  81. {
  82. op: "Find / Replace",
  83. args: [
  84. {
  85. "option": "Regex",
  86. "string": "should be changed"
  87. },
  88. "should be changed was changed",
  89. true,
  90. true,
  91. true,
  92. ],
  93. },
  94. ],
  95. },
  96. {
  97. name: "Jump: skips 1",
  98. input: [
  99. "shouldnt be changed",
  100. ].join("\n"),
  101. expectedOutput: [
  102. "shouldnt be changed",
  103. ].join("\n"),
  104. recipeConfig: [
  105. {
  106. op: "Jump",
  107. args: [1, 10],
  108. },
  109. {
  110. op: "Find / Replace",
  111. args: [
  112. {
  113. "option": "Regex",
  114. "string": "shouldnt be changed"
  115. },
  116. "shouldnt be changed was changed",
  117. true,
  118. true,
  119. true,
  120. ],
  121. },
  122. ],
  123. },
  124. {
  125. name: "Conditional Jump: Skips 0",
  126. input: [
  127. "match",
  128. "should be changed 1",
  129. "should be changed 2",
  130. ].join("\n"),
  131. expectedOutput: [
  132. "match",
  133. "should be changed 1 was changed",
  134. "should be changed 2 was changed"
  135. ].join("\n"),
  136. recipeConfig: [
  137. {
  138. op: "Conditional Jump",
  139. args: ["match", 0, 0],
  140. },
  141. {
  142. op: "Find / Replace",
  143. args: [
  144. {
  145. "option": "Regex",
  146. "string": "should be changed 1"
  147. },
  148. "should be changed 1 was changed",
  149. true,
  150. true,
  151. true,
  152. ],
  153. },
  154. {
  155. op: "Find / Replace",
  156. args: [
  157. {
  158. "option": "Regex",
  159. "string": "should be changed 2"
  160. },
  161. "should be changed 2 was changed",
  162. true,
  163. true,
  164. true,
  165. ],
  166. },
  167. ],
  168. },
  169. {
  170. name: "Comment: nothing",
  171. input: "",
  172. expectedOutput: "",
  173. recipeConfig: [
  174. {
  175. "op": "Comment",
  176. "args": [""]
  177. }
  178. ]
  179. },
  180. {
  181. name: "Fork, Comment, Base64",
  182. input: "cat\nsat\nmat",
  183. expectedOutput: "Y2F0\nc2F0\nbWF0\n",
  184. recipeConfig: [
  185. {
  186. "op": "Fork",
  187. "args": ["\\n", "\\n", false]
  188. },
  189. {
  190. "op": "Comment",
  191. "args": ["Testing 123"]
  192. },
  193. {
  194. "op": "To Base64",
  195. "args": ["A-Za-z0-9+/="]
  196. }
  197. ]
  198. },
  199. {
  200. name: "Conditional Jump: Skips 1",
  201. input: [
  202. "match",
  203. "should not be changed",
  204. "should be changed",
  205. ].join("\n"),
  206. expectedOutput: [
  207. "match",
  208. "should not be changed",
  209. "should be changed was changed"
  210. ].join("\n"),
  211. recipeConfig: [
  212. {
  213. op: "Conditional Jump",
  214. args: ["match", 1, 10],
  215. },
  216. {
  217. op: "Find / Replace",
  218. args: [
  219. {
  220. "option": "Regex",
  221. "string": "should not be changed"
  222. },
  223. "should not be changed was changed",
  224. true,
  225. true,
  226. true,
  227. ],
  228. },
  229. {
  230. op: "Find / Replace",
  231. args: [
  232. {
  233. "option": "Regex",
  234. "string": "should be changed"
  235. },
  236. "should be changed was changed",
  237. true,
  238. true,
  239. true,
  240. ],
  241. },
  242. ],
  243. },
  244. {
  245. name: "Conditional Jump: Skips negatively",
  246. input: [
  247. "match",
  248. ].join("\n"),
  249. expectedOutput: [
  250. "replaced",
  251. ].join("\n"),
  252. recipeConfig: [
  253. {
  254. op: "Jump",
  255. args: [1],
  256. },
  257. {
  258. op: "Find / Replace",
  259. args: [
  260. {
  261. "option": "Regex",
  262. "string": "match"
  263. },
  264. "replaced",
  265. true,
  266. true,
  267. true,
  268. ],
  269. },
  270. {
  271. op: "Conditional Jump",
  272. args: ["match", -2, 10],
  273. },
  274. ],
  275. },
  276. ]);