Skip to content

Commit

Permalink
Add some tests and use the label part in PandA
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom authored and Tom committed Jul 28, 2017
1 parent fc4a297 commit c9ae6ed
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
11 changes: 7 additions & 4 deletions malcolm/modules/pandablocks/parts/pandablocksmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from malcolm.compat import OrderedDict
from malcolm.core import call_with_params
from malcolm.modules.builtin.parts import GroupPart, IconPart
from malcolm.modules.builtin.parts import GroupPart, IconPart, LabelPart
from malcolm.tags import widget, group, inport, outport, config
from malcolm.modules.builtin.vmetas import BooleanMeta, NumberMeta, StringMeta, \
ChoiceMeta, TableMeta
Expand Down Expand Up @@ -57,7 +57,7 @@ def __init__(self, client, block_name, block_data):
self.block_data = block_data
self.parts = OrderedDict()
# Make an icon
self._make_icon()
self._make_icon_label()
for field_name, field_data in block_data.fields.items():
self.make_parts_for(field_name, field_data)

Expand Down Expand Up @@ -102,10 +102,13 @@ def make_parts_for(self, field_name, field_data):
else:
raise ValueError("Unknown type %r subtype %r" % (type, subtyp))

def _make_icon(self):
svg_name = self.block_name.rstrip("0123456789") + ".svg"
def _make_icon_label(self):
block_type = self.block_name.rstrip("0123456789")
svg_name = block_type + ".svg"
part = call_with_params(IconPart, svg=os.path.join(SVG_DIR, svg_name))
self._add_part("icon", part)
part = call_with_params(LabelPart, initialValue=block_type)
self._add_part("label", part)

def _make_scale_offset(self, field_name):
group_tag = self._make_group("outputs")
Expand Down
25 changes: 25 additions & 0 deletions tests/test_modules/test_builtin/test_grouppart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest

from malcolm.core import call_with_params
from malcolm.modules.builtin.parts import GroupPart


class TestGroupPart(unittest.TestCase):

def setUp(self):
self.o = call_with_params(
GroupPart, name="things", description="A group of things")
self.setter = list(self.o.create_attribute_models())[0][2]

def test_init(self):
assert self.o.name == "things"
assert self.o.attr.value == "expanded"
assert self.o.attr.meta.description == "A group of things"
assert self.o.attr.meta.tags == ("widget:group", "config")

def test_setter(self):
assert self.o.attr.value == "expanded"
self.setter("collapsed")
assert self.o.attr.value == "collapsed"
with self.assertRaises(ValueError):
self.setter("anything else")
22 changes: 22 additions & 0 deletions tests/test_modules/test_builtin/test_iconpart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import unittest

from malcolm.core import call_with_params
from malcolm.modules.builtin.parts import IconPart


class TestIconPart(unittest.TestCase):

def setUp(self):
svg_name = "/tmp/test_icon.svg"
self.svg_text = '<svg><rect width="300" height="100"/></svg>'
with open(svg_name, "w") as f:
f.write(self.svg_text)
self.o = call_with_params(
IconPart, svg=svg_name)
list(self.o.create_attribute_models())

def test_init(self):
assert self.o.name == "icon"
assert self.o.attr.value == self.svg_text
assert self.o.attr.meta.description == "SVG icon for Block"
assert self.o.attr.meta.tags == ("widget:icon",)
32 changes: 32 additions & 0 deletions tests/test_modules/test_builtin/test_labelpart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest
from mock import MagicMock

from malcolm.core import call_with_params, Controller, Process
from malcolm.modules.builtin.parts import LabelPart


class TestLabelPart(unittest.TestCase):

def setUp(self):
self.o = call_with_params(
LabelPart, initialValue="My label", group="mygroup")
self.p = Process("proc")
self.c = Controller(self.p, "mri", [self.o])
self.p.add_controller("mri", self.c)
self.p.start()
self.b = self.c.block_view()

def tearDown(self):
self.p.stop(1)

def test_init(self):
assert self.o.name == "label"
assert self.o.attr.value == "My label"
assert self.o.attr.meta.tags == (
"widget:textinput", "config", "group:mygroup")
assert self.b.meta.label == "My label"

def test_setter(self):
self.b.label.put_value("My label2")
assert self.b.label.value == "My label2"
assert self.b.meta.label == "My label2"
2 changes: 2 additions & 0 deletions tests/test_modules/test_pandablocks/test_pandablocksmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_block_fields_adder(self):
o = PandABlocksMaker(self.client, "ADDER1", block_data)
assert list(o.parts) == [
'icon',
'label',
'inputs',
'INPA',
'INPA.CURRENT',
Expand Down Expand Up @@ -136,6 +137,7 @@ def test_block_fields_pulse(self):
o = PandABlocksMaker(self.client, "PULSE2", block_data)
assert list(o.parts) == [
'icon',
'label',
'parameters',
'DELAY',
'DELAY.UNITS',
Expand Down

0 comments on commit c9ae6ed

Please sign in to comment.