Skip to content

Commit

Permalink
Refactor ChoiceArrayMeta to allow only the same set of choices for al…
Browse files Browse the repository at this point in the history
…l elements
  • Loading branch information
GDYendell committed Jul 11, 2016
1 parent 2695094 commit aed33cc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
16 changes: 8 additions & 8 deletions malcolm/core/choicearraymeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from malcolm.core.serializable import Serializable


@Serializable.register("malcolm:core/ChoiceArrayMeta:1.0", "choices_list")
@Serializable.register("malcolm:core/ChoiceArrayMeta:1.0")
class ChoiceArrayMeta(ScalarMeta):
"""Meta object containing information for a choice array"""

def __init__(self, name, description, choices_list):
def __init__(self, name, description, choices):
super(ChoiceArrayMeta, self).__init__(
name, description, "choices_list")
name, description)

self.choices_list = choices_list # A list of lists of choices
self.choices = choices

def validate(self, value):
"""
Expand All @@ -33,7 +33,7 @@ def validate(self, value):
for i, choice in enumerate(value):
if choice is None:
raise ValueError("Array elements can not be null")
if choice not in self.choices_list[i]:
if choice not in self.choices:
raise ValueError("%s is not a valid value for element %s" %
(choice, i))

Expand All @@ -44,7 +44,7 @@ def to_dict(self):

d = OrderedDict()
d["typeid"] = self.typeid
d["choices_list"] = self.choices_list
d["choices"] = self.choices
d.update(super(ChoiceArrayMeta, self).to_dict())

return d
Expand All @@ -60,8 +60,8 @@ def from_dict(cls, name, d):
"""

description = d['description']
choices_list = d['choices_list']
choice_array_meta = cls(name, description, choices_list)
choices = d['choices']
choice_array_meta = cls(name, description, choices)
choice_array_meta.tags = d['tags']
choice_array_meta.writeable = d['writeable']
choice_array_meta.label = d['label']
Expand Down
13 changes: 6 additions & 7 deletions tests/test_core/test_choicearraymeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,27 @@
import unittest

from malcolm.core.choicearraymeta import ChoiceArrayMeta
from malcolm.core.serializable import Serializable


class TestChoiceArrayMeta(unittest.TestCase):

def setUp(self):
self.meta = ChoiceArrayMeta("test_meta", "test description", [[1, 2, 3], [4, 5, 6]])
self.meta = ChoiceArrayMeta("test_meta", "test description", [1, 2, 3])

def test_init(self):
self.assertEqual("test_meta", self.meta.name)
self.assertEqual("test description", self.meta.description)
self.assertEqual(self.meta.label, "test_meta")
self.assertEqual(self.meta.typeid, "malcolm:core/ChoiceArrayMeta:1.0")
self.assertEqual(self.meta.choices_list, [[1, 2, 3], [4, 5, 6]])
self.assertEqual(self.meta.choices, [1, 2, 3])

def test_validate_none(self):
self.assertIsNone(self.meta.validate(None))

def test_validate(self):
response = self.meta.validate([2, 6])
response = self.meta.validate([2, 3])

self.assertEqual([2, 6], response)
self.assertEqual([2, 3], response)

def test_not_iterable_raises(self):
value = 1
Expand All @@ -45,7 +44,7 @@ def test_invalid_choice_raises(self):
def test_to_dict(self):
expected = OrderedDict()
expected["typeid"] = "malcolm:core/ChoiceArrayMeta:1.0"
expected['choices_list'] = [[1, 2, 3], [4, 5, 6]]
expected['choices'] = [1, 2, 3]
expected["description"] = "test description"
expected["tags"] = []
expected["writeable"] = True
Expand All @@ -55,7 +54,7 @@ def test_to_dict(self):
def test_from_dict(self):
d = OrderedDict()
d["typeid"] = "malcolm:core/ChoiceArrayMeta:1.0"
d['choices_list'] = [[1, 2, 3], [4, 5, 6]]
d['choices'] = [1, 2, 3]
d["description"] = "test array description"
d["tags"] = ["tag"]
d["writeable"] = False
Expand Down

0 comments on commit aed33cc

Please sign in to comment.