/Plugins/Ycm/Plugin/third_party/ycmd/third_party/bottle/test/test_plugins.py

https://bitbucket.org/WscriChy/vim-configuration
Python | 213 lines | 176 code | 36 blank | 1 comment | 2 complexity | 5fbd7bad19ef3abed87b553732d13cc8 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. import unittest
  3. import tools
  4. class MyPlugin(object):
  5. def __init__(self):
  6. self.app = None
  7. self.add_args = {}
  8. self.add_content = ''
  9. def setup(self, app):
  10. self.app = app
  11. def apply(self, func, config):
  12. def wrapper(*a, **ka):
  13. ka.update(self.add_args)
  14. self.lastcall = func, a, ka
  15. return ''.join(func(*a, **ka)) + self.add_content
  16. return wrapper
  17. def my_decorator(func):
  18. def wrapper(*a, **ka):
  19. return list(func(*a, **ka))[-1]
  20. class TestPluginManagement(tools.ServerTestBase):
  21. def verify_installed(self, plugin, otype, **config):
  22. self.assertEqual(type(plugin), otype)
  23. self.assertEqual(plugin.config, config)
  24. self.assertEqual(plugin.app, self.app)
  25. self.assertTrue(plugin in self.app.plugins)
  26. def test_install_plugin(self):
  27. plugin = MyPlugin()
  28. installed = self.app.install(plugin)
  29. self.assertEqual(plugin, installed)
  30. self.assertTrue(plugin in self.app.plugins)
  31. def test_install_decorator(self):
  32. installed = self.app.install(my_decorator)
  33. self.assertEqual(my_decorator, installed)
  34. self.assertTrue(my_decorator in self.app.plugins)
  35. def test_install_non_plugin(self):
  36. self.assertRaises(TypeError, self.app.install, 'I am not a plugin')
  37. def test_uninstall_by_instance(self):
  38. plugin = self.app.install(MyPlugin())
  39. plugin2 = self.app.install(MyPlugin())
  40. self.app.uninstall(plugin)
  41. self.assertTrue(plugin not in self.app.plugins)
  42. self.assertTrue(plugin2 in self.app.plugins)
  43. def test_uninstall_by_type(self):
  44. plugin = self.app.install(MyPlugin())
  45. plugin2 = self.app.install(MyPlugin())
  46. self.app.uninstall(MyPlugin)
  47. self.assertTrue(plugin not in self.app.plugins)
  48. self.assertTrue(plugin2 not in self.app.plugins)
  49. def test_uninstall_by_name(self):
  50. plugin = self.app.install(MyPlugin())
  51. plugin2 = self.app.install(MyPlugin())
  52. plugin.name = 'myplugin'
  53. self.app.uninstall('myplugin')
  54. self.assertTrue(plugin not in self.app.plugins)
  55. self.assertTrue(plugin2 in self.app.plugins)
  56. def test_uninstall_all(self):
  57. plugin = self.app.install(MyPlugin())
  58. plugin2 = self.app.install(MyPlugin())
  59. self.app.uninstall(True)
  60. self.assertFalse(self.app.plugins)
  61. def test_route_plugin(self):
  62. plugin = MyPlugin()
  63. plugin.add_content = ';foo'
  64. @self.app.route('/a')
  65. @self.app.route('/b', apply=[plugin])
  66. def a(): return 'plugin'
  67. self.assertBody('plugin', '/a')
  68. self.assertBody('plugin;foo', '/b')
  69. def test_plugin_oder(self):
  70. self.app.install(MyPlugin()).add_content = ';global-1'
  71. self.app.install(MyPlugin()).add_content = ';global-2'
  72. l1 = MyPlugin()
  73. l1.add_content = ';local-1'
  74. l2 = MyPlugin()
  75. l2.add_content = ';local-2'
  76. @self.app.route('/a')
  77. @self.app.route('/b', apply=[l1, l2])
  78. def a(): return 'plugin'
  79. self.assertBody('plugin;global-2;global-1', '/a')
  80. self.assertBody('plugin;local-2;local-1;global-2;global-1', '/b')
  81. def test_skip_by_instance(self):
  82. g1 = self.app.install(MyPlugin())
  83. g1.add_content = ';global-1'
  84. g2 = self.app.install(MyPlugin())
  85. g2.add_content = ';global-2'
  86. l1 = MyPlugin()
  87. l1.add_content = ';local-1'
  88. l2 = MyPlugin()
  89. l2.add_content = ';local-2'
  90. @self.app.route('/a', skip=[g2, l2])
  91. @self.app.route('/b', apply=[l1, l2], skip=[g2, l2])
  92. def a(): return 'plugin'
  93. self.assertBody('plugin;global-1', '/a')
  94. self.assertBody('plugin;local-1;global-1', '/b')
  95. def test_skip_by_class(self):
  96. g1 = self.app.install(MyPlugin())
  97. g1.add_content = ';global-1'
  98. @self.app.route('/a')
  99. @self.app.route('/b', skip=[MyPlugin])
  100. def a(): return 'plugin'
  101. self.assertBody('plugin;global-1', '/a')
  102. self.assertBody('plugin', '/b')
  103. def test_skip_by_name(self):
  104. g1 = self.app.install(MyPlugin())
  105. g1.add_content = ';global-1'
  106. g1.name = 'test'
  107. @self.app.route('/a')
  108. @self.app.route('/b', skip=['test'])
  109. def a(): return 'plugin'
  110. self.assertBody('plugin;global-1', '/a')
  111. self.assertBody('plugin', '/b')
  112. def test_skip_all(self):
  113. g1 = self.app.install(MyPlugin())
  114. g1.add_content = ';global-1'
  115. @self.app.route('/a')
  116. @self.app.route('/b', skip=[True])
  117. def a(): return 'plugin'
  118. self.assertBody('plugin;global-1', '/a')
  119. self.assertBody('plugin', '/b')
  120. def test_skip_nonlist(self):
  121. g1 = self.app.install(MyPlugin())
  122. g1.add_content = ';global-1'
  123. @self.app.route('/a')
  124. @self.app.route('/b', skip=g1)
  125. def a(): return 'plugin'
  126. self.assertBody('plugin;global-1', '/a')
  127. self.assertBody('plugin', '/b')
  128. class TestPluginAPI(tools.ServerTestBase):
  129. def setUp(self):
  130. super(TestPluginAPI, self).setUp()
  131. @self.app.route('/', test='plugin.cfg')
  132. def test(**args):
  133. return ', '.join('%s:%s' % (k,v) for k,v in args.items())
  134. def test_callable(self):
  135. def plugin(func):
  136. def wrapper(*a, **ka):
  137. return func(test='me', *a, **ka) + '; tail'
  138. return wrapper
  139. self.app.install(plugin)
  140. self.assertBody('test:me; tail', '/')
  141. def test_apply(self):
  142. class Plugin(object):
  143. def apply(self, func, cfg):
  144. def wrapper(*a, **ka):
  145. return func(test=cfg['config']['test'], *a, **ka) + '; tail'
  146. return wrapper
  147. def __call__(self, func):
  148. raise AssertionError("Plugins must not be called "\
  149. "if they implement 'apply'")
  150. self.app.install(Plugin())
  151. self.assertBody('test:plugin.cfg; tail', '/')
  152. def test_instance_method_wrapper(self):
  153. class Plugin(object):
  154. api=2
  155. def apply(self, callback, route):
  156. return self.b
  157. def b(self): return "Hello"
  158. self.app.install(Plugin())
  159. self.assertBody('Hello', '/')
  160. def test_setup(self):
  161. class Plugin(object):
  162. def __call__(self, func): return func
  163. def setup(self, app): self.app = app
  164. plugin = self.app.install(Plugin())
  165. self.assertEquals(getattr(plugin, 'app', None), self.app)
  166. def test_close(self):
  167. class Plugin(object):
  168. def __call__(self, func): return func
  169. def close(self): self.closed = True
  170. plugin = self.app.install(Plugin())
  171. plugin2 = self.app.install(Plugin())
  172. self.app.uninstall(plugin)
  173. self.assertTrue(getattr(plugin, 'closed', False))
  174. self.app.close()
  175. self.assertTrue(getattr(plugin2, 'closed', False))
  176. if __name__ == '__main__': #pragma: no cover
  177. unittest.main()