Arve Knudsen avatar Arve Knudsen committed 3aa7e30

Plugin testid: test/fix loadTestsFromNames

Comments (0)

Files changed (3)

nose2/plugins/testid.py

         self._loaded = False
 
     def nextId(self):
+        """Increment ID and return it.
+        
+        XXX: Make private?
+        """
         self.id += 1
         return self.id
 
     def startTest(self, event):
+        """Implement hook"""
         testid = event.test.id().split('\n')[0]
         if testid not in self.tests:
             id_ = self.nextId()
         event.message('#%s ' % id_, (2,))
 
     def loadTestsFromName(self, event):
-        """Implement loading of tests from name.
+        """Implement hook.
         
         If the name is a number, it might be an ID assigned by us. If we can
         find a test to which we have assigned that ID, event.name is changed to
         the test's real ID. In this way, tests can be referred to via sequential
         numbers.
         """
-        id_ = self.argToId(event.name)
-        if id_ is None:
-            return
-
-        self.loadIds()
-        if id_ in self.ids:
-            # Translate to test's real ID
-            event.name = self.ids[id_]
+        test_id = self.__real_id_from_id(event.name)
+        if test_id is not None:
+            event.name = test_id
 
     def loadTestsFromNames(self, event):
-        self.loadIds()
+        """Implement hook."""
         new_names = []
-        for name in event.names:
-            if self.argToId(name) in self.ids:
-                new_names.append(self.ids[name])
-            else:
-                new_names.append(name)
-        event.names[:] = new_names
+        for i, name in enumerate(event.names[:]):
+            test_id = self.__real_id_from_id(name)
+            if test_id is not None:
+                event.names[i] = test_id
 
     def stopTestRun(self, event):
+        """Implement hook."""
         fh = open(self.idfile, 'w')
         pickle.dump({'ids': self.ids, 'tests': self.tests}, fh)
 
-    def argToId(self, name):
-        """Try to translate name to numeric ID."""
-        m = self.idpat.match(name)
-        if m:
-            return int(m.groups()[0])
-
     def loadIds(self):
-        """Load previously pickled 'ids' and 'tests' attributes."""
+        """Load previously pickled 'ids' and 'tests' attributes.
+        
+        XXX: Make private?
+        """
         if self._loaded:
             return
 
         try:
             fh = open(self.idfile, 'r')
         except EnvironmentError:
-            # XXX: Should we set _loaded to True?
+            self._loaded = True
             return
         try:
             data = pickle.load(fh)
             self.tests = data['tests']
         self.id = max(self.ids.keys())
         self._loaded = True
+
+
+    def __real_id_from_id(self, name):
+        """Try to translate one of our IDs to real test ID."""
+        m = self.idpat.match(name)
+        if m is None:
+            return None
+
+        id_ = int(m.groups()[0])
+
+        self.loadIds()
+        # Translate to test's real ID
+        try:
+            return self.ids[id_]
+        except KeyError:
+            return None

nose2/tests/_common.py

     def __init__(self, name):
         super(FakeLoadFromNameEvent, self).__init__()
         self.name = name
+
+class FakeLoadFromNamesEvent(_FakeEventBase):
+    """Fake LoadFromNamesEvent."""
+    def __init__(self, names):
+        super(FakeLoadFromNamesEvent, self).__init__()
+        self.names = names

nose2/tests/test_testid_plugin.py

 import tempfile
 
 from ..plugins import testid
-from ._common import FakeStartTestEvent, FakeLoadFromNameEvent
+from ._common import (FakeStartTestEvent, FakeLoadFromNameEvent,
+        FakeLoadFromNamesEvent)
 
 class UnitTestTestId(TestCase):
     """Test class TestId.
         event = FakeLoadFromNameEvent('1')
         plug.loadTestsFromName(event)
 
-        # The numeric ID should be translated to one of our test methods
-        cls_name, mthd_name = event.name.split('.')[-2:]
-        self.assertEqual(cls_name, type(self).__name__)
-        self.assertTrue(mthd_name.startswith('test_'))
+        # The numeric ID should be translated to this test's ID
+        self.assertEqual(event.name, self.id())
 
     def test_load_tests_from_name_no_ids(self):
         """Test calling loadTestsFromName when no IDs have been saved."""
         plug = self.__create()
         event = FakeLoadFromNameEvent('1')
         plug.loadTestsFromName(event)
+
         # The event's name should be unchanged, since no IDs should be mapped
         self.assertEqual(event.name, '1')
 
 
+    def test_load_tests_from_names(self):
+        """Test loadTestsFromNames method."""
+        plug = self.__create()
+        # By first starting/stopping a test, an ID is assigned by the plugin
+        plug.startTest(FakeStartTestEvent(self))
+        plug.stopTestRun(None)
+        event = FakeLoadFromNamesEvent(['1', '2'])
+        plug.loadTestsFromNames(event)
+
+        name1, name2 = event.names
+        # The first numeric ID should be translated to this test's ID
+        self.assertEqual(name1, self.id())
+        # The second one should not have a match
+        self.assertEqual(name2, '2')
+
+
     def __create(self):
         """Create a TestId instance."""
         return testid.TestId()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.