Bug 1260624 - Add support for simple comparisons of ReadOnlyNamespaces. r=ted
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 30 Mar 2016 14:00:40 +0900
changeset 291155 2d041429d9935db8db978c783b05eb65abddd5e4
parent 291154 fffe8dc49621deab94739de3eb816556d4c6c572
child 291156 dc8b68b4f67a2f2418b5e55f912a5ac5de796ac0
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersted
bugs1260624
milestone48.0a1
Bug 1260624 - Add support for simple comparisons of ReadOnlyNamespaces. r=ted
python/mozbuild/mozbuild/test/test_containers.py
python/mozbuild/mozbuild/util.py
--- a/python/mozbuild/mozbuild/test/test_containers.py
+++ b/python/mozbuild/mozbuild/test/test_containers.py
@@ -33,16 +33,23 @@ class TestReadOnlyNamespace(unittest.Tes
             value = test.missing
 
         with self.assertRaises(Exception):
             test.foo = 2
 
         with self.assertRaises(Exception):
             del test.foo
 
+        self.assertEqual(test, test)
+        self.assertEqual(test, ReadOnlyNamespace(foo=1, bar=2))
+        self.assertNotEqual(test, ReadOnlyNamespace(foo='1', bar=2))
+        self.assertNotEqual(test, ReadOnlyNamespace(foo=1, bar=2, qux=3))
+        self.assertNotEqual(test, ReadOnlyNamespace(foo=1, qux=3))
+        self.assertNotEqual(test, ReadOnlyNamespace(foo=3, bar='42'))
+
 
 class TestReadOnlyDict(unittest.TestCase):
     def test_basic(self):
         original = {'foo': 1, 'bar': 2}
 
         test = ReadOnlyDict(original)
 
         self.assertEqual(original, test)
--- a/python/mozbuild/mozbuild/util.py
+++ b/python/mozbuild/mozbuild/util.py
@@ -79,16 +79,22 @@ class ReadOnlyNamespace(object):
             super(ReadOnlyNamespace, self).__setattr__(k, v)
 
     def __delattr__(self, key):
         raise Exception('Object does not support deletion.')
 
     def __setattr__(self, key, value):
         raise Exception('Object does not support assignment.')
 
+    def __ne__(self, other):
+        return not (self == other)
+
+    def __eq__(self, other):
+        return self is other or self.__dict__ == other.__dict__
+
 
 class ReadOnlyDict(dict):
     """A read-only dictionary."""
     def __init__(self, *args, **kwargs):
         dict.__init__(self, *args, **kwargs)
 
     def __delitem__(self, key):
         raise Exception('Object does not support deletion.')