syntax_parse_test.py 13 KB

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