cmp3 0.6.3__tar.gz → 0.6.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cmp3
3
- Version: 0.6.3
3
+ Version: 0.6.4
4
4
  Summary: This project updates the concept of __cmp__ for python3.
5
5
  Author-email: Johannes <johannes.programming@gmail.com>
6
6
  License: The MIT License (MIT)
@@ -26,7 +26,7 @@ keywords = []
26
26
  name = "cmp3"
27
27
  readme = "README.rst"
28
28
  requires-python = ">=3.11"
29
- version = "0.6.3"
29
+ version = "0.6.4"
30
30
 
31
31
  [project.license]
32
32
  file = "LICENSE.txt"
@@ -0,0 +1,113 @@
1
+ import math
2
+ import unittest
3
+ from typing import *
4
+
5
+ from cmp3.core import CmpABC, cmp, cmp_poset, cmpDeco
6
+
7
+
8
+ class TestCmpFunction(unittest.TestCase):
9
+ def test_portingguide_less(self: Self) -> None:
10
+ self.assertEqual(cmp(1, 2), -1)
11
+ self.assertEqual(cmp(-5, -1), -1)
12
+
13
+ def test_portingguide_equal(self: Self) -> None:
14
+ self.assertEqual(cmp(3, 3), 0)
15
+ self.assertEqual(cmp("a", "a"), 0)
16
+
17
+ def test_portingguide_greater(self: Self) -> None:
18
+ self.assertEqual(cmp(2, 1), 1)
19
+ self.assertEqual(cmp("b", "a"), 1)
20
+
21
+ def test_poset_mode_uses_poset_semantics(self: Self) -> None:
22
+ # For totally ordered ints, this should behave like normal cmp
23
+ self.assertEqual(cmp(1, 2, mode="poset"), -1)
24
+ self.assertEqual(cmp(2, 1, mode="poset"), 1)
25
+ self.assertEqual(cmp(5, 5, mode="poset"), 0)
26
+
27
+ def test_invalid_mode_raises(self: Self) -> None:
28
+ with self.assertRaises(ValueError):
29
+ cmp(1, 2, mode="something-else")
30
+
31
+
32
+ class TestCmpPoset(unittest.TestCase):
33
+ def test_total_order_ints(self: Self) -> None:
34
+ self.assertEqual(cmp_poset(1, 2), -1)
35
+ self.assertEqual(cmp_poset(2, 1), 1)
36
+ self.assertEqual(cmp_poset(3, 3), 0)
37
+
38
+ def test_incomparable_returns_nan(self: Self) -> None:
39
+ class Incomparable:
40
+ def __le__(self: Self, other: Any) -> bool:
41
+ # Always False, so no order relation is established
42
+ return False
43
+
44
+ result: Any
45
+ x: Incomparable
46
+ y: Incomparable
47
+
48
+ x = Incomparable()
49
+ y = Incomparable()
50
+ result = cmp_poset(x, y)
51
+ self.assertTrue(math.isnan(result))
52
+
53
+
54
+ # A concrete implementation of CmpABC for testing
55
+ class Box(CmpABC):
56
+ def __init__(self: Self, value: Any) -> None:
57
+ self.value = value
58
+
59
+ def __cmp__(self: Self, other: Any) -> Any:
60
+ if not isinstance(other, Box):
61
+ return NotImplemented
62
+ return (self.value > other.value) - (self.value < other.value)
63
+
64
+
65
+ @cmpDeco
66
+ class DecoratedBox:
67
+ def __init__(self: Self, value: Any) -> None:
68
+ self.value = value
69
+
70
+ def __cmp__(self: Self, other: Any) -> Any:
71
+ if not isinstance(other, DecoratedBox):
72
+ return NotImplemented
73
+ return (self.value > other.value) - (self.value < other.value)
74
+
75
+
76
+ class TestCmpABCAndDeco(unittest.TestCase):
77
+ def test_cmpabc_is_abstract(self: Self) -> None:
78
+ with self.assertRaises(TypeError):
79
+ CmpABC() # type: ignore[abstract] # must not be instantiable
80
+
81
+ def test_cmpabc_comparisons(self: Self) -> None:
82
+ a: Box
83
+ b: Box
84
+ c: Box
85
+ a = Box(1)
86
+ b = Box(2)
87
+ c = Box(1)
88
+
89
+ self.assertTrue(a < b)
90
+ self.assertTrue(a <= b)
91
+ self.assertTrue(b > a)
92
+ self.assertTrue(b >= a)
93
+ self.assertTrue(a == c)
94
+ self.assertTrue(a != b)
95
+
96
+ def test_cmpdeco_on_regular_class(self: Self) -> None:
97
+ x: DecoratedBox
98
+ y: DecoratedBox
99
+ z: DecoratedBox
100
+ x = DecoratedBox(10)
101
+ y = DecoratedBox(20)
102
+ z = DecoratedBox(10)
103
+
104
+ self.assertTrue(x < y)
105
+ self.assertTrue(x <= y)
106
+ self.assertTrue(y > x)
107
+ self.assertTrue(y >= x)
108
+ self.assertTrue(x == z)
109
+ self.assertTrue(x != y)
110
+
111
+
112
+ if __name__ == "__main__":
113
+ unittest.main()
@@ -38,6 +38,7 @@ class TestCmpPoset(unittest.TestCase):
38
38
 
39
39
  def test_poset_incomparable(self: Self) -> None:
40
40
  # incomparable in subset ordering
41
+ res: Any
41
42
  res = core.cmp_poset({1}, {2})
42
43
  self.assertTrue(math.isnan(res))
43
44
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cmp3
3
- Version: 0.6.3
3
+ Version: 0.6.4
4
4
  Summary: This project updates the concept of __cmp__ for python3.
5
5
  Author-email: Johannes <johannes.programming@gmail.com>
6
6
  License: The MIT License (MIT)
@@ -11,4 +11,5 @@ src/cmp3.egg-info/requires.txt
11
11
  src/cmp3.egg-info/top_level.txt
12
12
  src/cmp3/core/__init__.py
13
13
  src/cmp3/tests/__init__.py
14
- src/cmp3/tests/test_cmp3_core.py
14
+ src/cmp3/tests/test_all.py
15
+ src/cmp3/tests/test_core.py
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes