Permalink
Browse files

Add comparison operators to atom.Entry.

  • Loading branch information...
1 parent db5db52 commit d12460006f5fc6d68d6f4aaa92ecab7eb491cecf Tommi Virtanen committed Nov 20, 2005
Showing with 179 additions and 0 deletions.
  1. +57 −0 atomat/atom.py
  2. +122 −0 atomat/test/test_atom.py
View
@@ -1,4 +1,5 @@
from zope.interface import implements
+import sets
from atomat import iatom
class XHTMLContent(object):
@@ -50,3 +51,59 @@ def __repr__(self):
attrs.append('%s=%r' % (attr, val))
return '%s(%s)' % (self.__class__.__name__,
', '.join(attrs))
+
+ def __lt__(self, other):
+ try:
+ other = iatom.IEntry(other)
+ except TypeError:
+ return NotImplemented
+ def _getPublished(entry):
+ p = entry.published
+ if p is None:
+ p = entry.updated
+ return p
+
+ me = _getPublished(self)
+ him = _getPublished(other)
+ if me != him:
+ return me < him
+ if self.id != other.id:
+ return self.id < other.id
+ if self.title != other.title:
+ return self.title < other.title
+ raise RuntimeError
+
+ def __le__(self, other):
+ return self==other or self<other
+
+ def __gt__(self, other):
+ try:
+ other = iatom.IEntry(other)
+ except TypeError:
+ return NotImplemented
+ return other < self
+
+ def __ge__(self, other):
+ return self==other or self>other
+
+ def __eq__(self, other):
+ try:
+ other = iatom.IEntry(other)
+ except TypeError:
+ return NotImplemented
+
+ attrs = sets.Set()
+ attrs.update([x for x in dir(self) if not x.startswith('_')])
+ attrs.update([x for x in dir(other) if not x.startswith('_')])
+
+ class _SENTINEL_1(object): pass
+ class _SENTINEL_2(object): pass
+ for attr in attrs:
+ if (getattr(self, attr, _SENTINEL_1)
+ != getattr(other, attr, _SENTINEL_2)):
+ return False
+
+ return True
+
+ def __ne__(self, other):
+ return not (self==other)
View
@@ -0,0 +1,122 @@
+from twisted.trial import unittest
+import datetime
+from atomat import atom
+
+class Compare(unittest.TestCase):
+ def check_decreasing_sequence(self, a, b, c):
+ self.assertEquals(a, a)
+ self.assertEquals(b, b)
+ self.assertEquals(c, c)
+ self.assertNotEquals(a, b)
+ self.assertNotEquals(a, c)
+ self.assertNotEquals(b, a)
+ self.assertNotEquals(b, c)
+ self.assertNotEquals(c, a)
+ self.assertNotEquals(c, b)
+ self.failUnless(a <= a, 'a <= a')
+ self.failUnless(b <= b, 'b <= b')
+ self.failUnless(c <= c, 'c <= c')
+ self.failUnless(a >= a, 'a >= a')
+ self.failUnless(b >= b, 'b >= b')
+ self.failUnless(c >= c, 'c >= c')
+
+ self.failUnless(b < a, 'b < a')
+ self.failUnless(b <= a, 'b <= a')
+ self.failUnless(c < b, 'c < b')
+ self.failUnless(c <= b, 'c <= b')
+ self.failUnless(c < a, 'c < a')
+ self.failUnless(c <= a, 'c <= a')
+
+ self.failIf(a < b, 'a < b')
+ self.failIf(a <= b, 'a <= b')
+ self.failIf(a < c, 'a < c')
+ self.failIf(a <= c, 'a <= c')
+ self.failIf(b < c, 'b < c')
+ self.failIf(b <= c, 'b <= c')
+
+ self.failUnless(a > b, 'a > b')
+ self.failUnless(a >= b, 'a >= b')
+ self.failUnless(b > c, 'b > c')
+ self.failUnless(b >= c, 'b >= c')
+ self.failUnless(a > c, 'a > c')
+ self.failUnless(a >= c, 'a >= c')
+
+ self.failIf(a < b, 'a < b')
+ self.failIf(a <= b, 'a <= b')
+ self.failIf(a < c, 'a < c')
+ self.failIf(a <= c, 'a <= c')
+ self.failIf(b < c, 'b < c')
+ self.failIf(b <= c, 'b <= c')
+
+ self.failIf(b > a, 'b > a')
+ self.failIf(b >= a, 'b >= a')
+ self.failIf(c > a, 'c > a')
+ self.failIf(c >= a, 'c >= a')
+ self.failIf(c > b, 'c > b')
+ self.failIf(c >= b, 'c >= b')
+
+ def test_compare_published(self):
+ a = atom.Entry(id='foo',
+ title='Foo',
+ updated=datetime.datetime(2005,11,20, 13, 0, 2))
+ b = atom.Entry(id='bar',
+ title='Bar',
+ updated=datetime.datetime(2005,11,20, 12,42,16))
+ c = atom.Entry(id='quux',
+ title='Quux',
+ updated=datetime.datetime(2005,11,20, 18,10,1),
+ published=datetime.datetime(2005,11,20, 12,40,16))
+ self.check_decreasing_sequence(a, b, c)
+
+ def test_compare_id(self):
+ a = atom.Entry(id='quux',
+ title='Foo',
+ updated=datetime.datetime(2005,11,20, 13, 0, 2))
+ b = atom.Entry(id='bar',
+ title='Bar',
+ updated=datetime.datetime(2005,11,20, 13, 0, 2))
+ c = atom.Entry(id='foo',
+ title='Quux',
+ updated=datetime.datetime(2005,11,20, 13, 0, 2),
+ published=datetime.datetime(2005,11,20, 12,40,16))
+ self.check_decreasing_sequence(a, b, c)
+
+ def test_compare_title(self):
+ a = atom.Entry(id='i',
+ title='Quux',
+ updated=datetime.datetime(2005,11,20, 13, 0, 2))
+ b = atom.Entry(id='i',
+ title='Bar',
+ updated=datetime.datetime(2005,11,20, 13, 0, 2))
+ c = atom.Entry(id='i',
+ title='Foo',
+ updated=datetime.datetime(2005,11,20, 13, 0, 2),
+ published=datetime.datetime(2005,11,20, 12,40,16))
+ self.check_decreasing_sequence(a, b, c)
+
+ def test_equality(self):
+ a = atom.Entry(id='foo',
+ title='Foo',
+ updated=datetime.datetime(2005,11,20, 13, 0, 2))
+ b = atom.Entry(id='foo',
+ title='Foo',
+ updated=datetime.datetime(2005,11,20, 13, 0, 2))
+ self.failIfIdentical(a, b)
+ self.failIfEquals(a, 42)
+ self.failUnless(a == b, 'a == b')
+ self.failIf(a != b, 'a != b')
+
+ def test_equality_extraAttribute(self):
+ a = atom.Entry(id='foo',
+ title='Foo',
+ updated=datetime.datetime(2005,11,20, 13, 0, 2))
+ b = atom.Entry(id='foo',
+ title='Foo',
+ updated=datetime.datetime(2005,11,20, 13, 0, 2),
+ link='http://www.example.com/')
+ self.failIfEquals(a, b)
+ self.failUnless(a != b, 'a != b')
+
+ self.failIfEquals(b, a)
+ self.failUnless(b != a, 'b != a')
+

0 comments on commit d124600

Please sign in to comment.