def test_valid_pack(self):
        """Test packing valid enum values."""

        field = ('a', 'b', SimpleEnum)  # signed byte: -128, 127
        out = ElementEnum.valid(field)
        self.assertTrue(out)
        elem = ElementEnum(field)
        test_values = [
            ({
                'a': 0
            }, b'\x00'),
            ({
                'a': 2
            }, b'\x02'),
            ({
                'a': SimpleEnum.one
            }, b'\x01'),
            ({
                'a': SimpleEnum.two
            }, b'\x02'),
        ]
        for (in_val, out_val) in test_values:
            with self.subTest((out_val, in_val)):  # pylint: disable=no-member
                ret = elem.pack(in_val)
                self.assertEqual(ret, out_val)
Exemplo n.º 2
0
    def test_valid_unpack(self):
        """Test unpacking valid enum values."""

        field = ('a', 'b', SimpleEnum)  # signed byte: -128, 127
        out = ElementEnum.valid(field)
        self.assertTrue(out)
        elem = ElementEnum(field)
        test_values = [
            (SimpleEnum.zero, b'\x00'),
            (SimpleEnum.one, b'\x01'),
            (SimpleEnum.two, b'\x02'),
        ]
        for (out_val, in_val) in test_values:
            with self.subTest((out_val, in_val)):  # pylint: disable=no-member
                (ret, unused) = elem.unpack({}, in_val)
                self.assertEqual(unused, b'')
                self.assertEqual(ret, out_val)
Exemplo n.º 3
0
    def test_invalid_pack(self):
        """Test packing invalid enum values."""

        field = ('a', 'b', SimpleEnum)  # signed byte: -128, 127
        out = ElementEnum.valid(field)
        self.assertTrue(out)
        elem = ElementEnum(field)
        test_values = [
            {'a': -1},
            {'a': 3},
            {'a': 'three'},
            {'a': 'ONE'},
        ]
        msg = '{} is not a valid {}'
        for val in test_values:
            with self.subTest(val):  # pylint: disable=no-member
                with self.assertRaises(ValueError) as cm:
                    elem.pack(val)
                self.assertEqual(str(cm.exception), msg.format(val['a'], 'SimpleEnum'))
Exemplo n.º 4
0
    def test_invalid_unpack(self):
        """Test unpacking invalid enum values."""

        field = ('a', 'b', SimpleEnum)  # signed byte: -128, 127
        out = ElementEnum.valid(field)
        self.assertTrue(out)
        elem = ElementEnum(field)
        test_values = [
            (b'\xFF', -1),
            (b'\x03', 3),
            (b'\x7F', 127),
            (b'\x10', 16),
            (b'\x80', -128),
        ]
        # msg = '{} is not a valid {}'
        for (in_val, out_val) in test_values:
            with self.subTest((in_val, out_val)):  # pylint: disable=no-member
                # with self.assertRaises(ValueError) as cm:
                with self.assertRaises(ValueError):
                    elem.unpack({}, in_val)