commit:     ce821ea62cd40b0110fc128f92e140da4e7482cf
Author:     Virgil Dupras <vdupras <AT> gentoo <DOT> org>
AuthorDate: Mon Sep 17 23:31:27 2018 +0000
Commit:     Virgil Dupras <vdupras <AT> gentoo <DOT> org>
CommitDate: Mon Sep 17 23:31:27 2018 +0000
URL:        https://gitweb.gentoo.org/proj/gentoolkit.git/commit/?id=ce821ea6

Atom.intersects: simplify name-only matching logic

Remove old "optimization". Slicing two strings can't possibly be faster
than checking if `.category` is `None`: the call to `.cp` earlier
ensures that we've already populated that property.

Also, add tests for it.

 pym/gentoolkit/atom.py           | 12 +++---------
 pym/gentoolkit/test/test_atom.py | 11 +++++++++--
 2 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/pym/gentoolkit/atom.py b/pym/gentoolkit/atom.py
index b5b755c..b86c89c 100644
--- a/pym/gentoolkit/atom.py
+++ b/pym/gentoolkit/atom.py
@@ -209,15 +209,9 @@ class Atom(portage.dep.Atom, CPV):
                # Our "cp" (cat/pkg) must match exactly:
                if self.cp != other.cp:
                        # Check to see if one is name only:
-                       # Avoid slow partitioning if we're definitely not 
matching
-                       # (yes, this is hackish, but it's faster):
-                       if self.cp[-1:] != other.cp[-1:]:
-                               return False
-
-                       if ((not self.category and self.name == other.name) or
-                               (not other.category and other.name == 
self.name)):
-                               return True
-                       return False
+                       # We don't bother checking if self.category is None: it 
can't be
+                       # because we're an Atom subclass and that would be 
invalid.
+                       return (not other.category and self.name == other.name)
 
                # Slot dep only matters if we both have one. If we do they
                # must be identical:

diff --git a/pym/gentoolkit/test/test_atom.py b/pym/gentoolkit/test/test_atom.py
index 399905e..6177222 100644
--- a/pym/gentoolkit/test/test_atom.py
+++ b/pym/gentoolkit/test/test_atom.py
@@ -8,6 +8,7 @@
 import unittest
 
 from gentoolkit.atom import Atom
+from gentoolkit.cpv import CPV
 from gentoolkit.test import cmp
 
 """Atom test suite (verbatim) from pkgcore."""
@@ -140,10 +141,16 @@ class TestGentoolkitAtom(unittest.TestCase):
                                result, that_atom.intersects(this_atom),
                                '%s intersecting %s should be %s' % (that, 
this, result))
 
+       def test_intersects_nameonly(self):
+               atom = Atom('cat/pkg')
+               self.assertTrue(atom.intersects(CPV('pkg')))
+               self.assertFalse(atom.intersects(CPV('other')))
+               self.assertFalse(atom.intersects(CPV('dkg')))
+
 
 def test_main():
-        suite = unittest.TestLoader().loadTestsFromTestCase(TestGentoolkitAtom)
-        unittest.TextTestRunner(verbosity=2).run(suite)
+               suite = 
unittest.TestLoader().loadTestsFromTestCase(TestGentoolkitAtom)
+               unittest.TextTestRunner(verbosity=2).run(suite)
 test_main.__test__ = False
 
 

Reply via email to