|
| 1 | +import pytest |
| 2 | +from pypdf import PdfReader |
| 3 | + |
| 4 | +from .conftest import RESOURCES_ROOT, chdir, run_cli |
| 5 | + |
| 6 | + |
| 7 | +def test_booklet_fewer_args(capsys, tmp_path): |
| 8 | + with chdir(tmp_path): |
| 9 | + exit_code = run_cli(["cat", str(RESOURCES_ROOT / "box.pdf")]) |
| 10 | + assert exit_code == 2 |
| 11 | + captured = capsys.readouterr() |
| 12 | + assert "Missing argument" in captured.err |
| 13 | + |
| 14 | + |
| 15 | +def test_booklet_extra_args(capsys, tmp_path): |
| 16 | + with chdir(tmp_path): |
| 17 | + exit_code = run_cli( |
| 18 | + ["booklet", str(RESOURCES_ROOT / "box.pdf"), "a.pdf", "b.pdf"] |
| 19 | + ) |
| 20 | + assert exit_code == 2 |
| 21 | + captured = capsys.readouterr() |
| 22 | + assert "unexpected extra argument" in captured.err |
| 23 | + |
| 24 | + |
| 25 | +def test_booklet_page_size(capsys, tmp_path): |
| 26 | + in_fname = str(RESOURCES_ROOT / "input8.pdf") |
| 27 | + |
| 28 | + with chdir(tmp_path): |
| 29 | + exit_code = run_cli( |
| 30 | + [ |
| 31 | + "booklet", |
| 32 | + in_fname, |
| 33 | + "output8.pdf", |
| 34 | + ] |
| 35 | + ) |
| 36 | + in_reader = PdfReader(in_fname) |
| 37 | + out_reader = PdfReader("output8.pdf") |
| 38 | + |
| 39 | + assert exit_code == 0 |
| 40 | + |
| 41 | + assert len(in_reader.pages) == 8 |
| 42 | + assert len(out_reader.pages) == 4 |
| 43 | + |
| 44 | + in_height = in_reader.pages[0].mediabox.height |
| 45 | + in_width = in_reader.pages[0].mediabox.width |
| 46 | + out_height = out_reader.pages[0].mediabox.height |
| 47 | + out_width = out_reader.pages[0].mediabox.width |
| 48 | + |
| 49 | + assert out_width == in_width * 2 |
| 50 | + assert in_height == out_height |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + ("page_count", "expected", "expected_bc"), |
| 55 | + [ |
| 56 | + ("8", "81\n27\n63\n45\n", "81\n27\n63\n45\n"), |
| 57 | + ("7", "71\n2\n63\n45\n", "71\n2b\n63\n45\n"), |
| 58 | + ("6", "61\n25\n43\n\n", "61\n25\n43\nc\n"), |
| 59 | + ("5", "51\n2\n43\n\n", "51\n2b\n43\nc\n"), |
| 60 | + ("4", "41\n23\n", "41\n23\n"), |
| 61 | + ("3", "31\n2\n", "31\n2b\n"), |
| 62 | + ("2", "21\n\n", "21\nc\n"), |
| 63 | + ("1", "1\n\n", "1b\nc\n"), |
| 64 | + ], |
| 65 | +) |
| 66 | +def test_booklet_order(capsys, tmp_path, page_count, expected, expected_bc): |
| 67 | + with chdir(tmp_path): |
| 68 | + exit_code = run_cli( |
| 69 | + [ |
| 70 | + "cat", |
| 71 | + "-o", |
| 72 | + f"input{page_count}.pdf", |
| 73 | + str(RESOURCES_ROOT / "input8.pdf"), |
| 74 | + f":{page_count}", |
| 75 | + ] |
| 76 | + ) |
| 77 | + assert exit_code == 0 |
| 78 | + |
| 79 | + exit_code = run_cli( |
| 80 | + [ |
| 81 | + "booklet", |
| 82 | + f"input{page_count}.pdf", |
| 83 | + f"output{page_count}.pdf", |
| 84 | + ] |
| 85 | + ) |
| 86 | + captured = capsys.readouterr() |
| 87 | + assert exit_code == 0, captured.err |
| 88 | + |
| 89 | + exit_code = run_cli( |
| 90 | + [ |
| 91 | + "extract-text", |
| 92 | + f"output{page_count}.pdf", |
| 93 | + ] |
| 94 | + ) |
| 95 | + captured = capsys.readouterr() |
| 96 | + assert exit_code == 0, captured.err |
| 97 | + assert captured.out == expected |
| 98 | + |
| 99 | + exit_code = run_cli( |
| 100 | + [ |
| 101 | + "booklet", |
| 102 | + "--centerfold-file", |
| 103 | + str(RESOURCES_ROOT / "c.pdf"), |
| 104 | + "--blank-page-file", |
| 105 | + str(RESOURCES_ROOT / "b.pdf"), |
| 106 | + f"input{page_count}.pdf", |
| 107 | + f"outputbc{page_count}.pdf", |
| 108 | + ] |
| 109 | + ) |
| 110 | + captured = capsys.readouterr() |
| 111 | + assert exit_code == 0, captured.err |
| 112 | + |
| 113 | + exit_code = run_cli( |
| 114 | + [ |
| 115 | + "extract-text", |
| 116 | + f"outputbc{page_count}.pdf", |
| 117 | + ] |
| 118 | + ) |
| 119 | + captured = capsys.readouterr() |
| 120 | + assert exit_code == 0, captured.err |
| 121 | + assert captured.out == expected_bc |
0 commit comments