Exemplo n.º 1
0
    def test_decode_byte_field(self):
        """ Test for Field.byte.decode """
        self.assertEqual(0, Field.byte('test').decode(bytearray([0])))
        self.assertEqual(1, Field.byte('test').decode(bytearray([1])))
        self.assertEqual(255, Field.byte('test').decode(bytearray([255])))

        with self.assertRaises(ValueError):
            Field.byte('test').decode('ab')
Exemplo n.º 2
0
    def test_input_with_crc(self):
        """ Test encoding with crc. """
        spec = MasterCommandSpec(
            'TE', [Field.byte('one'),
                   Field.byte('two'),
                   Field.crc()], [])
        spec_input = spec.create_input(1, {"one": 255, "two": 128})

        self.assertEqual(13, len(spec_input))
        self.assertEqual(bytearray(b'STRTE\x01\xff\x80C\x01\x7f\r\n'),
                         spec_input)
Exemplo n.º 3
0
    def test_create_input(self):
        """ Test for MasterCommandSpec.create_input """
        basic_action = MasterCommandSpec('BA', [
            Field.byte('actionType'),
            Field.byte('actionNumber'),
            Field.padding(11)
        ], [])
        ba_input = basic_action.create_input(1, {
            "actionType": 2,
            "actionNumber": 4
        })

        self.assertEqual(21, len(ba_input))
        self.assertEqual(
            bytearray(b'STRBA\x01\x02\x04' + (b'\x00' * 11) + b'\r\n'),
            ba_input)
Exemplo n.º 4
0
    def test_create_basic_action(self):
        """ Test for MasterCommandSpec.basic_action """
        self.master_version = (3, 143, 113)
        basic_action = MasterCommandSpec('BA', [
            Field.byte('actionType'),
            Field.byte('actionNumber'),
            Field.byte('parameter'),
            Field.padding(10)
        ], [])
        ba_input = basic_action.create_input(1, {
            "actionType": 2,
            "actionNumber": 4,
            "parameter": 9
        })

        self.assertEqual(21, len(ba_input))
        self.assertEqual(
            bytearray(b'STRBA\x01\x02\x04\x09' + (b'\x00' * 10) + b'\r\n'),
            ba_input)
Exemplo n.º 5
0
    def test_encode_byte_field(self):
        """ Test for Field.byte.encode """
        self.assertEqual(bytearray([0]), Field.byte('test').encode(0))
        self.assertEqual(bytearray([1]), Field.byte('test').encode(1))
        self.assertEqual(bytearray([255]), Field.byte('test').encode(255))

        with self.assertRaises(ValueError):
            Field.byte('test').encode(-1)
        with self.assertRaises(ValueError):
            Field.byte('test').encode(1024)