def test_DictionaryObject_read_from_stream_broken(): stream = BytesIO(b"< /S /GoTo >>") pdf = None with pytest.raises(PdfReadError) as exc: DictionaryObject.read_from_stream(stream, pdf) assert (exc.value.args[0] == "Dictionary read error at byte 0x2: stream must begin with '<<'")
def test_DictionaryObject_read_from_stream_stream_no_stream_length(strict): stream = BytesIO(b"<< /S /GoTo >>stream\n") class Tst: # to replace pdf strict = False pdf = Tst() pdf.strict = strict with pytest.raises(PdfReadError) as exc: DictionaryObject.read_from_stream(stream, pdf) assert exc.value.args[0] == "Stream length not defined"
def test_DictionaryObject_read_from_stream_stream_stream_valid( strict, length, should_fail): stream = BytesIO(b"<< /S /GoTo /Length %d >>stream\nBT /F1\nendstream\n" % length) class Tst: # to replace pdf strict = True pdf = Tst() pdf.strict = strict with pytest.raises(PdfReadError) as exc: do = DictionaryObject.read_from_stream(stream, pdf) # TODO: What should happen with the stream? assert do == {"/S": "/GoTo"} if length in (6, 10): assert b"BT /F1" in do._StreamObject__data raise PdfReadError("__ALLGOOD__") print(exc.value) assert should_fail ^ (exc.value.args[0] == "__ALLGOOD__")
def test_DictionaryObject_read_from_stream_stream_no_newline(): stream = BytesIO(b"<< /S /GoTo >>stream") pdf = None with pytest.raises(PdfReadError) as exc: DictionaryObject.read_from_stream(stream, pdf) assert exc.value.args[0] == "Stream data must be followed by a newline"
def test_DictionaryObject_read_from_stream_unexpected_end(): stream = BytesIO(b"<< \x00/S /GoTo") pdf = None with pytest.raises(PdfStreamError) as exc: DictionaryObject.read_from_stream(stream, pdf) assert exc.value.args[0] == "Stream has ended unexpectedly"
def test_DictionaryObject_read_from_stream(): stream = BytesIO(b"<< /S /GoTo >>") pdf = None out = DictionaryObject.read_from_stream(stream, pdf) assert out.get_object() == {NameObject("/S"): NameObject("/GoTo")}