syntax_parse_test.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 ycm.tests.test_utils import MockVimModule
  19. MockVimModule()
  20. import os
  21. from hamcrest import assert_that, contains_inanyorder, has_item, has_items
  22. from unittest import TestCase
  23. from ycm import syntax_parse
  24. from ycmd.utils import ReadFile
  25. def ContentsOfTestFile( test_file ):
  26. dir_of_script = os.path.dirname( os.path.abspath( __file__ ) )
  27. full_path_to_test_file = os.path.join( dir_of_script, 'testdata', test_file )
  28. return ReadFile( full_path_to_test_file )
  29. class SyntaxTest( TestCase ):
  30. def test_KeywordsFromSyntaxListOutput_PythonSyntax( self ):
  31. expected_keywords = ( 'bytearray', 'IndexError', 'all', 'help', 'vars',
  32. 'SyntaxError', 'global', 'elif', 'unicode', 'sorted', 'memoryview',
  33. 'isinstance', 'except', 'nonlocal', 'NameError', 'finally',
  34. 'BytesWarning', 'dict', 'IOError', 'pass', 'oct', 'bin', 'SystemExit',
  35. 'return', 'StandardError', 'format', 'TabError', 'break', 'next',
  36. 'not', 'UnicodeDecodeError', 'False', 'RuntimeWarning', 'list', 'iter',
  37. 'try', 'reload', 'Warning', 'round', 'dir', 'cmp', 'set', 'bytes',
  38. 'UnicodeTranslateError', 'intern', 'issubclass', 'yield', 'Ellipsis',
  39. 'hash', 'locals', 'BufferError', 'slice', 'for', 'FloatingPointError',
  40. 'sum', 'VMSError', 'getattr', 'abs', 'print', 'import', 'True',
  41. 'FutureWarning', 'ImportWarning', 'None', 'EOFError', 'len',
  42. 'frozenset', 'ord', 'super', 'raise', 'TypeError', 'KeyboardInterrupt',
  43. 'UserWarning', 'filter', 'range', 'staticmethod', 'SystemError', 'or',
  44. 'BaseException', 'pow', 'RuntimeError', 'float', 'MemoryError',
  45. 'StopIteration', 'globals', 'divmod', 'enumerate', 'apply',
  46. 'LookupError', 'open', 'basestring', 'from', 'UnicodeError', 'zip',
  47. 'hex', 'long', 'IndentationError', 'int', 'chr', '__import__', 'type',
  48. 'Exception', 'continue', 'tuple', 'reduce', 'reversed', 'else',
  49. 'assert', 'UnicodeEncodeError', 'input', 'with', 'hasattr', 'delattr',
  50. 'setattr', 'raw_input', 'PendingDeprecationWarning', 'compile',
  51. 'ArithmeticError', 'while', 'del', 'str', 'property', 'def', 'and',
  52. 'GeneratorExit', 'ImportError', 'xrange', 'is', 'EnvironmentError',
  53. 'KeyError', 'coerce', 'SyntaxWarning', 'file', 'in', 'unichr', 'ascii',
  54. 'any', 'as', 'if', 'OSError', 'DeprecationWarning', 'min',
  55. 'UnicodeWarning', 'execfile', 'id', 'complex', 'bool', 'ValueError',
  56. 'NotImplemented', 'map', 'exec', 'buffer', 'max', 'class', 'object',
  57. 'repr', 'callable', 'ZeroDivisionError', 'eval', '__debug__',
  58. 'ReferenceError', 'AssertionError', 'classmethod', 'UnboundLocalError',
  59. 'NotImplementedError', 'lambda', 'AttributeError', 'OverflowError',
  60. 'WindowsError' )
  61. assert_that( syntax_parse._KeywordsFromSyntaxListOutput(
  62. ContentsOfTestFile( 'python_syntax' ) ),
  63. contains_inanyorder( *expected_keywords ) )
  64. def test_KeywordsFromSyntaxListOutput_CppSyntax( self ):
  65. expected_keywords = (
  66. 'int_fast32_t', 'FILE', 'size_t', 'bitor', 'typedef', 'const', 'struct',
  67. 'uint8_t', 'fpos_t', 'thread_local', 'unsigned', 'uint_least16_t', 'do',
  68. 'intptr_t', 'uint_least64_t', 'return', 'auto', 'void', '_Complex',
  69. 'break', '_Alignof', 'not', 'using', '_Static_assert', '_Thread_local',
  70. 'public', 'uint_fast16_t', 'this', 'continue', 'char32_t', 'int16_t',
  71. 'intmax_t', 'static', 'clock_t', 'sizeof', 'int_fast64_t', 'mbstate_t',
  72. 'try', 'xor', 'uint_fast32_t', 'int_least8_t', 'div_t', 'volatile',
  73. 'template', 'char16_t', 'new', 'ldiv_t', 'int_least16_t', 'va_list',
  74. 'uint_least8_t', 'goto', 'noreturn', 'enum', 'static_assert', 'bitand',
  75. 'compl', 'imaginary', 'jmp_buf', 'throw', 'asm', 'ptrdiff_t', 'uint16_t',
  76. 'or', 'uint_fast8_t', '_Bool', 'int32_t', 'float', 'private', 'restrict',
  77. 'wint_t', 'operator', 'not_eq', '_Imaginary', 'alignas', 'union', 'long',
  78. 'uint_least32_t', 'int_least64_t', 'friend', 'uintptr_t', 'int8_t',
  79. 'else', 'export', 'int_fast8_t', 'catch', 'true', 'case', 'default',
  80. 'double', '_Noreturn', 'signed', 'typename', 'while', 'protected',
  81. 'wchar_t', 'wctrans_t', 'uint64_t', 'delete', 'and', 'register', 'false',
  82. 'int', 'uintmax_t', 'off_t', 'char', 'int64_t', 'int_fast16_t', 'DIR',
  83. '_Atomic', 'time_t', 'xor_eq', 'namespace', 'virtual', 'complex', 'bool',
  84. 'mutable', 'if', 'int_least32_t', 'sig_atomic_t', 'and_eq', 'ssize_t',
  85. 'alignof', '_Alignas', '_Generic', 'extern', 'class', 'typeid', 'short',
  86. 'for', 'uint_fast64_t', 'wctype_t', 'explicit', 'or_eq', 'switch',
  87. 'uint32_t', 'inline' )
  88. assert_that( syntax_parse._KeywordsFromSyntaxListOutput(
  89. ContentsOfTestFile( 'cpp_syntax' ) ),
  90. contains_inanyorder( *expected_keywords ) )
  91. def test_KeywordsFromSyntaxListOutput_JavaSyntax( self ):
  92. expected_keywords = (
  93. 'code', 'text', 'cols', 'datetime', 'disabled', 'shape', 'codetype',
  94. 'alt', 'compact', 'style', 'valuetype', 'short', 'finally', 'continue',
  95. 'extends', 'valign', 'bordercolor', 'do', 'return', 'rel', 'rules',
  96. 'void', 'nohref', 'abbr', 'background', 'scrolling', 'instanceof',
  97. 'name', 'summary', 'try', 'default', 'noshade', 'coords', 'dir', 'frame',
  98. 'usemap', 'ismap', 'static', 'hspace', 'vlink', 'for', 'selected', 'rev',
  99. 'vspace', 'content', 'method', 'version', 'volatile', 'above', 'new',
  100. 'charoff', 'public', 'alink', 'enum', 'codebase', 'if', 'noresize',
  101. 'interface', 'checked', 'byte', 'super', 'throw', 'src', 'language',
  102. 'package', 'standby', 'script', 'longdesc', 'maxlength', 'cellpadding',
  103. 'throws', 'tabindex', 'color', 'colspan', 'accesskey', 'float', 'while',
  104. 'private', 'height', 'boolean', 'wrap', 'prompt', 'nowrap', 'size',
  105. 'rows', 'span', 'clip', 'bgcolor', 'top', 'long', 'start', 'scope',
  106. 'scheme', 'type', 'final', 'lang', 'visibility', 'else', 'assert',
  107. 'transient', 'link', 'catch', 'true', 'serializable', 'target', 'lowsrc',
  108. 'this', 'double', 'align', 'value', 'cite', 'headers', 'below',
  109. 'protected', 'declare', 'classid', 'defer', 'false', 'synchronized',
  110. 'int', 'abstract', 'accept', 'hreflang', 'char', 'border', 'id',
  111. 'native', 'rowspan', 'charset', 'archive', 'strictfp', 'readonly',
  112. 'axis', 'cellspacing', 'profile', 'multiple', 'object', 'action',
  113. 'pagex', 'pagey', 'marginheight', 'data', 'class', 'frameborder',
  114. 'enctype', 'implements', 'break', 'gutter', 'url', 'clear', 'face',
  115. 'switch', 'marginwidth', 'width', 'left' )
  116. assert_that( syntax_parse._KeywordsFromSyntaxListOutput(
  117. ContentsOfTestFile( 'java_syntax' ) ),
  118. contains_inanyorder( *expected_keywords ) )
  119. def test_KeywordsFromSyntaxListOutput_PhpSyntax_ContainsFunctions( self ):
  120. assert_that( syntax_parse._KeywordsFromSyntaxListOutput(
  121. ContentsOfTestFile( 'php_syntax' ) ),
  122. has_items( 'array_change_key_case' ) )
  123. def test_KeywordsFromSyntaxListOutput_PhpSyntax_ContainsPreProc( self ):
  124. assert_that( syntax_parse._KeywordsFromSyntaxListOutput(
  125. ContentsOfTestFile( 'php_syntax' ) ),
  126. has_items( 'skip', 'function' ) )
  127. def test_KeywordsFromSyntaxListOutput_Basic( self ):
  128. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  129. foogroup xxx foo bar
  130. zoo goo
  131. links to Statement""" ),
  132. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  133. def test_KeywordsFromSyntaxListOutput_Function( self ):
  134. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  135. foogroup xxx foo bar
  136. zoo goo
  137. links to Function""" ),
  138. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  139. def test_KeywordsFromSyntaxListOutput_ContainedArgAllowed( self ):
  140. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  141. 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
  142. contained replace_child has_attributes specified insertdocument assign node_name hwstat addshape get_attribute_node html_dump_mem userlist
  143. links to Function""" ), # noqa
  144. has_items( 'gzclose', 'userlist', 'ldap_search' ) )
  145. def test_KeywordsFromSyntaxListOutput_JunkIgnored( self ):
  146. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  147. --- Syntax items ---
  148. foogroup xxx foo bar
  149. zoo goo
  150. links to Statement
  151. Spell cluster=NONE
  152. NoSpell cluster=NONE""" ),
  153. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  154. def test_KeywordsFromSyntaxListOutput_MultipleStatementGroups( self ):
  155. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  156. foogroup xxx foo bar
  157. links to Statement
  158. bargroup xxx zoo goo
  159. links to Statement""" ),
  160. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  161. def test_KeywordsFromSyntaxListOutput_StatementAndTypeGroups( self ):
  162. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  163. foogroup xxx foo bar
  164. links to Statement
  165. bargroup xxx zoo goo
  166. links to Type""" ),
  167. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  168. def test_KeywordsFromSyntaxListOutput_StatementHierarchy( self ):
  169. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  170. baa xxx foo bar
  171. links to Foo
  172. Foo xxx zoo goo
  173. links to Bar
  174. Bar xxx qux moo
  175. links to Statement""" ),
  176. contains_inanyorder( 'foo', 'bar', 'zoo',
  177. 'goo', 'qux', 'moo' ) )
  178. def test_KeywordsFromSyntaxListOutput_TypeHierarchy( self ):
  179. assert_that( syntax_parse._KeywordsFromSyntaxListOutput( """
  180. baa xxx foo bar
  181. links to Foo
  182. Foo xxx zoo goo
  183. links to Bar
  184. Bar xxx qux moo
  185. links to Type""" ),
  186. contains_inanyorder( 'foo', 'bar', 'zoo',
  187. 'goo', 'qux', 'moo' ) )
  188. def test_KeywordsFromSyntaxListOutput_StatementAndTypeHierarchy( self ):
  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 test_SyntaxGroupsFromOutput_Basic( self ):
  205. assert_that( syntax_parse._SyntaxGroupsFromOutput( """
  206. foogroup xxx foo bar
  207. zoo goo
  208. links to Statement""" ),
  209. has_item( 'foogroup' ) )
  210. def test_ExtractKeywordsFromGroup_Basic( self ):
  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 test_ExtractKeywordsFromGroup_Commas( self ):
  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 test_ExtractKeywordsFromGroup_WithLinksTo( self ):
  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 test_ExtractKeywordsFromGroup_KeywordStarts( self ):
  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',
  240. 'baa', 'zoo', 'goo' ) )
  241. def test_ExtractKeywordsFromGroup_KeywordMiddle( self ):
  242. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  243. syntax_parse.SyntaxGroup( '', [
  244. 'foo contained bar',
  245. 'zoo goo'
  246. ] ) ),
  247. contains_inanyorder( 'foo', 'contained',
  248. 'bar', 'zoo', 'goo' ) )
  249. def test_ExtractKeywordsFromGroup_KeywordAssign( self ):
  250. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  251. syntax_parse.SyntaxGroup( '', [
  252. 'nextgroup=zoo skipwhite foo bar',
  253. 'zoo goo',
  254. ] ) ),
  255. contains_inanyorder( 'foo', 'bar', 'zoo', 'goo' ) )
  256. def test_ExtractKeywordsFromGroup_KeywordAssignAndMiddle( self ):
  257. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  258. syntax_parse.SyntaxGroup( '', [
  259. 'nextgroup=zoo foo skipnl bar',
  260. 'zoo goo',
  261. ] ) ),
  262. contains_inanyorder( 'foo', 'skipnl', 'bar', 'zoo', 'goo' ) )
  263. def test_ExtractKeywordsFromGroup_KeywordWithoutNextgroup( self ):
  264. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  265. syntax_parse.SyntaxGroup( '', [
  266. 'skipempty foo bar',
  267. 'zoo goo',
  268. ] ) ),
  269. contains_inanyorder( 'skipempty', 'foo',
  270. 'bar', 'zoo', 'goo' ) )
  271. def test_ExtractKeywordsFromGroup_ContainedSyntaxArgAllowed( self ):
  272. assert_that( syntax_parse._ExtractKeywordsFromGroup(
  273. syntax_parse.SyntaxGroup( '', [
  274. 'contained foo zoq',
  275. 'contained bar goo',
  276. 'far'
  277. ] ) ),
  278. contains_inanyorder( 'foo', 'zoq', 'bar', 'goo', 'far' ) )