Skip to content

Commit e37b72a

Browse files
committed
Add PyTypeObjectWrapper#<=>
1 parent 29a04fc commit e37b72a

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

lib/pycall/pytypeobject_wrapper.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ def subclass?(other)
6060
end
6161
end
6262

63+
def <=>(other)
64+
return 0 if equal?(other)
65+
case other
66+
when PyTypeObjectWrapper
67+
return super if __pyptr__ == other.__pyptr__
68+
other = other.__pyptr__
69+
when Class, Module
70+
return -1 if subclass?(other)
71+
return 1 if other > self
72+
end
73+
74+
return nil unless other.is_a?(PyTypePtr)
75+
return 0 if __pyptr__ == other
76+
return -1 if __pyptr__.subclass?(other)
77+
return 1 if other.subclass?(__pyptr__)
78+
nil
79+
end
80+
6381
private
6482

6583
def register_python_type_mapping

spec/pycall/pytypeobject_wrapper_spec.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,79 @@ module PyCall
4141
end
4242
end
4343

44+
describe '#<=>(other)' do
45+
context 'when the value of other is a PyTypeObjectWrapper' do
46+
context 'when the given class is a superclass in Python of the receiver' do
47+
it 'returns -1' do
48+
expect(PyCall.builtins.list <=> PyCall.builtins.object).to eq(-1)
49+
end
50+
end
51+
52+
context 'when the given class is a subclass in Python of the receiver' do
53+
it 'returns 1' do
54+
expect(PyCall.builtins.object <=> PyCall.builtins.list).to eq(1)
55+
end
56+
end
57+
58+
context 'when the given class is the receiver' do
59+
it 'returns 0' do
60+
expect(PyCall.builtins.list <=> PyCall.builtins.list).to eq(0)
61+
end
62+
end
63+
end
64+
65+
context 'when the value of other is a PyTypePtr' do
66+
context 'when the given class is a superclass in Python of the receiver' do
67+
it 'returns -1' do
68+
expect(PyCall.builtins.list <=> PyCall.builtins.object.__pyptr__).to eq(-1)
69+
end
70+
end
71+
72+
context 'when the given class is a subclass in Python of the receiver' do
73+
it 'returns 1' do
74+
expect(PyCall.builtins.object <=> PyCall.builtins.list.__pyptr__).to eq(1)
75+
end
76+
end
77+
78+
context 'when the given class is the receiver' do
79+
it 'returns 0' do
80+
expect(PyCall.builtins.list <=> PyCall.builtins.list.__pyptr__).to eq(0)
81+
end
82+
end
83+
end
84+
85+
context 'when the value of other is a Class' do
86+
context 'when the given class is a superclass of the receiver' do
87+
it 'returns -1' do
88+
expect(PyCall.builtins.list <=> Object).to eq(-1)
89+
expect(PyCall.builtins.list <=> PyObjectWrapper).to eq(-1)
90+
end
91+
end
92+
93+
context 'when the given class is a subclass of the receiver' do
94+
let(:subclass) { Class.new(PyCall.builtins.list) }
95+
96+
it 'returns 1' do
97+
expect(PyCall.builtins.list <=> subclass).to eq(1)
98+
end
99+
end
100+
101+
context 'when the given class is neither a superclass or a subclass of the receiver' do
102+
it 'returns nil' do
103+
expect(PyCall.builtins.list <=> PyTypeObjectWrapper).to eq(nil)
104+
expect(PyCall.builtins.list <=> Array).to eq(nil)
105+
end
106+
end
107+
end
108+
109+
context 'when the other cases' do
110+
it 'returns nil' do
111+
expect(PyCall.builtins.list <=> Conversion.from_ruby(42)).to eq(nil)
112+
expect(PyCall.builtins.list <=> 42).to eq(nil)
113+
end
114+
end
115+
end
116+
44117
describe '#===' do
45118
specify do
46119
expect(PyCall.builtins.tuple === PyCall.tuple()).to eq(true)

0 commit comments

Comments
 (0)