def test_enum(self): """ Tests for the enum type. """ items = ["m", "s", "m/s", "m/(s*s)", "m*s", "dimensionless", "other"] units = Enum(items) # existing selections self.assertEqual(units.other, "other") self.assertEqual(units.M, "m") self.assertEqual(units['m/s'], "m/s") self.assertEqual(units.get('m/s'), "m/s") self.assertEqual(units[0], "m") self.assertEqual(units[-1], "other") # not existing selections should fail self.assertRaises(Exception, units.__getitem__, '5') self.assertRaises(Exception, units.__getattr__, 'xx') self.assertRaises(Exception, units.get, 'xx', 'default') self.assertRaises(Exception, units.__getitem__, 99) self.assertRaises(Exception, units.__getitem__, -99) # test in operator self.assertTrue("other" in units) self.assertTrue("ot21her" not in units) # test typical dict methods self.assertEqual(units.values(), items) self.assertEqual(units.items(), list(zip(items, items))) self.assertEqual(units.keys(), items) # call will either return correct enum label or return None self.assertEqual(units('m'), 'm') self.assertEqual(units('m/(s*s)'), 'm/(s*s)') self.assertEqual(units(5), 'dimensionless') self.assertEqual(units(99), None) self.assertEqual(units('xxx'), None)
def test_enum(self): """ Tests for the enum type. """ items = ["m", "s", "m/s", "m/(s*s)", "m*s", "dimensionless", "other"] units = Enum(items) # existing selections assert units.other == "other" assert units.M == "m" assert units['m/s'] == "m/s" assert units.get('m/s') == "m/s" assert units[0] == "m" assert units[-1] == "other" # not existing selections should fail with pytest.raises(Exception): units.__getitem__('5') with pytest.raises(Exception): units.__getattr__('xx') with pytest.raises(Exception): units.get('xx', 'default') with pytest.raises(Exception): units.__getitem__(99) with pytest.raises(Exception): units.__getitem__(-99) # test in operator assert "other" in units assert "ot21her" not in units # test typical dict methods assert units.values() == items assert units.items() == list(zip(items, items)) assert units.keys() == items # call will either return correct enum label or return None assert units('m') == 'm' assert units('m/(s*s)') == 'm/(s*s)' assert units(5) == 'dimensionless' assert units(99) is None assert units('xxx') is None