Code.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.js";
  11. const JPATH_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(JPATH_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(JPATH_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(JPATH_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(JPATH_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(JPATH_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(JPATH_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(JPATH_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(JPATH_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(JPATH_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. ]);