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 e37b72a commit a515c51
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/pycall/pytypeobject_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ def <=>(other)
nil
end

def <(other)
cmp = self <=> other
cmp && cmp < 0
end

def >(other)
cmp = self <=> other
cmp && cmp > 0
end

def <=(other)
cmp = self <=> other
cmp && cmp <= 0
end

def >=(other)
cmp = self <=> other
cmp && cmp >= 0
end

private

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

describe '#<' do
specify do
expect(PyCall.builtins.list < PyCall.builtins.list).to eq(false)
expect(PyCall.builtins.list < PyCall.builtins.object).to eq(true)
expect(PyCall.builtins.object < PyCall.builtins.list).to eq(false)
expect(PyCall.builtins.list < PyCall.builtins.dict).to eq(nil)
expect(PyCall.builtins.list < Object).to eq(true)
expect(PyCall.builtins.list < Array).to eq(nil)
expect(PyCall.builtins.list < Conversion.from_ruby(42)).to eq(nil)
expect(PyCall.builtins.list < 42).to eq(nil)
end
end

describe '#>' do
specify do
expect(PyCall.builtins.list > PyCall.builtins.list).to eq(false)
expect(PyCall.builtins.list > PyCall.builtins.object).to eq(false)
expect(PyCall.builtins.object > PyCall.builtins.list).to eq(true)
expect(PyCall.builtins.list > PyCall.builtins.dict).to eq(nil)
expect(PyCall.builtins.list > Object).to eq(false)
expect(PyCall.builtins.list > Array).to eq(nil)
expect(PyCall.builtins.list > Conversion.from_ruby(42)).to eq(nil)
expect(PyCall.builtins.list > 42).to eq(nil)
end
end

describe '#<=' do
specify do
expect(PyCall.builtins.list <= PyCall.builtins.list).to eq(true)
expect(PyCall.builtins.list <= PyCall.builtins.object).to eq(true)
expect(PyCall.builtins.object <= PyCall.builtins.list).to eq(false)
expect(PyCall.builtins.list <= PyCall.builtins.dict).to eq(nil)
expect(PyCall.builtins.list <= Object).to eq(true)
expect(PyCall.builtins.list <= Array).to eq(nil)
expect(PyCall.builtins.list <= Conversion.from_ruby(42)).to eq(nil)
expect(PyCall.builtins.list <= 42).to eq(nil)
end
end

describe '#>=' do
specify do
expect(PyCall.builtins.list >= PyCall.builtins.list).to eq(true)
expect(PyCall.builtins.list >= PyCall.builtins.object).to eq(false)
expect(PyCall.builtins.object >= PyCall.builtins.list).to eq(true)
expect(PyCall.builtins.list >= PyCall.builtins.dict).to eq(nil)
expect(PyCall.builtins.list >= Object).to eq(false)
expect(PyCall.builtins.list >= Array).to eq(nil)
expect(PyCall.builtins.list >= Conversion.from_ruby(42)).to eq(nil)
expect(PyCall.builtins.list >= 42).to eq(nil)
end
end

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

0 comments on commit a515c51

Please sign in to comment.