-from collections import OrderedDict
+from collections import OrderedDict, Iterable
from contextlib import contextmanager
from .ast import *
def __init__(self, builder):
object.__setattr__(self, "_builder", builder)
- def __iadd__(self, submodules):
- for submodule in submodules:
- self._builder._add_submodule(submodule)
+ def __iadd__(self, modules):
+ if isinstance(modules, Iterable):
+ for module in modules:
+ self._builder._add_submodule(module)
+ else:
+ module = modules
+ self._builder._add_submodule(module)
return self
def __setattr__(self, name, submodule):
def _add_submodule(self, submodule, name=None):
if not hasattr(submodule, "get_fragment"):
- raise TypeError("Trying to add {!r}, which does not implement .get_fragment(), as "
+ raise TypeError("Trying to add '{!r}', which does not implement .get_fragment(), as "
"a submodule".format(submodule))
self._submodules.append((submodule, name))
(eq (sig c2) (const 1'd1))
)
""")
+
+ def test_submodule_anon(self):
+ m1 = Module()
+ m2 = Module()
+ m1.submodules += m2
+ self.assertEqual(m1._submodules, [(m2, None)])
+
+ def test_submodule_anon_multi(self):
+ m1 = Module()
+ m2 = Module()
+ m3 = Module()
+ m1.submodules += m2, m3
+ self.assertEqual(m1._submodules, [(m2, None), (m3, None)])
+
+ def test_submodule_named(self):
+ m1 = Module()
+ m2 = Module()
+ m1.submodules.foo = m2
+ self.assertEqual(m1._submodules, [(m2, "foo")])
+
+ def test_submodule_wrong(self):
+ m = Module()
+ with self.assertRaises(TypeError,
+ msg="Trying to add '1', which does not implement .get_fragment(), as a submodule"):
+ m.submodules.foo = 1
+ with self.assertRaises(TypeError,
+ msg="Trying to add '1', which does not implement .get_fragment(), as a submodule"):
+ m.submodules += 1