- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_match.py
More file actions
Latest commit
132 lines (98 loc) · 4.33 KB
/
Copy pathtest_match.py
File metadata and controls
132 lines (98 loc) · 4.33 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
"""
match.py is a grep-like utility.
Usage: match.py [OPTION]... PATTERN [FILE]...
See the docstring of each test_* function for specific requirements.
"""
deftest_basic(call, tmpdir):
"""Search the named input FILEs (or standard input if no files are named)
for lines matching the given PATTERN (substring match). By default, print
the matching lines.
"""
tmpdir.chdir()
tmpdir.join('one').write(b'one\n', 'wb')
tmpdir.join('two').write(b'two\n', 'wb')
assertcall('o', input='zero\n') == ('zero\n', '', 0)
assertcall('o', 'one', input='zero\n') == ('one\n', '', 0)
# prefix output lines with the file name when working with multiple files
assertcall('o', 'one', 'two') == ('one:one\ntwo:two\n', '', 0)
assertcall('e', input='ONE') == ('', '', 1)
assertcall('E', input='one') == ('', '', 1)
deftest_status(call, tmpdir):
"""Exit with status 0 if any matches are found and with 1 if not.
Exit with status 2 in case of error.
"""
tmpdir.chdir()
assertcall('o', input='one\n') == ('one\n', '', 0)
assertcall('x', input='one\n') == ('', '', 1)
# three doesn't exist
out, err, status=call('x', 'three')
assertout==''
assertstatus==2
# no pattern is given
out, err, status=call()
assertout==''
assertstatus==2
deftest_newlines(call, tmpdir):
"""Lines are terminated by a line feed character ('\\n'). If the last line
to be output does not end in a newline, append one. If there are no lines
(i.e. zero bytes), don't output anything.
"""
assertcall('t', input='one\ntwo\r\nthree\n') == ('two\r\nthree\n', '', 0)
assertcall('', input='one') == ('one\n', '', 0)
assertcall('', input='\n') == ('\n', '', 0)
assertcall('') == ('', '', 1)
deftest_no_encoding(call, tmpdir):
"""match.py is not encoding-aware; it operates on binary data (bytes in,
bytes out).
"""
ae_bytes='aeæ\n'.encode('latin-1')
tmpdir.chdir()
tmpdir.join('one').write(ae_bytes, 'wb')
assertcall('a', input=ae_bytes, encoding=None) == (ae_bytes, b'', 0)
assertcall('a', 'one', encoding=None) == (ae_bytes, b'', 0)
deftest_only_matching(call):
"""`-o`, `--only-matching`: print only the matched (non-empty) parts of
a matching line, with each such part on a separate output line.
"""
assertcall('-o', '', input='zero') == ('', '', 0)
assertcall('-o', 'e', input='one') == ('e\n', '', 0)
assertcall('-o', 'e', input='two') == ('', '', 1)
assertcall('-o', 'e', input='three') == ('e\ne\n', '', 0)
deftest_invert_match(call):
"""`-v`, `--invert-match`: invert the sense of matching, to select
non-matching lines.
"""
assertcall('-v', '', input='zero') == ('', '', 1)
assertcall('-v', 'e', input='one') == ('', '', 1)
assertcall('-v', 'e', input='two') == ('two\n', '', 0)
assertcall('-v', 'e', input='one\ntwo\n') == ('two\n', '', 0)
deftest_ignore_case(call):
"""`-i`, `--ignore-case`: ignore case distinctions in both the PATTERN
and the input files.
"""
assertcall('-i', '', input='zero') == ('zero\n', '', 0)
assertcall('-i', 'e', input='one') == ('one\n', '', 0)
assertcall('-i', 'e', input='ONE') == ('ONE\n', '', 0)
assertcall('-i', 'E', input='one') == ('one\n', '', 0)
assertcall('-i', 'E', input='ONE') == ('ONE\n', '', 0)
assertcall('-i', 'e', input='two') == ('', '', 1)
deftest_regexp(call, tmpdir):
"""`-e PATTERN`, `--regexp=PATTERN`: use PATTERN as the pattern. This
can be used to specify multiple search patterns, or to protect a pattern
beginning with a hyphen (-).
"""
assertcall('-e', '-o', input='-one') == ('-one\n', '', 0)
assertcall('-o', '-e', 'e', '-e', 'o', input='one') == ('o\ne\n', '', 0)
# when -e is used, all arguments are FILE(s) (there's no PATTERN)
tmpdir.chdir()
tmpdir.join('e').write('three')
assertcall('e', '-e', 'e', input='one') == ('three\n', '', 0)
deftest_no_filename(call, tmpdir):
"""`-h`, `--no-filename`: suppress the prefixing of file names on output.
This is the default when there is only one file (or only standard input)
to search.
"""
tmpdir.chdir()
tmpdir.join('one').write(b'one\n', 'wb')
tmpdir.join('two').write(b'two\n', 'wb')
assertcall('-h', 'o', 'one', 'two') == ('one\ntwo\n', '', 0)