Code.mjs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**
  2. * Code tests.
  3. *
  4. * @author tlwr [toby@toby.codes]
  5. * @author Matt C [matt@artemisbot.uk]
  6. *
  7. * @copyright Crown Copyright 2017
  8. * @license Apache-2.0
  9. */
  10. import TestRegister from "../../TestRegister";
  11. const JSON_TEST_DATA = {
  12. "store": {
  13. "book": [{
  14. "category": "reference",
  15. "author": "Nigel Rees",
  16. "title": "Sayings of the Century",
  17. "price": 8.95
  18. }, {
  19. "category": "fiction",
  20. "author": "Evelyn Waugh",
  21. "title": "Sword of Honour",
  22. "price": 12.99
  23. }, {
  24. "category": "fiction",
  25. "author": "Herman Melville",
  26. "title": "Moby Dick",
  27. "isbn": "0-553-21311-3",
  28. "price": 8.99
  29. }, {
  30. "category": "fiction",
  31. "author": "J. R. R. Tolkien",
  32. "title": "The Lord of the Rings",
  33. "isbn": "0-395-19395-8",
  34. "price": 22.99
  35. }],
  36. "bicycle": {
  37. "color": "red",
  38. "price": 19.95
  39. },
  40. "newspaper": [{
  41. "format": "broadsheet",
  42. "title": "Financial Times",
  43. "price": 2.75
  44. }, {
  45. "format": "tabloid",
  46. "title": "The Guardian",
  47. "price": 2.00
  48. }]
  49. }
  50. };
  51. TestRegister.addTests([
  52. {
  53. name: "To Camel case (dumb)",
  54. input: "hello world",
  55. expectedOutput: "helloWorld",
  56. recipeConfig: [
  57. {
  58. "op": "To Camel case",
  59. "args": [false]
  60. }
  61. ],
  62. },
  63. {
  64. name: "To Snake case (dumb)",
  65. input: "hello world",
  66. expectedOutput: "hello_world",
  67. recipeConfig: [
  68. {
  69. "op": "To Snake case",
  70. "args": [false]
  71. }
  72. ],
  73. },
  74. {
  75. name: "To Kebab case (dumb)",
  76. input: "hello world",
  77. expectedOutput: "hello-world",
  78. recipeConfig: [
  79. {
  80. "op": "To Kebab case",
  81. "args": [false]
  82. }
  83. ],
  84. },
  85. {
  86. name: "To Camel case (smart)",
  87. input: [
  88. "test='hello'",
  89. "echo $test",
  90. "a_camel_case_function",
  91. "$a_camel_case_variable;",
  92. "function function_name() {",
  93. " console.log('things inside quotes do not get broken');",
  94. " console.log(\"things inside quotes do not get broken\");",
  95. "}",
  96. ].join("\n"),
  97. expectedOutput: [
  98. "test='hello'",
  99. "echo $test",
  100. "aCamelCaseFunction",
  101. "$aCamelCaseVariable;",
  102. "function functionName() {",
  103. " console.log('things inside quotes do not get broken');",
  104. " console.log(\"things inside quotes do not get broken\");",
  105. "}",
  106. ].join("\n"),
  107. recipeConfig: [
  108. {
  109. "op": "To Camel case",
  110. "args": [true]
  111. }
  112. ],
  113. },
  114. {
  115. name: "To Snake case (smart)",
  116. input: [
  117. "test='hello'",
  118. "echo $test",
  119. "aSnakeCaseFunction",
  120. "$aSnakeCaseVariable;",
  121. "function functionName() {",
  122. " console.log('things inside quotes do not get broken');",
  123. " console.log(\"things inside quotes do not get broken\");",
  124. "}",
  125. ].join("\n"),
  126. expectedOutput: [
  127. "test='hello'",
  128. "echo $test",
  129. "a_snake_case_function",
  130. "$a_snake_case_variable;",
  131. "function function_name() {",
  132. " console.log('things inside quotes do not get broken');",
  133. " console.log(\"things inside quotes do not get broken\");",
  134. "}",
  135. ].join("\n"),
  136. recipeConfig: [
  137. {
  138. "op": "To Snake case",
  139. "args": [true]
  140. }
  141. ],
  142. },
  143. {
  144. name: "To Kebab case (smart)",
  145. input: [
  146. "test='hello'",
  147. "echo $test",
  148. "aKebabCaseFunction",
  149. "$aKebabCaseVariable;",
  150. "function functionName() {",
  151. " console.log('things inside quotes do not get broken');",
  152. " console.log(\"things inside quotes do not get broken\");",
  153. "}",
  154. ].join("\n"),
  155. expectedOutput: [
  156. "test='hello'",
  157. "echo $test",
  158. "a-kebab-case-function",
  159. "$a-kebab-case-variable;",
  160. "function function-name() {",
  161. " console.log('things inside quotes do not get broken');",
  162. " console.log(\"things inside quotes do not get broken\");",
  163. "}",
  164. ].join("\n"),
  165. recipeConfig: [
  166. {
  167. "op": "To Kebab case",
  168. "args": [true]
  169. }
  170. ],
  171. },
  172. {
  173. name: "JPath Expression: Empty JSON",
  174. input: "",
  175. expectedOutput: "Invalid input JSON: Unexpected end of JSON input",
  176. recipeConfig: [
  177. {
  178. "op": "JPath expression",
  179. "args": ["", "\n"]
  180. }
  181. ],
  182. },
  183. {
  184. name: "JPath Expression: Empty expression",
  185. input: JSON.stringify(JSON_TEST_DATA),
  186. expectedOutput: "Invalid JPath expression: we need a path",
  187. recipeConfig: [
  188. {
  189. "op": "JPath expression",
  190. "args": ["", "\n"]
  191. }
  192. ],
  193. },
  194. {
  195. name: "JPath Expression: Fetch of values from specific object",
  196. input: JSON.stringify(JSON_TEST_DATA),
  197. expectedOutput: [
  198. "\"Nigel Rees\"",
  199. "\"Evelyn Waugh\"",
  200. "\"Herman Melville\"",
  201. "\"J. R. R. Tolkien\""
  202. ].join("\n"),
  203. recipeConfig: [
  204. {
  205. "op": "JPath expression",
  206. "args": ["$.store.book[*].author", "\n"]
  207. }
  208. ],
  209. },
  210. {
  211. name: "JPath Expression: Fetch of all values with matching key",
  212. input: JSON.stringify(JSON_TEST_DATA),
  213. expectedOutput: [
  214. "\"Sayings of the Century\"",
  215. "\"Sword of Honour\"",
  216. "\"Moby Dick\"",
  217. "\"The Lord of the Rings\"",
  218. "\"Financial Times\"",
  219. "\"The Guardian\""
  220. ].join("\n"),
  221. recipeConfig: [
  222. {
  223. "op": "JPath expression",
  224. "args": ["$..title", "\n"]
  225. }
  226. ],
  227. },
  228. {
  229. name: "JPath Expression: All data in object",
  230. input: JSON.stringify(JSON_TEST_DATA),
  231. expectedOutput: [
  232. "[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}]",
  233. "{\"color\":\"red\",\"price\":19.95}",
  234. "[{\"format\":\"broadsheet\",\"title\":\"Financial Times\",\"price\":2.75},{\"format\":\"tabloid\",\"title\":\"The Guardian\",\"price\":2}]"
  235. ].join("\n"),
  236. recipeConfig: [
  237. {
  238. "op": "JPath expression",
  239. "args": ["$.store.*", "\n"]
  240. }
  241. ],
  242. },
  243. {
  244. name: "JPath Expression: Last element in array",
  245. input: JSON.stringify(JSON_TEST_DATA),
  246. expectedOutput: "{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}",
  247. recipeConfig: [
  248. {
  249. "op": "JPath expression",
  250. "args": ["$..book[-1:]", "\n"]
  251. }
  252. ],
  253. },
  254. {
  255. name: "JPath Expression: First 2 elements in array",
  256. input: JSON.stringify(JSON_TEST_DATA),
  257. expectedOutput: [
  258. "{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95}",
  259. "{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99}"
  260. ].join("\n"),
  261. recipeConfig: [
  262. {
  263. "op": "JPath expression",
  264. "args": ["$..book[:2]", "\n"]
  265. }
  266. ],
  267. },
  268. {
  269. name: "JPath Expression: All elements in array with property",
  270. input: JSON.stringify(JSON_TEST_DATA),
  271. expectedOutput: [
  272. "{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}",
  273. "{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}"
  274. ].join("\n"),
  275. recipeConfig: [
  276. {
  277. "op": "JPath expression",
  278. "args": ["$..book[?(@.isbn)]", "\n"]
  279. }
  280. ],
  281. },
  282. {
  283. name: "JPath Expression: All elements in array which meet condition",
  284. input: JSON.stringify(JSON_TEST_DATA),
  285. expectedOutput: [
  286. "{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99}",
  287. "{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}",
  288. "{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}"
  289. ].join("\n"),
  290. recipeConfig: [
  291. {
  292. "op": "JPath expression",
  293. "args": ["$..book[?(@.price<30 && @.category==\"fiction\")]", "\n"]
  294. }
  295. ],
  296. },
  297. {
  298. name: "JPath Expression: All elements in object",
  299. input: JSON.stringify(JSON_TEST_DATA),
  300. expectedOutput: [
  301. "{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95}",
  302. "{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}"
  303. ].join("\n"),
  304. recipeConfig: [
  305. {
  306. "op": "JPath expression",
  307. "args": ["$..book[?(@.price<10)]", "\n"]
  308. }
  309. ],
  310. },
  311. {
  312. name: "CSS selector",
  313. input: '<div id="test">\n<p class="a">hello</p>\n<p>world</p>\n<p class="a">again</p>\n</div>',
  314. expectedOutput: '<p class="a">hello</p>\n<p class="a">again</p>',
  315. recipeConfig: [
  316. {
  317. "op": "CSS selector",
  318. "args": ["#test p.a", "\\n"]
  319. }
  320. ]
  321. },
  322. {
  323. name: "XPath expression",
  324. input: '<div id="test">\n<p class="a">hello</p>\n<p>world</p>\n<p class="a">again</p>\n</div>',
  325. expectedOutput: '<p class="a">hello</p>\n<p class="a">again</p>',
  326. recipeConfig: [
  327. {
  328. "op": "XPath expression",
  329. "args": ["/div/p[@class=\"a\"]", "\\n"]
  330. }
  331. ]
  332. },
  333. {
  334. name: "To MessagePack: no content",
  335. input: "",
  336. expectedError: true,
  337. recipeConfig: [
  338. {
  339. "op": "To MessagePack",
  340. "args": []
  341. },
  342. {
  343. "op": "To Hex",
  344. "args": ["Space"]
  345. }
  346. ]
  347. },
  348. {
  349. name: "From MessagePack: no content",
  350. input: "",
  351. expectedOutput: "Could not decode MessagePack to JSON: Error: Could not parse",
  352. recipeConfig: [
  353. {
  354. "op": "From Hex",
  355. "args": ["Space"]
  356. },
  357. {
  358. "op": "From MessagePack",
  359. "args": []
  360. }
  361. ]
  362. },
  363. {
  364. name: "To MessagePack: valid json",
  365. input: JSON.stringify(JSON_TEST_DATA),
  366. expectedOutput: "81 a5 73 74 6f 72 65 83 a4 62 6f 6f 6b 94 84 a8 63 61 74 65 67 6f 72 79 a9 72 65 66 65 72 65 6e 63 65 a6 61 75 74 68 6f 72 aa 4e 69 67 65 6c 20 52 65 65 73 a5 74 69 74 6c 65 b6 53 61 79 69 6e 67 73 20 6f 66 20 74 68 65 20 43 65 6e 74 75 72 79 a5 70 72 69 63 65 cb 40 21 e6 66 66 66 66 66 84 a8 63 61 74 65 67 6f 72 79 a7 66 69 63 74 69 6f 6e a6 61 75 74 68 6f 72 ac 45 76 65 6c 79 6e 20 57 61 75 67 68 a5 74 69 74 6c 65 af 53 77 6f 72 64 20 6f 66 20 48 6f 6e 6f 75 72 a5 70 72 69 63 65 cb 40 29 fa e1 47 ae 14 7b 85 a8 63 61 74 65 67 6f 72 79 a7 66 69 63 74 69 6f 6e a6 61 75 74 68 6f 72 af 48 65 72 6d 61 6e 20 4d 65 6c 76 69 6c 6c 65 a5 74 69 74 6c 65 a9 4d 6f 62 79 20 44 69 63 6b a4 69 73 62 6e ad 30 2d 35 35 33 2d 32 31 33 31 31 2d 33 a5 70 72 69 63 65 cb 40 21 fa e1 47 ae 14 7b 85 a8 63 61 74 65 67 6f 72 79 a7 66 69 63 74 69 6f 6e a6 61 75 74 68 6f 72 b0 4a 2e 20 52 2e 20 52 2e 20 54 6f 6c 6b 69 65 6e a5 74 69 74 6c 65 b5 54 68 65 20 4c 6f 72 64 20 6f 66 20 74 68 65 20 52 69 6e 67 73 a4 69 73 62 6e ad 30 2d 33 39 35 2d 31 39 33 39 35 2d 38 a5 70 72 69 63 65 cb 40 36 fd 70 a3 d7 0a 3d a7 62 69 63 79 63 6c 65 82 a5 63 6f 6c 6f 72 a3 72 65 64 a5 70 72 69 63 65 cb 40 33 f3 33 33 33 33 33 a9 6e 65 77 73 70 61 70 65 72 92 83 a6 66 6f 72 6d 61 74 aa 62 72 6f 61 64 73 68 65 65 74 a5 74 69 74 6c 65 af 46 69 6e 61 6e 63 69 61 6c 20 54 69 6d 65 73 a5 70 72 69 63 65 cb 40 06 00 00 00 00 00 00 83 a6 66 6f 72 6d 61 74 a7 74 61 62 6c 6f 69 64 a5 74 69 74 6c 65 ac 54 68 65 20 47 75 61 72 64 69 61 6e a5 70 72 69 63 65 02",
  367. recipeConfig: [
  368. {
  369. "op": "To MessagePack",
  370. "args": []
  371. },
  372. {
  373. "op": "To Hex",
  374. "args": ["Space"]
  375. }
  376. ]
  377. },
  378. {
  379. name: "From MessagePack: valid msgpack",
  380. input: "81 a5 73 74 6f 72 65 83 a4 62 6f 6f 6b 94 84 a8 63 61 74 65 67 6f 72 79 a9 72 65 66 65 72 65 6e 63 65 a6 61 75 74 68 6f 72 aa 4e 69 67 65 6c 20 52 65 65 73 a5 74 69 74 6c 65 b6 53 61 79 69 6e 67 73 20 6f 66 20 74 68 65 20 43 65 6e 74 75 72 79 a5 70 72 69 63 65 cb 40 21 e6 66 66 66 66 66 84 a8 63 61 74 65 67 6f 72 79 a7 66 69 63 74 69 6f 6e a6 61 75 74 68 6f 72 ac 45 76 65 6c 79 6e 20 57 61 75 67 68 a5 74 69 74 6c 65 af 53 77 6f 72 64 20 6f 66 20 48 6f 6e 6f 75 72 a5 70 72 69 63 65 cb 40 29 fa e1 47 ae 14 7b 85 a8 63 61 74 65 67 6f 72 79 a7 66 69 63 74 69 6f 6e a6 61 75 74 68 6f 72 af 48 65 72 6d 61 6e 20 4d 65 6c 76 69 6c 6c 65 a5 74 69 74 6c 65 a9 4d 6f 62 79 20 44 69 63 6b a4 69 73 62 6e ad 30 2d 35 35 33 2d 32 31 33 31 31 2d 33 a5 70 72 69 63 65 cb 40 21 fa e1 47 ae 14 7b 85 a8 63 61 74 65 67 6f 72 79 a7 66 69 63 74 69 6f 6e a6 61 75 74 68 6f 72 b0 4a 2e 20 52 2e 20 52 2e 20 54 6f 6c 6b 69 65 6e a5 74 69 74 6c 65 b5 54 68 65 20 4c 6f 72 64 20 6f 66 20 74 68 65 20 52 69 6e 67 73 a4 69 73 62 6e ad 30 2d 33 39 35 2d 31 39 33 39 35 2d 38 a5 70 72 69 63 65 cb 40 36 fd 70 a3 d7 0a 3d a7 62 69 63 79 63 6c 65 82 a5 63 6f 6c 6f 72 a3 72 65 64 a5 70 72 69 63 65 cb 40 33 f3 33 33 33 33 33 a9 6e 65 77 73 70 61 70 65 72 92 83 a6 66 6f 72 6d 61 74 aa 62 72 6f 61 64 73 68 65 65 74 a5 74 69 74 6c 65 af 46 69 6e 61 6e 63 69 61 6c 20 54 69 6d 65 73 a5 70 72 69 63 65 cb 40 06 00 00 00 00 00 00 83 a6 66 6f 72 6d 61 74 a7 74 61 62 6c 6f 69 64 a5 74 69 74 6c 65 ac 54 68 65 20 47 75 61 72 64 69 61 6e a5 70 72 69 63 65 02",
  381. expectedOutput: JSON.stringify(JSON_TEST_DATA),
  382. recipeConfig: [
  383. {
  384. "op": "From Hex",
  385. "args": ["Space"]
  386. },
  387. {
  388. "op": "From MessagePack",
  389. "args": []
  390. }
  391. ]
  392. }
  393. ]);