def test_something_typeConversions(self): import math self.assertEqual(complex(1), complex(Something(1))) self.assertEqual(oct(1), oct(Something(1))) self.assertEqual(hex(16), hex(Something(16))) self.assertEqual(math.trunc(math.pi), math.trunc(maybe(math.pi)))
def test_nothing_cmp(self): if PY2: self.assertEqual(0, cmp(Nothing(), Nothing())) self.assertEqual(1, cmp(1, Nothing())) self.assertEqual(1, cmp(Something(5), Nothing())) self.assertEqual(1, cmp(5, Nothing())) self.assertEqual(-1, cmp(Nothing(), Something(5))) self.assertEqual(-1, cmp(Nothing(), 5))
def test_something_cmp_handlesComparisonBetweenSomethings(self): l = [Something(10), Something(3)] sortedl = sorted(l) self.assertTrue(isinstance(sortedl[0], Something)) self.assertTrue(isinstance(sortedl[1], Something)) self.assertEqual(sortedl[0], 3) self.assertEqual(sortedl[1], 10)
def test_comparisons(self): self.assertReallyEqual(Something(1), 1) self.assertReallyEqual(1, Something(1)) self.assertReallyEqual(Something(1), Something(1)) self.assertReallyEqual(Nothing(), Nothing()) self.assertReallyNotEqual(Something(1), Something(2)) self.assertReallyNotEqual(Something(1), Nothing()) self.assertReallyNotEqual(Nothing(), Something(1))
def test_something_cmp(self): l = [Something(1), 2, Nothing()] sortedl = sorted(l) self.assertTrue(isinstance(sortedl[0], Nothing)) self.assertTrue(isinstance(sortedl[1], Something)) self.assertTrue(isinstance(sortedl[2], int)) self.assertEqual(sortedl[0], None) self.assertEqual(sortedl[1], 1) self.assertEqual(sortedl[2], 2)
def test_something_conversions(self): s = "value" d = dict(name="Eran") n = 123 f = 3.14 if PY2: self.assertEqual(unicode(Something(s)), s) self.assertEqual(long(Something(n)), n) self.assertIsInstance(long(Something(f)), long) self.assertEqual(str(Something(s)), "Something(%s)" % s) self.assertEqual(repr(Something(s)), "Something(%s)" % repr(s)) self.assertEqual(repr(Something(d)), "Something(%s)" % repr(d)) self.assertEqual(int(Something(n)), n) self.assertIsInstance(int(Something(n)), int) self.assertEqual(float(Something(f)), f) self.assertIsInstance(float(Something(f)), float)
def test_nothing_ltSomething_isTrue(self): self.assertTrue(Nothing() < Something(1))
def test_nothing_neSomething(self): self.assertTrue(Nothing() != Something(2)) self.assertTrue(Something(1) != Nothing())
def test_nothing_notEqualToSomething(self): self.assertFalse(Nothing() == Something(2)) self.assertFalse(Something(1) == Nothing())
def test_something_cmp_greaterThanNothing(self): l = [Something(0), Nothing()] sortedl = sorted(l) self.assertTrue(isinstance(sortedl[0], Nothing)) self.assertTrue(isinstance(sortedl[1], Something))
def test_something_geValue_comparesTheUnderlyingValue(self): self.assertTrue(Something(11) > 2) self.assertFalse(Something(1) > 2)
def test_something_geSomething_comparesTheUnderlyingValue(self): self.assertTrue(Something(11) > Something(2)) self.assertTrue(Something(11) >= Something(2)) self.assertTrue(Something(11) >= Something(11)) self.assertFalse(Something(1) > Something(2))
def test_something_geNothing_isTrue(self): self.assertTrue(Something("value") >= Nothing())
def test_something_leNothing_isFalse(self): self.assertFalse(Something("value") <= Nothing())
def test_something_ltSomething_usesValue(self): self.assertFalse(Something(3) < Something(1)) self.assertTrue(Something(3) > Something(1))
def test_nothing_gtAnything_isFalse(self): self.assertFalse(Nothing() > Nothing()) self.assertFalse(Nothing() > Something(123)) self.assertFalse(Nothing() > None) self.assertFalse(Nothing() > "Value")
def test_nothing_leAnything_isTrue(self): self.assertTrue(Nothing() <= Nothing()) self.assertTrue(Nothing() <= Something(123)) self.assertTrue(Nothing() <= None) self.assertTrue(Nothing() <= "Value")
def test_nothing_geNotNoneOrNothing_isFalse(self): self.assertFalse(Nothing() >= Something(2)) self.assertFalse(Nothing() >= "some")