Пример #1
0
    def test_split(self):
        with pytest.raises(ValueError):
            list(Record.split(BYTES, address=-1))

        with pytest.raises(ValueError):
            list(Record.split(BYTES, address=(1 << 32)))

        with pytest.raises(ValueError):
            list(Record.split(BYTES, address=((1 << 32) - 128)))

        with pytest.raises(ValueError):
            list(Record.split(BYTES, columns=257))

        with pytest.raises(ValueError):
            list(Record.split(BYTES, columns=253, tag=Record.TAG_TYPE.DATA_16))

        with pytest.raises(ValueError):
            list(Record.split(BYTES, columns=252, tag=Record.TAG_TYPE.DATA_24))

        with pytest.raises(ValueError):
            list(Record.split(BYTES, columns=251, tag=Record.TAG_TYPE.DATA_32))

        ans_out = list(
            Record.split(HEXBYTES,
                         header=b'Hello, World!',
                         start=0,
                         tag=Tag.DATA_16))
        ans_ref = [
            Record(0, Tag.HEADER, b'Hello, World!'),
            Record(0, Tag.DATA_16, HEXBYTES),
            Record(0, Tag.COUNT_16, b'\x00\x01'),
            Record(0, Tag.START_16, b''),
        ]
        assert ans_out == ans_ref

        ans_out = list(
            Record.split(HEXBYTES,
                         standalone=False,
                         address=7,
                         columns=5,
                         align=3))
        ans_ref = [
            Record.build_data(7, HEXBYTES[:4]),
            Record.build_data(11, HEXBYTES[4:9]),
            Record.build_data(16, HEXBYTES[9:14]),
            Record.build_data(21, HEXBYTES[14:]),
        ]
        assert ans_out == ans_ref
Пример #2
0
 def test_build_data_max_columns(self):
     for tag in (1, 2, 3):
         tag = Tag(tag)
         max_columns = Record.TAG_TO_COLUMN_SIZE[tag]
         record = Record.build_data(0x1234, bytes(max_columns), tag=tag)
         assert len(record.data) == max_columns
         assert record.count == 255
Пример #3
0
    def test_check(self):
        record = Record.build_data(0, b'')
        for tag in range(len(Tag), 256):
            record.tag = tag
            record.update_checksum()
            with pytest.raises(ValueError):
                record.check()

        record = Record.build_data(0, b'Hello, World!')
        record.check()
        record.tag = len(Tag)
        with pytest.raises(ValueError):
            record.check()

        record = Record.build_header(b'')
        record.address = 1
        record.update_checksum()
        with pytest.raises(ValueError, match='address error'):
            record.check()

        record = Record.build_count(0x1234)
        record.address = 1
        record.update_checksum()
        with pytest.raises(ValueError, match='address error'):
            record.check()

        record = Record.build_count(0x123456)
        record.address = 1
        record.update_checksum()
        with pytest.raises(ValueError, match='address error'):
            record.check()

        record = Record.build_data(0, bytearray(252))
        record.data.append(1)
        record.update_count()
        record.update_checksum()
        with pytest.raises(ValueError, match='count overflow'):
            record.check()

        record = Record.build_data(0, b'Hello, World!')
        record.count += 1
        record.update_checksum()
        with pytest.raises(ValueError, match='count error'):
            record.check()
Пример #4
0
    def test_build_data_doctest(self):
        ans_out = str(Record.build_data(0x1234, b'Hello, World!'))
        ans_ref = 'S110123448656C6C6F2C20576F726C642140'
        assert ans_out == ans_ref

        ans_out = str(
            Record.build_data(0x1234, b'Hello, World!', tag=Tag.DATA_16))
        ans_ref = 'S110123448656C6C6F2C20576F726C642140'
        assert ans_out == ans_ref

        ans_out = str(
            Record.build_data(0x123456, b'Hello, World!', tag=Tag.DATA_24))
        ans_ref = 'S21112345648656C6C6F2C20576F726C6421E9'
        assert ans_out == ans_ref

        ans_out = str(
            Record.build_data(0x12345678, b'Hello, World!', tag=Tag.DATA_32))
        ans_ref = 'S3121234567848656C6C6F2C20576F726C642170'
        assert ans_out == ans_ref
Пример #5
0
    def test_check(self):
        record = Record.build_data(0, b'')
        for tag in range(len(Tag), 256):
            record.tag = tag
            record.update_checksum()
            with pytest.raises(ValueError):
                record.check()

        record = Record.build_data(0, b'Hello, World!')
        record.check()
        record.tag = len(Tag)
        with pytest.raises(ValueError):
            record.check()

        record = Record.build_header(b'')
        record.address = 1
        record.update_checksum()
        with pytest.raises(ValueError):
            record.check()

        record = Record.build_count(0x1234)
        record.address = 1
        record.update_checksum()
        with pytest.raises(ValueError):
            record.check()

        record = Record.build_count(0x123456)
        record.address = 1
        record.update_checksum()
        with pytest.raises(ValueError):
            record.check()

        record = Record.build_data(0, b'Hello, World!')
        record.count += 1
        record.update_checksum()
        with pytest.raises(ValueError):
            record.check()
Пример #6
0
    def test___eq___doctest(self):
        record1 = BinaryRecord.build_data(0, b'Hello, World!')
        record2 = BinaryRecord.build_data(0, b'Hello, World!')
        assert record1 == record2

        record1 = BinaryRecord.build_data(0, b'Hello, World!')
        record2 = BinaryRecord.build_data(1, b'Hello, World!')
        assert record1 != record2

        record1 = BinaryRecord.build_data(0, b'Hello, World!')
        record2 = BinaryRecord.build_data(0, b'hello, world!')
        assert record1 != record2

        record1 = MotorolaRecord.build_header(b'Hello, World!')
        record2 = MotorolaRecord.build_data(0, b'hello, world!')
        assert record1 != record2
Пример #7
0
 def test_build_data_tag(self):
     for tag in [0] + list(range(4, len(Tag))):
         tag = Tag(tag)
         with pytest.raises(ValueError):
             Record.build_data(0x1234, b'Hello, World!', tag=tag)