Revision: 31d6b524cf2f
Author:   Pekka Klärck
Date:     Thu Nov 17 12:53:04 2011
Log: Build SuiteStatistics using separate SuiteStatisticsBuilder. Finally figured out a relatively clean solution! Will change TagStatistics to use separate builder too.
http://code.google.com/p/robotframework/source/detail?r=31d6b524cf2f

Modified:
 /src/robot/model/statistics.py
 /src/robot/model/stats.py
 /src/robot/model/suitestatistics.py

=======================================
--- /src/robot/model/statistics.py      Mon Nov 14 13:07:07 2011
+++ /src/robot/model/statistics.py      Thu Nov 17 12:53:04 2011
@@ -13,7 +13,7 @@
 #  limitations under the License.

 from .tagstatistics import TagStatistics
-from .suitestatistics import SuiteStatistics
+from .suitestatistics import SuiteStatisticsBuilder
 from .totalstatistics import TotalStatistics
 from .visitor import SuiteVisitor

@@ -23,10 +23,12 @@
     def __init__(self, suite, suite_stat_level=-1, tag_stat_include=None,
tag_stat_exclude=None, tag_stat_combine=None, tag_doc=None,
                  tag_stat_link=None):
+        suite_builder = SuiteStatisticsBuilder(suite_stat_level)
         self.tags = TagStatistics(suite.criticality, tag_stat_include,
                                   tag_stat_exclude, tag_stat_combine,
                                   tag_doc, tag_stat_link)
- self.suite = StatisticsBuilder(self.tags, suite_stat_level).build(suite)
+        suite.visit(StatisticsBuilder(suite_builder, self.tags))
+        self.suite = suite_builder.root
         self.total = TotalStatistics(self.suite)

     def visit(self, visitor):
@@ -35,30 +37,19 @@

 class StatisticsBuilder(SuiteVisitor):

-    def __init__(self, tag_stats, suite_stat_level):
-        self._tag_stats = tag_stats
-        self._parents = []
-        self._suite_stat_level = suite_stat_level
+    def __init__(self, suite_builder, tag_builder):
+        self._suite_builder = suite_builder
+        self._tag_builder = tag_builder

     def start_suite(self, suite):
-        new  = SuiteStatistics(suite)
- if self._suite_stat_level == -1 or len(self._parents) < self._suite_stat_level:
-            self._current_suite_stat.suites.append(new)
-        self._parents.append(self._current_suite_stat)
-        self._current_suite_stat = new
-        self._current_suite = suite
-
-    def build(self, suite):
-        self._current_suite_stat = SuiteStatistics(suite)
-        self.visit_suite(suite)
-        return self._current_suite_stat.suites[0]
+        self._suite_builder.start_suite(suite)

     def end_suite(self, suite):
-        if self._parents:
-            self._parents[-1].all.add_stat(self._current_suite_stat.all)
- self._parents[-1].critical.add_stat(self._current_suite_stat.critical)
-            self._current_suite_stat = self._parents.pop(-1)
+        self._suite_builder.end_suite()

     def visit_test(self, test):
-        self._current_suite_stat.add_test(test)
-        self._tag_stats.add_test(test)
+        self._suite_builder.add_test(test)
+        self._tag_builder.add_test(test)
+
+    def visit_keyword(self, kw):
+        pass
=======================================
--- /src/robot/model/stats.py   Thu Nov 17 00:49:02 2011
+++ /src/robot/model/stats.py   Thu Nov 17 12:53:04 2011
@@ -41,8 +41,6 @@
         else:
             self.failed += 1

-    # TODO: Are all methods below actually used somewhere??
-
     def add_stat(self, other):
         self.passed += other.passed
         self.failed += other.failed
=======================================
--- /src/robot/model/suitestatistics.py Mon Nov 14 04:41:32 2011
+++ /src/robot/model/suitestatistics.py Thu Nov 17 12:53:04 2011
@@ -27,6 +27,12 @@
         if test.critical == 'yes':
             self.critical.add_test(test)

+    def add_child_stats(self, stats, add_to_suites=True):
+        self.all.add_stat(stats.all)
+        self.critical.add_stat(stats.critical)
+        if add_to_suites:
+            self.suites.append(stats)
+
     def visit(self, visitor):
         visitor.visit_suite_statistics(self)

@@ -35,3 +41,37 @@
         for s in self.suites:
             for stat in s:
                 yield stat
+
+
+class SuiteStatisticsBuilder(object):
+
+    def __init__(self, suite_stat_level):
+        self._stat_level = suite_stat_level
+        self._stats_stack = []
+        self.root = None
+
+    @property
+    def current(self):
+        return self._stats_stack[-1] if self._stats_stack else None
+
+    def start_suite(self, suite):
+        self._stats_stack.append(SuiteStatistics(suite))
+        if self.root is None:
+            self.root = self.current
+
+    def add_test(self, test):
+        self.current.add_test(test)
+
+    def end_suite(self):
+        stats = self._stats_stack.pop()
+        if self.current:
+            self.current.add_child_stats(stats, self._is_child_included())
+
+    def _is_child_included(self):
+        return self._include_all_levels() or self._level_below_threshold()
+
+    def _include_all_levels(self):
+        return self._stat_level == -1
+
+    def _level_below_threshold(self):
+        return len(self._stats_stack) < self._stat_level

Reply via email to