def test_plugin_with_exception_callback(self): plugin = Plugin(exceptions=0) async def on_processed(message, env): if env.exception: plugin.exceptions += 1 plugin.on_after_processed()(on_processed) async def on_has_text(message, env): raise Exception plugin.on_has_text()(on_has_text) app = Kutana() app.register_plugins([plugin]) loop = asyncio.get_event_loop() set_logger_level(logging.CRITICAL) loop.run_until_complete(app.process(DebugManager(), "message")) set_logger_level(logging.ERROR) self.assertEqual(plugin.exceptions, 1)
def test_plugin_no_text(self): plugin = Plugin() async def on_has_text(message, env): return "DONE" plugin.on_has_text()(on_has_text) wrapper = plugin._callbacks[0][1] res = asyncio.get_event_loop().run_until_complete( wrapper(Message("", ("attachment"), 0, 0, 0, {}), DebugEnvironment(None, 0))) self.assertNotEqual(res, "DONE")
def test_environments(self): self.target = ["echo 123"] with self.debug_controller(self.target): plugin1 = Plugin() plugin2 = Plugin() self.plugins = (plugin1, plugin2) async def do_skip(message, attachments, env): env.A = "A" env.eenv.B = "B" return "GOON" async def do_check_one(message, attachments, env): self.assertEqual(env.get("A"), "A") return "GOON" plugin1.on_has_text()(do_skip) plugin1.on_has_text()(do_check_one) async def do_check(message, attachments, env): self.assertIsNone(env.get("A")) self.assertEqual(env.eenv.get("B"), "B") await env.reply("echo 123") plugin2.on_has_text()(do_check)