-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_command.py
148 lines (105 loc) · 3.37 KB
/
test_command.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
from mock import patch
from src.command import FileSystem, STDOut, LCOMFactory, PrinterFactory, \
Runner, Printer
from src.lcom import LCOM4, LCOMAlgorithm
from src.reflection import Reflection
class FakeReflection(Reflection):
def module_name(self):
return 'foo'
def name(self):
return 'FakeReflection'
def classes(self):
return [self]
class FakeFileSystem(object):
def __init__(self):
self.result = []
def find(self, path, filename=None):
return self.result
class FakeLCOM(LCOMAlgorithm):
def __init__(self):
self.result = 1
def name(self):
return 'FakeLCOM'
def calculate(self, ref):
return self.result
class FakePrinter(Printer):
def __init__(self):
self.output = []
def render(self, algorithm, classes, average):
self.output.append((
algorithm,
classes,
average
))
class TestFileSystem(object):
def setup_method(self):
self.fs = FileSystem()
def test_find_scans_file(self):
result = self.fs.find('src/command.py')
result = {ref.name() for ref in result}
assert result == {
'src.command',
}
def test_find_scans_directory(self):
result = self.fs.find('src/')
result = {ref.name() for ref in result}
assert result == {
'src',
'src.command',
'src.lcom',
'src.reflection'
}
def test_find_can_filter_by_file_name(self):
result = self.fs.find('src/', 'command.py')
result = {ref.name() for ref in result}
assert result == {'src.command'}
class TestLCOMAlgorithmFactory(object):
def test_create_lcom4(self):
result = LCOMFactory.create(LCOMFactory.LCOM4)
assert isinstance(result, LCOM4)
class TestSTDOutPrinter(object):
def test_print(self):
output = list()
def fnc(text):
text = text.strip()
output.append(text)
with patch('sys.stdout') as stdout:
stdout.write = fnc
STDOut().render('lcom0', [('Foo', 1)], 1.0)
expected = [
'Calculating LCOM using lcom0',
'',
'\n'.join([
'+---------+------+',
'| Method | LCOM |',
'+---------+------+',
'| Foo | 1 |',
'+---------+------+',
'| Average | 1.00 |',
'+---------+------+',
]),
'',
]
assert output == expected
class TestPrinterFactory(object):
def test_create_std(self):
result = PrinterFactory.create(PrinterFactory.STD)
assert isinstance(result, STDOut)
class TestRunner(object):
def setup_method(self):
self.fs = FakeFileSystem()
self.lcom = FakeLCOM()
self.printer = FakePrinter()
def test_handle_without_classes(self):
runner = Runner(self.fs, self.lcom, self.printer)
runner.handle('/foo')
assert self.printer.output == [('FakeLCOM', [], 0)]
def test_handle_with_classes(self):
self.fs.result = [FakeReflection()]
runner = Runner(self.fs, self.lcom, self.printer)
runner.handle(['/foo'])
assert self.printer.output == [(
'FakeLCOM',
[('FakeReflection', 1)],
1.0
)]