Example #1
0
 def test_non_alphanumeric_names(self):
     with self.assertRaises(ValueError) as context:
         RGB = NamedFrozenMapMeta('RGB', ['r_e_d', 'gr33n', '&lue'])
     self.assertEqual(
         str(context.exception),
         ('Type names and field names can only contain alphanumeric'
          " characters and underscores: '&lue'"))
Example #2
0
 def test_getattr_AttributeError(self):
     RGB = NamedFrozenMapMeta('RGB', ['red', 'green', 'blue'])
     rgb = RGB(red='rouge', green='forest', blue='azul')
     with self.assertRaises(AttributeError) as context:
         rgb.gray
     self.assertEqual(str(context.exception),
                      "'RGB' object has no attribute 'gray'")
Example #3
0
 def test_create(self):
     RGB = NamedFrozenMapMeta('RGB', ['red', 'green', 'blue'])
     rgb = RGB(red='rouge', green='forest', blue='azul')
     self.assertIsInstance(rgb, RGB)
     self.assertTrue(hasattr(rgb, 'red'))
     self.assertTrue(hasattr(rgb, 'green'))
     self.assertTrue(hasattr(rgb, 'blue'))
Example #4
0
 def test_setattr_TypeError(self):
     RGB = NamedFrozenMapMeta('RGB', ['red', 'green', 'blue'])
     rgb = RGB(red='rouge', green='forest', blue='azul')
     with self.assertRaises(TypeError) as context:
         rgb.blue = 'topaz'
     self.assertEqual(str(context.exception),
                      "'RGB' object does not support attribute assignment")
     with self.assertRaises(TypeError) as context:
         rgb.gray = 'pewter'
     self.assertEqual(str(context.exception),
                      "'RGB' object does not support attribute assignment")
Example #5
0
 def test_len(self):
     RGB = NamedFrozenMapMeta('RGB', ['red', 'green', 'blue'])
     rgb = RGB(red='rouge', green='forest', blue='azul')
     self.assertEqual(len(rgb), 3)
Example #6
0
 def test_getitem_KeyError(self):
     RGB = NamedFrozenMapMeta('RGB', ['red', 'green', 'blue'])
     rgb = RGB(red='rouge', green='forest', blue='azul')
     with self.assertRaises(KeyError) as context:
         rgb['grey']
     self.assertEqual(str(context.exception), "'grey'")
Example #7
0
 def test_no_duplicate_fields(self):
     with self.assertRaises(ValueError) as context:
         RGB = NamedFrozenMapMeta('RGB', ['duck', 'duck', 'goose'])
     self.assertEqual(str(context.exception),
                      "Encountered duplicate field name: 'duck'")
Example #8
0
 def test_no_leading_underscore_fields(self):
     with self.assertRaises(ValueError) as context:
         RGB = NamedFrozenMapMeta('RGB', ['_red', '__green', '___blue'])
     self.assertEqual(
         str(context.exception),
         "Field names cannot start with an underscore: '_red'")
Example #9
0
 def test_cannot_start_with_number(self):
     with self.assertRaises(ValueError) as context:
         RGB = NamedFrozenMapMeta('RGB', ['4ed', '6reen', '3lue'])
     self.assertEqual(
         str(context.exception),
         "Type names and field names cannot start with a number: '4ed'")
Example #10
0
 def test_non_keyword_names(self):
     with self.assertRaises(ValueError) as context:
         RGB = NamedFrozenMapMeta('RGB', ['and', 'except', 'import'])
     self.assertEqual(
         str(context.exception),
         "Type names and field names cannot be a keyword: 'and'")
Example #11
0
 def test_class_fields(self):
     RGB = NamedFrozenMapMeta('RGB', ['red', 'green', 'blue'])
     self.assertEqual(RGB._fields, ('red', 'green', 'blue'))
Example #12
0
 def test_iter(self):
     RGB = NamedFrozenMapMeta('RGB', ['red', 'green', 'blue'])
     rgb = RGB(red='rouge', green='forest', blue='azul')
     self.assertEqual(frozenset(iter(rgb)),
                      frozenset(['red', 'green', 'blue']))
Example #13
0
 def test_hash(self):
     RGB = NamedFrozenMapMeta('RGB', ['red', 'green', 'blue'])
     rgb = RGB(red='rouge', green='forest', blue='azul')
     self.assertIsInstance(hash(rgb), int)