def test_endswith_exhaustive(ending): # Exhaustive test of the various combinations. Should catch most # edge conditions. for base in ("A" + ending, "BA" + ending + "A" + ending): for repeat in range(0, 15): s = base * repeat infile = StringIO(s) for marker in ("A", "A\n"): for look in range(5): lookahead = base * look for readhint in range(4, 10): infile.seek(0) reader = RecordReader.EndsWith(\ infile, marker, readhint, lookahead) count = count_records(reader) assert count == repeat + look, \ (count, ending, base, repeat, marker, look, readhint) infile.seek(0) reader = RecordReader.EndsWith(infile, marker, lookahead=lookahead) count = count_records(reader) assert count == repeat + look, \ (count, ending, repeat, marker, look)
def test_endswith_generic(): to_eol_data = ( ("A\n", 1), ("AA\n", 1), ("B\nA\n", 1), ("B\nB\nA\n", 1), ("A\nA\n", 2), ("A A\nA\n", 2), # this changes with an "A\n" reader ("A", 1), ("A\nA A\nA\n", 3), # this changes with an "A\n" reader ("A\nA A\nA", 3), # this changes with an "A\n" reader ) for s, expected in to_eol_data: reader = RecordReader.EndsWith(StringIO(s), "A") count = count_records(reader) assert count == expected, (s, expected, count) # skips to EOL newline_data = ( ("A\n", 1), #("AA\n", 1), # not legal ("B\nA\n", 1), ("B\nB\nA\n", 1), ("A\nA\n", 2), ("A A\nA\n", 1), # this changed with an "A\n" reader ("A", 1), ("A\nA A\nA\n", 2), # this changed with an "A\n" reader ("A\nA A\nA", 2), # this changed with an "A\n" reader ) for s, expected in newline_data: reader = RecordReader.EndsWith(StringIO(s), "A\n") count = count_records(reader) assert count == expected, (s, expected, count) # expects newline
def test_endswith_errors(): # Check the failure cases. # Could no record at all # Could be some records followed by an incomplete record # Could be a line which partially matches the data for s in ("B", "B\n", "A\nB\n", "A\nB\nA\nB\n", "A\nB\nA\n ", "AA", "AA\n", "A\nB\nA X\n"): has_error = 0 infile = StringIO(s) try: reader = RecordReader.EndsWith(infile, "A\n") except RecordReader.ReaderError: has_error = 1 if not has_error: while not has_error: try: rec = reader.next() except RecordReader.ReaderError: has_error = 1 if not has_error and rec is None: break if not has_error: raise AssertionError, "should not get here with %r" % s # Could no record at all # Could be some records followed by an incomplete record # *Allowed* to read rest of line for s in ("B", "B\n", "A\nB\n", "A\nB\nA\nB\n", "A\nB\nA\n "): has_error = 0 infile = StringIO(s) try: reader = RecordReader.EndsWith(infile, "A") except RecordReader.ReaderError: has_error = 1 if not has_error: while not has_error: try: rec = reader.next() except RecordReader.ReaderError: has_error = 1 if not has_error and rec is None: break if not has_error: raise AssertionError, "should not get here with %r" % s
def test_endswith_SP(): # Check using a SWISS-PROT-like format for ending in ("\n", "\r", "\r\n"): s = string.replace(data1, "\n", ending) for final in ("", ending): d = s + final loop = 0 for i in range(5, 20): infile = StringIO(d) reader = RecordReader.EndsWith(infile, "//", i) count = count_records(reader) assert count == 3, (ending, final, i, count, d) for i in range(5, 20): infile = StringIO(d) reader = RecordReader.EndsWith(infile, "//\n", i) count = count_records(reader) assert count == 3, (ending, final, i, count, d)
def test_endswith_remainder(): # Make sure the remainder method works for repeat in range(20): vals = map(lambda x: "%d\nA\n" % x, range(repeat)) data = string.join(vals, "") infile = StringIO(data) for look in range(10) + range(10, len(data), 5): infile.seek(look) lookahead = data[:look] reader = RecordReader.EndsWith(infile, "A", lookahead=lookahead) all = "" while 1: file, lh = reader.remainder() pos = file.tell() rest = file.read() assert all + lh + rest == data, (all, lh, rest, data) file.seek(pos) assert data.startswith(all), (data, all) record = reader.next() if record is None: break all = all + record assert all == data, (all, data)
def test_end_lines(): lookahead = open(sp_sample).read() reader = RecordReader.EndsWith(StringIO(""), "//\n", lookahead = lookahead) assert test_count(reader) == 8
def test_end(): reader = RecordReader.EndsWith(open(sp_sample), "//\n") assert test_count(reader) == 8