Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix subclass behaviors #30

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add PyTypeObjectWrapper#subclass?
mrkn committed Sep 11, 2017
commit 29a04fc409e2e2a7c037fff2498be09aa58c716b
11 changes: 11 additions & 0 deletions lib/pycall/pytypeobject_wrapper.rb
Original file line number Diff line number Diff line change
@@ -49,6 +49,17 @@ def ===(other)
end
end

def subclass?(other)
case other
when PyTypeObjectWrapper
__pyptr__.subclass?(other.__pyptr__)
when Class, Module
other >= self || false
else
__pyptr__.subclass?(other)
end
end

private

def register_python_type_mapping
31 changes: 31 additions & 0 deletions spec/pycall/pytypeobject_wrapper_spec.rb
Original file line number Diff line number Diff line change
@@ -10,6 +10,37 @@ module PyCall
PyCall.wrap_class(simple_class)
end

describe '#subclass?(other)' do
subject { PyCall.builtins.list }

context 'when the value of other is a PyTypeObjectWrapper' do
specify do
expect(subject.subclass?(PyCall.builtins.object)).to eq(true)
expect(subject.subclass?(PyCall.builtins.list)).to eq(true)
expect(subject.subclass?(PyCall.builtins.dict)).to eq(false)
end
end

context 'when the value of other is a Class' do
specify do
expect(subject.subclass?(Object)).to eq(true)
expect(subject.subclass?(PyObjectWrapper)).to eq(true)
expect(subject.subclass?(PyTypeObjectWrapper)).to eq(false)
expect(subject.subclass?(Array)).to eq(false)
end
end

context 'when the other cases' do
it 'behaves as well as PyTypePtr#subclass?' do
expect(subject.subclass?(PyCall.builtins.object.__pyptr__)).to eq(true)
expect(subject.subclass?(PyCall.builtins.list.__pyptr__)).to eq(true)
expect(subject.subclass?(PyCall.builtins.dict.__pyptr__)).to eq(false)
expect { subject.subclass?(Conversion.from_ruby(12)) }.to raise_error(TypeError)
expect { subject.subclass?(12) }.to raise_error(TypeError)
end
end
end

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