Commits

Ned Batchelder  committed 9fc1c8f

Dict literals shouldn't count as many different exits.

  • Participants
  • Parent commits b6ae086

Comments (0)

Files changed (5)

 Next version
 ------------
 
+- Branch coverage.
+
 - The tracer code has changed, it's a few percent faster.
 
 - Some exceptions reported by the command line interface have been cleaned up
   so that tracebacks inside coverage.py aren't shown.  Fixes `issue 23`_
   
+- XML reporting has file paths that let Cobertura find the source code.
+
 .. _issue 23: http://bitbucket.org/ned/coveragepy/issue/23
   
 
 
 * Power
 
-- Branch coverage
++ Branch coverage
     Titus' idea:
         1: if a:
         2:     b = 2

File coverage/parser.py

         normalized to the first line of multiline statements.
         
         """
-        all_arcs = self.byte_parser._all_arcs()
-        m2fl = self.first_line
-        all_arcs = [(m2fl(l1), m2fl(l2)) for (l1,l2) in all_arcs]
+        all_arcs = []
+        for l1, l2 in self.byte_parser._all_arcs():
+            fl1 = self.first_line(l1)
+            fl2 = self.first_line(l2)
+            if fl1 != fl2:
+                all_arcs.append((fl1, fl2))
         return sorted(all_arcs)
 
+    def exit_counts(self):
+        """Return a dict mapping line numbers to number of exits from that line."""
+        exit_counts = {}
+        for l1,l2 in self.arcs():
+            if l1 == -1:
+                continue
+            if l1 not in exit_counts:
+                exit_counts[l1] = 0
+            exit_counts[l1] += 1
+        
+        return exit_counts
+
+
 ## Opcodes that guide the ByteParser.
 
 def _opcode(name):
                 else:
                     arc_width, arc_chars = 0, {}
                     
+                exit_counts = cp.exit_counts()
+
                 for i, ltext in enumerate(cp.lines):
                     lineno = i+1
-                    m0 = m1 = m2 = a = ' '
+                    m0 = m1 = m2 = m3 = a = ' '
                     if lineno in cp.statement_starts:
                         m0 = '-'
                     if lineno in cp.docstrings:
                         m1 = '"'
                     if lineno in cp.excluded:
                         m2 = 'x'
+                    exits = exit_counts.get(lineno, 0)
+                    if exits > 1:
+                        m3 = str(exits)
                     a = arc_chars.get(lineno, '').ljust(arc_width)
-                    print("%4d %s%s%s%s %s" % (lineno, m0, m1, m2, a, ltext))
+                    print("%4d %s%s%s%s%s %s" % (lineno, m0, m1, m2, m3, a, ltext))
 
     def arc_ascii_art(self, arcs):
         """Draw arcs as ascii art.

File coverage/results.py

 
     def branch_lines(self):
         """Returns lines that have more than one exit."""
-        exit_counts = {}
-        for l1,l2 in self.arc_possibilities():
-            if l1 == -1:
-                continue
-            if l1 not in exit_counts:
-                exit_counts[l1] = 0
-            exit_counts[l1] += 1
-        
+        exit_counts = self.parser.exit_counts()
         return [l1 for l1,count in exit_counts.items() if count > 1]
 
     def total_branches(self):
-        exit_counts = {}
-        for l1,l2 in self.arc_possibilities():
-            if l1 == -1:
-                continue
-            if l1 not in exit_counts:
-                exit_counts[l1] = 0
-            exit_counts[l1] += 1
-        
+        exit_counts = self.parser.exit_counts()
         return sum([count for count in exit_counts.values() if count > 1])
         
     def missing_branch_arcs(self):

File test/test_arcs.py

                 """,
                 arcz=".1 12 .3 3. 24 45 56 67 7B 89 9B BC C.",
                 arcz_missing="67 7B", arcz_unpredicted="68")
+
+
+class MiscArcTest(CoverageTest):
+    """Miscellaneous arc-measuring tests."""
+
+    def test_dict_literal(self):
+        self.check_coverage("""\
+            d = {
+                'a': 2,
+                'b': 3,
+                'c': {
+                    'd': 5,
+                    'e': 6,
+                    }
+                }
+            assert d
+            """,
+            arcz=".1 19 9.")