def test_flag_basics(self): cls = define_enum('FoobarAllFlags', dict(FOO=1, BAR=2, FOOBAR=3).items(), is_flags=True) foo = cls.FOO bar = cls.BAR foobar = foo | bar self.assertIs(foobar, cls.FOOBAR) foo2 = foobar & foo self.assertIs(foo2, foo) bar2 = foobar ^ foo self.assertIs(bar2, bar) bar3 = foobar & ~foo self.assertIs(bar3, bar)
def test_multiple_names(self): cls = define_enum('FFooBBar', ( ('FOO', 1), ('F', 1), ('BAR', 2), ('B', 2), )) self.assertIs(cls.F, cls.FOO) self.assertEqual(cls.F.name, 'FOO') self.assertNotEqual(cls.F.name, 'F') # This is actually the string. self.assertEqual(cls.F, 'FOO') self.assertEqual(cls.F, 'F') self.assertNotEqual(cls.F, 'BAR') self.assertNotEqual(cls.F, 'B') self.assertRaises(ValueError, lambda: cls.F == 'x')
def define_foobar(self, **kwargs): return define_enum('Foobar', ( ('FOO', 1), ('BAR', 2), ), **kwargs)
import pickle from av.enums import EnumType, define_enum from .common import TestCase # This must be at the top-level. PickleableFooBar = define_enum('PickleableFooBar', [('FOO', 1)]) class TestEnums(TestCase): def define_foobar(self, **kwargs): return define_enum('Foobar', ( ('FOO', 1), ('BAR', 2), ), **kwargs) def test_basics(self): cls = self.define_foobar() self.assertIsInstance(cls, EnumType) foo = cls.FOO self.assertIsInstance(foo, cls) self.assertEqual(foo.name, 'FOO') self.assertEqual(foo.value, 1)
import pickle from av.enums import EnumType, define_enum from .common import TestCase # This must be at the top-level. PickleableFooBar = define_enum('PickleableFooBar', [('FOO', 1)]) class TestEnums(TestCase): def define_foobar(self, **kwargs): return define_enum('Foobar', ( ('FOO', 1), ('BAR', 2), ), **kwargs) def test_basics(self): cls = self.define_foobar() self.assertIsInstance(cls, EnumType) foo = cls.FOO self.assertIsInstance(foo, cls) self.assertEqual(foo.name, 'FOO') self.assertEqual(foo.value, 1) self.assertNotIsInstance(foo, PickleableFooBar)