11import unittest
2- from swift_code_metrics ._metrics import Framework , Dependency , Metrics , SyntheticData , FrameworkData
2+ from swift_code_metrics ._metrics import Framework , Dependency , Metrics , SyntheticData , FrameworkData , SubModule
33from swift_code_metrics ._parser import SwiftFile
44from functional import seq
55
1717 is_test = False
1818)
1919
20+ example_file2 = SwiftFile (
21+ path = '/my/path/class.swift' ,
22+ framework_name = 'Test' ,
23+ loc = 1 ,
24+ imports = ['Foundation' , 'dep1' , 'dep2' ],
25+ interfaces = ['prot1' , 'prot2' , 'prot3' , 'prot4' ,
26+ 'prot5' , 'prot6' , 'prot7' , 'prot8' ],
27+ structs = ['struct1' , 'struct2' ],
28+ classes = ['class1' , 'class2' ],
29+ methods = ['meth1' , 'meth2' , 'meth3' , 'testMethod' ],
30+ n_of_comments = 7 ,
31+ is_shared = False ,
32+ is_test = False
33+ )
34+
2035
2136class FrameworkTests (unittest .TestCase ):
2237
2338 def setUp (self ):
2439 self .frameworks = [Framework ('BusinessLogic' ), Framework ('UIKit' ), Framework ('Other' )]
2540 self .framework = Framework ('AwesomeName' )
26- self .framework .raw_files ['Group1' ] = {}
27- self .framework .raw_files ['Group1' ]['File1' ] = example_swiftfile
28- self .framework .raw_files ['Group1' ]['File2' ] = example_swiftfile
41+ self .framework .submodule .files = [example_swiftfile , example_file2 ]
2942 seq (self .frameworks ) \
3043 .for_each (lambda f : self .framework .append_import (f ))
3144
@@ -119,7 +132,7 @@ def test_distance_main_sequence(self):
119132 is_shared = False ,
120133 is_test = False
121134 )
122- self .app_layer .raw_files [ 'File' ] = example_file
135+ self .app_layer .submodule . files . append ( example_file )
123136
124137 self .assertAlmostEqual (0.286 ,
125138 Metrics .distance_main_sequence (self .app_layer , self .frameworks ),
@@ -138,23 +151,7 @@ def test_abstractness_no_concretes(self):
138151 self .assertEqual (0 , Metrics .abstractness (self .foundation_kit ))
139152
140153 def test_abstractness_concretes (self ):
141-
142- example_file = SwiftFile (
143- path = '/my/path/class.swift' ,
144- framework_name = 'Test' ,
145- loc = 1 ,
146- imports = ['Foundation' , 'dep1' , 'dep2' ],
147- interfaces = ['prot1' , 'prot2' , 'prot3' , 'prot4' ,
148- 'prot5' , 'prot6' , 'prot7' , 'prot8' ],
149- structs = ['struct1' , 'struct2' ],
150- classes = ['class1' , 'class2' ],
151- methods = ['meth1' , 'meth2' , 'meth3' , 'testMethod' ],
152- n_of_comments = 7 ,
153- is_shared = False ,
154- is_test = False
155- )
156-
157- self .foundation_kit .raw_files ['File' ] = example_file
154+ self .foundation_kit .submodule .files .append (example_file2 )
158155 self .assertEqual (2 , Metrics .abstractness (self .foundation_kit ))
159156
160157 def test_fan_in_test_frameworks (self ):
@@ -313,7 +310,7 @@ def test_init_swift_file(self):
313310 def test_append_framework (self ):
314311 test_framework = Framework ('Test' )
315312 test_framework .append_import (Framework ('Imported' ))
316- test_framework .raw_files [ 'File' ] = example_swiftfile
313+ test_framework .submodule . files . append ( example_swiftfile )
317314
318315 self .framework_data .append_framework (test_framework )
319316 self .assertEqual (2 , self .framework_data .loc )
@@ -350,5 +347,62 @@ def test_as_dict(self):
350347 self .assertEqual (expected_dict , self .framework_data .as_dict )
351348
352349
350+ class SubModuleTests (unittest .TestCase ):
351+
352+ def setUp (self ):
353+ self .submodule = SubModule (
354+ name = "BusinessModule" ,
355+ files = [example_swiftfile ],
356+ submodules = [
357+ SubModule (
358+ name = "Helper" ,
359+ files = [example_file2 ],
360+ submodules = []
361+ )
362+ ]
363+ )
364+
365+ def test_n_of_files (self ):
366+ self .assertEqual (2 , self .submodule .n_of_files )
367+
368+ def test_data (self ):
369+ data = SyntheticData (
370+ loc = 2 ,
371+ noc = 14 ,
372+ number_of_interfaces = 11 ,
373+ number_of_concrete_data_structures = 6 ,
374+ number_of_methods = 8 ,
375+ number_of_tests = 2
376+ )
377+ self .assertEqual (data , self .submodule .data )
378+
379+ def test_empty_data (self ):
380+ data = SyntheticData (
381+ loc = 0 ,
382+ noc = 0 ,
383+ number_of_interfaces = 0 ,
384+ number_of_concrete_data_structures = 0 ,
385+ number_of_methods = 0 ,
386+ number_of_tests = 0
387+ )
388+ self .assertEqual (data , SubModule (name = "" , files = [], submodules = []).data )
389+
390+ def test_dict_repr (self ):
391+ self .assertEqual ({
392+ "BusinessModule" : {
393+ "n_of_files" : 2 ,
394+ "metric" : {
395+ "loc" : 2 ,
396+ "n_a" : 11 ,
397+ "n_c" : 6 ,
398+ "noc" : 14 ,
399+ "nom" : 8 ,
400+ "not" : 2 ,
401+ "poc" : 87.5
402+ }
403+ }
404+ }, self .submodule .as_dict )
405+
406+
353407if __name__ == '__main__' :
354408 unittest .main ()
0 commit comments