Пример #1
0
def test_until():
    # Can only read at most one record
    print "Testing Until"
    test_data = (
        "A",
        "A\n",
        "A\nB",
        "A\nB\n",
        "A\nBCDE\nQWE\nTRYU\nA\n",
        "A\nA\nA\nA\nA\nA\nA\n",
        "AB",
        "AB\n",
        "AB\nAC",
        "AB\nAC\n",
    )

    for ending in ("\n", "\r", "\r\n"):
        for pre in ("", "B\n", "BA\n", "CA\nBA\n"):
            pre_nl = string.replace(pre, "\n", ending)
            for look in (0, 1, 3):
                for text in test_data:
                    text_nl = string.replace(text, "\n", ending)
                    s = pre_nl + text_nl
                    reader = RecordReader.Until(StringIO(s[look:]),
                                                "A",
                                                lookahead=s[:look],
                                                sizehint=4)
                    found_record = None
                    while 1:
                        rec = reader.next()
                        if rec is None:
                            break
                        assert not found_record, \
                               "Already found %r but also found %r in %r" % \
                               (found_record, rec, s)
                        found_record = rec

                    assert found_record == pre_nl, \
                           "Expecting record %r, found %r in %r" % \
                           (pre_nl, found_record, s)
                    infile, remainder = reader.remainder()
                    remainder = remainder + infile.read()
                    assert remainder == text_nl, \
                           "Expecting remainder %r, found %r in %r" % \
                           (text_nl, remainder, s)
Пример #2
0
def test_until_lines():
    lookahead = "a\nID\nb\nID\n"
    reader = RecordReader.Until(StringIO(""), "ID", lookahead = lookahead)
    assert test_count(reader, check_remainder = 0) == 1
    assert reader.remainder()[1]
Пример #3
0
def test_until():
    s = "a\nID\nb\nID\n"
    reader = RecordReader.Until(StringIO(s), "ID")
    assert test_count(reader, check_remainder = 0) == 1
    assert reader.remainder()[1]