PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/languages/test/test_languages.py

https://gitlab.com/Smileyt/KomodoEdit
Python | 275 lines | 262 code | 6 blank | 7 comment | 11 complexity | d9794940e6cfbded472180d36a98429d MD5 | raw file
  1. # Copyright (c) 2012-2013 ActiveState Software Inc.
  2. # See the file LICENSE.txt for licensing information.
  3. import unittest
  4. from xpcom import components
  5. from xpcom.server import UnwrapObject
  6. from testlib import tag
  7. class koLanguageSvcTestCase(unittest.TestCase):
  8. """Base class for koIDocument test cases."""
  9. _langRegistrySvc = None
  10. _langRegistrySvcUnwrapped = None
  11. @property
  12. def langRegistrySvc(self):
  13. if self._langRegistrySvc is None:
  14. self._langRegistrySvc = components.classes['@activestate.com/koLanguageRegistryService;1'].\
  15. getService(components.interfaces.koILanguageRegistryService)
  16. return self._langRegistrySvc
  17. @property
  18. def langRegistrySvcUnwrapped(self):
  19. if self._langRegistrySvcUnwrapped is None:
  20. langRegSvc = self.langRegistrySvc
  21. self._langRegistrySvcUnwrapped = UnwrapObject(langRegSvc)
  22. return self._langRegistrySvcUnwrapped
  23. #######################################
  24. ## Language detection from content ##
  25. #######################################
  26. def _assertPossibleLanguagesAre(self, head, tail, expected):
  27. actual = self.langRegistrySvcUnwrapped.guessLanguageFromContents(head, tail)
  28. if actual != expected:
  29. errmsg = """guessed possible languages do not match expected results:
  30. Expected: %s
  31. Got: %s
  32. ---------------- head of document --------------------------------
  33. %s
  34. ---------------- tail of document --------------------------------
  35. %s
  36. ------------------------------------------------------------------
  37. """ % (expected, actual, head, tail)
  38. self.fail(errmsg)
  39. def _assertEmacsLocalVarsAre(self, head, tail, expected):
  40. actual = self.langRegistrySvcUnwrapped._getEmacsLocalVariables(head, tail)
  41. if actual != expected:
  42. errmsg = """Emacs-style local variable results are not as expected:
  43. Expected: %s
  44. Got: %s
  45. ---------------- head of document --------------------------------
  46. %s
  47. ---------------- tail of document --------------------------------
  48. %s
  49. ------------------------------------------------------------------
  50. """ % (expected, actual, head, tail)
  51. self.fail(errmsg)
  52. def test_perl_shebang(self):
  53. perlHeads = [
  54. "#!perl",
  55. "#!perl5 -w",
  56. "#!/bin/perl",
  57. "#! /bin/perl -dwh ",
  58. "#!/bin/PeRl",
  59. "#!/bin/myperl",
  60. ]
  61. for head in perlHeads:
  62. self._assertPossibleLanguagesAre(head, "", ["Perl"])
  63. notPerlHeads = [
  64. "foo",
  65. "#!/bin/erl",
  66. ]
  67. for head in notPerlHeads:
  68. self._assertPossibleLanguagesAre(head, "", [])
  69. def test_python_shebang(self):
  70. pythonHeads = [
  71. "#!python",
  72. "#!python22 -w",
  73. "#!/bin/python",
  74. "#!/opt/Python-2.6.5/bin/python",
  75. "#! /bin/python -dwh ",
  76. "#!/bin/PyThOn",
  77. "#!/usr/bin/env python",
  78. "#!/bin/mypython",
  79. ]
  80. for head in pythonHeads:
  81. self._assertPossibleLanguagesAre(head, "", ["Python"])
  82. notPythonHeads = [
  83. "foo",
  84. "#!/bin/ython",
  85. ]
  86. for head in notPythonHeads:
  87. self._assertPossibleLanguagesAre(head, "", [])
  88. def test_sh_shebang(self):
  89. heads = [
  90. "#!/bin/sh",
  91. "#!/bin/bash",
  92. "#!/usr/bin/bash",
  93. ]
  94. for head in heads:
  95. self._assertPossibleLanguagesAre(head, "", ["Bash"])
  96. def test_tcl_shebang(self):
  97. tclHeads = [
  98. "#!tclsh",
  99. "#!tclsh82",
  100. "#!/bin/tclsh",
  101. "#!/bin/expect",
  102. "#! /bin/wish -v ",
  103. "#!/bin/TcLSh",
  104. "#!/bin/mytclsh",
  105. ]
  106. for head in tclHeads:
  107. self._assertPossibleLanguagesAre(head, "", ["Tcl"])
  108. notTclHeads = [
  109. "foo",
  110. "#!/bin/clsh",
  111. ]
  112. for head in notTclHeads:
  113. self._assertPossibleLanguagesAre(head, "", [])
  114. @tag("knownfailure", "bug 28775")
  115. def test_sh_to_tcl_exec(self):
  116. # File is executed as shell script, but then re-exec'd as a Tcl script.
  117. tclHeads = [
  118. """\
  119. #!/bin/sh
  120. # the next line restarts using tclsh \\
  121. exec tclsh "$0" "$@"
  122. """,
  123. """\
  124. #!/bin/sh
  125. # the next line restarts using tclsh \\
  126. exec wish "$0" "$@"
  127. """,
  128. """\
  129. #!/bin/sh
  130. # the next line restarts using tclsh \\
  131. exec expect "$0" "$@"
  132. """,
  133. ]
  134. for head in heads:
  135. self._assertPossibleLanguagesAre(head, "", ["Bash"])
  136. def test_emacs_local_variables(self):
  137. # Ensure the pref to use emacs-style local variables is on.
  138. globalPrefs = components.classes["@activestate.com/koPrefService;1"].\
  139. getService(components.interfaces.koIPrefService).prefs
  140. emacsLocalModeVariableDetection = globalPrefs.getBooleanPref("emacsLocalModeVariableDetection")
  141. globalPrefs.setBooleanPref("emacsLocalModeVariableDetection", 1)
  142. headsAndTailsAndVarsAndLangs = [
  143. ("# -*- mode: Tcl -*-", "", {"mode": "Tcl"}, ["Tcl"]),
  144. ("# -*- Mode: Tcl -*-", "", {"mode": "Tcl"}, ["Tcl"]),
  145. ("# -*- mode: tcl -*-", "", {"mode": "tcl"}, ["Tcl"]),
  146. ("# *- mode: Tcl -*- blah blah", "", {}, []),
  147. ("", """\
  148. # Using a prefix and suffix.
  149. PREFIX Local Variables: SUFFIX
  150. PREFIX mode: Tcl SUFFIX
  151. PREFIX End: SUFFIX
  152. """, {"mode": "Tcl"}, ["Tcl"]),
  153. ("", """\
  154. Local Variables:
  155. mode: Tcl
  156. End:
  157. """, {"mode": "Tcl"}, ["Tcl"]),
  158. ("", """\
  159. # Using a realistic prefix.
  160. # Local Variables:
  161. # mode: Tcl
  162. # End:
  163. """, {"mode": "Tcl"}, ["Tcl"]),
  164. ("", """\
  165. # Make sure the "End:" in a variable value does not screw up parsing.
  166. PREFIX Local variables: SUFFIX
  167. PREFIX foo: End: SUFFIX
  168. PREFIX tab-width: 4 SUFFIX
  169. PREFIX End: SUFFIX
  170. """, {"foo": "End:", "tab-width": "4"}, []),
  171. ("", """\
  172. # Must use proper prefix.
  173. PREFIX Local Variables: SUFFIX
  174. mode: Tcl SUFFIX
  175. PREFIX End: SUFFIX
  176. """, ValueError, []),
  177. ("", """\
  178. # Must use proper suffix.
  179. PREFIX Local Variables: SUFFIX
  180. PREFIX mode: Tcl
  181. PREFIX End: SUFFIX
  182. """, ValueError, []),
  183. ("", """\
  184. # Whitespace after the prefix and before the suffix is allowed.
  185. # The suffix on the "End:" line is not checked.
  186. PREFIX Local Variables: SUFFIX
  187. PREFIX mode: Tcl SUFFIX
  188. PREFIX End:
  189. """, {"mode": "Tcl"}, ["Tcl"]),
  190. ("", """\
  191. # In the Local Variables block variable names are NOT lowercase'd, therefore
  192. # the user must specify the lowercase "mode" to properly set the mode.
  193. PREFIX Local Variables: SUFFIX
  194. PREFIX MoDe: tcl SUFFIX
  195. PREFIX End: SUFFIX
  196. """, {"MoDe": "tcl"}, []),
  197. ("# -*- Mode: perl -*-", """\
  198. # The local variables block beats the one-liner.
  199. Local Variables:
  200. mode: Tcl
  201. End:
  202. """, {"mode": "Tcl"}, ["Tcl"]),
  203. ]
  204. for head, tail, vars, langs in headsAndTailsAndVarsAndLangs:
  205. if isinstance(vars, dict):
  206. self._assertEmacsLocalVarsAre(head, tail, vars)
  207. elif issubclass(vars, Exception):
  208. self.assertRaises(ValueError, self.langRegistrySvcUnwrapped._getEmacsLocalVariables,
  209. head, tail)
  210. else:
  211. raise "Unexpected test case 'vars' type: %s" % type(vars)
  212. self._assertPossibleLanguagesAre(head, tail, langs)
  213. # Restore the pref.
  214. globalPrefs.setBooleanPref("emacsLocalModeVariableDetection",
  215. emacsLocalModeVariableDetection)
  216. def test_bug28775(self):
  217. # The problem here was that if there was a blank line with an
  218. # LF line terminator (i.e. Unix EOLs) before the "Local Variables"
  219. # line, then the LF would get included in the "prefix" and
  220. # subsequent lines would look like they didn't have the proper
  221. # prefix.
  222. tail = """
  223. # ;;; Local Variables: ***
  224. # ;;; mode: tcl ***
  225. # ;;; End: ***
  226. """
  227. self._assertEmacsLocalVarsAre("", tail, {"mode": "tcl"})
  228. ########################################
  229. ## Language detection from filename ##
  230. ########################################
  231. def _check_filepath_language(self, filepath, expected_lang):
  232. found_lang = self.langRegistrySvc.suggestLanguageForFile(filepath)
  233. self.assertEqual(found_lang, expected_lang,
  234. "Incorrect language given for path %r, got %r, expected %r"
  235. % (filepath, found_lang, expected_lang))
  236. def test_html_recognition(self):
  237. self._check_filepath_language("foo.html", "HTML")
  238. def test_django_recognition(self):
  239. self._check_filepath_language("foo.django.html", "Django")
  240. def test_django_recognition(self):
  241. self._check_filepath_language("foo.django.html", "Django")
  242. #def test_template_toolkit_recognition(self):
  243. # self._check_filepath_language("ttk-test01.ttkt.html", "Template Toolkit")
  244. def test_erb_recognition(self):
  245. self._check_filepath_language("foo.html.erb", "RHTML")
  246. def test_scss_recognition(self):
  247. self._check_filepath_language("foo.css.scss", "SCSS")