rubysolo/dentaku

Bitwise operators & and | returning errors on non-integer arguments

sliiser opened this issue · 0 comments

The bitwise operators & and | naively just call that ruby method on it's arguments without any type checking. That results in a NoMethodError or a TypeError in a lot of cases. Due to them being similar to the boolean operators && and || it can easily happen by accident.

Current behaviour:

Dentaku.evaluate!('"foo" & "bar"') #=> NoMethodError: undefined method `&' for "foo":String
Dentaku.evaluate!('1.0 & "bar"') #=> NoMethodError: undefined method `&' for 0.1e1:BigDecimal
Dentaku.evaluate!('1 & "bar"') #=> TypeError: String can't be coerced into Integer

Expected behaviour:

Dentaku.evaluate!('"foo" & "bar"') #=> Dentaku::ArgumentError
Dentaku.evaluate!('1.0 & "bar"') #=>  Dentaku::ArgumentError
Dentaku.evaluate!('1 & "bar"') #=>  Dentaku::ArgumentError
Dentaku.evaluate('1.0 & "bar"') #=> nil

Dentaku::ParseError could also be an expected result if a non-compatible argument is passed explicitly, but the arguments could come from variables and be of unknown type at parse time.