diff --git a/lib/geocoder/results/test.rb b/lib/geocoder/results/test.rb index d029d1a71b5b8418a5fcf2d0eec6f3c08e565042..fd0c2e0e54302d590678566d38383658c617d89b 100644 --- a/lib/geocoder/results/test.rb +++ b/lib/geocoder/results/test.rb @@ -4,26 +4,12 @@ module Geocoder module Result class Test < Base - def address - @data['address'] + %w[latitude longitude city state state_code province + province_code postal_code country country_code address].each do |attr| + define_method(attr) do + @data[attr.to_s] || @data[attr.to_sym] + end end - - def state - @data['state'] - end - - def state_code - @data['state_code'] - end - - def country - @data['country'] - end - - def country_code - @data['country_code'] - end - end end end diff --git a/test/error_handling_test.rb b/test/error_handling_test.rb index ca4cec56c88727d33c796c57b1a19310d7e7fb85..9d84bdfdc008fe2d25671d36d052e3dd8fadaa40 100644 --- a/test/error_handling_test.rb +++ b/test/error_handling_test.rb @@ -10,7 +10,7 @@ class ErrorHandlingTest < Test::Unit::TestCase def test_does_not_choke_on_timeout # keep test output clean: suppress timeout warning orig = $VERBOSE; $VERBOSE = nil - all_lookups.each do |l| + all_lookups_except_test.each do |l| Geocoder::Configuration.lookup = l assert_nothing_raised { Geocoder.search("timeout") } end @@ -19,7 +19,7 @@ class ErrorHandlingTest < Test::Unit::TestCase def test_always_raise_timeout_error Geocoder::Configuration.always_raise = [TimeoutError] - all_lookups.each do |l| + all_lookups_except_test.each do |l| lookup = Geocoder.send(:get_lookup, l) assert_raises TimeoutError do lookup.send(:results, "timeout") @@ -29,7 +29,7 @@ class ErrorHandlingTest < Test::Unit::TestCase def test_always_raise_socket_error Geocoder::Configuration.always_raise = [SocketError] - all_lookups.each do |l| + all_lookups_except_test.each do |l| lookup = Geocoder.send(:get_lookup, l) assert_raises SocketError do lookup.send(:results, "socket_error") diff --git a/test/lookup_test.rb b/test/lookup_test.rb index 0b627c71cd8f4ed12cf878b837b838a8e19b1bc2..9cce80b7751c06470161fa5062c5399808abf08f 100644 --- a/test/lookup_test.rb +++ b/test/lookup_test.rb @@ -4,7 +4,7 @@ require 'test_helper' class LookupTest < Test::Unit::TestCase def test_search_returns_empty_array_when_no_results - all_lookups.each do |l| + all_lookups_except_test.each do |l| lookup = Geocoder.send(:get_lookup, l) assert_equal [], lookup.send(:results, "no results"), "Lookup #{l} does not return empty array when no results." diff --git a/test/result_test.rb b/test/result_test.rb index 6fa142073fbf2f15dfb09beda9c774c6b3b973e7..85bb0f4f7ee26bf2534a3cc3ce8562c7eecd58b3 100644 --- a/test/result_test.rb +++ b/test/result_test.rb @@ -4,7 +4,7 @@ require 'test_helper' class ResultTest < Test::Unit::TestCase def test_result_has_required_attributes - all_lookups.each do |l| + all_lookups_except_test.each do |l| Geocoder::Configuration.lookup = l result = Geocoder.search([45.423733, -75.676333]).first assert_result_has_required_attributes(result) diff --git a/test/test_helper.rb b/test/test_helper.rb index 864a819c0cbd434638002d3bc152e21c70d9e5ce..8ac6b3e0a6a939e7635eca5e6f63479a532c4a4c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -281,6 +281,10 @@ class Test::Unit::TestCase Geocoder.valid_lookups end + def all_lookups_except_test + Geocoder.valid_lookups - [:test] + end + def street_lookups all_lookups - [:freegeoip] end diff --git a/test/test_mode_test.rb b/test/test_mode_test.rb index ec8b4db1504a3efeea1bb4db8ad34d91bbe9ddc8..32dcd54660c764dcf1b8e6db0287b5bfcf3cf9c2 100644 --- a/test/test_mode_test.rb +++ b/test/test_mode_test.rb @@ -5,6 +5,7 @@ class TestModeTest < Test::Unit::TestCase def setup @_original_lookup = Geocoder::Configuration.lookup + Geocoder::Configuration.lookup = :test end def teardown @@ -13,17 +14,17 @@ class TestModeTest < Test::Unit::TestCase end def test_search_with_known_stub - Geocoder::Configuration.lookup = :test + coordinates = [40.7143528, -74.0059731] attributes = { - 'latitude' => 40.7143528, - 'longitude' => -74.0059731, - 'address' => 'New York, NY, USA', - 'state' => 'New York', - 'state_code' => 'NY', - 'country' => 'United States', + 'coordinates' => coordinates, + 'latitude' => coordinates[0], + 'longitude' => coordinates[1], + 'address' => 'New York, NY, USA', + 'state' => 'New York', + 'state_code' => 'NY', + 'country' => 'United States', 'country_code' => 'US', } - coordinates = [attributes['latitude'], attributes['longitude']] Geocoder::Lookup::Test.add_stub("New York, NY", [attributes]) @@ -42,8 +43,6 @@ class TestModeTest < Test::Unit::TestCase end def test_search_with_unknown_stub - Geocoder::Configuration.lookup = :test - assert_raise ArgumentError do Geocoder.search("New York, NY") end