Przeglądaj źródła

Merge pull request #37 from noricor/linelength

Add new linelength plugin
Andrey 4 lat temu
rodzic
commit
efce4239cb

+ 15 - 0
metrixpp/ext/std/code/longlines.ini

@@ -0,0 +1,15 @@
+;
+;    Metrix++, Copyright 2009-2019, Metrix++ Project
+;    Link: https://github.com/metrixplusplus/metrixplusplus
+;
+;    This file is a part of Metrix++ Tool.
+;
+
+[Plugin]
+version: 1.0
+package: std.code
+module:  longlines
+class:   Plugin
+depends: None
+actions: collect
+enabled: True

+ 42 - 0
metrixpp/ext/std/code/longlines.py

@@ -0,0 +1,42 @@
+#
+#    Metrix++, Copyright 2009-2019, Metrix++ Project
+#    Link: https://github.com/metrixplusplus/metrixplusplus
+#
+#    This file is a part of Metrix++ Tool.
+#
+
+from metrixpp.mpp import api
+import re
+
+class Plugin(api.Plugin,
+             api.IConfigurable,
+             api.Child,
+             api.MetricPluginMixin):
+
+    def declare_configuration(self, parser):
+        parser.add_option("--std.code.longlines", "--scll",
+            action="store_true", default=False,
+            help="Enables collection of long lines metric [default: %default]")
+        parser.add_option("--std.code.longlines.limit", "--sclll",
+            default=80,
+            help="Modifies the limit for maximum line-length [default: %default]")
+
+    def configure(self, options):
+        self.is_active_ll = options.__dict__['std.code.longlines']
+        self.threshold = int(options.__dict__['std.code.longlines.limit'])
+
+    def initialize(self):
+        pattern_to_search = r'''.{%s,}''' % (self.threshold + 1)
+        self.declare_metric(
+                self.is_active_ll,
+                self.Field('numbers', int),
+                re.compile(pattern_to_search),
+                marker_type_mask=api.Marker.T.CODE,
+                region_type_mask=api.Region.T.ANY,
+                exclude_subregions=True)
+
+        super(Plugin, self).initialize(fields=self.get_fields())
+
+        if self.is_active_ll == True:
+            self.subscribe_by_parents_interface(api.ICode)
+

+ 15 - 0
metrixpp/tests/general/test_basic.py

@@ -300,6 +300,21 @@ class Test(tests.common.TestCase):
         runner = tests.common.ToolRunner('view', ['--format=txt'], prefix='txt')
         self.assertExec(runner.run())
 
+    def test_std_longlines_metrics(self):
+
+        runner = tests.common.ToolRunner('collect',
+                                         ['--std.code.longlines',
+                                          '--std.code.longlines.limit=50'])
+        self.assertExec(runner.run())
+
+        runner = tests.common.ToolRunner('view',
+                                         ['--nest-regions', '--format=txt'],
+                                         prefix='nest_per_file',
+                                         dirs_list=['./simple.cpp'])
+        self.assertExec(runner.run())
+
+        runner = tests.common.ToolRunner('view', ['--format=txt'], prefix='txt')
+        self.assertExec(runner.run())
 
     def test_std_complexity_maxindent(self):
 

+ 6 - 0
metrixpp/tests/general/test_basic/test_help_collect_default_stdout.gold.txt

@@ -68,6 +68,12 @@ Options:
                         Enables collection of total lines metric (per region
                         detalization) - number of any type of lines (blank,
                         code, comments, etc.)[default: False]
+  --std.code.longlines, --scll
+                        Enables collection of long lines metric [default:
+                        False]
+  --std.code.longlines.limit=STD.CODE.LONGLINES.LIMIT, --sclll=STD.CODE.LONGLINES.LIMIT
+                        Modifies the limit for maximum line-length [default:
+                        80]
   --std.code.magic.numbers, --scmn
                         Enables collection of magic numbers metric [default:
                         False]

+ 0 - 0
metrixpp/tests/general/test_basic/test_std_longlines_metrics_collect_default_stdout.gold.txt


+ 99 - 0
metrixpp/tests/general/test_basic/test_std_longlines_metrics_view_nest_per_file_stdout.gold.txt

@@ -0,0 +1,99 @@
+./simple.cpp:0: info: Metrics per '__global__' region
+	Region name    : __global__
+	Region type    : global
+	Offsets        : 0-697
+	Line numbers   : 1-71
+	Modified       : None
+	std.code.longlines:numbers: 0
+
+.   ./simple.cpp:4: info: Metrics per 'hmm' region
+    	Region name    : hmm
+    	Region type    : namespace
+    	Offsets        : 2-696
+    	Line numbers   : 3-70
+    	Modified       : None
+    	std.code.longlines:numbers: 0
+
+.   .   ./simple.cpp:9: info: Metrics per 'A' region
+        	Region name    : A
+        	Region type    : class
+        	Offsets        : 94-692
+        	Line numbers   : 9-68
+        	Modified       : None
+        	std.code.longlines:numbers: 0
+
+.   .   .   ./simple.cpp:12: info: Metrics per 'A' region
+            	Region name    : A
+            	Region type    : function
+            	Offsets        : 106-252
+            	Line numbers   : 12-23
+            	Modified       : None
+            	std.code.longlines:numbers: 0
+
+.   .   .   ./simple.cpp:26: info: Metrics per 'func' region
+            	Region name    : func
+            	Region type    : function
+            	Offsets        : 256-405
+            	Line numbers   : 26-40
+            	Modified       : None
+            	std.code.longlines:numbers: 0
+
+.   .   .   .   ./simple.cpp:28: info: Metrics per 'embeded' region
+                	Region name    : embeded
+                	Region type    : class
+                	Offsets        : 285-391
+                	Line numbers   : 28-38
+                	Modified       : None
+                	std.code.longlines:numbers: 0
+
+.   .   .   .   .   ./simple.cpp:30: info: Metrics per 'embeded' region
+                    	Region name    : embeded
+                    	Region type    : function
+                    	Offsets        : 306-387
+                    	Line numbers   : 30-37
+                    	Modified       : None
+                    	std.code.longlines:numbers: 0
+
+.   .   .   ./simple.cpp:42: info: Metrics per 'func_to_be_removed_in_new_sources' region
+            	Region name    : func_to_be_removed_in_new_sources
+            	Region type    : function
+            	Offsets        : 408-596
+            	Line numbers   : 42-56
+            	Modified       : None
+            	std.code.longlines:numbers: 1
+
+.   .   .   .   ./simple.cpp:44: info: Metrics per 'embeded' region
+                	Region name    : embeded
+                	Region type    : class
+                	Offsets        : 466-577
+                	Line numbers   : 44-54
+                	Modified       : None
+                	std.code.longlines:numbers: 0
+
+.   .   .   .   .   ./simple.cpp:46: info: Metrics per 'embeded' region
+                    	Region name    : embeded
+                    	Region type    : function
+                    	Offsets        : 487-573
+                    	Line numbers   : 46-53
+                    	Modified       : None
+                    	std.code.longlines:numbers: 0
+
+.   .   .   ./simple.cpp:58: info: Metrics per 'never' region
+            	Region name    : never
+            	Region type    : function
+            	Offsets        : 599-669
+            	Line numbers   : 58-65
+            	Modified       : None
+            	std.code.longlines:numbers: 0
+
+./simple.cpp:: info: Overall metrics for 'std.code.longlines:numbers' metric
+	Average        : 0.09090909
+	Minimum        : 0
+	Maximum        : 1
+	Total          : 1.0
+	Distribution   : 11 regions in total (including 0 suppressed)
+	  Metric value : Ratio : R-sum : Number of regions
+	             0 : 0.909 : 0.909 : 10	||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+	             1 : 0.091 : 1.000 :  1	|||||||||
+
+

+ 15 - 0
metrixpp/tests/general/test_basic/test_std_longlines_metrics_view_txt_stdout.gold.txt

@@ -0,0 +1,15 @@
+./:: info: Overall metrics for 'std.code.longlines:numbers' metric
+	Average        : 0.0625
+	Minimum        : 0
+	Maximum        : 1
+	Total          : 1.0
+	Distribution   : 16 regions in total (including 0 suppressed)
+	  Metric value : Ratio : R-sum : Number of regions
+	             0 : 0.938 : 0.938 : 15	|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+	             1 : 0.062 : 1.000 :  1	||||||
+
+./:: info: Directory content:
+	File           : file_deleted_in_new_sources.cpp
+	File           : simple.cpp
+
+