Skip to content

Commit

Permalink
add support for floating point comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Geers committed Apr 1, 2018
1 parent fafc32f commit a93b9d3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/jmespath/nodes/comparator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@ def check(left_value, right_value)

class Gt < Comparator
def check(left_value, right_value)
left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value > right_value
left_value.is_a?(Numeric) && right_value.is_a?(Numeric) && left_value > right_value
end
end

class Gte < Comparator
def check(left_value, right_value)
left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value >= right_value
left_value.is_a?(Numeric) && right_value.is_a?(Numeric) && left_value >= right_value
end
end

class Lt < Comparator
def check(left_value, right_value)
left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value < right_value
left_value.is_a?(Numeric) && right_value.is_a?(Numeric) && left_value < right_value
end
end

class Lte < Comparator
def check(left_value, right_value)
left_value.is_a?(Integer) && right_value.is_a?(Integer) && left_value <= right_value
left_value.is_a?(Numeric) && right_value.is_a?(Numeric) && left_value <= right_value
end
end
end
Expand Down
32 changes: 31 additions & 1 deletion spec/compliance/boolean.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@
"given": {
"one": 1,
"two": 2,
"three": 3
"three": 3,
"one_dot_zero": 1.0,
"two_dot_zero": 2.0
},
"cases": [
{
Expand Down Expand Up @@ -251,6 +253,34 @@
{
"expression": "two < one || three < one",
"result": false
},
{
"expression": "one_dot_zero < two_dot_zero",
"result": true
},
{
"expression": "two_dot_zero > one_dot_zero",
"result": true
},
{
"expression": "one_dot_zero <= one_dot_zero",
"result": true
},
{
"expression": "one_dot_zero >= one_dot_zero",
"result": true
},
{
"expression": "one_dot_zero == two_dot_zero",
"result": false
},
{
"expression": "one_dot_zero != two_dot_zero",
"result": true
},
{
"expression": "one_dot_zero == one_dot_zero",
"result": true
}
]
}
Expand Down
19 changes: 19 additions & 0 deletions spec/jmespath_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,24 @@ module JMESPath
expect(JMESPath.search('`1` < `2`', {})).to be(true)
end

context 'boolean comparison' do
it 'supports floating point numbers' do
expect(JMESPath.search('`1.0` == `1.0`', {})).to be true
expect(JMESPath.search('`2.0` != `1.0`', {})).to be true
expect(JMESPath.search('`2.0` > `1.0`', {})).to be true
expect(JMESPath.search('`1.0` < `2.0`', {})).to be true
expect(JMESPath.search('`2.0` >= `1.0`', {})).to be true
expect(JMESPath.search('`1.0` <= `2.0`', {})).to be true
expect(JMESPath.search('`1.0` <= `1.0`', {})).to be true
expect(JMESPath.search('`1.0` >= `1.0`', {})).to be true

expect(JMESPath.search('`2.0` == `1.0`', {})).to be false
expect(JMESPath.search('`1.0` != `1.0`', {})).to be false
expect(JMESPath.search('`1.0` > `1.0`', {})).to be false
expect(JMESPath.search('`2.0` < `2.0`', {})).to be false
expect(JMESPath.search('`1.0` >= `2.0`', {})).to be false
expect(JMESPath.search('`2.0` <= `1.0`', {})).to be false
end
end
end
end

0 comments on commit a93b9d3

Please sign in to comment.