#8728 Fix RubyTimestamp's '<=>' implementation to correctly compare to other types

Fixes #8730

Fixes #8733
This commit is contained in:
Armin 2017-11-27 14:34:49 +01:00 committed by Armin Braun
parent 7537412cda
commit d0dce28b21
3 changed files with 15 additions and 5 deletions

View file

@ -8,11 +8,6 @@ module LogStash
class Timestamp
include Comparable
# TODO (colin) implement in Java
def <=>(other)
self.time <=> other.time
end
def eql?(other)
self.== other
end

View file

@ -36,6 +36,11 @@ describe LogStash::Timestamp do
expect{LogStash::Timestamp.new("foobar")}.to raise_error
end
it "compares to any type" do
t = LogStash::Timestamp.new
expect(t == '-').to be_falsey
end
end
end

View file

@ -254,5 +254,15 @@ public class JrubyTimestampExtLibrary implements Library {
{
return RubyFixnum.newFixnum(context.runtime, this.timestamp.getTime().getYear());
}
@JRubyMethod(name = "<=>", required = 1)
public IRubyObject op_cmp(final ThreadContext context, final IRubyObject other) {
if (other instanceof JrubyTimestampExtLibrary.RubyTimestamp) {
return ((RubyTime) ruby_time(context)).op_cmp(
context, ((JrubyTimestampExtLibrary.RubyTimestamp) other).ruby_time(context)
);
}
return context.nil;
}
}
}