Skip to content

Commit

Permalink
Add PyTypeObjectWrapper#<=>
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkn committed Sep 11, 2017
1 parent 29a04fc commit e37b72a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/pycall/pytypeobject_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ def subclass?(other)
end
end

def <=>(other)
return 0 if equal?(other)
case other
when PyTypeObjectWrapper
return super if __pyptr__ == other.__pyptr__
other = other.__pyptr__
when Class, Module
return -1 if subclass?(other)
return 1 if other > self
end

return nil unless other.is_a?(PyTypePtr)
return 0 if __pyptr__ == other
return -1 if __pyptr__.subclass?(other)
return 1 if other.subclass?(__pyptr__)
nil
end

private

def register_python_type_mapping
Expand Down
73 changes: 73 additions & 0 deletions spec/pycall/pytypeobject_wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,79 @@ module PyCall
end
end

describe '#<=>(other)' do
context 'when the value of other is a PyTypeObjectWrapper' do
context 'when the given class is a superclass in Python of the receiver' do
it 'returns -1' do
expect(PyCall.builtins.list <=> PyCall.builtins.object).to eq(-1)
end
end

context 'when the given class is a subclass in Python of the receiver' do
it 'returns 1' do
expect(PyCall.builtins.object <=> PyCall.builtins.list).to eq(1)
end
end

context 'when the given class is the receiver' do
it 'returns 0' do
expect(PyCall.builtins.list <=> PyCall.builtins.list).to eq(0)
end
end
end

context 'when the value of other is a PyTypePtr' do
context 'when the given class is a superclass in Python of the receiver' do
it 'returns -1' do
expect(PyCall.builtins.list <=> PyCall.builtins.object.__pyptr__).to eq(-1)
end
end

context 'when the given class is a subclass in Python of the receiver' do
it 'returns 1' do
expect(PyCall.builtins.object <=> PyCall.builtins.list.__pyptr__).to eq(1)
end
end

context 'when the given class is the receiver' do
it 'returns 0' do
expect(PyCall.builtins.list <=> PyCall.builtins.list.__pyptr__).to eq(0)
end
end
end

context 'when the value of other is a Class' do
context 'when the given class is a superclass of the receiver' do
it 'returns -1' do
expect(PyCall.builtins.list <=> Object).to eq(-1)
expect(PyCall.builtins.list <=> PyObjectWrapper).to eq(-1)
end
end

context 'when the given class is a subclass of the receiver' do
let(:subclass) { Class.new(PyCall.builtins.list) }

it 'returns 1' do
expect(PyCall.builtins.list <=> subclass).to eq(1)
end
end

context 'when the given class is neither a superclass or a subclass of the receiver' do
it 'returns nil' do
expect(PyCall.builtins.list <=> PyTypeObjectWrapper).to eq(nil)
expect(PyCall.builtins.list <=> Array).to eq(nil)
end
end
end

context 'when the other cases' do
it 'returns nil' do
expect(PyCall.builtins.list <=> Conversion.from_ruby(42)).to eq(nil)
expect(PyCall.builtins.list <=> 42).to eq(nil)
end
end
end

describe '#===' do
specify do
expect(PyCall.builtins.tuple === PyCall.tuple()).to eq(true)
Expand Down

0 comments on commit e37b72a

Please sign in to comment.