Permalink
Browse files

Address gotcha wherein JRuby coerces numeric inputs to Java Float/Lon…

…g constructors
  • Loading branch information...
1 parent e3ee3ac commit 0ba1857e3d8b18326a93d768e721ddbb3dcad506 @mrwalker mrwalker committed Aug 12, 2011
Showing with 14 additions and 2 deletions.
  1. +6 −2 lib/cascading/expr_stub.rb
  2. +8 −0 spec/expr_spec.rb
@@ -109,8 +109,12 @@ def specific_to_java(value, type)
# would fail. We therefore punt if value is a String.
return value if value.kind_of?(::String)
case type
- when java.lang.Float.java_class then java.lang.Float.new(value) rescue value
- when java.lang.Long.java_class then java.lang.Long.new(value) rescue value
+ when java.lang.Float.java_class
+ return value if value.kind_of?(::Integer)
+ java.lang.Float.new(value) rescue value
+ when java.lang.Long.java_class
+ return value if value.kind_of?(::Float)
+ java.lang.Long.new(value) rescue value
else value
end
end
View
@@ -76,6 +76,14 @@
e = ExprStub.new('x:long + y:int')
lambda{ e.eval(:x => '2', :y => 3) }.should raise_error ExprArgException
+ # eval should not coerce floats to Java Longs
+ e = ExprStub.new('x:long + y:int')
+ lambda{ e.eval(:x => 2.0, :y => 3) }.should raise_error ExprArgException
+
+ # eval should not coerce integers to Java Floats
+ e = ExprStub.new('x:int + y:float')
+ lambda{ e.eval(:x => 2, :y => 3) }.should raise_error ExprArgException
+
e = ExprStub.new('x:float + y:int')
lambda{ e.eval(:x => 'blah', :y => 3) }.should raise_error ExprArgException

0 comments on commit 0ba1857

Please sign in to comment.