def test_values_can_be_any_hashable_type(self): """Test simple enums accept values of any hashable type""" Confused = enum(INT=0, TUPLE=(1, 2), STR='yup') self.assertEqual(Confused.get_id_by_label('TUPLE'), (1, 2)) with self.assertRaisesRegexp(EnumConstructionException, 'hashable'): Confused = enum(LIST=[1, 2])
def test_choices_are_ordered_by_value(self): Shuffled = enum(FRUIT=2, COFFEE=0, OATMEAL=1) self.assertEqual(Shuffled.choices, Breakfast.choices)
import unittest2 as unittest from richenum import enum from richenum import EnumConstructionException Breakfast = enum(COFFEE=0, OATMEAL=1, FRUIT=2) class SimpleEnumTestSuite(unittest.TestCase): def test_members_are_accessible_through_attributes(self): self.assertEqual(Breakfast.COFFEE, 0) def test_lookup_by_name(self): self.assertEqual(Breakfast.get_id_by_label('COFFEE'), 0) def test_lookup_by_value(self): self.assertEqual(Breakfast.get_label_by_id(0), 'COFFEE') def test_can_cast_to_list_of_choices(self): self.assertEqual(Breakfast.choices, [(0, 'COFFEE'), (1, 'OATMEAL'), (2, 'FRUIT')]) def test_choices_are_ordered_by_value(self): Shuffled = enum(FRUIT=2, COFFEE=0, OATMEAL=1) self.assertEqual(Shuffled.choices, Breakfast.choices) def test_values_can_be_any_hashable_type(self): try: Confused = enum(INT=0, TUPLE=(1, 2), STR='yup') self.assertEqual(Confused.get_id_by_label('TUPLE'), (1, 2)) except:
from richenum import enum # Charts CHART_KIND = enum(SCATTER="scatter", KDE="kde", HEX="hex") SHOT_COLOR = enum(MADE="#F44336", MISSED="#8BC34A") HEX_GRID_SIZE = 40 # Reddit I/O BOT_NAME = "shot-bot" ACCEPTED_VARIATIONS = "%s|%s|%s|%s|%s|%s|%s|%s|%s" % ( CHART_KIND.SCATTER, CHART_KIND.KDE, CHART_KIND.HEX, CHART_KIND.SCATTER.upper(), CHART_KIND.KDE.upper(), CHART_KIND.HEX.upper(), CHART_KIND.SCATTER.capitalize(), CHART_KIND.KDE.capitalize(), CHART_KIND.HEX.capitalize()) REGEX = enum(PLAYER_QUERY_PATTERN=r"\[\[(\w+ \w+)\]\]", CHART_KIND_PATTERN=r"(T|t)ype=(%s)" % ACCEPTED_VARIATIONS) REDDIT_TEMPLATE = enum( COMMENT_TEMPLATE="{linked_comment}\n\n" \ "---\n\n" \ "{instructions}\n\n" \ "---\n\n" \ "{signature}", LINK_TEMPLATE="**[{query_string}]({url})**\n\n", USAGE_TEMPLATE="**^Usage:**\n\n" \ "*^[[<first ^name> ^<last ^name>]]*\n\n" "^((optional: *type={scatter|hex}*, defaulted to scatter)^)\n\n" "^(Currently limited to 2015-16 season)\n\n",
import unittest2 as unittest from richenum import enum from richenum import EnumConstructionException Breakfast = enum( COFFEE=0, OATMEAL=1, FRUIT=2) class SimpleEnumTestSuite(unittest.TestCase): def test_members_are_accessible_through_attributes(self): self.assertEqual(Breakfast.COFFEE, 0) def test_lookup_by_name(self): self.assertEqual(Breakfast.get_id_by_label('COFFEE'), 0) def test_lookup_by_value(self): self.assertEqual(Breakfast.get_label_by_id(0), 'COFFEE') def test_can_cast_to_list_of_choices(self): self.assertEqual( Breakfast.choices, [(0, 'COFFEE'), (1, 'OATMEAL'), (2, 'FRUIT')]) def test_choices_are_ordered_by_value(self): Shuffled = enum(FRUIT=2, COFFEE=0, OATMEAL=1) self.assertEqual(Shuffled.choices, Breakfast.choices)
def test_values_can_be_any_hashable_type(self): """Test simple enums accept values of any hashable type""" Confused = enum(INT=0, TUPLE=(1, 2), STR='yup') assert Confused.get_id_by_label('TUPLE') == (1, 2) with pytest.raises(EnumConstructionException, match=r"hashable"): Confused = enum(LIST=[1, 2])