parse.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. describe('parse', function()
  2. local parse = require 'parse'
  3. test('comments', function()
  4. local document
  5. document = parse('#')
  6. expect(document.definitions).to.equal({})
  7. document = parse('#{}')
  8. expect(document.definitions).to.equal({})
  9. expect(parse('{}').definitions).to_not.equal({})
  10. expect(function() parse('{}#a$b@') end).to_not.fail()
  11. expect(function() parse('{a(b:"#")}') end).to_not.fail()
  12. end)
  13. test('document', function()
  14. local document
  15. expect(function() parse() end).to.fail()
  16. expect(function() parse('foo') end).to.fail()
  17. expect(function() parse('query') end).to.fail()
  18. expect(function() parse('query{} foo') end).to.fail()
  19. document = parse('')
  20. expect(document.kind).to.equal('document')
  21. expect(document.definitions).to.equal({})
  22. document = parse('query{} mutation{} {}')
  23. expect(document.kind).to.equal('document')
  24. expect(#document.definitions).to.equal(3)
  25. end)
  26. describe('operation', function()
  27. local operation
  28. test('shorthand', function()
  29. operation = parse('{}').definitions[1]
  30. expect(operation.kind).to.equal('operation')
  31. expect(operation.name).to_not.exist()
  32. expect(operation.operation).to.equal('query')
  33. end)
  34. test('operationType', function()
  35. operation = parse('query{}').definitions[1]
  36. expect(operation.operation).to.equal('query')
  37. operation = parse('mutation{}').definitions[1]
  38. expect(operation.operation).to.equal('mutation')
  39. expect(function() parse('kneeReplacement{}') end).to.fail()
  40. end)
  41. test('name', function()
  42. operation = parse('query{}').definitions[1]
  43. expect(operation.name).to_not.exist()
  44. operation = parse('query queryName{}').definitions[1]
  45. expect(operation.name).to.exist()
  46. expect(operation.name.value).to.equal('queryName')
  47. end)
  48. test('variableDefinitions', function()
  49. expect(function() parse('query(){}') end).to.fail()
  50. expect(function() parse('query(x){}') end).to.fail()
  51. operation = parse('query name($a:Int,$b:Int){}').definitions[1]
  52. expect(operation.name.value).to.equal('name')
  53. expect(operation.variableDefinitions).to.exist()
  54. expect(#operation.variableDefinitions).to.equal(2)
  55. operation = parse('query($a:Int,$b:Int){}').definitions[1]
  56. expect(operation.variableDefinitions).to.exist()
  57. expect(#operation.variableDefinitions).to.equal(2)
  58. end)
  59. test('directives', function()
  60. local operation = parse('query{}').definitions[1]
  61. expect(operation.directives).to_not.exist()
  62. local operation = parse('query @a{}').definitions[1]
  63. expect(#operation.directives).to.exist()
  64. local operation = parse('query name @a{}').definitions[1]
  65. expect(#operation.directives).to.exist()
  66. local operation = parse('query ($a:Int) @a {}').definitions[1]
  67. expect(#operation.directives).to.exist()
  68. local operation = parse('query name ($a:Int) @a {}').definitions[1]
  69. expect(#operation.directives).to.exist()
  70. end)
  71. end)
  72. describe('fragmentDefinition', function()
  73. local fragment
  74. test('fragmentName', function()
  75. expect(function() parse('fragment {}') end).to.fail()
  76. expect(function() parse('fragment on x {}') end).to.fail()
  77. expect(function() parse('fragment on on x {}') end).to.fail()
  78. fragment = parse('fragment x on y {}').definitions[1]
  79. expect(fragment.kind).to.equal('fragmentDefinition')
  80. expect(fragment.name.value).to.equal('x')
  81. end)
  82. test('typeCondition', function()
  83. expect(function() parse('fragment x {}') end).to.fail()
  84. fragment = parse('fragment x on y {}').definitions[1]
  85. expect(fragment.typeCondition.name.value).to.equal('y')
  86. end)
  87. test('selectionSet', function()
  88. expect(function() parse('fragment x on y') end).to.fail()
  89. fragment = parse('fragment x on y {}').definitions[1]
  90. expect(fragment.selectionSet).to.exist()
  91. end)
  92. end)
  93. test('selectionSet', function()
  94. local selectionSet
  95. expect(function() parse('{') end).to.fail()
  96. expect(function() parse('}') end).to.fail()
  97. selectionSet = parse('{}').definitions[1].selectionSet
  98. expect(selectionSet.kind).to.equal('selectionSet')
  99. expect(selectionSet.selections).to.equal({})
  100. selectionSet = parse('{a b}').definitions[1].selectionSet
  101. expect(#selectionSet.selections).to.equal(2)
  102. end)
  103. describe('field', function()
  104. local field
  105. test('name', function()
  106. expect(function() parse('{$a}') end).to.fail()
  107. expect(function() parse('{@a}') end).to.fail()
  108. expect(function() parse('{.}') end).to.fail()
  109. expect(function() parse('{,}') end).to.fail()
  110. field = parse('{a}').definitions[1].selectionSet.selections[1]
  111. expect(field.kind).to.equal('field')
  112. expect(field.name.value).to.equal('a')
  113. end)
  114. test('alias', function()
  115. expect(function() parse('{a:b:}') end).to.fail()
  116. expect(function() parse('{a:b:c}') end).to.fail()
  117. expect(function() parse('{:a}') end).to.fail()
  118. field = parse('{a}').definitions[1].selectionSet.selections[1]
  119. expect(field.alias).to_not.exist()
  120. field = parse('{a:b}').definitions[1].selectionSet.selections[1]
  121. expect(field.alias).to.exist()
  122. expect(field.alias.kind).to.equal('alias')
  123. expect(field.alias.name.value).to.equal('a')
  124. expect(field.name.value).to.equal('b')
  125. end)
  126. test('arguments', function()
  127. expect(function() parse('{a()}') end).to.fail()
  128. field = parse('{a}').definitions[1].selectionSet.selections[1]
  129. expect(field.arguments).to_not.exist()
  130. field = parse('{a(b:false)}').definitions[1].selectionSet.selections[1]
  131. expect(field.arguments).to.exist()
  132. end)
  133. test('directives', function()
  134. expect(function() parse('{a@skip(b:false)(c:true)}') end).to.fail()
  135. field = parse('{a}').definitions[1].selectionSet.selections[1]
  136. expect(field.directives).to_not.exist()
  137. field = parse('{a@skip}').definitions[1].selectionSet.selections[1]
  138. expect(field.directives).to.exist()
  139. field = parse('{a(b:1)@skip}').definitions[1].selectionSet.selections[1]
  140. expect(field.directives).to.exist()
  141. end)
  142. test('selectionSet', function()
  143. expect(function() parse('{{}}') end).to.fail()
  144. field = parse('{a}').definitions[1].selectionSet.selections[1]
  145. expect(field.selectionSet).to_not.exist()
  146. field = parse('{a{}}').definitions[1].selectionSet.selections[1]
  147. expect(field.selectionSet).to.exist()
  148. field = parse('{a{a}}').definitions[1].selectionSet.selections[1]
  149. expect(field.selectionSet).to.exist()
  150. field = parse('{a(b:1)@skip{a}}').definitions[1].selectionSet.selections[1]
  151. expect(field.selectionSet).to.exist()
  152. end)
  153. end)
  154. test('fragmentSpread', function()
  155. local fragmentSpread
  156. expect(function() parse('{..a}') end).to.fail()
  157. expect(function() parse('{...}') end).to.fail()
  158. fragmentSpread = parse('{...a}').definitions[1].selectionSet.selections[1]
  159. expect(fragmentSpread.kind).to.equal('fragmentSpread')
  160. expect(fragmentSpread.name.value).to.equal('a')
  161. end)
  162. describe('inlineFragment', function()
  163. local inlineFragment
  164. test('typeCondition', function()
  165. expect(function() parse('{...on{}}') end).to.fail()
  166. inlineFragment = parse('{...{}}').definitions[1].selectionSet.selections[1]
  167. expect(inlineFragment.kind).to.equal('inlineFragment')
  168. expect(inlineFragment.typeCondition).to_not.exist()
  169. inlineFragment = parse('{...on a{}}').definitions[1].selectionSet.selections[1]
  170. expect(inlineFragment.typeCondition).to.exist()
  171. expect(inlineFragment.typeCondition.name.value).to.equal('a')
  172. end)
  173. test('selectionSet', function()
  174. expect(function() parse('{... on a}') end).to.fail()
  175. inlineFragment = parse('{...{}}').definitions[1].selectionSet.selections[1]
  176. expect(inlineFragment.selectionSet).to.exist()
  177. inlineFragment = parse('{... on a{}}').definitions[1].selectionSet.selections[1]
  178. expect(inlineFragment.selectionSet).to.exist()
  179. end)
  180. end)
  181. test('arguments', function()
  182. local arguments
  183. expect(function() parse('{a()}') end).to.fail()
  184. arguments = parse('{a(b:1)}').definitions[1].selectionSet.selections[1].arguments
  185. expect(#arguments).to.equal(1)
  186. arguments = parse('{a(b:1 c:1)}').definitions[1].selectionSet.selections[1].arguments
  187. expect(#arguments).to.equal(2)
  188. end)
  189. test('argument', function()
  190. local argument
  191. expect(function() parse('{a(b)}') end).to.fail()
  192. expect(function() parse('{a(@b)}') end).to.fail()
  193. expect(function() parse('{a($b)}') end).to.fail()
  194. expect(function() parse('{a(b::)}') end).to.fail()
  195. expect(function() parse('{a(:1)}') end).to.fail()
  196. expect(function() parse('{a(b:)}') end).to.fail()
  197. expect(function() parse('{a(:)}') end).to.fail()
  198. expect(function() parse('{a(b c)}') end).to.fail()
  199. argument = parse('{a(b:1)}').definitions[1].selectionSet.selections[1].arguments[1]
  200. expect(argument.kind).to.equal('argument')
  201. expect(argument.name.value).to.equal('b')
  202. expect(argument.value.value).to.equal('1')
  203. end)
  204. test('directives', function()
  205. local directives
  206. expect(function() parse('{a@}') end).to.fail()
  207. expect(function() parse('{a@@}') end).to.fail()
  208. directives = parse('{a@b}').definitions[1].selectionSet.selections[1].directives
  209. expect(#directives).to.equal(1)
  210. directives = parse('{a@b(c:1)@d}').definitions[1].selectionSet.selections[1].directives
  211. expect(#directives).to.equal(2)
  212. end)
  213. test('directive', function()
  214. local directive
  215. expect(function() parse('{a@b()}') end).to.fail()
  216. directive = parse('{a@b}').definitions[1].selectionSet.selections[1].directives[1]
  217. expect(directive.kind).to.equal('directive')
  218. expect(directive.name.value).to.equal('b')
  219. expect(directive.arguments).to_not.exist()
  220. directive = parse('{a@b(c:1)}').definitions[1].selectionSet.selections[1].directives[1]
  221. expect(directive.arguments).to.exist()
  222. end)
  223. test('variableDefinitions', function()
  224. local variableDefinitions
  225. expect(function() parse('query(){}') end).to.fail()
  226. expect(function() parse('query(a){}') end).to.fail()
  227. expect(function() parse('query(@a){}') end).to.fail()
  228. expect(function() parse('query($a){}') end).to.fail()
  229. variableDefinitions = parse('query($a:Int){}').definitions[1].variableDefinitions
  230. expect(#variableDefinitions).to.equal(1)
  231. variableDefinitions = parse('query($a:Int $b:Int){}').definitions[1].variableDefinitions
  232. expect(#variableDefinitions).to.equal(2)
  233. end)
  234. describe('variableDefinition', function()
  235. local variableDefinition
  236. test('variable', function()
  237. variableDefinition = parse('query($a:Int){}').definitions[1].variableDefinitions[1]
  238. expect(variableDefinition.kind).to.equal('variableDefinition')
  239. expect(variableDefinition.variable.name.value).to.equal('a')
  240. end)
  241. test('type', function()
  242. expect(function() parse('query($a){}') end).to.fail()
  243. expect(function() parse('query($a:){}') end).to.fail()
  244. expect(function() parse('query($a Int){}') end).to.fail()
  245. variableDefinition = parse('query($a:Int){}').definitions[1].variableDefinitions[1]
  246. expect(variableDefinition.type.name.value).to.equal('Int')
  247. end)
  248. test('defaultValue', function()
  249. expect(function() parse('query($a:Int=){}') end).to.fail()
  250. variableDefinition = parse('query($a:Int){}').definitions[1].variableDefinitions[1]
  251. expect(variableDefinition.defaultValue).to_not.exist()
  252. variableDefinition = parse('query($a:Int=1){}').definitions[1].variableDefinitions[1]
  253. expect(variableDefinition.defaultValue).to.exist()
  254. end)
  255. end)
  256. describe('value', function()
  257. local value
  258. local function run(input, result, type)
  259. local value = parse('{x(y:' .. input .. ')}').definitions[1].selectionSet.selections[1].arguments[1].value
  260. if type then expect(value.kind).to.equal(type) end
  261. if result then expect(value.value).to.equal(result) end
  262. return value
  263. end
  264. test('variable', function()
  265. expect(function() parse('{x(y:$)}') end).to.fail()
  266. expect(function() parse('{x(y:$a$)}') end).to.fail()
  267. value = run('$a')
  268. expect(value.kind).to.equal('variable')
  269. expect(value.name.value).to.equal('a')
  270. end)
  271. test('int', function()
  272. expect(function() parse('{x(y:01)}') end).to.fail()
  273. expect(function() parse('{x(y:-01)}') end).to.fail()
  274. expect(function() parse('{x(y:--1)}') end).to.fail()
  275. expect(function() parse('{x(y:+0)}') end).to.fail()
  276. run('0', '0', 'int')
  277. run('-0', '-0', 'int')
  278. run('1234', '1234', 'int')
  279. run('-1234', '-1234', 'int')
  280. end)
  281. test('float', function()
  282. expect(function() parse('{x(y:.1)}') end).to.fail()
  283. expect(function() parse('{x(y:1.)}') end).to.fail()
  284. expect(function() parse('{x(y:1..)}') end).to.fail()
  285. expect(function() parse('{x(y:0e1.0)}') end).to.fail()
  286. run('0.0', '0.0', 'float')
  287. run('-0.0', '-0.0', 'float')
  288. run('12.34', '12.34', 'float')
  289. run('1e0', '1e0', 'float')
  290. run('1e3', '1e3', 'float')
  291. run('1.0e3', '1.0e3', 'float')
  292. run('1.0e+3', '1.0e+3', 'float')
  293. run('1.0e-3', '1.0e-3', 'float')
  294. run('1.00e-30', '1.00e-30', 'float')
  295. end)
  296. test('boolean', function()
  297. run('true', 'true', 'boolean')
  298. run('false', 'false', 'boolean')
  299. end)
  300. test('string', function()
  301. expect(function() parse('{x(y:")}') end).to.fail()
  302. expect(function() parse('{x(y:\'\')}') end).to.fail()
  303. expect(function() parse('{x(y:"\n")}') end).to.fail()
  304. run('"yarn"', 'yarn', 'string')
  305. run('"th\\"read"', 'th"read', 'string')
  306. end)
  307. test('enum', function()
  308. run('a', 'a', 'enum')
  309. end)
  310. test('list', function()
  311. expect(function() parse('{x(y:[)}') end).to.fail()
  312. value = run('[]')
  313. expect(value.values).to.equal({})
  314. value = run('[a 1]')
  315. expect(value).to.equal({
  316. kind = 'list',
  317. values = {
  318. {
  319. kind = 'enum',
  320. value = 'a'
  321. },
  322. {
  323. kind = 'int',
  324. value = '1'
  325. }
  326. }
  327. })
  328. value = run('[a [b] c]')
  329. expect(value).to.equal({
  330. kind = 'list',
  331. values = {
  332. {
  333. kind = 'enum',
  334. value = 'a'
  335. },
  336. {
  337. kind = 'list',
  338. values = {
  339. {
  340. kind = 'enum',
  341. value = 'b'
  342. }
  343. }
  344. },
  345. {
  346. kind = 'enum',
  347. value = 'c'
  348. }
  349. }
  350. })
  351. end)
  352. test('object', function()
  353. expect(function() parse('{x(y:{a})}') end).to.fail()
  354. expect(function() parse('{x(y:{a:})}') end).to.fail()
  355. expect(function() parse('{x(y:{a::})}') end).to.fail()
  356. expect(function() parse('{x(y:{1:1})}') end).to.fail()
  357. expect(function() parse('{x(y:{"foo":"bar"})}') end).to.fail()
  358. value = run('{}')
  359. expect(value.kind).to.equal('inputObject')
  360. expect(value.values).to.equal({})
  361. value = run('{a:1}')
  362. expect(value.values).to.equal({
  363. {
  364. name = 'a',
  365. value = {
  366. kind = 'int',
  367. value = '1'
  368. }
  369. }
  370. })
  371. value = run('{a:1 b:2}')
  372. expect(#value.values).to.equal(2)
  373. end)
  374. end)
  375. test('namedType', function()
  376. expect(function() parse('query($a:$b){}') end).to.fail()
  377. local namedType = parse('query($a:b){}').definitions[1].variableDefinitions[1].type
  378. expect(namedType.kind).to.equal('namedType')
  379. expect(namedType.name.value).to.equal('b')
  380. end)
  381. test('listType', function()
  382. local listType
  383. expect(function() parse('query($a:[]){}') end).to.fail()
  384. listType = parse('query($a:[b]){}').definitions[1].variableDefinitions[1].type
  385. expect(listType.kind).to.equal('listType')
  386. expect(listType.type.kind).to.equal('namedType')
  387. expect(listType.type.name.value).to.equal('b')
  388. listType = parse('query($a:[[b]]){}').definitions[1].variableDefinitions[1].type
  389. expect(listType.kind).to.equal('listType')
  390. expect(listType.type.kind).to.equal('listType')
  391. end)
  392. test('nonNullType', function()
  393. local nonNullType
  394. expect(function() parse('query($a:!){}') end).to.fail()
  395. expect(function() parse('query($a:b!!){}') end).to.fail()
  396. nonNullType = parse('query($a:b!){}').definitions[1].variableDefinitions[1].type
  397. expect(nonNullType.kind).to.equal('nonNullType')
  398. expect(nonNullType.type.kind).to.equal('namedType')
  399. expect(nonNullType.type.name.value).to.equal('b')
  400. nonNullType = parse('query($a:[b]!){}').definitions[1].variableDefinitions[1].type
  401. expect(nonNullType.kind).to.equal('nonNullType')
  402. expect(nonNullType.type.kind).to.equal('listType')
  403. end)
  404. end)