syntax_parse_test.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. # Copyright (C) 2013 Google Inc.
  2. # 2016 YouCompleteMe contributors
  3. #
  4. # This file is part of YouCompleteMe.
  5. #
  6. # YouCompleteMe is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # YouCompleteMe is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  18. from __future__ import unicode_literals
  19. from __future__ import print_function
  20. from __future__ import division
  21. from __future__ import absolute_import
  22. # Not installing aliases from python-future; it's unreliable and slow.
  23. from builtins import * # noqa
  24. from ycm.tests.test_utils import MockVimModule
  25. MockVimModule()
  26. import os
  27. from hamcrest import assert_that, contains_inanyorder, has_item, has_items
  28. from ycm import syntax_parse
  29. from ycmd.utils import ReadFile
  30. def ContentsOfTestFile( test_file ):
  31. dir_of_script = os.path.dirname( os.path.abspath( __file__ ) )
  32. full_path_to_test_file = os.path.join( dir_of_script, 'testdata', test_file )
  33. return ReadFile( full_path_to_test_file )
  34. def KeywordsFromSyntaxListOutput_PythonSyntax_test():
  35. expected_keywords = (
  36. 'bytearray', 'IndexError', 'all', 'help', 'vars', 'SyntaxError', 'global',
  37. 'elif', 'unicode', 'sorted', 'memoryview', 'isinstance', 'except',
  38. 'nonlocal', 'NameError', 'finally', 'BytesWarning', 'dict', 'IOError',
  39. 'pass', 'oct', 'bin', 'SystemExit', 'return', 'StandardError', 'format',
  40. 'TabError', 'break', 'next', 'not', 'UnicodeDecodeError', 'False',
  41. 'RuntimeWarning', 'list', 'iter', 'try', 'reload', 'Warning', 'round',
  42. 'dir', 'cmp', 'set', 'bytes', 'UnicodeTranslateError', 'intern',
  43. 'issubclass', 'yield', 'Ellipsis', 'hash', 'locals', 'BufferError',
  44. 'slice', 'for', 'FloatingPointError', 'sum', 'VMSError', 'getattr', 'abs',
  45. 'print', 'import', 'True', 'FutureWarning', 'ImportWarning', 'None',
  46. 'EOFError', 'len', 'frozenset', 'ord', 'super', 'raise', 'TypeError',
  47. 'KeyboardInterrupt', 'UserWarning', 'filter', 'range', 'staticmethod',
  48. 'SystemError', 'or', 'BaseException', 'pow', 'RuntimeError', 'float',
  49. 'MemoryError', 'StopIteration', 'globals', 'divmod', 'enumerate', 'apply',
  50. 'LookupError', 'open', 'basestring', 'from', 'UnicodeError', 'zip', 'hex',
  51. 'long', 'IndentationError', 'int', 'chr', '__import__', 'type',
  52. 'Exception', 'continue', 'tuple', 'reduce', 'reversed', 'else', 'assert',
  53. 'UnicodeEncodeError', 'input', 'with', 'hasattr', 'delattr', 'setattr',
  54. 'raw_input', 'PendingDeprecationWarning', 'compile', 'ArithmeticError',
  55. 'while', 'del', 'str', 'property', 'def', 'and', 'GeneratorExit',
  56. 'ImportError', 'xrange', 'is', 'EnvironmentError', 'KeyError', 'coerce',
  57. 'SyntaxWarning', 'file', 'in', 'unichr', 'ascii', 'any', 'as', 'if',
  58. 'OSError', 'DeprecationWarning', 'min', 'UnicodeWarning', 'execfile', 'id',
  59. 'complex', 'bool', 'ValueError', 'NotImplemented', 'map', 'exec', 'buffer',
  60. 'max', 'class', 'object', 'repr', 'callable', 'ZeroDivisionError', 'eval',
  61. '__debug__', 'ReferenceError', 'AssertionError', 'classmethod',
  62. 'UnboundLocalError', 'NotImplementedError', 'lambda', 'AttributeError',
  63. 'OverflowError', 'WindowsError' )
  64. assert_that( syntax_parse._KeywordsFromSyntaxListOutput(
  65. ContentsOfTestFile( 'python_syntax' ) ),
  66. contains_inanyorder( *expected_keywords ) )
  67. def KeywordsFromSyntaxListOutput_CppSyntax_test():
  68. expected_keywords = (
  69. 'int_fast32_t', 'FILE', 'size_t', 'bitor', 'typedef', 'const', 'struct',
  70. 'uint8_t', 'fpos_t', 'thread_local', 'unsigned', 'uint_least16_t', 'do',
  71. 'intptr_t', 'uint_least64_t', 'return', 'auto', 'void', '_Complex',
  72. 'break', '_Alignof', 'not', 'using', '_Static_assert', '_Thread_local',
  73. 'public', 'uint_fast16_t', 'this', 'continue', 'char32_t', 'int16_t',
  74. 'intmax_t', 'static', 'clock_t', 'sizeof', 'int_fast64_t', 'mbstate_t',
  75. 'try', 'xor', 'uint_fast32_t', 'int_least8_t', 'div_t', 'volatile',
  76. 'template', 'char16_t', 'new', 'ldiv_t', 'int_least16_t', 'va_list',
  77. 'uint_least8_t', 'goto', 'noreturn', 'enum', 'static_assert', 'bitand',
  78. 'compl', 'imaginary', 'jmp_buf', 'throw', 'asm', 'ptrdiff_t', 'uint16_t',
  79. 'or', 'uint_fast8_t', '_Bool', 'int32_t', 'float', 'private', 'restrict',
  80. 'wint_t', 'operator', 'not_eq', '_Imaginary', 'alignas', 'union', 'long',
  81. 'uint_least32_t', 'int_least64_t', 'friend', 'uintptr_t', 'int8_t', 'else',
  82. 'export', 'int_fast8_t', 'catch', 'true', 'case', 'default', 'double',
  83. '_Noreturn', 'signed', 'typename', 'while', 'protected', 'wchar_t',
  84. 'wctrans_t', 'uint64_t', 'delete', 'and', 'register', 'false', 'int',
  85. 'uintmax_t', 'off_t', 'char', 'int64_t', 'int_fast16_t', 'DIR', '_Atomic',
  86. 'time_t', 'xor_eq', 'namespace', 'virtual', 'complex', 'bool', 'mutable',
  87. 'if', 'int_least32_t', 'sig_atomic_t', 'and_eq', 'ssize_t', 'alignof',
  88. '_Alignas', '_Generic', 'extern', 'class', 'typeid', 'short', 'for',
  89. 'uint_fast64_t', 'wctype_t', 'explicit', 'or_eq', 'switch', 'uint32_t',
  90. 'inline' )
  91. assert_that( syntax_parse._KeywordsFromSyntaxListOutput(
  92. ContentsOfTestFile( 'cpp_syntax' ) ),
  93. contains_inanyorder( *expected_keywords ) )
  94. def KeywordsFromSyntaxListOutput_JavaSyntax_test():
  95. expected_keywords = (
  96. 'code', 'text', 'cols', 'datetime', 'disabled', 'shape', 'codetype', 'alt',
  97. 'compact', 'style', 'valuetype', 'short', 'finally', 'continue', 'extends',
  98. 'valign', 'bordercolor', 'do', 'return', 'rel', 'rules', 'void',
  99. 'nohref', 'abbr', 'background', 'scrolling', 'instanceof', 'name',
  100. 'summary', 'try', 'default', 'noshade', 'coords', 'dir', 'frame', 'usemap',
  101. 'ismap', 'static', 'hspace', 'vlink', 'for', 'selected', 'rev', 'vspace',
  102. 'content', 'method', 'version', 'volatile', 'above', 'new', 'charoff',
  103. 'public', 'alink', 'enum', 'codebase', 'if', 'noresize', 'interface',
  104. 'checked', 'byte', 'super', 'throw', 'src', 'language', 'package',
  105. 'standby', 'script', 'longdesc', 'maxlength', 'cellpadding', 'throws',
  106. 'tabindex', 'color', 'colspan', 'accesskey', 'float', 'while', 'private',
  107. 'height', 'boolean', 'wrap', 'prompt', 'nowrap', 'size', 'rows', 'span',
  108. 'clip', 'bgcolor', 'top', 'long', 'start', 'scope', 'scheme', 'type',
  109. 'final', 'lang', 'visibility', 'else', 'assert', 'transient', 'link',
  110. 'catch', 'true', 'serializable', 'target', 'lowsrc', 'this', 'double',
  111. 'align', 'value', 'cite', 'headers', 'below', 'protected', 'declare',
  112. 'classid', 'defer', 'false', 'synchronized', 'int', 'abstract', 'accept',
  113. 'hreflang', 'char', 'border', 'id', 'native', 'rowspan', 'charset',
  114. 'archive', 'strictfp', 'readonly', 'axis', 'cellspacing', 'profile',
  115. 'multiple', 'object', 'action', 'pagex', 'pagey', 'marginheight', 'data',
  116. 'class', 'frameborder', 'enctype', 'implements', 'break', 'gutter', 'url',
  117. 'clear', 'face', 'switch', 'marginwidth', 'width', 'left' )
  118. assert_that( syntax_parse._KeywordsFromSyntaxListOutput(
  119. ContentsOfTestFile( 'java_syntax' ) ),
  120. contains_inanyorder( *expected_keywords ) )
  121. def KeywordsFromSyntaxListOutput_PhpSyntax_ContainsFunctions_test():
  122. assert_that( syntax_parse._KeywordsFromSyntaxListOutput(
  123. ContentsOfTestFile( 'php_syntax' ) ),
  124. has_items( 'array_change_key_case' ) )
  125. def KeywordsFromSyntaxListOutput_PhpSyntax_ContainsPreProc_test():
  126. assert_that( syntax_parse._KeywordsFromSyntaxListOutput(
  127. ContentsOfTestFile( 'php_syntax' ) ),
  128. has_items( 'skip', 'function' ) )
  129. def KeywordsFromSyntaxListOutput_Basic_test():
  130. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  131. foogroup xxx foo bar
  132. zoo goo
  133. links to Statement""" ),
  134. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  135. def KeywordsFromSyntaxListOutput_Function_test():
  136. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  137. foogroup xxx foo bar
  138. zoo goo
  139. links to Function""" ),
  140. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  141. def KeywordsFromSyntaxListOutput_ContainedArgAllowed_test():
  142. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  143. phpFunctions xxx contained gzclose yaz_syntax html_entity_decode fbsql_read_blob png2wbmp mssql_init cpdf_set_title gztell fbsql_insert_id empty cpdf_restore mysql_field_type closelog swftext ldap_search curl_errno gmp_div_r mssql_data_seek getmyinode printer_draw_pie mcve_initconn ncurses_getmaxyx defined
  144. contained replace_child has_attributes specified insertdocument assign node_name hwstat addshape get_attribute_node html_dump_mem userlist
  145. links to Function""" ), # noqa
  146. has_items( 'gzclose', 'userlist', 'ldap_search' ) )
  147. def KeywordsFromSyntaxListOutput_JunkIgnored_test():
  148. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  149. --- Syntax items ---
  150. foogroup xxx foo bar
  151. zoo goo
  152. links to Statement
  153. Spell cluster=NONE
  154. NoSpell cluster=NONE""" ),
  155. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  156. def KeywordsFromSyntaxListOutput_MultipleStatementGroups_test():
  157. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  158. foogroup xxx foo bar
  159. links to Statement
  160. bargroup xxx zoo goo
  161. links to Statement""" ),
  162. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  163. def KeywordsFromSyntaxListOutput_StatementAndTypeGroups_test():
  164. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  165. foogroup xxx foo bar
  166. links to Statement
  167. bargroup xxx zoo goo
  168. links to Type""" ),
  169. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  170. def KeywordsFromSyntaxListOutput_StatementHierarchy_test():
  171. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  172. baa xxx foo bar
  173. links to Foo
  174. Foo xxx zoo goo
  175. links to Bar
  176. Bar xxx qux moo
  177. links to Statement""" ),
  178. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo', 'qux', 'moo' ) )
  179. def KeywordsFromSyntaxListOutput_TypeHierarchy_test():
  180. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  181. baa xxx foo bar
  182. links to Foo
  183. Foo xxx zoo goo
  184. links to Bar
  185. Bar xxx qux moo
  186. links to Type""" ),
  187. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo', 'qux', 'moo' ) )
  188. def KeywordsFromSyntaxListOutput_StatementAndTypeHierarchy_test():
  189. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  190. tBaa xxx foo bar
  191. links to tFoo
  192. tFoo xxx zoo goo
  193. links to tBar
  194. tBar xxx qux moo
  195. links to Type
  196. sBaa xxx na bar
  197. links to sFoo
  198. sFoo xxx zoo nb
  199. links to sBar
  200. sBar xxx qux nc
  201. links to Statement""" ),
  202. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo', 'qux', 'moo',
  203. 'na', 'nb', 'nc' ) )
  204. def SyntaxGroupsFromOutput_Basic_test():
  205. assert_that( syntax_parse._SyntaxGroupsFromOutput( """
  206. foogroup xxx foo bar
  207. zoo goo
  208. links to Statement""" ),
  209. has_item( 'foogroup' ) )
  210. def ExtractKeywordsFromGroup_Basic_test():
  211. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  212. syntax_parse.SyntaxGroup( '', [
  213. 'foo bar',
  214. 'zoo goo',
  215. ] ) ),
  216. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  217. def ExtractKeywordsFromGroup_Commas_test():
  218. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  219. syntax_parse.SyntaxGroup( '', [
  220. 'foo, bar,',
  221. 'zoo goo',
  222. ] ) ),
  223. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  224. def ExtractKeywordsFromGroup_WithLinksTo_test():
  225. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  226. syntax_parse.SyntaxGroup( '', [
  227. 'foo bar',
  228. 'zoo goo',
  229. 'links to Statement'
  230. ] ) ),
  231. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  232. def ExtractKeywordsFromGroup_KeywordStarts_test():
  233. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  234. syntax_parse.SyntaxGroup( '', [
  235. 'foo bar',
  236. 'contained boo baa',
  237. 'zoo goo',
  238. ] ) ),
  239. contains_inanyorder( 'foo', 'bar', 'boo', 'baa', 'zoo', 'goo' ) )
  240. def ExtractKeywordsFromGroup_KeywordMiddle_test():
  241. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  242. syntax_parse.SyntaxGroup( '', [
  243. 'foo contained bar',
  244. 'zoo goo'
  245. ] ) ),
  246. contains_inanyorder( 'foo', 'contained', 'bar', 'zoo', 'goo' ) )
  247. def ExtractKeywordsFromGroup_KeywordAssign_test():
  248. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  249. syntax_parse.SyntaxGroup( '', [
  250. 'nextgroup=zoo skipwhite foo bar',
  251. 'zoo goo',
  252. ] ) ),
  253. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  254. def ExtractKeywordsFromGroup_KeywordAssignAndMiddle_test():
  255. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  256. syntax_parse.SyntaxGroup( '', [
  257. 'nextgroup=zoo foo skipnl bar',
  258. 'zoo goo',
  259. ] ) ),
  260. contains_inanyorder( 'foo', 'skipnl', 'bar', 'zoo', 'goo' ) )
  261. def ExtractKeywordsFromGroup_KeywordWithoutNextgroup_test():
  262. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  263. syntax_parse.SyntaxGroup( '', [
  264. 'skipempty foo bar',
  265. 'zoo goo',
  266. ] ) ),
  267. contains_inanyorder( 'skipempty', 'foo', 'bar', 'zoo', 'goo' ) )
  268. def ExtractKeywordsFromGroup_ContainedSyntaxArgAllowed_test():
  269. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  270. syntax_parse.SyntaxGroup( '', [
  271. 'contained foo zoq',
  272. 'contained bar goo',
  273. 'far'
  274. ] ) ),
  275. contains_inanyorder( 'foo', 'zoq', 'bar', 'goo', 'far' ) )