syntax_parse_test.py 13 KB

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