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

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

View file

@ -7,11 +7,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

@ -211,6 +211,16 @@ public final class JrubyTimestampExtLibrary {
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;
}
private static RubyTimestamp fromRString(final Ruby runtime, final RubyString string) {
return RubyTimestamp.newRubyTimestamp(runtime, new Timestamp(string.toString()));
}