Commit 773d8fb2 authored by Kirill Smelkov's avatar Kirill Smelkov

*_test: Verify panic argument

In tests in places where the code checks that something panics, verify
not only that _PanicError is raised, but also what was the argument
passed to panic.

This makes sure that tested code panics in expected place, not just
panics "somewhere".

To keep signal/noise ratio high introduce `panics` which is similar to
`pytest.raises` and asserts that wrapped code panics with expected
argument.
parent f858ed70
...@@ -94,8 +94,8 @@ def test_chan(): ...@@ -94,8 +94,8 @@ def test_chan():
assert ch.recv() == None assert ch.recv() == None
assert ch.recv_() == (None, False) assert ch.recv_() == (None, False)
assert ch.recv_() == (None, False) assert ch.recv_() == (None, False)
with raises(_PanicError): ch.send(0) with panics("send on closed channel"): ch.send(0)
with raises(_PanicError): ch.close() with panics("close of closed channel"): ch.close()
# sync: send vs recv # sync: send vs recv
ch = chan() ch = chan()
...@@ -115,7 +115,7 @@ def test_chan(): ...@@ -115,7 +115,7 @@ def test_chan():
waitBlocked(ch.send) waitBlocked(ch.send)
ch.close() ch.close()
go(_) go(_)
with raises(_PanicError): ch.send(0) with panics("send on closed channel"): ch.send(0)
# close vs recv # close vs recv
ch = chan() ch = chan()
...@@ -527,7 +527,7 @@ def _test_blockforever(): ...@@ -527,7 +527,7 @@ def _test_blockforever():
z = nilchan z = nilchan
with raises(BlocksForever): z.send(0) with raises(BlocksForever): z.send(0)
with raises(BlocksForever): z.recv() with raises(BlocksForever): z.recv()
with raises(_PanicError): z.close() # to fully cover nilchan ops with panics("close of nil channel"): z.close() # to fully cover nilchan ops
# select{} & nil-channel only # select{} & nil-channel only
with raises(BlocksForever): select() with raises(BlocksForever): select()
...@@ -644,10 +644,8 @@ def test_deferrecover(): ...@@ -644,10 +644,8 @@ def test_deferrecover():
def nofunc(): def nofunc():
defer(lambda: v.append('xx')) defer(lambda: v.append('xx'))
with raises(_PanicError) as exc: with panics("function nofunc uses defer, but not @func"):
nofunc() nofunc()
assert exc.value.args == ("function nofunc uses defer, but not @func",)
# panic in deferred call - all defers are called # panic in deferred call - all defers are called
v = [] v = []
...@@ -658,7 +656,7 @@ def test_deferrecover(): ...@@ -658,7 +656,7 @@ def test_deferrecover():
defer(lambda: panic(3)) defer(lambda: panic(3))
defer(lambda: v.append(4)) defer(lambda: v.append(4))
with raises(_PanicError): _() with panics(3): _()
assert v == [4, 2, 1] assert v == [4, 2, 1]
...@@ -695,7 +693,7 @@ def test_deferrecover(): ...@@ -695,7 +693,7 @@ def test_deferrecover():
panic("bbb") panic("bbb")
with raises(_PanicError): _() with panics(2): _()
assert v == [3, 'recovered 1', 1] assert v == [3, 'recovered 1', 1]
...@@ -746,7 +744,7 @@ def test_deferrecover(): ...@@ -746,7 +744,7 @@ def test_deferrecover():
panic("zzz") panic("zzz")
with raises(_PanicError): _() with panics("zzz"): _()
assert v == ['not recovered'] assert v == ['not recovered']
...@@ -835,3 +833,43 @@ def bench_defer(b): ...@@ -835,3 +833,43 @@ def bench_defer(b):
for i in xrange(b.N): for i in xrange(b.N):
_() _()
# ---- misc ----
# panics is similar to pytest.raises and asserts that wrapped code panics with arg.
class panics:
def __init__(self, arg):
self.arg = arg
def __enter__(self):
self.raises = raises(_PanicError)
self.exc_info = self.raises.__enter__()
def __exit__(self, exc_type, exc_val, exc_tb):
ok = self.raises.__exit__(exc_type, exc_val, exc_tb)
if not ok:
return ok
# _PanicError raised - let's check panic argument
assert self.exc_info.value.args == (self.arg,)
return ok
def test_panics():
# no panic -> "did not raise"
with raises(raises.Exception, match="DID NOT RAISE"):
with panics(""):
pass
# raise different type -> exception propagates
with raises(RuntimeError, match="hello world"):
with panics(""):
raise RuntimeError("hello world")
# panic with different argument
with raises(AssertionError, match=r"assert \('bbb',\) == \('aaa',\)"):
with panics("aaa"):
panic("bbb")
# panic with expected argument
with panics(123):
panic(123)
...@@ -20,10 +20,11 @@ ...@@ -20,10 +20,11 @@
from __future__ import print_function, absolute_import from __future__ import print_function, absolute_import
from golang import go, chan, _PanicError from golang import go, chan
from golang import sync, context from golang import sync, context
import time, threading import time, threading
from pytest import raises from pytest import raises
from golang.golang_test import panics
from six.moves import range as xrange from six.moves import range as xrange
import six import six
...@@ -82,7 +83,7 @@ def test_waitgroup(): ...@@ -82,7 +83,7 @@ def test_waitgroup():
wg.done() wg.done()
assert ch.recv() == 'a' assert ch.recv() == 'a'
with raises(_PanicError): with panics("sync: negative WaitGroup counter"):
wg.done() wg.done()
......
...@@ -20,9 +20,9 @@ ...@@ -20,9 +20,9 @@
from __future__ import print_function, absolute_import from __future__ import print_function, absolute_import
from golang import select, _PanicError from golang import select
from golang import time from golang import time
from pytest import raises from golang.golang_test import panics
# all timer tests operate in dt units # all timer tests operate in dt units
dt = 10*time.millisecond dt = 10*time.millisecond
...@@ -162,5 +162,5 @@ def test_timer_stop_drain(): ...@@ -162,5 +162,5 @@ def test_timer_stop_drain():
def test_timer_reset_armed(): def test_timer_reset_armed():
# reset while armed # reset while armed
t = time.Timer(10*dt) t = time.Timer(10*dt)
with raises(_PanicError): with panics("Timer.reset: the timer is armed; must be stopped or expired"):
t.reset(5*dt) t.reset(5*dt)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment