def peek_tests(**kwargs): """Peek tests""" fobj = io.StringIO('abc~def~gh!i~jkl') rdr = ReadLines(fobj, delimiter='~', **kwargs) assert next(rdr) == 'abc~' assert rdr.peek(0) == '' assert rdr.peek(2) == 'de' assert rdr.peek() == 'def~' assert rdr.peek() == 'def~' assert next(rdr) == 'def~' assert rdr.peek() == 'gh!i~' rdr.delimiter = '!' assert rdr.peek() == 'gh!' rdr.delimiter = '~' assert rdr.peek() == 'gh!i~' assert rdr.peek(4) == 'gh!i' assert rdr.peek(20) == 'gh!i~jkl' assert next(rdr) == 'gh!i~' assert rdr.peek() == 'jkl' assert rdr.peek() == 'jkl' assert next(rdr) == 'jkl' assert rdr.peek() == '' assert rdr.peek(10) == '' fobj = io.StringIO('abc~def~gh!i~jkl') rdr = ReadLines(fobj, delimiter='~', **kwargs) assert next(rdr) == 'abc~' assert rdr.peek(0) == '' assert rdr.peek(2) == 'de' assert rdr.peek() == 'def~' assert rdr.peek() == 'def~' assert next(rdr) == 'def~' rdr.delimiter = '!' assert rdr.peek() == 'gh!' rdr.delimiter = '~' assert rdr.peek() == 'gh!i~' assert rdr.peek(4) == 'gh!i' assert rdr.peek(20) == 'gh!i~jkl' assert next(rdr) == 'gh!i~' assert rdr.peek() == 'jkl' assert rdr.peek() == 'jkl' assert next(rdr) == 'jkl' assert rdr.peek() == '' assert rdr.peek(10) == '' fobj = io.StringIO('') rdr = ReadLines(fobj, delimiter='~', **kwargs) assert rdr.peek(0) == '' assert rdr.peek(2) == '' assert rdr.peek() == ''
def test_errors_delimiter_bin(): """Test delimiter override with an invalid delimiter for a text stream""" fobj = io.BytesIO(b'abc~def~') rdr = ReadLines(fobj, delimiter=b'~') assert next(rdr) == b'abc~' rdr.delimiter = '~' with pytest.raises(TypeError): next(rdr) rdr.delimiter = 1 with pytest.raises(TypeError): next(rdr) rdr.delimiter = re.compile('~') with pytest.raises(TypeError): next(rdr) rdr.delimiter = b'~' assert next(rdr) == b'def~'
def peek_tests_strip(**kwargs): """Peek tests with stripped delimiters""" fobj = io.StringIO('abc~def~gh!i~jkl~') rdr = ReadLines(fobj, delimiter='~', strip_delimiter=True, **kwargs) assert next(rdr) == 'abc' assert rdr.peek(0) == '' assert rdr.peek(2) == 'de' assert rdr.peek() == 'def' assert rdr.peek() == 'def' assert next(rdr) == 'def' assert rdr.peek() == 'gh!i' rdr.delimiter = '!' assert rdr.peek() == 'gh' rdr.delimiter = '~' assert rdr.peek() == 'gh!i' assert rdr.peek(4) == 'gh!i' assert rdr.peek(20) == 'gh!i~jkl~' assert next(rdr) == 'gh!i' assert rdr.peek() == 'jkl' assert rdr.peek() == 'jkl' assert next(rdr) == 'jkl' assert rdr.peek() == '' assert rdr.peek(10) == '' fobj = io.StringIO('abc~def~gh!i~jkl~') rdr = ReadLines(fobj, delimiter='~', strip_delimiter=True, **kwargs) assert next(rdr) == 'abc' assert rdr.peek(0) == '' assert rdr.peek(2) == 'de' assert rdr.peek() == 'def' assert rdr.peek() == 'def' assert next(rdr) == 'def' rdr.delimiter = '!' assert rdr.peek() == 'gh' rdr.delimiter = '~' assert rdr.peek() == 'gh!i' assert rdr.peek(4) == 'gh!i' assert rdr.peek(20) == 'gh!i~jkl~' assert next(rdr) == 'gh!i' assert rdr.peek() == 'jkl' assert rdr.peek() == 'jkl' assert next(rdr) == 'jkl' assert rdr.peek() == '' assert rdr.peek(10) == ''
def delimiter_tests_strip(**kwargs): """Delimiter override tests with stripped delimiters""" fobj = io.StringIO('abc~def~gh~i!j') rdr = ReadLines(fobj, delimiter='~', strip_delimiter=True, **kwargs) assert next(rdr) == 'abc' assert next(rdr) == 'def' rdr.delimiter = '!' assert rdr.delimiter == '!' assert next(rdr) == 'gh~i' assert next(rdr) == 'j' assert fobj.seek(2) == 2 rdr = ReadLines(fobj, delimiter='~', strip_delimiter=True, **kwargs) assert next(rdr) == 'c' assert next(rdr) == 'def' rdr.delimiter = '!' assert rdr.delimiter == '!' assert next(rdr) == 'gh~i' assert next(rdr) == 'j'
def delimiter_tests(**kwargs): """Delimiter override tests""" fobj = io.StringIO('abc~def~gh~i!j') rdr = ReadLines(fobj, delimiter='~', **kwargs) assert next(rdr) == 'abc~' assert next(rdr) == 'def~' rdr.delimiter = '!' assert rdr.delimiter == '!' assert next(rdr) == 'gh~i!' assert next(rdr) == 'j' assert fobj.seek(0) == 0 rdr = ReadLines(fobj, delimiter='~', **kwargs) assert next(rdr) == 'abc~' assert next(rdr) == 'def~' delimiter = re.compile('!') rdr.delimiter = delimiter assert rdr.delimiter is delimiter assert next(rdr) == 'gh~i!' assert next(rdr) == 'j' assert fobj.seek(2) == 2 rdr = ReadLines(fobj, delimiter='~', **kwargs) assert next(rdr) == 'c~' assert next(rdr) == 'def~' rdr.delimiter = '!' assert rdr.delimiter == '!' assert next(rdr) == 'gh~i!' assert next(rdr) == 'j' fobj = io.StringIO('') rdr = ReadLines(fobj, delimiter='~', **kwargs) assert rdr.peek() == '' rdr.delimiter = '!' assert rdr.delimiter == '!' assert rdr.peek() == ''