From fcda834a18405bee937198daf154a94a3993cbcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Wed, 16 May 2012 00:51:05 +0200 Subject: Add __contains__ to Spec. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphaƫl Barrois --- src/semantic_version/base.py | 5 +++++ tests/test_base.py | 13 +++++++++++++ tests/test_match.py | 11 +++++++++++ 3 files changed, 29 insertions(+) diff --git a/src/semantic_version/base.py b/src/semantic_version/base.py index 9bd5932..a3fbdba 100644 --- a/src/semantic_version/base.py +++ b/src/semantic_version/base.py @@ -255,6 +255,11 @@ class Spec(object): else: # pragma: no cover raise ValueError('Unexpected match kind: %r' % self.kind) + def __contains__(self, version): + if isinstance(version, Version): + return self.match(version) + return False + def __str__(self): return '%s%s' % (self.kind, self.spec) diff --git a/tests/test_base.py b/tests/test_base.py index 28bfc7a..ee6e348 100755 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -145,5 +145,18 @@ class VersionTestCase(unittest.TestCase): self.assertNotEqual(text, base.Version(text, partial=True)) +class SpecTestCase(unittest.TestCase): + def test_equality(self): + spec1 = base.Spec('==0.1.0') + spec2 = base.Spec('==0.1.0') + self.assertEqual(spec1, spec2) + self.assertFalse(spec1 == '==0.1.0') + + def test_to_string(self): + spec = base.Spec('==0.1.0') + self.assertEqual('==0.1.0', str(spec)) + self.assertEqual(base.Spec.KIND_EQUAL, spec.kind) + + if __name__ == '__main__': # pragma: no cover unittest.main() diff --git a/tests/test_match.py b/tests/test_match.py index cd3acea..9d05c7c 100755 --- a/tests/test_match.py +++ b/tests/test_match.py @@ -83,6 +83,17 @@ class MatchTestCase(unittest.TestCase): version = semantic_version.Version(version_txt) self.assertTrue(spec.match(version), "%r does not match %r" % (version, spec)) self.assertTrue(semantic_version.match(spec_txt, version_txt)) + self.assertTrue(version in spec, "%r not in %r" % (version, spec)) + + def test_contains(self): + spec = semantic_version.Spec('<=0.1.1') + self.assertFalse('0.1.0' in spec, "0.1.0 should not be in %r" % spec) + + version = semantic_version.Version('0.1.1+4.2') + self.assertFalse(version in spec, "%r should not be in %r" % (version, spec)) + + version = semantic_version.Version('0.1.1-rc1+4.2') + self.assertTrue(version in spec, "%r should be in %r" % (version, spec)) if __name__ == '__main__': # pragma: no cover -- cgit v1.2.1