Skip to content

Commit

Permalink
Loosen up the Geos::Geometry#== operator on non-Geometry comparisons.
Browse files Browse the repository at this point in the history
The #eql? method still behaves strictly and does type checking as
before, but the == operator has been loosened to allow for more
generalized comparisons, akin to comparing numeric values to Strings and
the like.
  • Loading branch information
dark-panda committed Jan 3, 2014
1 parent dedad78 commit 77f12f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/ffi-geos/geometry.rb
Expand Up @@ -332,9 +332,16 @@ def eql?(geom)
check_geometry(geom)
bool_result(FFIGeos.GEOSEquals_r(Geos.current_handle, self.ptr, geom.ptr))
end
alias :== :eql?
alias :equals? :eql?

def ==(geom)
if geom.is_a?(Geos::Geometry)
self.eql?(geom)
else
false
end
end

def eql_exact?(geom, tolerance)
check_geometry(geom)
bool_result(FFIGeos.GEOSEqualsExact_r(Geos.current_handle, self.ptr, geom.ptr, tolerance))
Expand Down
12 changes: 11 additions & 1 deletion test/geometry_tests.rb
Expand Up @@ -1581,11 +1581,21 @@ def test_eql
geom_a = read('POINT(1.0 1.0)')
geom_b = read('POINT(2.0 2.0)')

%w{ eql? equals? == }.each do |method|
%w{ eql? equals? }.each do |method|
assert(geom_a.send(method, geom_a), "Expected geoms to be equal using #{method}")
refute(geom_a.send(method, geom_b), "Expected geoms to not be equal using #{method}")
end
end

def test_equals_operator
geom_a = read('POINT(1.0 1.0)')
geom_b = read('POINT(2.0 2.0)')

assert(geom_a == geom_a, "Expected geoms to be equal using ==")
refute(geom_a == geom_b, "Expected geoms to not be equal using ==")
refute(geom_a == "test", "Expected geoms to not be equal using ==")
end

def test_eql_exact
geom_a = read('POINT(1.0 1.0)')
geom_b = read('POINT(2.0 2.0)')
Expand Down

0 comments on commit 77f12f8

Please sign in to comment.