import re

from .default import DefaultParser


class NoseParser(DefaultParser):
    name = "nose"

    def command_matches(self, command):
        return "nosetests" in command

    def num_passed(self, result):
        return self.num_total(result) - self.num_failed(result)

    def num_total(self, result):
        # Ran 2 test(s) in nnn seconds.
        m = re.findall('Ran (\d+) tests?', result.output)
        return int(m[-1][0])
    
    def num_failed(self, result):
        # If failed, you'll see one of
        # FAILED (failures=1)
        # FAILED (failures=1, errors=1)
        # FAILED (errors=1)
        m = re.findall('FAILED \(failures=(\d+)', result.output)
        if len(m) > 0:
            return int(m[-1][0])
        else:
            m = re.findall('FAILED \(errors=(\d+)', result.output)
            if len(m) > 0:
                return int(m[-1][0])
            return 0

@@ -426,25 +426,18 @@
 nt(m%5B-1%5D
-%5B0%5D)%0A    
+)%0A
 %0A    def
@@ -584,27 +584,18 @@
     
-# FAILED (errors=1)
+failed = 0
 %0A   
@@ -624,16 +624,18 @@
 AILED %5C(
+.*
 failures
@@ -685,38 +685,41 @@
  0:%0A            
-return
+failed +=
  int(m%5B-1%5D%5B0%5D)%0A 
@@ -704,35 +704,32 @@
 led += int(m%5B-1%5D
-%5B0%5D
 )%0A        else:%0A
@@ -726,18 +726,205 @@
     
-else:%0A    
+failed += self.num_error(result)%0A        return failed%0A%0A    def num_error(self, result):%0A        # If errored, you'll see one of%0A        # FAILED (failures=1, errors=1)%0A        # FAILED (errors=1)%0A
     
@@ -952,16 +952,18 @@
 AILED %5C(
+.*
 errors=(
@@ -988,28 +988,24 @@
 ut)%0A        
-    
 if len(m) %3E 
@@ -999,36 +999,32 @@
  if len(m) %3E 0:%0A
-    
             retu
@@ -1039,17 +1039,10 @@
 %5B-1%5D
-%5B0%5D)%0A    
+)%0A
     

fix parsing of nosetest results