-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunittests.py
164 lines (126 loc) · 4.88 KB
/
unittests.py
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
164
import unittest
import os
from collections import defaultdict
from typing import Any, DefaultDict
from src import functions, parser
from io import StringIO
# Functions tests
class CatTest(unittest.TestCase):
def test(self):
cat = functions.Cat()
test_stream = StringIO()
cat.set_out_stream(test_stream)
cat.evaluate('test_files/test_1.txt')
self.assertEqual(test_stream.getvalue(), 'simple test')
class EchoTest(unittest.TestCase):
def test(self):
echo = functions.Echo()
test_stream_out = StringIO()
echo.set_out_stream(test_stream_out)
echo.evaluate('test', 1, 2, 2.93)
test_stream = echo.get_out_stream()
self.assertEqual(test_stream.getvalue(), 'test 1 2 2.93\n')
class WcTest(unittest.TestCase):
def test(self):
wc = functions.Wc()
test_stream_out = StringIO()
wc.set_out_stream(test_stream_out)
wc.evaluate('test_files/test_1.txt')
test_stream = wc.get_out_stream()
self.assertEqual(
test_stream.getvalue(),
' 1 2 11 test_files/test_1.txt\n\n')
class PwdTest(unittest.TestCase):
def test(self):
pwd = functions.Pwd()
test_stream_out = StringIO()
pwd.set_out_stream(test_stream_out)
pwd.evaluate()
test_stream = pwd.get_out_stream()
self.assertEqual(test_stream.getvalue(), os.getcwd()+'\n')
class GrepTest(unittest.TestCase):
def test(self):
grep = functions.Grep()
test_stream_out = StringIO()
grep.set_out_stream(test_stream_out)
grep.evaluate(' Please ', 'test_files/test_2.txt', '-A', '0', '-i')
test_stream = grep.get_out_stream()
self.assertEqual(
test_stream.getvalue()[:10], '2 library ')
# parser tests
class PublicParser(parser.Parser):
def split_by_tokens(self, command: str) -> list:
self._Parser__split_by_tokens(command)
return self._Parser__rez
def substitute_inside_2apostrophe(self) -> list:
self._Parser__substitute_inside_2apostrophe()
return self._Parser__rez
def join_inside_single_apostrophe(self) -> list:
self._Parser__join_inside_single_apostrophe()
return self._Parser__rez
def get_rez(self):
return self._Parser__rez
class ParserTestSplit(unittest.TestCase):
def test(self):
namespace: DefaultDict[str, Any] = defaultdict(lambda: '')
_parser = PublicParser(namespace)
self.assertEqual(
_parser.split_by_tokens('wc test_1.txt | echo'),
['wc', ' ', 'test_1.txt', ' ', '|', ' ', 'echo'])
class ParserTestSubstitute(unittest.TestCase):
def test(self):
namespace: DefaultDict[str, Any] = defaultdict(lambda: '')
_parser = PublicParser(namespace)
self.assertEqual(_parser.substitute_on_segment(
['wc', '\"', '$', 'test_1', '\"'], namespace),
['wc', '"', '', '"']
)
class ParserTestSubstituteInside2Apostrophe(unittest.TestCase):
def test(self):
namespace: DefaultDict[str, Any] = defaultdict(lambda: '')
namespace['test'] = 'test_1.txt'
_parser = PublicParser(namespace)
_parser.split_by_tokens('wc \"$test\" | echo')
self.assertEqual(
_parser.substitute_inside_2apostrophe(),
['wc', ' ', '"', 'test_1.txt', '"', ' ', '|', ' ', 'echo'])
class ParserTestJoin(unittest.TestCase):
def test(self):
namespace: DefaultDict[str, Any] = defaultdict(lambda: '')
_parser = PublicParser(namespace)
_parser.split_by_tokens('echo \'one two\'')
_parser.substitute_inside_2apostrophe()
self.assertEqual(
_parser.join_inside_single_apostrophe(),
['echo', ' ', 'one two', "'"]
)
class ParserTestParse(unittest.TestCase):
def test(self):
namespace: DefaultDict[str, Any] = defaultdict(lambda: '')
_parser = PublicParser(namespace)
_parser.parse('echo one two')
self.assertEqual(_parser.get_rez(), ['echo', 'one', 'two'])
namespace: DefaultDict[str, Any] = defaultdict(lambda: '')
namespace['test'] = 'test_files/test_1.txt'
_parser = PublicParser(namespace)
_parser.parse('echo \"$test two\"')
self.assertEqual(
_parser.get_rez(),
['echo', 'test_files/test_1.txt', 'two']
)
namespace: DefaultDict[str, Any] = defaultdict(lambda: '')
_parser = PublicParser(namespace)
_parser.parse('echo \"$test two\"')
self.assertEqual(
_parser.get_rez(),
['echo', 'two']
)
namespace: DefaultDict[str, Any] = defaultdict(lambda: '')
_parser = PublicParser(namespace)
_parser.parse('echo \'test two\'')
self.assertEqual(
_parser.get_rez(),
['echo', 'test two']
)
if __name__ == '__main__':
unittest.main()