def test_getExtra_deprecated(self): """ `Enum.getExtra` is deprecated. """ enum = Enum('doc', []) self.assertThat( lambda: enum.getExtra(u'a', u'extra'), is_deprecated(Contains('use Enum.extra')))
def test_as_pairs(self): """ Representing an enumeration as a list of pairs. """ values = enum_values_fixture() enum = Enum('doc', values) pairs = [(e.get('id', e.value), e.desc) for e in values] self.assertThat(enum.as_pairs(), Equals(pairs))
def test_findAll_deprecated(self): """ `Enum.findAll` is deprecated. """ enum = Enum('doc', []) self.assertThat( lambda: enum.findAll(foo=u'a'), is_deprecated(Contains('use Enum.find_all')))
def test_getDesc_deprecated(self): """ `Enum.getDesc` is deprecated. """ enum = Enum('doc', []) self.assertThat( lambda: enum.getDesc(u'a'), is_deprecated(Contains('use Enum.desc')))
def test_asPairs_deprecated(self): """ `Enum.asPairs` is deprecated. """ enum = Enum('doc', []) self.assertThat( lambda: enum.asPairs(), is_deprecated(Contains('use Enum.as_pairs')))
def test_find_invalid_usage(self): """ Passing fewer or more than one query raises `ValueError`. """ enum = Enum('doc', []) self.assertThat(enum.find, raises(ValueError)) self.assertThat( lambda: enum.find(foo=u'a', bar=u'b'), raises(ValueError))
def test_find(self): """ Finding an enumeration item by extra value gets the first matching item or ``None`` if there are no matches. """ values = enum_values_fixture() enum = Enum('doc', values) self.assertThat(enum.find(quux=u'hello'), Is(values[0])) self.assertThat(enum.find(frob=u'world'), Is(values[0])) self.assertThat(enum.find(quux=u'goodbye'), Is(values[1])) self.assertThat(enum.find(haha=u'nothanks'), Is(None))
def test_hidden(self): """ Enumeration items that have their ``hidden`` flag set are not listed in the result of `Enum.as_pairs`. """ values = [ EnumItem(u'foo', u'Foo', hidden=True), EnumItem(u'bar', u'Bar'), EnumItem(u'pop', u'Pop')] enum = Enum('doc', values) enum.get(u'pop').hidden = True pairs = enum.as_pairs() self.assertThat(pairs, Equals([(u'bar', u'Bar')]))
def test_get(self): """ Getting an enumeration item by value returns the relevant `EnumItem` instance or raises `InvalidEnumItem` in the case where no item is represented by the given value. """ values = enum_values_fixture() enum = Enum('doc', values) self.assertThat( values, Equals([enum.get(e.value) for e in values])) self.assertThat( lambda: enum.get(u'DOES_NOT_EXIST'), raises(InvalidEnumItem))
def test_desc(self): """ Getting an enumeration item's description by value returns the description or an empty `unicode` string if no item is represented by the given value. """ values = [ EnumItem(u'foo', u'Foo', hidden=True), EnumItem(u'bar', u'Bar')] enum = Enum('doc', values) self.assertThat( [e.desc for e in enum], Equals([enum.desc(e.value) for e in enum])) self.assertThat(enum.desc(u'DOES_NOT_EXIST'), Equals(u''))
def test_find_all(self): """ Finding all enumeration items by extra value gets an iterable of all matching items. Passing fewer or more than one query raises `ValueError`. """ values = enum_values_fixture() enum = Enum('doc', values) results = list(enum.find_all(frob=u'world')) self.assertThat( results, Equals([values[0], values[2]])) self.assertThat( enum.find_all(asdf=u'qwer'), After(list, Equals([])))
def test_fromPairs_deprecated(self): """ `Enum.fromPairs` is deprecated. """ self.assertThat( lambda: Enum.fromPairs('doc', []), is_deprecated(Contains('use Enum.from_pairs')))
def test_from_pairs(self): """ Construct an enumeration from an iterable of pairs. """ pairs = [ (u'foo', u'Foo'), (u'bar', u'Bar')] enum = Enum.from_pairs('doc', pairs) self.assertThat(enum.doc, Equals('doc')) self.assertThat(enum.as_pairs(), Equals(pairs))
def test_duplicate_values(self): """ Constructing an enumeration with duplicate values results in `ValueError` being raised. """ values = [ EnumItem(u'foo', u'Foo'), EnumItem(u'bar', u'Bar'), EnumItem(u'foo', u'Not Foo', quux=u'frob')] self.assertThat( lambda: Enum('doc', values), raises(ValueError)) pairs = [(e.value, e.desc) for e in values] self.assertThat( lambda: Enum.from_pairs('doc', pairs), raises(ValueError))
def ListEnumeration(theList): """ An L{IEnumeration} adapter for the C{list} type. This is deprecated, use L{Enum.from_pairs} instead. """ # If this isn't a grouped input, turn it into one with one unnamed group. if (theList and len(theList[0]) > 1 and not isinstance(theList[0][1], (tuple, list))): theList = [(None, theList)] items = [] for groupName, values in theList: for value, desc in values: items.append(EnumItem(value, desc, group=groupName)) return Enum('', items)
def test_get_extra(self): """ Getting an enumeration item extra value by enumeration value returns the extra's value or a default value, defaulting to ``None``. """ enum = Enum('doc', enum_values_fixture()) self.assertThat( enum.extra(u'foo', 'quux'), Equals(u'hello')) self.assertThat( enum.extra(u'foo', 'frob'), Equals(u'world')) self.assertThat( enum.extra(u'bar', 'quux'), Equals(u'goodbye')) self.assertThat( enum.extra(u'bar', 'nope'), Is(None)) self.assertThat( enum.extra(u'bar', 'nope', u''), Equals(u''))
def test_extra(self): """ Extra parameters are retrieved by `EnumItem.get` if they exist otherwise a default value is returned instead. """ enum = Enum('doc', enum_values_fixture()) self.assertThat( enum.get(u'foo').get('quux'), Equals(u'hello')) self.assertThat( enum.get(u'foo').get('frob'), Equals(u'world')) self.assertThat( enum.get(u'bar').get('quux'), Equals(u'goodbye')) self.assertThat( enum.get(u'bar').get('boop'), Is(None)) self.assertThat( enum.get(u'bar').get('beep', 42), Equals(42))