@@ -21,6 +21,24 @@ def test_hashkey(self, key=cachetools.keys.hashkey):
21
21
self .assertEqual (key (1 , 2 , 3 ), key (1.0 , 2.0 , 3.0 ))
22
22
self .assertEqual (hash (key (1 , 2 , 3 )), hash (key (1.0 , 2.0 , 3.0 )))
23
23
24
+ def methodkey (self , key = cachetools .keys .methodkey ):
25
+ # similar to hashkey(), but ignores its first positional argument
26
+ self .assertEqual (key ("x" ), key ("y" ))
27
+ self .assertEqual (hash (key ("x" )), hash (key ("y" )))
28
+ self .assertEqual (key ("x" , 1 , 2 , 3 ), key ("y" , 1 , 2 , 3 ))
29
+ self .assertEqual (hash (key ("x" , 1 , 2 , 3 )), hash (key ("y" , 1 , 2 , 3 )))
30
+ self .assertEqual (key ("x" , 1 , 2 , 3 , x = 0 ), key ("y" , 1 , 2 , 3 , x = 0 ))
31
+ self .assertEqual (hash (key ("x" , 1 , 2 , 3 , x = 0 )), hash (key ("y" , 1 , 2 , 3 , x = 0 )))
32
+ self .assertNotEqual (key ("x" , 1 , 2 , 3 ), key ("x" , 3 , 2 , 1 ))
33
+ self .assertNotEqual (key ("x" , 1 , 2 , 3 ), key ("x" , 1 , 2 , 3 , x = None ))
34
+ self .assertNotEqual (key ("x" , 1 , 2 , 3 , x = 0 ), key ("x" , 1 , 2 , 3 , x = None ))
35
+ self .assertNotEqual (key ("x" , 1 , 2 , 3 , x = 0 ), key ("x" , 1 , 2 , 3 , y = 0 ))
36
+ with self .assertRaises (TypeError ):
37
+ hash ("x" , key ({}))
38
+ # untyped keys compare equal
39
+ self .assertEqual (key ("x" , 1 , 2 , 3 ), key ("y" , 1.0 , 2.0 , 3.0 ))
40
+ self .assertEqual (hash (key ("x" , 1 , 2 , 3 )), hash (key ("y" , 1.0 , 2.0 , 3.0 )))
41
+
24
42
def test_typedkey (self , key = cachetools .keys .typedkey ):
25
43
self .assertEqual (key (), key ())
26
44
self .assertEqual (hash (key ()), hash (key ()))
@@ -37,6 +55,23 @@ def test_typedkey(self, key=cachetools.keys.typedkey):
37
55
# typed keys compare unequal
38
56
self .assertNotEqual (key (1 , 2 , 3 ), key (1.0 , 2.0 , 3.0 ))
39
57
58
+ def test_typedmethodkey (self , key = cachetools .keys .typedmethodkey ):
59
+ # similar to typedkey(), but ignores its first positional argument
60
+ self .assertEqual (key ("x" ), key ("y" ))
61
+ self .assertEqual (hash (key ("x" )), hash (key ("y" )))
62
+ self .assertEqual (key ("x" , 1 , 2 , 3 ), key ("y" , 1 , 2 , 3 ))
63
+ self .assertEqual (hash (key ("x" , 1 , 2 , 3 )), hash (key ("y" , 1 , 2 , 3 )))
64
+ self .assertEqual (key ("x" , 1 , 2 , 3 , x = 0 ), key ("y" , 1 , 2 , 3 , x = 0 ))
65
+ self .assertEqual (hash (key ("x" , 1 , 2 , 3 , x = 0 )), hash (key ("y" , 1 , 2 , 3 , x = 0 )))
66
+ self .assertNotEqual (key ("x" , 1 , 2 , 3 ), key ("x" , 3 , 2 , 1 ))
67
+ self .assertNotEqual (key ("x" , 1 , 2 , 3 ), key ("x" , 1 , 2 , 3 , x = None ))
68
+ self .assertNotEqual (key ("x" , 1 , 2 , 3 , x = 0 ), key ("x" , 1 , 2 , 3 , x = None ))
69
+ self .assertNotEqual (key ("x" , 1 , 2 , 3 , x = 0 ), key ("x" , 1 , 2 , 3 , y = 0 ))
70
+ with self .assertRaises (TypeError ):
71
+ hash (key ("x" , {}))
72
+ # typed keys compare unequal
73
+ self .assertNotEqual (key ("x" , 1 , 2 , 3 ), key ("x" , 1.0 , 2.0 , 3.0 ))
74
+
40
75
def test_addkeys (self , key = cachetools .keys .hashkey ):
41
76
self .assertIsInstance (key (), tuple )
42
77
self .assertIsInstance (key (1 , 2 , 3 ) + key (4 , 5 , 6 ), type (key ()))
0 commit comments