Sha256: b9fcb79fe6352464a5f06b4e83a0111a30557344db1322040078ce11d9d2d6ab

Contents?: true

Size: 1.68 KB

Versions: 6

Compression:

Stored size: 1.68 KB

Contents

# encoding: UTF-8
require 'test_helper'

module Graticule
  module Geocoder
    class MultiTest < Test::Unit::TestCase

      def setup
        @mock_geocoders = [mock("geocoder 1"), mock("geocoder 2")]
        @mock_geocoders.each {|g| g.stubs(:locate) }
        @geocoder = Multi.new(*@mock_geocoders)
      end

      def test_locate_calls_each_geocoder_and_raises_error
        @mock_geocoders.each do |g|
          g.expects(:locate).with('test').raises(Graticule::AddressError)
        end
        assert_raises(Graticule::AddressError) { @geocoder.locate 'test' }
      end

      def test_locate_returns_first_result_without_calling_others
        result = mock("result")
        @mock_geocoders.first.expects(:locate).returns(result)
        @mock_geocoders.last.expects(:locate).never
        assert_equal result, @geocoder.locate('test')
      end

      def test_locate_with_custom_block
        @mock_geocoders.first.expects(:locate).returns(1)
        @mock_geocoders.last.expects(:locate).returns(2)
        @geocoder = Multi.new(*@mock_geocoders) {|r| r == 2 }
        assert_equal 2, @geocoder.locate('test')
      end

      def test_locate_with_custom_block_and_no_match
        @mock_geocoders.first.expects(:locate).returns(1)
        @mock_geocoders.last.expects(:locate).returns(2)
        @geocoder = Multi.new(*@mock_geocoders) {|r| r == 3 }
        assert_raises(Graticule::AddressError) { @geocoder.locate('test') }
      end

      def test_timeout
        @mock = @mock_geocoders.first
        def @mock.locate(*x)
          sleep 1
        end
        @geocoder = Multi.new(@mock, :timeout => 0.1)
        assert_raise(Timeout::Error) { @geocoder.locate('foo') }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graticule-2.7.2 test/graticule/geocoder/multi_test.rb
graticule-2.7.1 test/graticule/geocoder/multi_test.rb
graticule-2.7.0 test/graticule/geocoder/multi_test.rb
graticule-2.6.0 test/graticule/geocoder/multi_test.rb
graticule-2.5.0 test/graticule/geocoder/multi_test.rb
graticule-2.4.0 test/graticule/geocoder/multi_test.rb