FlowControl.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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: "Conditional Jump: Skips 1",
  171. input: [
  172. "match",
  173. "should not be changed",
  174. "should be changed",
  175. ].join("\n"),
  176. expectedOutput: [
  177. "match",
  178. "should not be changed",
  179. "should be changed was changed"
  180. ].join("\n"),
  181. recipeConfig: [
  182. {
  183. op: "Conditional Jump",
  184. args: ["match", 1, 10],
  185. },
  186. {
  187. op: "Find / Replace",
  188. args: [
  189. {
  190. "option": "Regex",
  191. "string": "should not be changed"
  192. },
  193. "should not be changed was changed",
  194. true,
  195. true,
  196. true,
  197. ],
  198. },
  199. {
  200. op: "Find / Replace",
  201. args: [
  202. {
  203. "option": "Regex",
  204. "string": "should be changed"
  205. },
  206. "should be changed was changed",
  207. true,
  208. true,
  209. true,
  210. ],
  211. },
  212. ],
  213. },
  214. {
  215. name: "Conditional Jump: Skips negatively",
  216. input: [
  217. "match",
  218. ].join("\n"),
  219. expectedOutput: [
  220. "replaced",
  221. ].join("\n"),
  222. recipeConfig: [
  223. {
  224. op: "Jump",
  225. args: [1],
  226. },
  227. {
  228. op: "Find / Replace",
  229. args: [
  230. {
  231. "option": "Regex",
  232. "string": "match"
  233. },
  234. "replaced",
  235. true,
  236. true,
  237. true,
  238. ],
  239. },
  240. {
  241. op: "Conditional Jump",
  242. args: ["match", -2, 10],
  243. },
  244. ],
  245. },
  246. ]);