# tillless-core/spec/tillless-core/location_spec.rb require 'spec_helper' module Tillless describe Location do before(:all) do # 127 Talga Road Rothbury NSW Australia @talgard_lat = -32.73075 @talgard_lon = 151.37991 # North Sydney Post Office, North Sydney NSW Australia @northsydpo_lat = -33.8384945 @northsydpo_lon = 151.2064807 # 2/10 Holdsworth Street Neutral Bay NSW Australia @holdsworthst_lat = -33.838236 @holdsworthst_lon = 151.214478 # 34 Ryan Street Lilyfield NSW 2040 Australia @ryanst_lat = -33.870203 @ryanst_lon = 151.167256 class MockLocation include Location # Create a new MockLocation end end before(:each) do @talgard = MockLocation.new(lat: @talgard_lat, lon: @talgard_lon) @northsydpo = MockLocation.new(lat: @northsydpo_lat, lon: @northsydpo_lon) @holdsworthst = MockLocation.new(lat: @holdsworthst_lat, lon: @holdsworthst_lon) @ryanst = MockLocation.new(lat: @ryanst_lat, lon: @ryanst_lon) end it 'should have lattitude and longitude' do @talgard.lat.should_not be nil @talgard.lon.should_not be nil @northsydpo.lat.should_not be nil @northsydpo.lon.should_not be nil @holdsworthst.lat.should_not be nil @holdsworthst.lon.should_not be nil @ryanst.lat.should_not be nil @ryanst.lon.should_not be nil end it 'should be !calibrated if lat and/or lon change' do @loc1 = MockLocation.new(lat: @talgard_lat, lon: @talgard_lon) @loc1.lat = @holdsworthst_lat @loc1.calibrated.should be false @loc1.calibrate @loc1.calibrated.should be true @loc1.lon = @holdsworthst_lon @loc1.calibrated.should be false @loc1.calibrate @loc1.calibrated.should be true end it 'should be calibrated if lat/lon set via set_and_calibrate' do @loc1 = MockLocation.new(lat: @talgard_lat, lon: @talgard_lon) @loc1.calibrated.should be true @loc1.set_and_calibrate(@holdsworthst_lat, @holdsworthst_lon) @loc1.calibrated.should be true end it 'should raise an exception if nearby? used on uncalibrated locations' do @loc1 = MockLocation.new(lat: @talgard_lat, lon: @talgard_lon) @loc1.lat = @holdsworthst_lat @loc1.calibrated.should be false expect { @loc1.nearby?(nil) }.to raise_error(Tillless::Location::LocationCalibrationException) @loc1.calibrate @loc1.lon = @holdsworthst_lon @loc1.calibrated.should be false expect { @loc1.nearby?(nil) }.to raise_error(Tillless::Location::LocationCalibrationException) end it 'should raise an exception if great_circle_distance_from_here_to used on uncalibrated locations' do @loc1 = MockLocation.new(lat: @talgard_lat, lon: @talgard_lon) @loc1.lat = @holdsworthst_lat @loc1.calibrated.should be false expect { @loc1.great_circle_distance_from_here_to(nil) }.to raise_error(Tillless::Location::LocationCalibrationException) @loc1.calibrate @loc1.lon = @holdsworthst_lon @loc1.calibrated.should be false expect { @loc1.great_circle_distance_from_here_to(nil) }.to raise_error(Tillless::Location::LocationCalibrationException) end it 'should be nearby if lat/lon are identical' do @loc1 = MockLocation.new(lat: @talgard_lat, lon: @talgard_lon) @loc2 = MockLocation.new(lat: @talgard_lat, lon: @talgard_lon) @loc1.lat.should == @loc2.lat @loc2.lat.should == @loc1.lat @loc1.lon.should == @loc2.lon @loc2.lon.should == @loc1.lon @loc1.nearby?(@loc2).should == true @loc2.nearby?(@loc1).should == true end it 'should not be nearby if lat/lon are different' do @loc1 = MockLocation.new(lat: @talgard_lat, lon: @talgard_lon) @loc2 = MockLocation.new(lat: @holdsworthst_lat, lon: @holdsworthst_lon) @loc1.lat.should_not eq(@loc2.lat) @loc2.lat.should_not eq(@loc1.lat) @loc1.lon.should_not eq(@loc2.lon) @loc2.lon.should_not eq(@loc1.lon) @loc1.nearby?(@loc2).should == false @loc2.nearby?(@loc1).should == false end it 'should be nearby if loc within min_distance for nearby?' do @loc1 = MockLocation.new(lat: @talgard_lat, lon: @talgard_lon) @loc2 = MockLocation.new(lat: @holdsworthst_lat, lon: @holdsworthst_lon) distance1 = @loc1.great_circle_distance_from_here_to(@loc2) distance2 = @loc2.great_circle_distance_from_here_to(@loc1) distance1.should == distance2 distance2.should == distance1 @loc1.nearby?(@loc2, distance1+1).should == true @loc2.nearby?(@loc1, distance1+1).should == true end it 'should not be nearby if loc greater than min_distance for nearby?' do @loc1 = MockLocation.new(lat: @talgard_lat, lon: @talgard_lon) @loc2 = MockLocation.new(lat: @holdsworthst_lat, lon: @holdsworthst_lon) distance1 = @loc1.great_circle_distance_from_here_to(@loc2) distance2 = @loc2.great_circle_distance_from_here_to(@loc1) distance1.should == distance2 distance2.should == distance1 @loc1.nearby?(@loc2, distance1-1).should == false @loc2.nearby?(@loc1, distance1-1).should == false end end end