示例#1
0
def test_get_file_from_arg_with_wrong_type():
    """
    Test exception is raise when object is not of expected types.
    """
    with pytest.raises(
            WaveFileNotSupported,
            match=esc(
                "'file' argument must be a string or <MockEmptyClass> instance, "
                "<class 'int'> given instead.")):
        get_stream_from_file(10, 'rb', MockEmptyClass)
示例#2
0
 def __init__(self, symbols, keywords):
     self._regex_token_pairs = tuple(
         (type_, rec(regex)) for type_, regex in tuple(
             ('KEYWORD', k + r'(?!\w)') for k in keywords) + tuple(
                 ('SYMBOL', esc(symbol)) for symbol in symbols) +
         (('STRING', r'\"(?:\\\"|[^"])*\"' + r'|'
           r"\'(?:\\\'|[^'])*\'"), ('FLOAT', r'\d+\.\d*|\.\d+'),
          ('INT', r'\d+'), ('NAME', r'\w+')))
     self._space_re = rec(r'[ \t]*(?:\#[^\n]*)?')
     self._empty_lines_re = rec(r'(?:(?:[ \t]*(?:\#[^\n]*)?)(?:\n|\Z))*')
     self._err_re = rec(r'[^\s]*')
示例#3
0
def test_check_head_chunk_wrong_type(mocker):
    """
    Test exception is raised when wave type tag has unknown type.
    """
    content = [b'BARB', 0, b'RIFF']
    # mock stream methods
    stream = mocker.MagicMock()
    stream.read.side_effect = lambda x: content.pop()

    with pytest.raises(WaveFileNotSupported,
                       match=esc('File does not appear to be a WAVE file.')):
        get_stream_handler(stream)
示例#4
0
def test_check_head_chunk_wrong_tag(mocker):
    """
    Test exception is raised when head tag has unknown type.
    """
    # mock stream methods
    stream = mocker.MagicMock()
    stream.read.return_value = b'XFIR'

    with pytest.raises(WaveFileNotSupported,
                       match=esc("Unsupported header for "
                                 "file 'XFIR'.")):
        get_stream_handler(stream)
示例#5
0
 def __init__(self, symbols, keywords):
     self._regex_token_pairs = tuple(
         (type_, rec(regex)) for type_, regex in
             tuple(('KEYWORD', k + r'(?!\w)') for k in keywords) +
             tuple(('SYMBOL', esc(symbol)) for symbol in symbols) +
             (     ('STRING',
                       r'\"(?:\\\"|[^"])*\"' + r'|'
                       r"\'(?:\\\'|[^'])*\'"),
                   ('FLOAT',   r'\d+\.\d*|\.\d+'),
                   ('INT',     r'\d+'),
                   ('NAME',    r'\w+')))
     self._space_re = rec(r'[ \t]*(?:\#[^\n]*)?')
     self._empty_lines_re = rec(r'(?:(?:[ \t]*(?:\#[^\n]*)?)(?:\n|\Z))*')
     self._err_re = rec(r'[^\s]*')
示例#6
0
def test_get_data_from_chunk_raises_error_if_corrupt(format, mocker):
    """
    Test data is read correctly from chunk for 24 bit
    """
    # mock Chunk methods
    chunk = mocker.MagicMock()
    chunk.getsize.return_value = 1

    format = FormatInfo(wFormatTag=1,
                        nChannels=2,
                        nSamplesPerSec=1,
                        nAvgBytesPerSec=6,
                        nBlockAlign=6,
                        wBitsPerSample=24)

    with pytest.raises(WaveFileIsCorrupted,
                       match=esc('Data size does not match frame '
                                 'size of 24 bits')):
        get_data_from_chunk(chunk, format, None)
示例#7
0
def test_get_fmt_chunk_fail(fmt_names, chunk_size, format_tag, sub_format_tag,
                            exception_type, error_msg, mocker):
    """
    Test head chunk is read correctly
    """
    read_list = [
        struct.pack('<H', sub_format_tag),
        struct.pack('<HHLLHH', format_tag, 1, 2, 3, 4, 5)
    ]
    # mock Chunk methods
    mocker.patch.object(chunk.Chunk, '__init__', return_value=None)
    mocker.patch.object(chunk.Chunk,
                        'getname',
                        side_effect=lambda: fmt_names.pop())
    mocker.patch.object(chunk.Chunk, 'getsize', return_value=chunk_size)
    mocker.patch.object(chunk.Chunk,
                        'read',
                        side_effect=lambda _: read_list[-1])
    mocker.patch.object(chunk.Chunk,
                        'seek',
                        side_effect=lambda _: read_list.pop())

    with pytest.raises(exception_type, match=esc(error_msg)):
        get_fmt_chunk(None, StreamHandler(True))
示例#8
0
def test_get_chunk_eof(mocker):
    mocker.patch.object(chunk.Chunk, '__init__', side_effect=EOFError)
    with pytest.raises(wavy.WaveFileIsCorrupted,
                       match=esc('Reached end of file prematurely.')):
        get_chunk(None)
示例#9
0
def test_check_format_info_fail(info, expected_msg):
    with pytest.raises(WaveFileIsCorrupted, match=esc(expected_msg)):
        check_format_info(info)
示例#10
0
def test_wave_file_invalid_args(sample_width, data, tags, error):
    """
    Test that WaveFile is created correctly.
    """
    with pytest.raises(wavy.WaveValueError, match=esc(error)):
        WaveFile(sample_width, 100, data, tags)