2008-09-10 17:47:37 +08:00
|
|
|
import py
|
2009-02-27 18:18:27 +08:00
|
|
|
from py.__.misc.warn import WarningPlugin
|
2008-09-10 17:47:37 +08:00
|
|
|
mypath = py.magic.autopath()
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class TestWarningPlugin:
|
2008-09-10 17:47:37 +08:00
|
|
|
def setup_method(self, method):
|
2009-02-27 18:18:27 +08:00
|
|
|
self.bus = py._com.PyPlugins()
|
|
|
|
self.wb = WarningPlugin(self.bus)
|
|
|
|
self.bus.register(self)
|
2008-09-10 17:47:37 +08:00
|
|
|
self.warnings = []
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def pyevent_WARNING(self, warning):
|
|
|
|
self.warnings.append(warning)
|
|
|
|
|
|
|
|
def test_event_generation(self):
|
2008-09-10 17:47:37 +08:00
|
|
|
self.wb.warn("hello")
|
|
|
|
assert len(self.warnings) == 1
|
|
|
|
|
|
|
|
def test_location(self):
|
|
|
|
self.wb.warn("again")
|
|
|
|
warning = self.warnings[0]
|
|
|
|
lno = self.test_location.im_func.func_code.co_firstlineno + 1
|
|
|
|
assert warning.lineno == lno
|
|
|
|
assert warning.path == mypath
|
|
|
|
locstr = "%s:%d: " %(mypath, lno+1,)
|
|
|
|
assert repr(warning).startswith(locstr)
|
|
|
|
assert str(warning) == warning.msg
|
|
|
|
|
|
|
|
def test_stacklevel(self):
|
|
|
|
def f():
|
|
|
|
self.wb.warn("x", stacklevel=2)
|
2009-02-27 18:18:27 +08:00
|
|
|
# 3
|
|
|
|
# 4
|
2008-09-10 17:47:37 +08:00
|
|
|
f()
|
2009-02-27 18:18:27 +08:00
|
|
|
lno = self.test_stacklevel.im_func.func_code.co_firstlineno + 5
|
|
|
|
warning = self.warnings[0]
|
2008-09-10 17:47:37 +08:00
|
|
|
assert warning.lineno == lno
|
|
|
|
|
|
|
|
def test_forwarding_to_warnings_module(self):
|
|
|
|
py.test.deprecated_call(self.wb.warn, "x")
|
|
|
|
|
|
|
|
def test_apiwarn(self):
|
|
|
|
self.wb.apiwarn("3.0", "xxx")
|
|
|
|
warning = self.warnings[0]
|
|
|
|
assert warning.msg == "xxx (since version 3.0)"
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_default():
|
2008-09-10 17:47:37 +08:00
|
|
|
from py.__.misc.warn import APIWARN
|
2009-02-27 18:18:27 +08:00
|
|
|
assert py._com.pyplugins.isregistered(APIWARN.im_self)
|