Browse Source

more tests for api docs

avkonst 11 years ago
parent
commit
f9b90d5c16

+ 2 - 2
mainline/mpp/api.py

@@ -32,7 +32,7 @@ class Data(object):
 
 
     def __init__(self):
     def __init__(self):
         self.data = {}
         self.data = {}
-
+        
     def get_data(self, namespace, field):
     def get_data(self, namespace, field):
         if namespace not in self.data.keys():
         if namespace not in self.data.keys():
             return None
             return None
@@ -44,7 +44,7 @@ class Data(object):
         if namespace not in self.data:
         if namespace not in self.data:
             self.data[namespace] = {}
             self.data[namespace] = {}
         self.data[namespace][field] = value
         self.data[namespace][field] = value
-    
+
     def iterate_namespaces(self):
     def iterate_namespaces(self):
         for namespace in self.data.keys():
         for namespace in self.data.keys():
             yield namespace
             yield namespace

+ 12 - 14
mainline/tests/system/test_api_tutorial.py

@@ -24,6 +24,17 @@ import os
 import tests.common
 import tests.common
 
 
 class Test(tests.common.TestCase):
 class Test(tests.common.TestCase):
+    
+    def setUp(self):
+        tests.common.TestCase.setUp(self)
+        self.METRIXPLUSPLUS_PATH = None
+        if 'METRIXPLUSPLUS_PATH' in os.environ.keys():
+            self.METRIXPLUSPLUS_PATH = os.environ['METRIXPLUSPLUS_PATH']
+        
+    def tearDown(self):
+        if self.METRIXPLUSPLUS_PATH != None:
+            os.environ['METRIXPLUSPLUS_PATH'] = self.METRIXPLUSPLUS_PATH
+        tests.common.TestCase.tearDown(self)
 
 
     def test_metric_plugin_api(self):
     def test_metric_plugin_api(self):
         
         
@@ -32,9 +43,6 @@ class Test(tests.common.TestCase):
         # files generated by this test are used by project documents page
         # files generated by this test are used by project documents page
         # so, if the test is changed, html docs should be updated accordingly
         # so, if the test is changed, html docs should be updated accordingly
         #
         #
-        METRIXPLUSPLUS_PATH = None
-        if 'METRIXPLUSPLUS_PATH' in os.environ.keys():
-            METRIXPLUSPLUS_PATH = os.environ['METRIXPLUSPLUS_PATH']
         
         
         for step in range(8):
         for step in range(8):
             opts = ['--log-level=INFO']
             opts = ['--log-level=INFO']
@@ -58,9 +66,6 @@ class Test(tests.common.TestCase):
                                          check_stderr=[(0, -1)])
                                          check_stderr=[(0, -1)])
             self.assertExec(runner.run())
             self.assertExec(runner.run())
 
 
-        if METRIXPLUSPLUS_PATH != None:
-            os.environ['METRIXPLUSPLUS_PATH'] = METRIXPLUSPLUS_PATH
-
     def test_runable_plugin_api(self):
     def test_runable_plugin_api(self):
         
         
         #
         #
@@ -68,10 +73,6 @@ class Test(tests.common.TestCase):
         # files generated by this test are used by project documents page
         # files generated by this test are used by project documents page
         # so, if the test is changed, html docs should be updated accordingly
         # so, if the test is changed, html docs should be updated accordingly
         #
         #
-        METRIXPLUSPLUS_PATH = None
-        if 'METRIXPLUSPLUS_PATH' in os.environ.keys():
-            METRIXPLUSPLUS_PATH = os.environ['METRIXPLUSPLUS_PATH']
-        
         runner = tests.common.ToolRunner('collect',
         runner = tests.common.ToolRunner('collect',
                                          ['--std.code.lines.total',
                                          ['--std.code.lines.total',
                                           '--log-level=INFO'],
                                           '--log-level=INFO'],
@@ -88,7 +89,7 @@ class Test(tests.common.TestCase):
                                          use_prev=True)
                                          use_prev=True)
         self.assertExec(runner.run())
         self.assertExec(runner.run())
         
         
-        for step in range(2):
+        for step in range(3):
             step = step + 1
             step = step + 1
             os.environ['METRIXPLUSPLUS_PATH'] = os.path.join(os.path.dirname(os.path.abspath(__file__)),
             os.environ['METRIXPLUSPLUS_PATH'] = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                                              'test_api_tutorial', 'ext', 'compare_step' + str(step))
                                                              'test_api_tutorial', 'ext', 'compare_step' + str(step))
@@ -99,9 +100,6 @@ class Test(tests.common.TestCase):
                                              use_prev=True)
                                              use_prev=True)
             self.assertExec(runner.run())
             self.assertExec(runner.run())
 
 
-        if METRIXPLUSPLUS_PATH != None:
-            os.environ['METRIXPLUSPLUS_PATH'] = METRIXPLUSPLUS_PATH
-
 if __name__ == '__main__':
 if __name__ == '__main__':
     unittest.main()
     unittest.main()
     
     

+ 3 - 11
mainline/tests/system/test_api_tutorial/ext/compare_step2/myext/compare.py

@@ -27,23 +27,15 @@ class Plugin(mpp.api.Plugin, mpp.api.IRunable):
         # get data file reader using standard metrix++ plugin
         # get data file reader using standard metrix++ plugin
         loader = self.get_plugin('mpp.dbf').get_loader()
         loader = self.get_plugin('mpp.dbf').get_loader()
         
         
-        # configure default paths if there are no paths specified
-        # in command line
-        paths = None
-        if len(args) == 0:
-            paths = [""]
-        else:
-            paths = args
-
-        # iterate and print files for every path in paths
+        # iterate and print file length for every path in args
         exit_code = 0
         exit_code = 0
-        for path in paths:
+        for path in (args if len(args) > 0 else [""]):
             file_iterator = loader.iterate_file_data(path)
             file_iterator = loader.iterate_file_data(path)
             if file_iterator == None:
             if file_iterator == None:
                 mpp.utils.report_bad_path(path)
                 mpp.utils.report_bad_path(path)
                 exit_code += 1
                 exit_code += 1
                 continue
                 continue
             for file_data in file_iterator:
             for file_data in file_iterator:
-                print file_data.get_path(), file_data.get_checksum()
+                print file_data.get_path()
         return exit_code
         return exit_code
 
 

+ 35 - 23
mainline/tests/system/test_api_tutorial/ext/compare_step3/myext/compare.py

@@ -19,7 +19,6 @@
 
 
 import mpp.api
 import mpp.api
 import mpp.utils
 import mpp.utils
-# load standard routines to print to stdout
 import mpp.cout
 import mpp.cout
 
 
 class Plugin(mpp.api.Plugin, mpp.api.IRunable):
 class Plugin(mpp.api.Plugin, mpp.api.IRunable):
@@ -29,34 +28,47 @@ class Plugin(mpp.api.Plugin, mpp.api.IRunable):
         # get previous db file loader
         # get previous db file loader
         loader_prev = self.get_plugin('mpp.dbf').get_loader_prev()
         loader_prev = self.get_plugin('mpp.dbf').get_loader_prev()
         
         
-        paths = None
-        if len(args) == 0:
-            paths = [""]
-        else:
-            paths = args
-
         exit_code = 0
         exit_code = 0
-        for path in paths:
+        for path in (args if len(args) > 0 else [""]):
+            added_lines = 0
             file_iterator = loader.iterate_file_data(path)
             file_iterator = loader.iterate_file_data(path)
             if file_iterator == None:
             if file_iterator == None:
                 mpp.utils.report_bad_path(path)
                 mpp.utils.report_bad_path(path)
                 exit_code += 1
                 exit_code += 1
                 continue
                 continue
             for file_data in file_iterator:
             for file_data in file_iterator:
-                # compare here with previous and notify about changes
-                file_data_prev = loader_prev.load_file_data(file_data.get_path())
-                if file_data_prev == None:
-                    mpp.cout.notify(file_data.get_path(), '', mpp.cout.SEVERITY_INFO,
-                                    'File has been added',
-                                    [('Checksum', file_data.get_checksum())])
-                elif file_data.get_checksum() != file_data_prev.get_checksum():
-                    mpp.cout.notify(file_data.get_path(), '', mpp.cout.SEVERITY_INFO,
-                                    'File has been changed',
-                                    [('New checksum', file_data.get_checksum()),
-                                     ('Old checksum', file_data_prev.get_checksum())])
-                else:
-                    mpp.cout.notify(file_data.get_path(), '', mpp.cout.SEVERITY_INFO,
-                                    'File has not been changed',
-                                    [('Checksum', file_data.get_checksum())])
+                added_lines += self._compare_file(file_data, loader, loader_prev)
+            mpp.cout.notify(path, '', mpp.cout.SEVERITY_INFO,
+                            "Change trend report",
+                            [('Added lines', added_lines)])
         return exit_code
         return exit_code
 
 
+    def _compare_file(self, file_data, loader, loader_prev):
+        # compare file with previous and return number of new lines
+        file_data_prev = loader_prev.load_file_data(file_data.get_path())
+        if file_data_prev == None:
+            return self._sum_file_regions_lines(file_data)
+        elif file_data.get_checksum() != file_data_prev.get_checksum():
+            return self._compare_file_regions(file_data, file_data_prev)
+
+    def _sum_file_regions_lines(self, file_data):
+        # just sum up the metric for all regions
+        result = 0
+        for region in file_data.iterate_regions():
+            result += region.get_data('std.code.lines', 'total')
+    
+    def _compare_file_regions(self, file_data, file_data_prev):
+        # compare every region with previous and return number of new lines
+        matcher = mpp.utils.FileRegionsMatcher(file_data, file_data_prev)
+        result = 0
+        for region in file_data.iterate_regions():
+            if matcher.is_matched(region.get_id()) == False:
+                # if added region, just add the lines
+                result += region.get_data('std.code.lines', 'total')
+            elif matcher.is_modified(region.get_id()):
+                # if modified, add the difference in lines
+                region_prev = file_data_prev.get_region(
+                    matcher.get_prev_id(region.get_id()))
+                result += (region.get_data('std.code.lines', 'total') -
+                           region_prev.get_data('std.code.lines', 'total'))
+        return result

+ 1 - 1
mainline/tests/system/test_api_tutorial/test_runable_plugin_api_compare_step2_stdout.gold.txt

@@ -1 +1 @@
-./test.cpp 579671338
+./test.cpp

+ 2 - 0
mainline/tests/system/test_api_tutorial/test_runable_plugin_api_compare_step3_stderr.gold.txt

@@ -0,0 +1,2 @@
+[LOG]: WARNING:	Logging enabled with INFO level
+[LOG]: WARNING:	Done (1 seconds). Exit code: 0

+ 3 - 0
mainline/tests/system/test_api_tutorial/test_runable_plugin_api_compare_step3_stdout.gold.txt

@@ -0,0 +1,3 @@
+:: info: Change trend report
+	Added lines    : 7
+