-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_cli_parsing.py
More file actions
163 lines (120 loc) · 5.09 KB
/
Copy pathtest_cli_parsing.py
File metadata and controls
163 lines (120 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""Tests for CLI argument parsing functions."""
import pytest
from bigocrpdf.cli import _parse_page_list, _parse_ranges, build_parser
class TestParsePageList:
"""Tests for _parse_page_list."""
def test_single_page(self):
assert _parse_page_list("3") == [3]
def test_range(self):
assert _parse_page_list("1-5") == [1, 2, 3, 4, 5]
def test_comma_separated(self):
assert _parse_page_list("1,3,7") == [1, 3, 7]
def test_mixed(self):
assert _parse_page_list("1-3,7,10-12") == [1, 2, 3, 7, 10, 11, 12]
def test_deduplicates(self):
assert _parse_page_list("1-3,2-4") == [1, 2, 3, 4]
def test_strips_whitespace(self):
assert _parse_page_list(" 1 , 3 - 5 ") == [1, 3, 4, 5]
def test_negative_raises(self):
import pytest
with pytest.raises(ValueError):
_parse_page_list("0,1,-1,2")
def test_empty_parts_skipped(self):
assert _parse_page_list(",1,,2,") == [1, 2]
def test_invalid_raises(self):
with pytest.raises(ValueError, match="Invalid page specification"):
_parse_page_list("abc")
def test_invalid_range_raises(self):
with pytest.raises(ValueError, match="Invalid page specification"):
_parse_page_list("1-abc")
def test_empty_string(self):
assert _parse_page_list("") == []
class TestParseRanges:
"""Tests for _parse_ranges."""
def test_single_range(self):
assert _parse_ranges("1-5") == [(1, 5)]
def test_multiple_ranges(self):
assert _parse_ranges("1-5,6-10,11-15") == [(1, 5), (6, 10), (11, 15)]
def test_single_page_becomes_range(self):
assert _parse_ranges("3") == [(3, 3)]
def test_mixed(self):
assert _parse_ranges("1-5,7") == [(1, 5), (7, 7)]
def test_empty_parts_skipped(self):
assert _parse_ranges(",1-5,,") == [(1, 5)]
def test_strips_whitespace(self):
assert _parse_ranges(" 1 - 5 , 6 - 10 ") == [(1, 5), (6, 10)]
def test_invalid_raises(self):
with pytest.raises(ValueError, match="Invalid range specification"):
_parse_ranges("abc")
def test_empty_string(self):
assert _parse_ranges("") == []
class TestBuildParser:
"""Tests for build_parser."""
def test_returns_parser(self):
p = build_parser()
assert p is not None
def test_ocr_subcommand(self):
p = build_parser()
args = p.parse_args(["ocr", "input.pdf", "-o", "output.pdf"])
assert args.command == "ocr"
assert str(args.input) == "input.pdf"
assert str(args.output) == "output.pdf"
def test_split_by_pages(self):
p = build_parser()
args = p.parse_args(["split", "in.pdf", "-o", "outdir", "--pages", "5"])
assert args.command == "split"
assert args.pages == 5
def test_split_by_size(self):
p = build_parser()
args = p.parse_args(["split", "in.pdf", "-o", "outdir", "--size", "10.5"])
assert args.command == "split"
assert args.size == 10.5
def test_merge_subcommand(self):
p = build_parser()
args = p.parse_args(["merge", "a.pdf", "b.pdf", "-o", "merged.pdf"])
assert args.command == "merge"
assert len(args.inputs) == 2
def test_compress_subcommand(self):
p = build_parser()
args = p.parse_args(["compress", "in.pdf", "-o", "out.pdf", "--quality", "40"])
assert args.command == "compress"
assert args.quality == 40
def test_rotate_subcommand(self):
p = build_parser()
args = p.parse_args(["rotate", "in.pdf", "-o", "out.pdf", "--angle", "90"])
assert args.command == "rotate"
assert args.angle == 90
def test_delete_subcommand(self):
p = build_parser()
args = p.parse_args(["delete", "in.pdf", "-o", "out.pdf", "--pages", "3,5"])
assert args.command == "delete"
assert args.pages == "3,5"
def test_extract_subcommand(self):
p = build_parser()
args = p.parse_args(["extract", "in.pdf", "-o", "out.pdf", "--pages", "1-3"])
assert args.command == "extract"
def test_info_subcommand(self):
p = build_parser()
args = p.parse_args(["info", "input.pdf"])
assert args.command == "info"
def test_export_odf_subcommand(self):
p = build_parser()
args = p.parse_args(["export-odf", "input.pdf"])
assert args.command == "export-odf"
def test_export_txt_subcommand(self):
p = build_parser()
args = p.parse_args(["export-txt", "input.pdf"])
assert args.command == "export-txt"
def test_verbose_flag(self):
p = build_parser()
args = p.parse_args(["-v", "info", "input.pdf"])
assert args.verbose is True
def test_ocr_language_default(self):
p = build_parser()
args = p.parse_args(["ocr", "in.pdf", "-o", "out.pdf"])
assert args.language == "latin"
def test_ocr_no_flags(self):
p = build_parser()
args = p.parse_args(["ocr", "in.pdf", "-o", "out.pdf", "--no-dewarp", "--no-deskew"])
assert args.no_dewarp is True
assert args.no_deskew is True