Skip to content

Commit

Permalink
Fixed "Comparable#== will no more rescue exceptions of #<=>" (bsc#933…
Browse files Browse the repository at this point in the history
…470)

Term and Path would raise exceptions when <=> was used to compare them
with something that was not a Term or Path.

In particular it happened during RSpec argument matching.

In older Ruby core, Comparable#== would silently mask such exceptions,
now it started producing warnings.
https://bugs.ruby-lang.org/issues/7688
  • Loading branch information
mvidner committed Jun 3, 2015
1 parent f088689 commit 6455806
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/ruby/yast/path.rb
Expand Up @@ -43,6 +43,7 @@ def empty?
end

def <=>(other)
return nil unless other.is_a? self.class
0.upto(size-1) do |i|
return 1 unless other.send(:components)[i]
#we strip enclosing quotes for complex expression
Expand Down
1 change: 1 addition & 0 deletions src/ruby/yast/term.rb
Expand Up @@ -88,6 +88,7 @@ def to_s
end

def <=> (other)
return nil unless other.is_a? self.class
res = value <=> other.value
return res if res != 0

Expand Down
5 changes: 5 additions & 0 deletions tests/ruby/path_spec.rb
Expand Up @@ -48,6 +48,11 @@
expect(Yast::Path.new('.ba')).to be >= Yast::Path.new('."a?"')
expect(Yast::Path.new('."b?"')).to be >= Yast::Path.new('.ab')
end

# bsc#933470
it "survives comparison with a non-Path" do
expect(Yast::Path.new('.foo') <=> 42).to eq nil
end
end

describe "#clone" do
Expand Down
12 changes: 6 additions & 6 deletions tests/ruby/term_spec.rb
@@ -1,10 +1,6 @@
#!/usr/bin/env rspec
# encoding: utf-8

# FIXME: this file was autoconverted from test/unit syntax without
# adjusting it to good RSpec style (http://betterspecs.org/).
# Please improve it whenever adding examples.

require_relative "test_helper"

require "yast/term"
Expand Down Expand Up @@ -47,15 +43,15 @@ def term(*params)
end
end

describe "#<<" do
describe "#<<" do # " <- unconfuse Emacs string highlighting
it "appends parameter to params" do
t = term(:HBox, 1, 2)
t << 3
expect(t[2]).to eq(3)
end
end

describe "comparison" do
describe "#<=> (comparison)" do
it "if value and params are equal, then it is equal terms" do
expect(term(:HBox)).to eq(term(:HBox))
end
Expand All @@ -75,6 +71,10 @@ def term(*params)
it "if value is equal, then use params to comparison" do
expect(term(:HBox, "test")).to be > term(:HBox)
end

it "if non-term, then uncomparable" do
expect(term(:HBox, "test") <=> 42).to eq nil
end
end

describe "#size" do
Expand Down

0 comments on commit 6455806

Please sign in to comment.