Repository navigation
Expand file tree
/
Copy pathtests.py
More file actions
83 lines (68 loc) · 3.12 KB
/
Copy pathtests.py
File metadata and controls
83 lines (68 loc) · 3.12 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
"""tests.py: handles some example unit-tests, many tests can be added here."""
import unittest
import re_handling
import output_handler
from cStringIO import StringIO
import sys
from contextlib import contextmanager
results = list()
results.append({'file_name': 'file', 'no_line': 0, 'start_pos': 3,
'matched_text': 'matched', 'line': 'actual line for matched'})
class TestForInterview(unittest.TestCase):
def test_re_handling_pass(self):
"""test regex pass"""
string_for_regex = 'ing'
regex = re_handling.ReHandling(string_for_regex)
self.assertEqual(regex.pattern.pattern, string_for_regex, 'regex test failed')
def test_re_handling_escape_char(self):
"""test regex with escape char"""
string_for_regex = '%sing'
regex = re_handling.ReHandling(string_for_regex)
self.assertEqual(regex.pattern.pattern, '\\' + string_for_regex, 'regex test failed')
def test_re_handling_search_pass(self):
"""test regex should find search"""
string_for_regex = 'ing'
string_to_search_in = 'find ing in data'
regex = re_handling.ReHandling(string_for_regex)
res = regex.search_regex_in_data(string_to_search_in).next()
self.assertEqual(res.start(), 5, 'regex test failed')
self.assertEqual(res.string, string_to_search_in, 'regex test failed')
def test_re_handling_search_fail(self):
"""test regex should not find search"""
string_for_regex = 'inggg'
string_to_search_in = 'find ing in data'
regex = re_handling.ReHandling(string_for_regex)
res = regex.search_regex_in_data(string_to_search_in)
self.assertEqual(len(list(res)), 0, 'regex test failed')
def test_regular_output(self):
"""test regular output"""
expected_string = '(file:0) actual line for matched'
local_output = handle_stdout(output_handler.RegularOutput())
self.assertEqual(local_output, expected_string, 'output test failed')
def test_underscore_output(self):
"""test underscore output"""
expected_string = '(file:0) actual line for matched\n ^^^^^^^'
local_output = handle_stdout(output_handler.UnderscoreOutput())
self.assertEqual(local_output, expected_string, 'output test failed')
def test_color_output(self):
"""test color output"""
expected_string = '(file:0) actual line for matched'
local_output = handle_stdout(output_handler.ColorOutput())
self.assertEqual(local_output, expected_string, 'output test failed')
def test_machine_output(self):
"""test machine output"""
expected_string = 'file:0:3:matched'
local_output = handle_stdout(output_handler.MachineOutput())
self.assertEqual(local_output, expected_string, 'output test failed')
def handle_stdout(output):
saved_stdout = sys.stdout
try:
out = StringIO()
sys.stdout = out
output.print_output(results)
local_output = out.getvalue().strip()
finally:
sys.stdout = saved_stdout
return local_output
if __name__ == '__main__':
unittest.main()