This repository was archived by the owner on Mar 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_grep_ab.py
More file actions
139 lines (123 loc) · 4.92 KB
/
test_grep_ab.py
File metadata and controls
139 lines (123 loc) · 4.92 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
from grep_ab import my_function
import unittest
import os
class TestsGrepAB(unittest.TestCase):
def setUp(self):
self.command = "grep -A {A} -B {B} '{pattern}' {file}"
def test_passing_regular_expression(self):
A = 4
B = 3
pattern = r'^-'
file = 'test_data/test_ok_file_1.txt'
out = os.popen(self.command.format(A=A, B=B,
pattern=pattern, file=file))
string_cli_out = out.read()
string_my_out = my_function(file, pattern, A, B)
self.assertEquals(string_cli_out, string_my_out)
def test_passing_not_matched_pattern(self):
A = 4
B = 3
pattern = r'^"I am'
file = 'test_data/test_ok_file_1.txt'
out = os.popen(self.command.format(A=A, B=B,
pattern=pattern, file=file))
string_cli_out = out.read()
string_my_out = my_function(file, pattern, A, B)
self.assertEquals(string_cli_out, string_my_out)
def test_ok(self):
A = 3
B = 3
pattern = r'ere'
file = 'test_data/test_ok_file_1.txt'
out = os.popen(self.command.format(A=A, B=B,
pattern=pattern, file=file))
string_cli_out = out.read()
string_my_out = my_function(file, pattern, A, B)
self.assertEquals(string_cli_out, string_my_out)
def test_ok_only_after(self):
A = 5
B = 0
pattern = r'ere'
file = 'test_data/test_ok_file_1.txt'
out = os.popen(self.command.format(A=A, B=B,
pattern=pattern, file=file))
string_cli_out = out.read()
string_my_out = my_function(file, pattern, A, B)
self.assertEquals(string_cli_out, string_my_out)
def test_ok_only_before(self):
A = 0
B = 5
pattern = r'ere'
file = 'test_data/test_ok_file_1.txt'
out = os.popen(self.command.format(A=A, B=B,
pattern=pattern, file=file))
string_cli_out = out.read()
string_my_out = my_function(file, pattern, A, B)
self.assertEquals(string_cli_out, string_my_out)
def test_match_all(self):
A = 3
B = 3
pattern = r'.*'
file = 'test_data/test_ok_file_1.txt'
out = os.popen(self.command.format(A=A, B=B,
pattern=pattern, file=file))
string_cli_out = out.read()
string_my_out = my_function(file, pattern, A, B)
self.assertEquals(string_cli_out, string_my_out)
@unittest.SkipTest
def test_match_separator(self):
A = 3
B = 3
pattern = r'\-\-'
file = 'test_data/test_ok_file_1.txt'
out = os.popen(self.command.format(A=A, B=B,
pattern=pattern, file=file))
string_cli_out = out.read()
string_my_out = my_function(file, pattern, A, B)
self.assertEquals(string_cli_out, string_my_out)
def test_only_matched_lines(self):
A = 0
B = 0
pattern = r'Interpret'
file = 'test_data/test_ok_file_1.txt'
out = os.popen(self.command.format(A=A, B=B,
pattern=pattern, file=file))
string_cli_out = out.read()
string_my_out = my_function(file, pattern, A, B)
self.assertEquals(string_cli_out, string_my_out)
def test_empty_file(self):
A = 4
B = 3
pattern = r'second'
file = 'test_data/test_empty_file.txt'
out = os.popen(self.command.format(A=A, B=B,
pattern=pattern, file=file))
string_cli_out = out.read()
string_my_out = my_function(file, pattern, A, B)
self.assertEquals(string_cli_out, string_my_out)
def test_non_existed_file(self):
with self.assertRaises(IOError):
my_function('test_data/test_non_existed_file.txt', 'test')
def test_all_lines_same(self):
A = 3
B = 3
pattern = r'same'
file = 'test_data/test_all_lines_same.txt'
out = os.popen(self.command.format(A=A, B=B,
pattern=pattern, file=file))
string_cli_out = out.read()
string_my_out = my_function(file, pattern, A, B)
self.assertEquals(string_cli_out, string_my_out)
def test_empty_pattern(self):
A = 3
B = 3
pattern = r''
file = 'test_data/test_ok_file_1.txt'
out = os.popen(self.command.format(A=A, B=B,
pattern=pattern, file=file))
string_cli_out = out.read()
string_my_out = my_function(file, pattern, A, B)
self.assertEquals(string_cli_out, string_my_out)
if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromTestCase(TestsGrepAB)
unittest.TextTestRunner(verbosity=2).run(suite)