luacats_grammar_spec.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. local t = require('test.testutil')
  2. local eq = t.eq
  3. local grammar = require('scripts/luacats_grammar')
  4. describe('luacats grammar', function()
  5. --- @param text string
  6. --- @param exp table<string,string>
  7. local function test(text, exp)
  8. it(string.format('can parse %q', text), function()
  9. eq(exp, grammar:match(text))
  10. end)
  11. end
  12. test('@param hello vim.type', {
  13. kind = 'param',
  14. name = 'hello',
  15. type = 'vim.type',
  16. })
  17. test('@param hello vim.type this is a description', {
  18. kind = 'param',
  19. name = 'hello',
  20. type = 'vim.type',
  21. desc = 'this is a description',
  22. })
  23. test('@param hello vim.type|string this is a description', {
  24. kind = 'param',
  25. name = 'hello',
  26. type = 'vim.type|string',
  27. desc = 'this is a description',
  28. })
  29. test('@param hello vim.type?|string? this is a description', {
  30. kind = 'param',
  31. name = 'hello',
  32. type = 'vim.type?|string?',
  33. desc = 'this is a description',
  34. })
  35. test('@return string hello this is a description', {
  36. kind = 'return',
  37. {
  38. name = 'hello',
  39. type = 'string',
  40. },
  41. desc = 'this is a description',
  42. })
  43. test('@return fun() hello this is a description', {
  44. kind = 'return',
  45. {
  46. name = 'hello',
  47. type = 'fun()',
  48. },
  49. desc = 'this is a description',
  50. })
  51. test('@return fun(a: string[]): string hello this is a description', {
  52. kind = 'return',
  53. {
  54. name = 'hello',
  55. type = 'fun(a: string[]): string',
  56. },
  57. desc = 'this is a description',
  58. })
  59. test('@return fun(a: table<string,any>): string hello this is a description', {
  60. kind = 'return',
  61. {
  62. name = 'hello',
  63. type = 'fun(a: table<string,any>): string',
  64. },
  65. desc = 'this is a description',
  66. })
  67. test('@param ... string desc', {
  68. kind = 'param',
  69. name = '...',
  70. type = 'string',
  71. desc = 'desc',
  72. })
  73. test('@param level (integer|string) desc', {
  74. kind = 'param',
  75. name = 'level',
  76. type = 'integer|string',
  77. desc = 'desc',
  78. })
  79. test('@return (string command) the command and arguments', {
  80. kind = 'return',
  81. {
  82. name = 'command',
  83. type = 'string',
  84. },
  85. desc = 'the command and arguments',
  86. })
  87. test('@return (string command, string[] args) the command and arguments', {
  88. kind = 'return',
  89. {
  90. name = 'command',
  91. type = 'string',
  92. },
  93. {
  94. name = 'args',
  95. type = 'string[]',
  96. },
  97. desc = 'the command and arguments',
  98. })
  99. test('@param rfc "rfc2396" | "rfc2732" | "rfc3986" | nil', {
  100. kind = 'param',
  101. name = 'rfc',
  102. type = '"rfc2396" | "rfc2732" | "rfc3986" | nil',
  103. })
  104. test('@param offset_encoding "utf-8" | "utf-16" | "utf-32" | nil', {
  105. kind = 'param',
  106. name = 'offset_encoding',
  107. type = '"utf-8" | "utf-16" | "utf-32" | nil',
  108. })
  109. -- handle a : after the param type
  110. test('@param a b: desc', {
  111. kind = 'param',
  112. name = 'a',
  113. type = 'b',
  114. desc = 'desc',
  115. })
  116. test(
  117. '@field prefix? string|table|(fun(diagnostic:vim.Diagnostic,i:integer,total:integer): string, string)',
  118. {
  119. kind = 'field',
  120. name = 'prefix?',
  121. type = 'string|table|(fun(diagnostic:vim.Diagnostic,i:integer,total:integer): string, string)',
  122. }
  123. )
  124. test('@field [integer] integer', {
  125. kind = 'field',
  126. name = '[integer]',
  127. type = 'integer',
  128. })
  129. test('@field [1] integer', {
  130. kind = 'field',
  131. name = '[1]',
  132. type = 'integer',
  133. })
  134. test('@param type `T` this is a generic type', {
  135. desc = 'this is a generic type',
  136. kind = 'param',
  137. name = 'type',
  138. type = '`T`',
  139. })
  140. end)