Add a Node.FS.__cmp__() method, fix the Node.FS.Entry.__cmp__() method, and add a...
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Tue, 11 Dec 2001 02:32:21 +0000 (02:32 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Tue, 11 Dec 2001 02:32:21 +0000 (02:32 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@135 fdb21ef1-2011-0410-befe-b5e4ea1792b1

src/engine/SCons/Node/FS.py
src/engine/SCons/Node/FSTests.py

index 67a5ae7230756e26bb0d800da7c7a508ae55b165..a898f01bd9c07dbf8625b4815d6acd88d53b85a2 100644 (file)
@@ -122,6 +122,9 @@ class FS:
         self.Top.path_ = os.path.join('.', '')
         self.cwd = self.Top
 
+    def __cmp__(self, other):
+       return cmp(self.__dict__, other.__dict__)
+
     def __doLookup(self, fsclass, name, directory=None):
         """This method differs from the File and Dir factory methods in
         one important way: the meaning of the directory parameter.
@@ -274,6 +277,18 @@ class Entry(SCons.Node.Node):
        """A FS node's string representation is its path name."""
        return self.path
 
+    def __cmp__(self, other):
+       if type(self) != types.StringType and type(other) != types.StringType:
+            try:
+                if self.__class__ != other.__class__:
+                    return 1
+            except:
+                return 1
+        return cmp(str(self), str(other))
+
+    def __hash__(self):
+       return hash(self.abspath_)
+
     def exists(self):
         return os.path.exists(self.abspath)
 
index 49920990934ac1aab9f0d3bd9c70508ec685b96e..d9d128e9f8a450bb38d26b5085b8dd35a01cbadc 100644 (file)
@@ -307,6 +307,57 @@ class FSTestCase(unittest.TestCase):
         assert not f1.dir.exists()
         f1.build()
         assert f1.dir.exists()
+
+        # Test comparison of FS objects
+        fs1 = SCons.Node.FS.FS()
+        fs2 = SCons.Node.FS.FS()
+        os.chdir('..')
+        fs3 = SCons.Node.FS.FS()
+        assert fs1 == fs2
+        assert fs1 == fs3
+
+        # Test comparison of Entry objects
+        e1 = fs3.Entry('cmp/entry')
+        e2 = fs3.Entry('cmp/../cmp/entry')
+        e3 = fs3.Entry('entry')
+        assert e1 == e2
+        assert e1 != e3
+        assert e1 == os.path.normpath("cmp/entry"), e1
+        assert e1 != os.path.normpath("c/entry"), e1
+
+        # Test comparison of Dir objects
+        d1 = fs3.Dir('cmp/dir')
+        d2 = fs3.Dir('cmp/../cmp/dir')
+        d3 = fs3.Dir('dir')
+        assert d1 == d2
+        assert d1 != d3
+        assert d1 == os.path.normpath("cmp/dir"), d1
+        assert d1 != os.path.normpath("c/dir"), d1
+
+        # Test comparison of File objects
+        f1 = fs3.File('cmp/file')
+        f2 = fs3.File('cmp/../cmp/file')
+        f3 = fs3.File('file')
+        assert f1 == f2
+        assert f1 != f3
+        assert f1 == os.path.normpath("cmp/file"), f1
+        assert f1 != os.path.normpath("c/file"), f1
+
+        # Test comparison of different type objects
+        f1 = fs1.File('cmp/xxx')
+        d2 = fs2.Dir('cmp/xxx')
+        assert f1 != d2, "%s == %s" % (f1.__class__, d2.__class__)
+
+        # Test hashing FS nodes
+        f = fs1.File('hash/f')
+        d = fs1.Dir('hash/d')
+        e = fs1.Entry('hash/e')
+        val = {}
+        val[f] = 'f'
+        val[d] = 'd'
+        val[e] = 'e'
+        for k, v in val.items():
+             assert k == "hash/" + v
         
         #XXX test exists()