File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,17 @@ def ===(other)
49
49
end
50
50
end
51
51
52
+ def subclass? ( other )
53
+ case other
54
+ when PyTypeObjectWrapper
55
+ __pyptr__ . subclass? ( other . __pyptr__ )
56
+ when Class , Module
57
+ other >= self || false
58
+ else
59
+ __pyptr__ . subclass? ( other )
60
+ end
61
+ end
62
+
52
63
private
53
64
54
65
def register_python_type_mapping
Original file line number Diff line number Diff line change @@ -10,6 +10,37 @@ module PyCall
10
10
PyCall . wrap_class ( simple_class )
11
11
end
12
12
13
+ describe '#subclass?(other)' do
14
+ subject { PyCall . builtins . list }
15
+
16
+ context 'when the value of other is a PyTypeObjectWrapper' do
17
+ specify do
18
+ expect ( subject . subclass? ( PyCall . builtins . object ) ) . to eq ( true )
19
+ expect ( subject . subclass? ( PyCall . builtins . list ) ) . to eq ( true )
20
+ expect ( subject . subclass? ( PyCall . builtins . dict ) ) . to eq ( false )
21
+ end
22
+ end
23
+
24
+ context 'when the value of other is a Class' do
25
+ specify do
26
+ expect ( subject . subclass? ( Object ) ) . to eq ( true )
27
+ expect ( subject . subclass? ( PyObjectWrapper ) ) . to eq ( true )
28
+ expect ( subject . subclass? ( PyTypeObjectWrapper ) ) . to eq ( false )
29
+ expect ( subject . subclass? ( Array ) ) . to eq ( false )
30
+ end
31
+ end
32
+
33
+ context 'when the other cases' do
34
+ it 'behaves as well as PyTypePtr#subclass?' do
35
+ expect ( subject . subclass? ( PyCall . builtins . object . __pyptr__ ) ) . to eq ( true )
36
+ expect ( subject . subclass? ( PyCall . builtins . list . __pyptr__ ) ) . to eq ( true )
37
+ expect ( subject . subclass? ( PyCall . builtins . dict . __pyptr__ ) ) . to eq ( false )
38
+ expect { subject . subclass? ( Conversion . from_ruby ( 12 ) ) } . to raise_error ( TypeError )
39
+ expect { subject . subclass? ( 12 ) } . to raise_error ( TypeError )
40
+ end
41
+ end
42
+ end
43
+
13
44
describe '#===' do
14
45
specify do
15
46
expect ( PyCall . builtins . tuple === PyCall . tuple ( ) ) . to eq ( true )
You can’t perform that action at this time.
0 commit comments