syntax_parse_test.py 13 KB

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