mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-04-14 06:40:52 +01:00
Merge pull request #421 from setrofim/next
types: make enum levels serializable
This commit is contained in:
commit
b4363e401c
@ -20,7 +20,9 @@ from unittest import TestCase
|
|||||||
from nose.tools import raises, assert_equal, assert_not_equal, assert_in, assert_not_in
|
from nose.tools import raises, assert_equal, assert_not_equal, assert_in, assert_not_in
|
||||||
from nose.tools import assert_true, assert_false
|
from nose.tools import assert_true, assert_false
|
||||||
|
|
||||||
from wa.utils.types import list_or_integer, list_or_bool, caseless_string, arguments, prioritylist
|
from wa.utils.types import (list_or_integer, list_or_bool, caseless_string,
|
||||||
|
arguments, prioritylist, enum, level)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestPriorityList(TestCase):
|
class TestPriorityList(TestCase):
|
||||||
@ -91,3 +93,18 @@ class TestPriorityList(TestCase):
|
|||||||
assert_equal(list(pl), ['a', 'b','y', 'x', 'm', 'n'])
|
assert_equal(list(pl), ['a', 'b','y', 'x', 'm', 'n'])
|
||||||
pl.add_after('z', 'm')
|
pl.add_after('z', 'm')
|
||||||
assert_equal(list(pl), ['a', 'b', 'y', 'x', 'm', 'z', 'n'])
|
assert_equal(list(pl), ['a', 'b', 'y', 'x', 'm', 'z', 'n'])
|
||||||
|
|
||||||
|
|
||||||
|
class TestEnumLevel(TestCase):
|
||||||
|
|
||||||
|
def test_serialize_level(self):
|
||||||
|
l = level('test', 1)
|
||||||
|
s = l.to_pod()
|
||||||
|
l2 = level.from_pod(s)
|
||||||
|
assert_equal(l, l2)
|
||||||
|
|
||||||
|
def test_deserialize_enum(self):
|
||||||
|
e = enum(['one', 'two', 'three'])
|
||||||
|
s = e.one.to_pod()
|
||||||
|
l = e.from_pod(s)
|
||||||
|
assert_equal(l, e.one)
|
||||||
|
@ -488,9 +488,17 @@ class level(object):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_pod(pod):
|
||||||
|
name, value_part = pod.split('(')
|
||||||
|
return level(name, numeric(value_part.rstrip(')')))
|
||||||
|
|
||||||
def __init__(self, name, value):
|
def __init__(self, name, value):
|
||||||
self.name = caseless_string(name)
|
self.name = caseless_string(name)
|
||||||
self.value = value
|
self.value = numeric(value)
|
||||||
|
|
||||||
|
def to_pod(self):
|
||||||
|
return repr(self)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
@ -545,6 +553,15 @@ def enum(args, start=0, step=1):
|
|||||||
|
|
||||||
class Enum(object):
|
class Enum(object):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_pod(cls, pod):
|
||||||
|
lv = level.from_pod(pod)
|
||||||
|
for enum_level in cls.values:
|
||||||
|
if enum_level == lv:
|
||||||
|
return enum_level
|
||||||
|
msg = 'Unexpected value "{}" for enum.'
|
||||||
|
raise ValueError(msg.format(pod))
|
||||||
|
|
||||||
def __new__(cls, name):
|
def __new__(cls, name):
|
||||||
for attr_name in dir(cls):
|
for attr_name in dir(cls):
|
||||||
if attr_name.startswith('__'):
|
if attr_name.startswith('__'):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user