mirror of
https://github.com/myhdl/myhdl.git
synced 2025-01-24 21:52:56 +08:00
refactor frequenty used exception kind check pattern
This commit is contained in:
parent
e0b4690f48
commit
af42ad7ecf
@ -40,6 +40,8 @@ import myhdl
|
||||
from myhdl._Cosimulation import Cosimulation, CosimulationError, _error
|
||||
from myhdl._compat import to_bytes, PYPY
|
||||
|
||||
from utils import raises_kind
|
||||
|
||||
exe = "python {0} ".format(os.path.abspath(__file__))
|
||||
|
||||
fromSignames = ['a', 'bb', 'ccc']
|
||||
@ -65,15 +67,13 @@ class TestCosimulation:
|
||||
gc.collect()
|
||||
|
||||
def testWrongExe(self):
|
||||
with pytest.raises(CosimulationError) as excinfo:
|
||||
Cosimulation("bla -x 45")
|
||||
assert excinfo.value.kind == _error.OSError
|
||||
with raises_kind(CosimulationError, _error.OSError):
|
||||
Cosimulation('bla -x 45')
|
||||
|
||||
def testNotUnique(self):
|
||||
cosim1 = Cosimulation(exe + "cosimNotUnique", **allSigs)
|
||||
with pytest.raises(CosimulationError) as excinfo:
|
||||
with raises_kind(CosimulationError, _error.MultipleCosim):
|
||||
Cosimulation(exe + "cosimNotUnique", **allSigs)
|
||||
assert excinfo.value.kind == _error.MultipleCosim
|
||||
|
||||
@staticmethod
|
||||
def cosimNotUnique():
|
||||
@ -151,9 +151,8 @@ class TestCosimulation:
|
||||
os.read(rf, MAXLINE)
|
||||
|
||||
def testTimeZero(self):
|
||||
with pytest.raises(CosimulationError) as excinfo:
|
||||
with raises_kind(CosimulationError, _error.TimeZero):
|
||||
Cosimulation(exe + "cosimTimeZero", **allSigs)
|
||||
assert excinfo.value.kind == _error.TimeZero
|
||||
|
||||
@staticmethod
|
||||
def cosimTimeZero():
|
||||
@ -165,9 +164,8 @@ class TestCosimulation:
|
||||
os.write(wt, to_bytes(buf))
|
||||
|
||||
def testNoComm(self):
|
||||
with pytest.raises(CosimulationError) as excinfo:
|
||||
with raises_kind(CosimulationError, _error.NoCommunication):
|
||||
Cosimulation(exe + "cosimNoComm", **allSigs)
|
||||
assert excinfo.value.kind == _error.NoCommunication
|
||||
|
||||
@staticmethod
|
||||
def cosimNoComm():
|
||||
@ -181,9 +179,8 @@ class TestCosimulation:
|
||||
os.read(rf, MAXLINE)
|
||||
|
||||
def testFromSignalsDupl(self):
|
||||
with pytest.raises(CosimulationError) as excinfo:
|
||||
with raises_kind(CosimulationError, _error.DuplicateSigNames):
|
||||
Cosimulation(exe + "cosimFromSignalsDupl", **allSigs)
|
||||
assert excinfo.value.kind == _error.DuplicateSigNames
|
||||
|
||||
@staticmethod
|
||||
def cosimFromSignalsDupl():
|
||||
@ -196,9 +193,8 @@ class TestCosimulation:
|
||||
os.write(wt, to_bytes(buf))
|
||||
|
||||
def testToSignalsDupl(self):
|
||||
with pytest.raises(CosimulationError) as excinfo:
|
||||
with raises_kind(CosimulationError, _error.DuplicateSigNames):
|
||||
Cosimulation(exe + "cosimToSignalsDupl", **allSigs)
|
||||
assert excinfo.value.kind == _error.DuplicateSigNames
|
||||
|
||||
@staticmethod
|
||||
def cosimToSignalsDupl():
|
||||
|
@ -33,6 +33,8 @@ from myhdl._Simulation import _error
|
||||
|
||||
from myhdl._simulator import _siglist
|
||||
|
||||
from utils import raises_kind
|
||||
|
||||
QUIET=1
|
||||
|
||||
class Shared:
|
||||
@ -41,23 +43,15 @@ class Shared:
|
||||
class SimArgs(TestCase):
|
||||
""" Simulation arguments """
|
||||
def test1(self):
|
||||
try:
|
||||
with raises_kind(SimulationError, _error.ArgType):
|
||||
Simulation(None)
|
||||
except SimulationError as e:
|
||||
assert e.kind == _error.ArgType
|
||||
except:
|
||||
raise AssertionError
|
||||
|
||||
def test2(self):
|
||||
def g():
|
||||
yield delay(10)
|
||||
i = g()
|
||||
try:
|
||||
with raises_kind(SimulationError, _error.DuplicatedArg):
|
||||
Simulation(i, i)
|
||||
except SimulationError as e:
|
||||
assert e.kind == _error.DuplicatedArg
|
||||
except:
|
||||
raise AssertionError
|
||||
|
||||
|
||||
class YieldNone(TestCase):
|
||||
|
@ -38,6 +38,8 @@ from myhdl._Waiter import _inferWaiter, _Waiter
|
||||
from myhdl._Waiter import _SignalWaiter,_SignalTupleWaiter, _DelayWaiter, \
|
||||
_EdgeWaiter, _EdgeTupleWaiter
|
||||
|
||||
from utils import raises_kind
|
||||
|
||||
|
||||
QUIET=1
|
||||
|
||||
@ -51,53 +53,32 @@ class TestAlwaysCompilation:
|
||||
|
||||
def testArgIsFunction(self):
|
||||
h = 5
|
||||
try:
|
||||
with raises_kind(AlwaysError, _error.ArgType):
|
||||
always(delay(3))(h)
|
||||
except AlwaysError as e:
|
||||
assert e.kind == _error.ArgType
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testArgIsNormalFunction(self):
|
||||
try:
|
||||
with raises_kind(AlwaysError, _error.ArgType):
|
||||
@always(delay(3))
|
||||
def h():
|
||||
yield None
|
||||
except AlwaysError as e:
|
||||
assert e.kind == _error.ArgType
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testArgHasNoArgs(self):
|
||||
try:
|
||||
with raises_kind(AlwaysError, _error.NrOfArgs):
|
||||
@always(delay(3))
|
||||
def h(n):
|
||||
return n
|
||||
except AlwaysError as e:
|
||||
assert e.kind == _error.NrOfArgs
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testDecArgType1(self):
|
||||
try:
|
||||
with raises_kind(AlwaysError, _error.DecArgType):
|
||||
@always
|
||||
def h(n):
|
||||
return n
|
||||
except AlwaysError as e:
|
||||
assert e.kind == _error.DecArgType
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testDecArgType2(self):
|
||||
try:
|
||||
with raises_kind(AlwaysError, _error.DecArgType):
|
||||
@always(g)
|
||||
def h(n):
|
||||
return n
|
||||
except AlwaysError as e:
|
||||
assert e.kind == _error.DecArgType
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
|
||||
|
||||
def SignalFunc1(a, b, c, d, r):
|
||||
|
@ -36,6 +36,8 @@ from myhdl._always_comb import always_comb, _AlwaysComb, _error
|
||||
|
||||
from myhdl._Waiter import _Waiter,_SignalWaiter,_SignalTupleWaiter
|
||||
|
||||
from utils import raises_kind
|
||||
|
||||
|
||||
QUIET=1
|
||||
|
||||
@ -49,32 +51,20 @@ class TestAlwaysCombCompilation:
|
||||
|
||||
def testArgIsFunction(self):
|
||||
h = 5
|
||||
try:
|
||||
with raises_kind(AlwaysCombError, _error.ArgType):
|
||||
always_comb(h)
|
||||
except AlwaysCombError as e:
|
||||
assert e.kind == _error.ArgType
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testArgIsNormalFunction(self):
|
||||
def h():
|
||||
yield None
|
||||
try:
|
||||
with raises_kind(AlwaysCombError, _error.ArgType):
|
||||
always_comb(h)
|
||||
except AlwaysCombError as e:
|
||||
assert e.kind == _error.ArgType
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testArgHasNoArgs(self):
|
||||
def h(n):
|
||||
return n
|
||||
try:
|
||||
with raises_kind(AlwaysCombError, _error.NrOfArgs):
|
||||
always_comb(h)
|
||||
except AlwaysCombError as e:
|
||||
assert e.kind == _error.NrOfArgs
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
## def testScope(self):
|
||||
## try:
|
||||
@ -134,24 +124,16 @@ class TestAlwaysCombCompilation:
|
||||
def h():
|
||||
c.next += 1
|
||||
a += 1
|
||||
try:
|
||||
with raises_kind(AlwaysCombError, _error.SignalAsInout % "c"):
|
||||
g = always_comb(h).gen
|
||||
except AlwaysCombError as e:
|
||||
assert e.kind == _error.SignalAsInout % "c"
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testInfer6(self):
|
||||
a, b, c, d = [Signal(0) for i in range(4)]
|
||||
def h():
|
||||
c.next = a
|
||||
x.next = c
|
||||
try:
|
||||
with raises_kind(AlwaysCombError, _error.SignalAsInout % "c"):
|
||||
g = always_comb(h).gen
|
||||
except AlwaysCombError as e:
|
||||
assert e.kind == _error.SignalAsInout % "c"
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testInfer7(self):
|
||||
a, b, c, d = [Signal(0) for i in range(4)]
|
||||
@ -202,12 +184,8 @@ class TestAlwaysCombCompilation:
|
||||
return e
|
||||
c.next = x
|
||||
g = a
|
||||
try:
|
||||
with raises_kind(AlwaysCombError, _error.EmbeddedFunction):
|
||||
g = always_comb(h)
|
||||
except AlwaysCombError as e:
|
||||
assert e.kind == _error.EmbeddedFunction
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
|
||||
class TestAlwaysCombSimulation1:
|
||||
|
@ -6,6 +6,8 @@ from myhdl import Signal, Simulation, instances, now
|
||||
|
||||
from myhdl._always_seq import always_seq, _AlwaysSeq, _error, AlwaysSeqError
|
||||
|
||||
from utils import raises_kind
|
||||
|
||||
|
||||
|
||||
def test_clock():
|
||||
@ -15,11 +17,10 @@ def test_clock():
|
||||
clock = Signal(bool(0))
|
||||
reset = ResetSignal(0, active=0, async=True)
|
||||
|
||||
with raises(AlwaysSeqError) as e:
|
||||
with raises_kind(AlwaysSeqError, _error.EdgeType):
|
||||
@always_seq(clock, reset=reset)
|
||||
def logic1():
|
||||
pass
|
||||
assert e.kind == _error.EdgeType
|
||||
|
||||
# should work with a valid Signal
|
||||
clock = Signal(bool(0))
|
||||
@ -37,11 +38,10 @@ def test_reset():
|
||||
clock = Signal(bool(0))
|
||||
reset = Signal(bool(0))
|
||||
|
||||
with raises(AlwaysSeqError) as e:
|
||||
with raises_kind(AlwaysSeqError, _error.ResetType):
|
||||
@always_seq(clock.posedge, reset=reset)
|
||||
def logic():
|
||||
pass
|
||||
assert e.kind == _error.ResetType
|
||||
|
||||
# should work with a valid Signal
|
||||
reset = ResetSignal(0, active=0, async=True)
|
||||
|
@ -34,6 +34,8 @@ from myhdl import Signal, Simulation, instances, InstanceError, \
|
||||
|
||||
from myhdl._instance import instance, _error
|
||||
|
||||
from utils import raises_kind
|
||||
|
||||
|
||||
|
||||
QUIET=1
|
||||
@ -48,29 +50,17 @@ class TestInstanceCompilation:
|
||||
|
||||
def testArgIsFunction(self):
|
||||
h = 5
|
||||
try:
|
||||
with raises_kind(InstanceError, _error.ArgType):
|
||||
instance(h)
|
||||
except InstanceError as e:
|
||||
assert e.kind == _error.ArgType
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testArgIsGeneratorFunction(self):
|
||||
try:
|
||||
with raises_kind(InstanceError, _error.ArgType):
|
||||
@instance
|
||||
def h():
|
||||
return None
|
||||
except InstanceError as e:
|
||||
assert e.kind == _error.ArgType
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testArgHasNoArgs(self):
|
||||
try:
|
||||
with raises_kind(InstanceError, _error.NrOfArgs):
|
||||
@instance
|
||||
def h(n):
|
||||
yield n
|
||||
except InstanceError as e:
|
||||
assert e.kind == _error.NrOfArgs
|
||||
else:
|
||||
raise AssertionError
|
||||
|
@ -38,6 +38,8 @@ import pytest
|
||||
from myhdl import delay, intbv, Signal, Simulation, _simulator, instance
|
||||
from myhdl._traceSignals import traceSignals, TraceSignalsError, _error
|
||||
|
||||
from utils import raises_kind
|
||||
|
||||
QUIET=1
|
||||
|
||||
def gen(clk):
|
||||
@ -123,30 +125,19 @@ def vcd_dir(tmpdir):
|
||||
class TestTraceSigs:
|
||||
|
||||
def testMultipleTraces(self, vcd_dir):
|
||||
try:
|
||||
with raises_kind(TraceSignalsError, _error.MultipleTraces):
|
||||
dut = top3()
|
||||
except TraceSignalsError as e:
|
||||
assert e.kind == _error.MultipleTraces
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testArgType1(self, vcd_dir):
|
||||
try:
|
||||
with raises_kind(TraceSignalsError, _error.ArgType):
|
||||
dut = traceSignals([1, 2])
|
||||
except TraceSignalsError as e:
|
||||
assert e.kind == _error.ArgType
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testReturnVal(self, vcd_dir):
|
||||
from myhdl import ExtractHierarchyError
|
||||
from myhdl._extractHierarchy import _error
|
||||
try:
|
||||
kind = _error.InconsistentToplevel % (2, "dummy")
|
||||
with raises_kind(ExtractHierarchyError, kind):
|
||||
dut = traceSignals(dummy)
|
||||
except ExtractHierarchyError as e:
|
||||
assert e.kind == _error.InconsistentToplevel % (2, "dummy")
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def testHierarchicalTrace1(self, vcd_dir):
|
||||
p = "%s.vcd" % fun.__name__
|
||||
|
10
myhdl/test/core/utils.py
Normal file
10
myhdl/test/core/utils.py
Normal file
@ -0,0 +1,10 @@
|
||||
from contextlib import contextmanager
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@contextmanager
|
||||
def raises_kind(exc, kind):
|
||||
with pytest.raises(exc) as excinfo:
|
||||
yield
|
||||
assert excinfo.value.kind == kind
|
Loading…
x
Reference in New Issue
Block a user