1
0
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:
Keerthan Jaic 2015-06-27 04:58:57 -04:00
parent e0b4690f48
commit af42ad7ecf
8 changed files with 53 additions and 113 deletions

View File

@ -40,6 +40,8 @@ import myhdl
from myhdl._Cosimulation import Cosimulation, CosimulationError, _error from myhdl._Cosimulation import Cosimulation, CosimulationError, _error
from myhdl._compat import to_bytes, PYPY from myhdl._compat import to_bytes, PYPY
from utils import raises_kind
exe = "python {0} ".format(os.path.abspath(__file__)) exe = "python {0} ".format(os.path.abspath(__file__))
fromSignames = ['a', 'bb', 'ccc'] fromSignames = ['a', 'bb', 'ccc']
@ -65,15 +67,13 @@ class TestCosimulation:
gc.collect() gc.collect()
def testWrongExe(self): def testWrongExe(self):
with pytest.raises(CosimulationError) as excinfo: with raises_kind(CosimulationError, _error.OSError):
Cosimulation("bla -x 45") Cosimulation('bla -x 45')
assert excinfo.value.kind == _error.OSError
def testNotUnique(self): def testNotUnique(self):
cosim1 = Cosimulation(exe + "cosimNotUnique", **allSigs) cosim1 = Cosimulation(exe + "cosimNotUnique", **allSigs)
with pytest.raises(CosimulationError) as excinfo: with raises_kind(CosimulationError, _error.MultipleCosim):
Cosimulation(exe + "cosimNotUnique", **allSigs) Cosimulation(exe + "cosimNotUnique", **allSigs)
assert excinfo.value.kind == _error.MultipleCosim
@staticmethod @staticmethod
def cosimNotUnique(): def cosimNotUnique():
@ -151,9 +151,8 @@ class TestCosimulation:
os.read(rf, MAXLINE) os.read(rf, MAXLINE)
def testTimeZero(self): def testTimeZero(self):
with pytest.raises(CosimulationError) as excinfo: with raises_kind(CosimulationError, _error.TimeZero):
Cosimulation(exe + "cosimTimeZero", **allSigs) Cosimulation(exe + "cosimTimeZero", **allSigs)
assert excinfo.value.kind == _error.TimeZero
@staticmethod @staticmethod
def cosimTimeZero(): def cosimTimeZero():
@ -165,9 +164,8 @@ class TestCosimulation:
os.write(wt, to_bytes(buf)) os.write(wt, to_bytes(buf))
def testNoComm(self): def testNoComm(self):
with pytest.raises(CosimulationError) as excinfo: with raises_kind(CosimulationError, _error.NoCommunication):
Cosimulation(exe + "cosimNoComm", **allSigs) Cosimulation(exe + "cosimNoComm", **allSigs)
assert excinfo.value.kind == _error.NoCommunication
@staticmethod @staticmethod
def cosimNoComm(): def cosimNoComm():
@ -181,9 +179,8 @@ class TestCosimulation:
os.read(rf, MAXLINE) os.read(rf, MAXLINE)
def testFromSignalsDupl(self): def testFromSignalsDupl(self):
with pytest.raises(CosimulationError) as excinfo: with raises_kind(CosimulationError, _error.DuplicateSigNames):
Cosimulation(exe + "cosimFromSignalsDupl", **allSigs) Cosimulation(exe + "cosimFromSignalsDupl", **allSigs)
assert excinfo.value.kind == _error.DuplicateSigNames
@staticmethod @staticmethod
def cosimFromSignalsDupl(): def cosimFromSignalsDupl():
@ -196,9 +193,8 @@ class TestCosimulation:
os.write(wt, to_bytes(buf)) os.write(wt, to_bytes(buf))
def testToSignalsDupl(self): def testToSignalsDupl(self):
with pytest.raises(CosimulationError) as excinfo: with raises_kind(CosimulationError, _error.DuplicateSigNames):
Cosimulation(exe + "cosimToSignalsDupl", **allSigs) Cosimulation(exe + "cosimToSignalsDupl", **allSigs)
assert excinfo.value.kind == _error.DuplicateSigNames
@staticmethod @staticmethod
def cosimToSignalsDupl(): def cosimToSignalsDupl():

View File

@ -33,6 +33,8 @@ from myhdl._Simulation import _error
from myhdl._simulator import _siglist from myhdl._simulator import _siglist
from utils import raises_kind
QUIET=1 QUIET=1
class Shared: class Shared:
@ -41,23 +43,15 @@ class Shared:
class SimArgs(TestCase): class SimArgs(TestCase):
""" Simulation arguments """ """ Simulation arguments """
def test1(self): def test1(self):
try: with raises_kind(SimulationError, _error.ArgType):
Simulation(None) Simulation(None)
except SimulationError as e:
assert e.kind == _error.ArgType
except:
raise AssertionError
def test2(self): def test2(self):
def g(): def g():
yield delay(10) yield delay(10)
i = g() i = g()
try: with raises_kind(SimulationError, _error.DuplicatedArg):
Simulation(i, i) Simulation(i, i)
except SimulationError as e:
assert e.kind == _error.DuplicatedArg
except:
raise AssertionError
class YieldNone(TestCase): class YieldNone(TestCase):

View File

@ -38,6 +38,8 @@ from myhdl._Waiter import _inferWaiter, _Waiter
from myhdl._Waiter import _SignalWaiter,_SignalTupleWaiter, _DelayWaiter, \ from myhdl._Waiter import _SignalWaiter,_SignalTupleWaiter, _DelayWaiter, \
_EdgeWaiter, _EdgeTupleWaiter _EdgeWaiter, _EdgeTupleWaiter
from utils import raises_kind
QUIET=1 QUIET=1
@ -51,53 +53,32 @@ class TestAlwaysCompilation:
def testArgIsFunction(self): def testArgIsFunction(self):
h = 5 h = 5
try: with raises_kind(AlwaysError, _error.ArgType):
always(delay(3))(h) always(delay(3))(h)
except AlwaysError as e:
assert e.kind == _error.ArgType
else:
raise AssertionError
def testArgIsNormalFunction(self): def testArgIsNormalFunction(self):
try: with raises_kind(AlwaysError, _error.ArgType):
@always(delay(3)) @always(delay(3))
def h(): def h():
yield None yield None
except AlwaysError as e:
assert e.kind == _error.ArgType
else:
raise AssertionError
def testArgHasNoArgs(self): def testArgHasNoArgs(self):
try: with raises_kind(AlwaysError, _error.NrOfArgs):
@always(delay(3)) @always(delay(3))
def h(n): def h(n):
return n return n
except AlwaysError as e:
assert e.kind == _error.NrOfArgs
else:
raise AssertionError
def testDecArgType1(self): def testDecArgType1(self):
try: with raises_kind(AlwaysError, _error.DecArgType):
@always @always
def h(n): def h(n):
return n return n
except AlwaysError as e:
assert e.kind == _error.DecArgType
else:
raise AssertionError
def testDecArgType2(self): def testDecArgType2(self):
try: with raises_kind(AlwaysError, _error.DecArgType):
@always(g) @always(g)
def h(n): def h(n):
return n return n
except AlwaysError as e:
assert e.kind == _error.DecArgType
else:
raise AssertionError
def SignalFunc1(a, b, c, d, r): def SignalFunc1(a, b, c, d, r):

View File

@ -36,6 +36,8 @@ from myhdl._always_comb import always_comb, _AlwaysComb, _error
from myhdl._Waiter import _Waiter,_SignalWaiter,_SignalTupleWaiter from myhdl._Waiter import _Waiter,_SignalWaiter,_SignalTupleWaiter
from utils import raises_kind
QUIET=1 QUIET=1
@ -49,32 +51,20 @@ class TestAlwaysCombCompilation:
def testArgIsFunction(self): def testArgIsFunction(self):
h = 5 h = 5
try: with raises_kind(AlwaysCombError, _error.ArgType):
always_comb(h) always_comb(h)
except AlwaysCombError as e:
assert e.kind == _error.ArgType
else:
raise AssertionError
def testArgIsNormalFunction(self): def testArgIsNormalFunction(self):
def h(): def h():
yield None yield None
try: with raises_kind(AlwaysCombError, _error.ArgType):
always_comb(h) always_comb(h)
except AlwaysCombError as e:
assert e.kind == _error.ArgType
else:
raise AssertionError
def testArgHasNoArgs(self): def testArgHasNoArgs(self):
def h(n): def h(n):
return n return n
try: with raises_kind(AlwaysCombError, _error.NrOfArgs):
always_comb(h) always_comb(h)
except AlwaysCombError as e:
assert e.kind == _error.NrOfArgs
else:
raise AssertionError
## def testScope(self): ## def testScope(self):
## try: ## try:
@ -134,24 +124,16 @@ class TestAlwaysCombCompilation:
def h(): def h():
c.next += 1 c.next += 1
a += 1 a += 1
try: with raises_kind(AlwaysCombError, _error.SignalAsInout % "c"):
g = always_comb(h).gen g = always_comb(h).gen
except AlwaysCombError as e:
assert e.kind == _error.SignalAsInout % "c"
else:
raise AssertionError
def testInfer6(self): def testInfer6(self):
a, b, c, d = [Signal(0) for i in range(4)] a, b, c, d = [Signal(0) for i in range(4)]
def h(): def h():
c.next = a c.next = a
x.next = c x.next = c
try: with raises_kind(AlwaysCombError, _error.SignalAsInout % "c"):
g = always_comb(h).gen g = always_comb(h).gen
except AlwaysCombError as e:
assert e.kind == _error.SignalAsInout % "c"
else:
raise AssertionError
def testInfer7(self): def testInfer7(self):
a, b, c, d = [Signal(0) for i in range(4)] a, b, c, d = [Signal(0) for i in range(4)]
@ -202,12 +184,8 @@ class TestAlwaysCombCompilation:
return e return e
c.next = x c.next = x
g = a g = a
try: with raises_kind(AlwaysCombError, _error.EmbeddedFunction):
g = always_comb(h) g = always_comb(h)
except AlwaysCombError as e:
assert e.kind == _error.EmbeddedFunction
else:
raise AssertionError
class TestAlwaysCombSimulation1: class TestAlwaysCombSimulation1:

View File

@ -6,6 +6,8 @@ from myhdl import Signal, Simulation, instances, now
from myhdl._always_seq import always_seq, _AlwaysSeq, _error, AlwaysSeqError from myhdl._always_seq import always_seq, _AlwaysSeq, _error, AlwaysSeqError
from utils import raises_kind
def test_clock(): def test_clock():
@ -15,11 +17,10 @@ def test_clock():
clock = Signal(bool(0)) clock = Signal(bool(0))
reset = ResetSignal(0, active=0, async=True) reset = ResetSignal(0, active=0, async=True)
with raises(AlwaysSeqError) as e: with raises_kind(AlwaysSeqError, _error.EdgeType):
@always_seq(clock, reset=reset) @always_seq(clock, reset=reset)
def logic1(): def logic1():
pass pass
assert e.kind == _error.EdgeType
# should work with a valid Signal # should work with a valid Signal
clock = Signal(bool(0)) clock = Signal(bool(0))
@ -37,11 +38,10 @@ def test_reset():
clock = Signal(bool(0)) clock = Signal(bool(0))
reset = Signal(bool(0)) reset = Signal(bool(0))
with raises(AlwaysSeqError) as e: with raises_kind(AlwaysSeqError, _error.ResetType):
@always_seq(clock.posedge, reset=reset) @always_seq(clock.posedge, reset=reset)
def logic(): def logic():
pass pass
assert e.kind == _error.ResetType
# should work with a valid Signal # should work with a valid Signal
reset = ResetSignal(0, active=0, async=True) reset = ResetSignal(0, active=0, async=True)

View File

@ -34,6 +34,8 @@ from myhdl import Signal, Simulation, instances, InstanceError, \
from myhdl._instance import instance, _error from myhdl._instance import instance, _error
from utils import raises_kind
QUIET=1 QUIET=1
@ -48,29 +50,17 @@ class TestInstanceCompilation:
def testArgIsFunction(self): def testArgIsFunction(self):
h = 5 h = 5
try: with raises_kind(InstanceError, _error.ArgType):
instance(h) instance(h)
except InstanceError as e:
assert e.kind == _error.ArgType
else:
raise AssertionError
def testArgIsGeneratorFunction(self): def testArgIsGeneratorFunction(self):
try: with raises_kind(InstanceError, _error.ArgType):
@instance @instance
def h(): def h():
return None return None
except InstanceError as e:
assert e.kind == _error.ArgType
else:
raise AssertionError
def testArgHasNoArgs(self): def testArgHasNoArgs(self):
try: with raises_kind(InstanceError, _error.NrOfArgs):
@instance @instance
def h(n): def h(n):
yield n yield n
except InstanceError as e:
assert e.kind == _error.NrOfArgs
else:
raise AssertionError

View File

@ -38,6 +38,8 @@ import pytest
from myhdl import delay, intbv, Signal, Simulation, _simulator, instance from myhdl import delay, intbv, Signal, Simulation, _simulator, instance
from myhdl._traceSignals import traceSignals, TraceSignalsError, _error from myhdl._traceSignals import traceSignals, TraceSignalsError, _error
from utils import raises_kind
QUIET=1 QUIET=1
def gen(clk): def gen(clk):
@ -123,30 +125,19 @@ def vcd_dir(tmpdir):
class TestTraceSigs: class TestTraceSigs:
def testMultipleTraces(self, vcd_dir): def testMultipleTraces(self, vcd_dir):
try: with raises_kind(TraceSignalsError, _error.MultipleTraces):
dut = top3() dut = top3()
except TraceSignalsError as e:
assert e.kind == _error.MultipleTraces
else:
raise AssertionError
def testArgType1(self, vcd_dir): def testArgType1(self, vcd_dir):
try: with raises_kind(TraceSignalsError, _error.ArgType):
dut = traceSignals([1, 2]) dut = traceSignals([1, 2])
except TraceSignalsError as e:
assert e.kind == _error.ArgType
else:
raise AssertionError
def testReturnVal(self, vcd_dir): def testReturnVal(self, vcd_dir):
from myhdl import ExtractHierarchyError from myhdl import ExtractHierarchyError
from myhdl._extractHierarchy import _error from myhdl._extractHierarchy import _error
try: kind = _error.InconsistentToplevel % (2, "dummy")
with raises_kind(ExtractHierarchyError, kind):
dut = traceSignals(dummy) dut = traceSignals(dummy)
except ExtractHierarchyError as e:
assert e.kind == _error.InconsistentToplevel % (2, "dummy")
else:
raise AssertionError
def testHierarchicalTrace1(self, vcd_dir): def testHierarchicalTrace1(self, vcd_dir):
p = "%s.vcd" % fun.__name__ p = "%s.vcd" % fun.__name__

10
myhdl/test/core/utils.py Normal file
View 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