Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add everything

  • Loading branch information...
commit 702a017f9caec7d104c4230eae4b8103adc80b06 1 parent 8ad3ac6
@greatseth authored
Showing with 4,637 additions and 0 deletions.
  1. +6 −0 CHANGELOG
  2. +19 −0 LICENSE
  3. +35 −0 README
  4. +24 −0 Rakefile
  5. +194 −0 lib/google/geo.rb
  6. +112 −0 test/fixtures/.svn/entries
  7. +1 −0  test/fixtures/.svn/format
  8. +10 −0 test/fixtures/.svn/text-base/invalid_map_key.xml.svn-base
  9. +10 −0 test/fixtures/.svn/text-base/missing_address.xml.svn-base
  10. +10 −0 test/fixtures/.svn/text-base/server_error.xml.svn-base
  11. +36 −0 test/fixtures/.svn/text-base/success.xml.svn-base
  12. +10 −0 test/fixtures/.svn/text-base/too_many_queries.xml.svn-base
  13. +10 −0 test/fixtures/.svn/text-base/unavailable_address.xml.svn-base
  14. +10 −0 test/fixtures/.svn/text-base/unknown_address.xml.svn-base
  15. +10 −0 test/fixtures/invalid_map_key.xml
  16. +10 −0 test/fixtures/missing_address.xml
  17. +10 −0 test/fixtures/server_error.xml
  18. +36 −0 test/fixtures/success.xml
  19. +62 −0 test/fixtures/success_with_multiple_addresses.xml
  20. +10 −0 test/fixtures/too_many_queries.xml
  21. +10 −0 test/fixtures/unavailable_address.xml
  22. +10 −0 test/fixtures/unknown_address.xml
  23. +102 −0 test/geo_test.rb
  24. +3 −0  vendor/mocha-0.4.0/COPYING
  25. +7 −0 vendor/mocha-0.4.0/MIT-LICENSE
  26. +35 −0 vendor/mocha-0.4.0/README
  27. +98 −0 vendor/mocha-0.4.0/RELEASE
  28. +126 −0 vendor/mocha-0.4.0/Rakefile
  29. +36 −0 vendor/mocha-0.4.0/examples/misc.rb
  30. +26 −0 vendor/mocha-0.4.0/examples/mocha.rb
  31. +65 −0 vendor/mocha-0.4.0/examples/stubba.rb
  32. +19 −0 vendor/mocha-0.4.0/lib/mocha.rb
  33. +35 −0 vendor/mocha-0.4.0/lib/mocha/any_instance_method.rb
  34. +113 −0 vendor/mocha-0.4.0/lib/mocha/auto_verify.rb
  35. +35 −0 vendor/mocha-0.4.0/lib/mocha/central.rb
  36. +62 −0 vendor/mocha-0.4.0/lib/mocha/class_method.rb
  37. +295 −0 vendor/mocha-0.4.0/lib/mocha/expectation.rb
  38. +6 −0 vendor/mocha-0.4.0/lib/mocha/expectation_error.rb
  39. +27 −0 vendor/mocha-0.4.0/lib/mocha/infinite_range.rb
  40. +37 −0 vendor/mocha-0.4.0/lib/mocha/inspect.rb
  41. +8 −0 vendor/mocha-0.4.0/lib/mocha/instance_method.rb
  42. +7 −0 vendor/mocha-0.4.0/lib/mocha/metaclass.rb
  43. +20 −0 vendor/mocha-0.4.0/lib/mocha/mock.rb
  44. +122 −0 vendor/mocha-0.4.0/lib/mocha/mock_methods.rb
  45. +100 −0 vendor/mocha-0.4.0/lib/mocha/object.rb
  46. +28 −0 vendor/mocha-0.4.0/lib/mocha/pretty_parameters.rb
  47. +23 −0 vendor/mocha-0.4.0/lib/mocha/setup_and_teardown.rb
  48. +30 −0 vendor/mocha-0.4.0/lib/mocha/standalone.rb
  49. +49 −0 vendor/mocha-0.4.0/lib/mocha/test_case_adapter.rb
  50. +2 −0  vendor/mocha-0.4.0/lib/mocha_standalone.rb
  51. +2 −0  vendor/mocha-0.4.0/lib/stubba.rb
  52. +36 −0 vendor/mocha-0.4.0/test/active_record_test_case.rb
  53. +75 −0 vendor/mocha-0.4.0/test/all_tests.rb
  54. +34 −0 vendor/mocha-0.4.0/test/execution_point.rb
  55. +18 −0 vendor/mocha-0.4.0/test/method_definer.rb
  56. +124 −0 vendor/mocha-0.4.0/test/mocha/any_instance_method_test.rb
  57. +163 −0 vendor/mocha-0.4.0/test/mocha/auto_verify_test.rb
  58. +124 −0 vendor/mocha-0.4.0/test/mocha/central_test.rb
  59. +196 −0 vendor/mocha-0.4.0/test/mocha/class_method_test.rb
  60. +357 −0 vendor/mocha-0.4.0/test/mocha/expectation_test.rb
  61. +50 −0 vendor/mocha-0.4.0/test/mocha/infinite_range_test.rb
  62. +90 −0 vendor/mocha-0.4.0/test/mocha/inspect_test.rb
  63. +22 −0 vendor/mocha-0.4.0/test/mocha/metaclass_test.rb
  64. +235 −0 vendor/mocha-0.4.0/test/mocha/mock_methods_test.rb
  65. +84 −0 vendor/mocha-0.4.0/test/mocha/mock_test.rb
  66. +165 −0 vendor/mocha-0.4.0/test/mocha/object_test.rb
  67. +32 −0 vendor/mocha-0.4.0/test/mocha/pretty_parameters_test.rb
  68. +76 −0 vendor/mocha-0.4.0/test/mocha/setup_and_teardown_test.rb
  69. +98 −0 vendor/mocha-0.4.0/test/mocha_acceptance_test.rb
  70. +105 −0 vendor/mocha-0.4.0/test/mocha_test_result_integration_test.rb
  71. +110 −0 vendor/mocha-0.4.0/test/standalone_acceptance_test.rb
  72. +102 −0 vendor/mocha-0.4.0/test/stubba_acceptance_test.rb
  73. +89 −0 vendor/mocha-0.4.0/test/stubba_integration_test.rb
  74. +85 −0 vendor/mocha-0.4.0/test/stubba_test_result_integration_test.rb
  75. +4 −0 vendor/mocha-0.4.0/test/test_helper.rb
View
6 CHANGELOG
@@ -0,0 +1,6 @@
+= 2.0 Multiple Addresses (and tests you can actually run!)
+ - Not insignificant API change: *** Google::Geo#locate ALWAYS RETURNS AN ARRAY ***
+ - There are now some nice examples in the README.
+ - Updated to include the fixtures and vendor dependencies for tests, yeknow, so you can actually run them.
+ - No namby pamby point releases. This is serious, folks.
+= 1.0 Initial Release
View
19 LICENSE
@@ -0,0 +1,19 @@
+This program is free software. It comes without any warranty, to
+the extent permitted by applicable law. You can redistribute it
+and/or modify it under the terms of the Do What The Fuck You Want
+To Public License, Version 2, as published by Sam Hocevar. See
+http://sam.zoy.org/wtfpl/COPYING for more details.
+
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ Version 2, December 2004
+
+Copyright (C) 2007-2008 Seth Thomas Rasmussen sethrasmussen@gmail.com
+
+Everyone is permitted to copy and distribute verbatim or modified
+copies of this license document, and changing it is allowed as long
+as the name is changed.
+
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
View
35 README
@@ -0,0 +1,35 @@
+= Google::Geo
+
+A simple, elegant library for getting geocoding information from Google Maps. Very much inspired by the google-geocode gem, but completely dependency free!
+
+== Examples
+
+ geo = Google::Geo.new API_KEY
+
+ addresses = geo.locate '1600 Amphitheatre Parkway, Mountain View, CA'
+
+ addresses.size # 1, :locate always returns an Array
+
+ address = addresses.first
+
+ address.country # 'US'
+ address.city # 'Mountain View'
+ address.full_address # '1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA'
+
+ address.query # '1600 Amphitheatre Parkway, Mountain View, CA'
+ address.accuracy # 8
+
+In the case of sufficiently vague queries, Google::Geo will return more than one:
+
+ addresses = geo.locate 'hell'
+
+ addresses.size # 2
+ addresses.map { |a| a.state } # ['PA', 'NC']
+
+== Contributors
+
+Seth Thomas Rasmussen - http://greatseth.com - sethrasmussen@gmail.com
+
+== License
+
+(c) 2007-2008 Seth Thomas Rasmussen and released under the WTFPL. See the LICENSE file for details.
View
24 Rakefile
@@ -0,0 +1,24 @@
+require 'rubygems'
+Gem.manage_gems
+require 'rake/gempackagetask'
+
+README = File.readlines("#{File.dirname __FILE__}/README")
+
+spec = Gem::Specification.new do |s|
+ s.name = 'google-geo'
+ s.version = '2.1'
+ s.summary = README[2]
+ s.files = FileList['{lib}/**/*'].to_a + FileList['{test}/**/*'].to_a + FileList['{vendor}/**/*'].to_a
+ s.require_path = 'lib'
+ # s.autorequire = 'google/geo'
+ s.test_files = FileList['{test}/**/*test.rb'].to_a
+ s.has_rdoc = true
+ s.extra_rdoc_files = %w( README CHANGELOG )
+ s.author, s.homepage, s.email = README[README.index(README.grep(/contributors/i)[0])+1].split(' - ')
+end
+
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.need_tar = true
+end
+
+task :default => "pkg/#{spec.name}-#{spec.version}.gem"
View
194 lib/google/geo.rb
@@ -0,0 +1,194 @@
+require 'open-uri'
+
+module Google
+
+# = Google::Geo
+#
+# A simple, elegant library for getting geocoding information from Google Maps. Very much inspired by the google-geocode gem, but completely dependency free!
+#
+# == Examples
+#
+# geo = Google::Geo.new API_KEY
+#
+# addresses = geo.locate '1600 Amphitheatre Parkway, Mountain View, CA'
+#
+# addresses.size # 1, :locate always returns an Array
+#
+# address = addresses.first
+#
+# address.country # 'US'
+# address.city # 'Mountain View'
+# address.full_address # '1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA'
+#
+# address.query # '1600 Amphitheatre Parkway, Mountain View, CA'
+# address.accuracy # 8
+#
+# In the case of sufficiently vague queries, Google will return more than one address:
+#
+# addresses = geo.locate 'heaven'
+#
+# addresses.size # 2
+# addresses.map { |a| a.state } # ['PA', 'NC']
+#
+# == Contributors
+#
+# Seth Thomas Rasmussen - http://greatseth.com - sethrasmussen@gmail.com
+class Geo
+ # API key provided by Google allowing access to the Geocode API
+ attr_accessor :key
+
+ def initialize(key)
+ @key = key
+ end
+
+ # Returns an array of Address objects, each with accessors for all the components of a location.
+ # The query argument should be a string.
+ def locate(query)
+ xml = open(uri(query)).read
+ res = Response.new(xml, key)
+
+ res.placemarks.map { |place| Address.new place, res.query }
+ end
+
+ # Generate a request URI from a given search string.
+ def uri(address) #:nodoc:
+ "http://maps.google.com/maps/geo?q=#{URI.escape address}&key=#{key}&output=xml"
+ end
+ private :uri
+
+ ###
+
+ module Parser #:nodoc:
+ # Fetch contents of an XML element of the response.
+ def fetch(element) #:nodoc:
+ @xml.slice %r{<#{element}>(.+?)</#{element}>}, 1
+ end
+
+ # Like fetch, but for the only piece of data locked away in an attribute.
+ def fetch_accuracy #:nodoc:
+ @xml.slice(%r{Accuracy="([^"]+)">}, 1).to_i
+ end
+ end
+
+ ###
+
+ # Represents locations returned in response to geocoding queries.
+ class Address
+ include Parser
+
+ attr_reader :street
+ alias :thoroughfare :street
+
+ attr_reader :city
+ alias :locality :city
+
+ attr_reader :zip
+ alias :postal_code :zip
+
+ attr_reader :county
+ alias :subadministrative_area :county
+
+ attr_reader :state
+ alias :administrative_area :state
+
+ attr_reader :country
+ alias :country_code :country
+
+ # An array containing the standard three elements of a coordinate triple: latitude, longitude, elevation.
+ attr_reader :coordinates
+
+ # A float, the standard first element of a coordinate triple.
+ attr_reader :longitude
+ alias :lng :longitude
+
+ # A float, the standard second element of a coordinate triple.
+ attr_reader :latitude
+ alias :lat :latitude
+
+ # A float, the standard third element of a coordinate triple.
+ attr_reader :elevation
+
+ # An integer, Google's rating of the accuracy of the supplied address.
+ attr_reader :accuracy
+
+ # All address attributes as one string, formatted by the service.
+ attr_reader :full_address
+ alias :to_s :full_address
+
+ # The address query sent to the service. i.e. The user input.
+ attr_reader :query
+
+ def initialize(placemark, query) #:nodoc
+ @xml = placemark
+ @query = query
+
+ {
+ :@street => :ThoroughfareName,
+ :@city => :LocalityName,
+ :@zip => :PostalCodeNumber,
+ :@county => :SubAdministrativeAreaName,
+ :@state => :AdministrativeAreaName,
+ :@country => :CountryNameCode,
+
+ :@full_address => :address
+ }.each do |attribute, element|
+ instance_variable_set(attribute, (fetch(element) rescue nil))
+ end
+
+ @longitude, @latitude, @elevation = @coordinates = fetch(:coordinates).split(',').map { |x| x.to_f }
+
+ @accuracy = fetch_accuracy
+ end
+ end
+
+ ###
+
+ class Response #:nodoc:
+ include Parser
+
+ attr_reader :query, :status, :placemarks
+
+ def initialize(xml, geo_key) #:nodoc
+ @xml, @geo_key = xml, geo_key
+
+ @query = fetch(:name)
+ @status = fetch(:code).to_i
+
+ check_for_errors
+
+ @placemarks = @xml.scan %r{<Placemark(?: id="p\d+")?>.+?</Placemark>}m
+ end
+
+ def check_for_errors #:nodoc:
+ case status
+ when 200 then # Success
+ when 500 then raise ServerError, "Unknown error from Google's server"
+ when 601 then raise MissingAddressError, "Missing address"
+ when 602 then raise UnknownAddressError, "Unknown address: #{@query}"
+ when 603 then raise UnavailableAddressError, "Unavailable address: #{@query}"
+ when 610 then raise InvalidMapKeyError, "Invalid map key: #{@geo_key}"
+ when 620 then raise TooManyQueriesError, "Too many queries for map key: #{@geo_key}"
+ else raise UnknownError, "Unknown error: #{@status}"
+ end
+ end
+ end
+
+ ###
+
+ class Error < Exception; end
+
+ class ServerError < Error; end
+
+ class AddressError < Error; end
+ class MissingAddressError < AddressError; end
+ class UnknownAddressError < AddressError; end
+ class UnavailableAddressError < AddressError; end
+
+ class MapKeyError < Error; end
+ class InvalidMapKeyError < MapKeyError; end
+ class TooManyQueriesError < MapKeyError; end
+
+ class UnknownError < Error; end
+end
+
+end
View
112 test/fixtures/.svn/entries
@@ -0,0 +1,112 @@
+8
+
+dir
+8324
+svn://c13-chd-dev1.cnet.com/chow/trunk/test/fixtures/google/geo/responses
+svn://c13-chd-dev1.cnet.com
+
+
+
+2007-04-30T22:13:48.965542Z
+8085
+rasmussens
+
+
+svn:special svn:externals svn:needs-lock
+
+
+
+
+
+
+
+
+
+
+
+1a9bab60-3a0f-0410-9831-8d1da0c3b437
+
+too_many_queries.xml
+file
+
+
+
+
+2007-05-06T03:31:31.000000Z
+a38e5a07e416cb1f9dc32d9fe5c5b0f4
+2007-04-30T22:13:48.965542Z
+8085
+rasmussens
+
+unknown_address.xml
+file
+
+
+
+
+2007-05-06T03:31:31.000000Z
+58d77755b601ddfc50bae326a4626bc4
+2007-04-30T22:13:48.965542Z
+8085
+rasmussens
+
+success.xml
+file
+
+
+
+
+2007-05-06T03:31:31.000000Z
+040c8bc673352d57556d85c7d38f343d
+2007-04-30T22:13:48.965542Z
+8085
+rasmussens
+
+unavailable_address.xml
+file
+
+
+
+
+2007-05-06T03:31:31.000000Z
+4008ba3986bbcc4eabecb189b9ff4659
+2007-04-30T22:13:48.965542Z
+8085
+rasmussens
+
+invalid_map_key.xml
+file
+
+
+
+
+2007-05-06T03:31:31.000000Z
+7a5abfce7e93b3e6b7e10066f0a41f61
+2007-04-30T22:13:48.965542Z
+8085
+rasmussens
+
+missing_address.xml
+file
+
+
+
+
+2007-05-06T03:31:31.000000Z
+c02ea3a04244a35bfad6512f48d7805a
+2007-04-30T22:13:48.965542Z
+8085
+rasmussens
+
+server_error.xml
+file
+
+
+
+
+2007-05-06T03:31:31.000000Z
+03656fe2f87d1e56a7b8b4655f64d205
+2007-04-30T22:13:48.965542Z
+8085
+rasmussens
+
View
1  test/fixtures/.svn/format
@@ -0,0 +1 @@
+8
View
10 test/fixtures/.svn/text-base/invalid_map_key.xml.svn-base
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>1600 Amphitheater Pkwy, Mountain View, CA</name>
+ <Status>
+ <code>610</code>
+ <request>geocode</request>
+ </Status>
+ </Response>
+</kml>
View
10 test/fixtures/.svn/text-base/missing_address.xml.svn-base
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>1600</name>
+ <Status>
+ <code>601</code>
+ <request>geocode</request>
+ </Status>
+ </Response>
+</kml>
View
10 test/fixtures/.svn/text-base/server_error.xml.svn-base
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>1600 Amphitheater Pkwy, Mountain View, CA</name>
+ <Status>
+ <code>500</code>
+ <request>geocode</request>
+ </Status>
+ </Response>
+</kml>
View
36 test/fixtures/.svn/text-base/success.xml.svn-base
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.1">
+ <Response>
+ <name>1600 Amphitheatre Parkway, Mountain View, CA</name>
+ <Status>
+ <code>200</code>
+ <request>geocode</request>
+ </Status>
+ <Placemark>
+ <address>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</address>
+ <AddressDetails xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" Accuracy="8">
+ <Country>
+ <CountryNameCode>US</CountryNameCode>
+ <AdministrativeArea>
+ <AdministrativeAreaName>CA</AdministrativeAreaName>
+ <SubAdministrativeArea>
+ <SubAdministrativeAreaName>Santa Clara</SubAdministrativeAreaName>
+ <Locality>
+ <LocalityName>Mountain View</LocalityName>
+ <Thoroughfare>
+ <ThoroughfareName>1600 Amphitheatre Pkwy</ThoroughfareName>
+ </Thoroughfare>
+ <PostalCode>
+ <PostalCodeNumber>94043</PostalCodeNumber>
+ </PostalCode>
+ </Locality>
+ </SubAdministrativeArea>
+ </AdministrativeArea>
+ </Country>
+ </AddressDetails>
+ <Point>
+ <coordinates>-122.083739,37.423021,0</coordinates>
+ </Point>
+ </Placemark>
+ </Response>
+</kml>
View
10 test/fixtures/.svn/text-base/too_many_queries.xml.svn-base
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>1600 Amphitheater Pkwy, Mountain View, CA</name>
+ <Status>
+ <code>620</code>
+ <request>geocode</request>
+ </Status>
+ </Response>
+</kml>
View
10 test/fixtures/.svn/text-base/unavailable_address.xml.svn-base
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>42-44 Hanway Street, London</name>
+ <Status>
+ <code>603</code>
+ <request>geocode</request>
+ </Status>
+ </Response>
+</kml>
View
10 test/fixtures/.svn/text-base/unknown_address.xml.svn-base
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>1600</name>
+ <Status>
+ <code>602</code>
+ <request>geocode</request>
+ </Status>
+ </Response>
+</kml>
View
10 test/fixtures/invalid_map_key.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>1600 Amphitheater Pkwy, Mountain View, CA</name>
+ <Status>
+ <code>610</code>
+ <request>geocode</request>
+ </Status>
+ </Response>
+</kml>
View
10 test/fixtures/missing_address.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>1600</name>
+ <Status>
+ <code>601</code>
+ <request>geocode</request>
+ </Status>
+ </Response>
+</kml>
View
10 test/fixtures/server_error.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>1600 Amphitheater Pkwy, Mountain View, CA</name>
+ <Status>
+ <code>500</code>
+ <request>geocode</request>
+ </Status>
+ </Response>
+</kml>
View
36 test/fixtures/success.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.1">
+ <Response>
+ <name>1600 Amphitheatre Parkway, Mountain View, CA</name>
+ <Status>
+ <code>200</code>
+ <request>geocode</request>
+ </Status>
+ <Placemark>
+ <address>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</address>
+ <AddressDetails xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" Accuracy="8">
+ <Country>
+ <CountryNameCode>US</CountryNameCode>
+ <AdministrativeArea>
+ <AdministrativeAreaName>CA</AdministrativeAreaName>
+ <SubAdministrativeArea>
+ <SubAdministrativeAreaName>Santa Clara</SubAdministrativeAreaName>
+ <Locality>
+ <LocalityName>Mountain View</LocalityName>
+ <Thoroughfare>
+ <ThoroughfareName>1600 Amphitheatre Pkwy</ThoroughfareName>
+ </Thoroughfare>
+ <PostalCode>
+ <PostalCodeNumber>94043</PostalCodeNumber>
+ </PostalCode>
+ </Locality>
+ </SubAdministrativeArea>
+ </AdministrativeArea>
+ </Country>
+ </AddressDetails>
+ <Point>
+ <coordinates>-122.083739,37.423021,0</coordinates>
+ </Point>
+ </Placemark>
+ </Response>
+</kml>
View
62 test/fixtures/success_with_multiple_addresses.xml
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>heaven</name>
+ <Status>
+ <code>200</code>
+ <request>geocode</request>
+ </Status>
+ <Placemark id="p1">
+ <address>Heaven, Pottsville, PA 17901, USA</address>
+ <AddressDetails xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" Accuracy="6">
+ <Country>
+ <CountryNameCode>US</CountryNameCode>
+ <AdministrativeArea>
+ <AdministrativeAreaName>PA</AdministrativeAreaName>
+ <SubAdministrativeArea>
+ <SubAdministrativeAreaName>Schuylkill</SubAdministrativeAreaName>
+ <Locality>
+ <LocalityName>Pottsville</LocalityName>
+ <Thoroughfare>
+ <ThoroughfareName>Heaven</ThoroughfareName>
+ </Thoroughfare>
+ <PostalCode>
+ <PostalCodeNumber>17901</PostalCodeNumber>
+ </PostalCode>
+ </Locality>
+ </SubAdministrativeArea>
+ </AdministrativeArea>
+ </Country>
+ </AddressDetails>
+ <Point>
+ <coordinates>-76.169968,40.650970,0</coordinates>
+ </Point>
+ </Placemark>
+ <Placemark id="p2">
+ <address>Heaven, Oakboro, NC 28129, USA</address>
+ <AddressDetails xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" Accuracy="6">
+ <Country>
+ <CountryNameCode>US</CountryNameCode>
+ <AdministrativeArea>
+ <AdministrativeAreaName>NC</AdministrativeAreaName>
+ <SubAdministrativeArea>
+ <SubAdministrativeAreaName>Stanly</SubAdministrativeAreaName>
+ <Locality>
+ <LocalityName>Oakboro</LocalityName>
+ <Thoroughfare>
+ <ThoroughfareName>Heaven</ThoroughfareName>
+ </Thoroughfare>
+ <PostalCode>
+ <PostalCodeNumber>28129</PostalCodeNumber>
+ </PostalCode>
+ </Locality>
+ </SubAdministrativeArea>
+ </AdministrativeArea>
+ </Country>
+ </AddressDetails>
+ <Point>
+ <coordinates>-80.330399,35.254227,0</coordinates>
+ </Point>
+ </Placemark>
+ </Response>
+</kml>
View
10 test/fixtures/too_many_queries.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>1600 Amphitheater Pkwy, Mountain View, CA</name>
+ <Status>
+ <code>620</code>
+ <request>geocode</request>
+ </Status>
+ </Response>
+</kml>
View
10 test/fixtures/unavailable_address.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>42-44 Hanway Street, London</name>
+ <Status>
+ <code>603</code>
+ <request>geocode</request>
+ </Status>
+ </Response>
+</kml>
View
10 test/fixtures/unknown_address.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.0">
+ <Response>
+ <name>1600</name>
+ <Status>
+ <code>602</code>
+ <request>geocode</request>
+ </Status>
+ </Response>
+</kml>
View
102 test/geo_test.rb
@@ -0,0 +1,102 @@
+require 'test/unit'
+$:.unshift "#{File.dirname __FILE__}/../vendor/mocha-0.4.0/lib"
+require 'mocha'
+require "#{File.dirname __FILE__}/../lib/google/geo"
+
+class Google::GeoTest < Test::Unit::TestCase
+ def setup
+ @geo = Google::Geo.new 'API_KEY'
+ end
+
+ def test_success
+ @geo.expects(:open).
+ # show that query is escaped
+ with("http://maps.google.com/maps/geo?q=1600%20Amphitheatre%20Parkway,%20Mountain%20View,%20CA&key=API_KEY&output=xml").
+ returns(response(:success))
+
+ query = '1600 Amphitheatre Parkway, Mountain View, CA'
+
+ address = @geo.locate(query).first
+
+ assert_equal '1600 Amphitheatre Pkwy', address.street
+ assert_equal address.street, address.thoroughfare
+
+ assert_equal 'Mountain View', address.city
+ assert_equal address.city, address.locality
+
+ assert_equal '94043', address.zip
+ assert_equal address.zip, address.postal_code
+
+ assert_equal 'Santa Clara', address.county
+ assert_equal address.county, address.subadministrative_area
+
+ assert_equal 'CA', address.state
+ assert_equal address.state, address.administrative_area
+
+ assert_equal 'US', address.country
+ assert_equal address.country, address.country_code
+
+ assert_equal -122.083739, address.longitude
+ assert_equal address.longitude, address.lng
+
+ assert_equal 37.423021, address.latitude
+ assert_equal address.latitude, address.lat
+
+ assert_equal 0, address.elevation
+
+ assert_equal [address.longitude, address.latitude, address.elevation], address.coordinates
+
+ assert_equal 8, address.accuracy
+
+ assert_equal '1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA', address.full_address
+ assert_equal query, address.query
+ end
+
+ def test_success_with_multiple_addresses
+ @geo.expects(:open).returns(response(:success_with_multiple_addresses))
+
+ heavens = @geo.locate('heaven')
+
+ assert_equal 2, heavens.size
+
+ assert_equal 'Heaven, Pottsville, PA 17901, USA', heavens[0].full_address
+ assert_equal 'Heaven, Oakboro, NC 28129, USA', heavens[1].full_address
+
+ heavens.each { |h| assert_kind_of Google::Geo::Address, h }
+ end
+
+ def test_invalid_map_key
+ @geo.expects(:open).returns(response(:invalid_map_key))
+ assert_raises(Google::Geo::InvalidMapKeyError) { @geo.locate 'foo' }
+ end
+
+ def test_missing_address
+ @geo.expects(:open).returns(response(:missing_address))
+ assert_raises(Google::Geo::MissingAddressError) { @geo.locate 'foo' }
+ end
+
+ def test_server_error
+ @geo.expects(:open).returns(response(:server_error))
+ assert_raises(Google::Geo::ServerError) { @geo.locate 'foo' }
+ end
+
+ def test_too_many_queries
+ @geo.expects(:open).returns(response(:too_many_queries))
+ assert_raises(Google::Geo::TooManyQueriesError) { @geo.locate 'foo' }
+ end
+
+ def test_unavailable_address
+ @geo.expects(:open).returns(response(:unavailable_address))
+ assert_raises(Google::Geo::UnavailableAddressError) { @geo.locate 'foo' }
+ end
+
+ def test_unknown_address
+ @geo.expects(:open).returns(response(:unknown_address))
+ assert_raises(Google::Geo::UnknownAddressError) { @geo.locate 'foo' }
+ end
+
+private
+ def response(filename)
+ File.new "#{File.dirname __FILE__}/fixtures/#{filename}.xml"
+ end
+end
View
3  vendor/mocha-0.4.0/COPYING
@@ -0,0 +1,3 @@
+Copyright Revieworld Ltd. 2006
+
+You may use, copy and redistribute this library under the same terms as Ruby itself (see http://www.ruby-lang.org/en/LICENSE.txt) or under the MIT license (see MIT-LICENSE file).
View
7 vendor/mocha-0.4.0/MIT-LICENSE
@@ -0,0 +1,7 @@
+Copyright (c) 2006 Revieworld Ltd.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
35 vendor/mocha-0.4.0/README
@@ -0,0 +1,35 @@
+= Mocha
+
+Mocha is a library for mocking and stubbing using a syntax like that of JMock[http://www.jmock.org], and SchMock[http://rubyforge.org/projects/schmock]. Most commonly Mocha is used in conjunction with Test::Unit[http://www.ruby-doc.org/core/classes/Test/Unit.html], but it can be used in other contexts.
+
+One of its main advantages is that it allows you to mock and stub methods on _real_ (non-mock) classes and instances. You can for example stub ActiveRecord[http://api.rubyonrails.com/classes/ActiveRecord/Base.html] instance methods like +create+, +save+, +destroy+ and even class methods like +find+ to avoid hitting the database in unit tests.
+
+Mocha provides a unified, simple and readable syntax for both traditional mocking and for mocking with _real_ objects.
+
+Mocha has been harvested from projects at Reevoo[http://www.reevoo.com] by me (James[http://blog.floehopper.org]) and my colleagues Ben[http://www.reevoo.com/blogs/bengriffiths], Chris[http://blog.seagul.co.uk] and Paul[http://po-ru.com]. Mocha is in use on real-world Rails[http://www.rubyonrails.org] projects.
+
+== Download and Installation
+
+Install the gem with the following command...
+
+ $ gem install mocha
+
+Or install the Rails[http://www.rubyonrails.org] plugin...
+
+ $ script/plugin install svn://rubyforge.org/var/svn/mocha/trunk
+
+Or download Mocha from here - http://rubyforge.org/projects/mocha
+
+== Examples
+
+* Quick Start - {Usage Examples}[link:examples/misc.html]
+* Traditional mocking - {Star Trek Example}[link:examples/mocha.html]
+* Setting expectations on real classes - {Order Example}[link:examples/stubba.html]
+* More examples on {Floehopper's Blog}[http://blog.floehopper.org]
+* {Mailing List Archives}[http://rubyforge.org/pipermail/mocha-developer/]
+
+== License
+
+Copyright Revieworld Ltd. 2006
+
+You may use, copy and redistribute this library under the same terms as {Ruby itself}[http://www.ruby-lang.org/en/LICENSE.txt] or under the {MIT license}[http://mocha.rubyforge.org/files/MIT-LICENSE.html].
View
98 vendor/mocha-0.4.0/RELEASE
@@ -0,0 +1,98 @@
+= 0.4.0
+
+- Allow naming of mocks (patch from Chris Roos).
+- Specify multiple return values for consecutive calls.
+- Improved consistency of expectation error messages.
+- Allow mocking of Object instance methods e.g. kind_of?, type.
+- Provide aliased versions of #expects and #stubs to allow mocking of these methods.
+- Added at_least, at_most, at_most_once methods to expectation.
+- Allow expects and stubs to take a hash of method and return values.
+- Eliminate warning: "instance variable @yield not initialized" (patch from Xavier Shay).
+- Restore instance methods on partial mocks (patch from Chris Roos).
+- Allow stubbing of a method with non-word characters in its name (patch from Paul Battley).
+- Removed coupling to Test::Unit.
+- Allow specified exception instance to be raised (patch from Chris Roos).
+- Make mock object_id appear in hex like normal Ruby inspect (patch from Paul Battley).
+- Fix path to object.rb in rdoc rake task (patch from Tomas Pospisek).
+- Reverse order in which expectations are matched, so that last expectation is matched first. This allows e.g. a call to #stubs to be effectively overridden by a call to #expects (patch from Tobias Lutke).
+- Stubba & SmartTestCase modules incorporated into Mocha module so only need to require 'mocha' - no longer need to require 'stubba'.
+- AutoMocha removed.
+
+= 0.3.3
+
+- Quick bug fix to restore instance methods on partial mocks (for Kevin Clark).
+
+= 0.3.2
+
+- Examples added.
+
+= 0.3.1
+
+- Dual licensing with MIT license added.
+
+= 0.3.0
+
+* Rails plugin.
+* Auto-verify for expectations on concrete classes.
+* Include each expectation verification in the test result assertion count.
+* Filter out noise from assertion backtraces.
+* Point assertion backtrace to line where failing expectation was created.
+* New yields method for expectations.
+* Create stubs which stub all method calls.
+* Mocks now respond_to? expected methods.
+
+= 0.2.1
+
+* Rename MochaAcceptanceTest::Rover#move method to avoid conflict with Rake (in Ruby 1.8.4 only?)
+
+= 0.2.0
+
+* Small change to SetupAndTeardown#teardown_stubs suggested by Luke Redpath (http://www.lukeredpath.co.uk) to allow use of Stubba with RSpec (http://rspec.rubyforge.org).
+* Reorganized directory structure and extracted addition of setup and teardown methods into SmartTestCase mini-library.
+* Addition of auto-verify for Mocha (but not Stubba). This means there is more significance in the choice of expects or stubs in that any expects on a mock will automatically get verified.
+
+So instead of...
+
+ wotsit = Mocha.new
+ wotsit.expects(:thingummy).with(5).returns(10)
+ doobrey = Doobrey.new(wotsit)
+ doobrey.hoojamaflip
+ wotsit.verify
+
+you need to do...
+
+ wotsit = mock()
+ wotsit.expects(:thingummy).with(5).returns(10)
+ doobrey = Doobrey.new(wotsit)
+ doobrey.hoojamaflip
+ # no need to verify
+
+There are also shortcuts as follows...
+
+instead of...
+
+ wotsit = Mocha.new
+ wotsit.expects(:thingummy).returns(10)
+ wotsit.expects(:summat).returns(25)
+
+you can have...
+
+ wotsit = mock(:thingummy => 5, :summat => 25)
+
+and instead of...
+
+ wotsit = Mocha.new
+ wotsit.stubs(:thingummy).returns(10)
+ wotsit.stubs(:summat).returns(25)
+
+you can have...
+
+ wotsit = stub(:thingummy => 5, :summat => 25)
+
+= 0.1.2
+
+* Minor tweaks
+
+= 0.1.1
+
+* Initial release.
View
126 vendor/mocha-0.4.0/Rakefile
@@ -0,0 +1,126 @@
+require 'rubygems'
+require 'rake/rdoctask'
+require 'rake/gempackagetask'
+require 'rake/contrib/sshpublisher'
+
+module Mocha
+ VERSION = "0.4.0"
+end
+
+desc "Default task is currently to run all tests"
+task :default => :test_all
+
+desc "Run all tests"
+task :test_all do
+ $: << "#{File.dirname(__FILE__)}/test"
+ require 'test/all_tests'
+end
+
+desc 'Generate RDoc'
+Rake::RDocTask.new do |task|
+ task.main = 'README'
+ task.title = 'Mocha'
+ task.rdoc_dir = 'doc'
+ task.template = "html_with_google_analytics"
+ task.options << "--line-numbers" << "--inline-source"
+ task.rdoc_files.include('README', 'RELEASE', 'COPYING', 'MIT-LICENSE', 'agiledox.txt', 'lib/mocha/auto_verify.rb', 'lib/mocha/mock_methods.rb', 'lib/mocha/expectation.rb', 'lib/mocha/object.rb')
+end
+task :rdoc => :examples
+
+desc "Upload RDoc to RubyForge"
+task :publish_rdoc => [:rdoc, :examples] do
+ Rake::SshDirPublisher.new("jamesmead@rubyforge.org", "/var/www/gforge-projects/mocha", "doc").upload
+end
+
+desc "Generate agiledox-like documentation for tests"
+file 'agiledox.txt' do
+ File.open('agiledox.txt', 'w') do |output|
+ tests = FileList['test/**/*_test.rb']
+ tests.each do |file|
+ m = %r".*/([^/].*)_test.rb".match(file)
+ output << m[1]+" should:\n"
+ test_definitions = File::readlines(file).select {|line| line =~ /.*def test.*/}
+ test_definitions.sort.each do |definition|
+ m = %r"test_(should_)?(.*)".match(definition)
+ output << " - "+m[2].gsub(/_/," ") << "\n"
+ end
+ end
+ end
+end
+
+desc "Convert example ruby files to syntax-highlighted html"
+task :examples do
+ require 'coderay'
+ mkdir_p 'doc/examples'
+ File.open('doc/examples/coderay.css', 'w') do |output|
+ output << CodeRay::Encoders[:html]::CSS.new.stylesheet
+ end
+ ['mocha', 'stubba', 'misc'].each do |filename|
+ File.open("doc/examples/#{filename}.html", 'w') do |file|
+ file << "<html>"
+ file << "<head>"
+ file << %q(<link rel="stylesheet" media="screen" href="coderay.css" type="text/css">)
+ file << "</head>"
+ file << "<body>"
+ file << CodeRay.scan_file("examples/#{filename}.rb").html.div
+ file << "</body>"
+ file << "</html>"
+ end
+ end
+end
+
+Gem::manage_gems
+
+specification = Gem::Specification.new do |s|
+ s.name = "mocha"
+ s.summary = "Mocking and stubbing library"
+ s.version = Mocha::VERSION
+ s.author = 'James Mead'
+ s.description = <<-EOF
+ Mocking and stubbing library with JMock/SchMock syntax, which allows mocking and stubbing of methods on real (non-mock) classes.
+ EOF
+ s.email = 'mocha-developer@rubyforge.org'
+ s.homepage = 'http://mocha.rubyforge.org'
+ s.rubyforge_project = 'mocha'
+
+ s.has_rdoc = true
+ s.extra_rdoc_files = ['README', 'COPYING']
+ s.rdoc_options << '--title' << 'Mocha' << '--main' << 'README' << '--line-numbers'
+
+ s.autorequire = 'mocha'
+ s.files = FileList['{lib,test,examples}/**/*.rb', '[A-Z]*'].exclude('TODO').to_a
+ s.test_file = "test/all_tests.rb"
+end
+
+Rake::GemPackageTask.new(specification) do |package|
+ package.need_zip = true
+ package.need_tar = true
+end
+
+task :verify_user do
+ raise "RUBYFORGE_USER environment variable not set!" unless ENV['RUBYFORGE_USER']
+end
+
+task :verify_password do
+ raise "RUBYFORGE_PASSWORD environment variable not set!" unless ENV['RUBYFORGE_PASSWORD']
+end
+
+desc "Publish package files on RubyForge."
+task :publish_packages => [:verify_user, :verify_password, :package] do
+ require 'meta_project'
+ require 'rake/contrib/xforge'
+ release_files = FileList[
+ "pkg/mocha-#{Mocha::VERSION}.gem",
+ "pkg/mocha-#{Mocha::VERSION}.tgz",
+ "pkg/mocha-#{Mocha::VERSION}.zip"
+ ]
+
+ Rake::XForge::Release.new(MetaProject::Project::XForge::RubyForge.new('mocha')) do |release|
+ release.user_name = ENV['RUBYFORGE_USER']
+ release.password = ENV['RUBYFORGE_PASSWORD']
+ release.files = release_files.to_a
+ release.release_name = "Mocha #{Mocha::VERSION}"
+ release.release_changes = ''
+ release.release_notes = ''
+ end
+end
View
36 vendor/mocha-0.4.0/examples/misc.rb
@@ -0,0 +1,36 @@
+# Mocking a class method
+
+ product = Product.new
+ Product.expects(:find).with(1).returns(product)
+ assert_equal product, Product.find(1)
+
+# Mocking an instance method on a real object
+
+ product = Product.new
+ product.expects(:save).returns(true)
+ assert product.save
+
+# Stubbing instance methods on real object
+
+ prices = [stub(:pence => 1000), stub(:pence => 2000)]
+ product = Product.new
+ product.stubs(:prices).returns(prices)
+ assert_equal [1000, 2000], product.prices.collect {|p| p.pence}
+
+# Stubbing an instance method on all instances of a class
+
+ Product.any_instance.stubs(:name).returns('stubbed_name')
+ product = Product.new
+ assert_equal 'stubbed_name', product.name
+
+# Traditional mocking
+
+ object = mock()
+ object.expects(:expected_method).with(:p1, :p2).returns(:result)
+ assert_equal :result, object.expected_method(:p1, :p2)
+
+# Shortcuts
+
+ object = stub(:method1 => :result1, :method2 => :result2)
+ assert_equal :result1, object.method1
+ assert_equal :result2, object.method2
View
26 vendor/mocha-0.4.0/examples/mocha.rb
@@ -0,0 +1,26 @@
+class Enterprise
+
+ def initialize(dilithium)
+ @dilithium = dilithium
+ end
+
+ def go(warp_factor)
+ warp_factor.times { @dilithium.nuke(:anti_matter) }
+ end
+
+end
+
+require 'test/unit'
+require 'rubygems'
+require 'mocha'
+
+class EnterpriseTest < Test::Unit::TestCase
+
+ def test_should_boldly_go
+ dilithium = mock()
+ dilithium.expects(:nuke).with(:anti_matter).at_least_once # auto-verified at end of test
+ enterprise = Enterprise.new(dilithium)
+ enterprise.go(2)
+ end
+
+end
View
65 vendor/mocha-0.4.0/examples/stubba.rb
@@ -0,0 +1,65 @@
+class Order
+
+ attr_accessor :shipped_on
+
+ def total_cost
+ line_items.inject(0) { |total, line_item| total + line_item.price } + shipping_cost
+ end
+
+ def total_weight
+ line_items.inject(0) { |total, line_item| total + line_item.weight }
+ end
+
+ def shipping_cost
+ total_weight * 5 + 10
+ end
+
+ class << self
+
+ def find_all
+ # Database.connection.execute('select * from orders...
+ end
+
+ def number_shipped_since(date)
+ find_all.select { |order| order.shipped_on > date }.size
+ end
+
+ def unshipped_value
+ find_all.inject(0) { |total, order| order.shipped_on ? total : total + order.total_cost }
+ end
+
+ end
+
+end
+
+require 'test/unit'
+require 'rubygems'
+require 'stubba'
+
+class OrderTest < Test::Unit::TestCase
+
+ # illustrates stubbing instance method
+ def test_should_calculate_shipping_cost_based_on_total_weight
+ order = Order.new
+ order.stubs(:total_weight).returns(10)
+ assert_equal 60, order.shipping_cost
+ end
+
+ # illustrates stubbing class method
+ def test_should_count_number_of_orders_shipped_after_specified_date
+ now = Time.now; week_in_secs = 7 * 24 * 60 * 60
+ order_1 = Order.new; order_1.shipped_on = now - 1 * week_in_secs
+ order_2 = Order.new; order_2.shipped_on = now - 3 * week_in_secs
+ Order.stubs(:find_all).returns([order_1, order_2])
+ assert_equal 1, Order.number_shipped_since(now - 2 * week_in_secs)
+ end
+
+ # illustrates stubbing instance method for all instances of a class
+ def test_should_calculate_value_of_unshipped_orders
+ Order.stubs(:find_all).returns([Order.new, Order.new, Order.new])
+ Order.any_instance.stubs(:shipped_on).returns(nil)
+ Order.any_instance.stubs(:total_cost).returns(10)
+ assert_equal 30, Order.unshipped_value
+ end
+
+end
View
19 vendor/mocha-0.4.0/lib/mocha.rb
@@ -0,0 +1,19 @@
+require 'mocha_standalone'
+require 'mocha/test_case_adapter'
+
+require 'test/unit/testcase'
+
+module Test
+
+ module Unit
+
+ class TestCase
+
+ include Mocha::Standalone
+ include Mocha::TestCaseAdapter
+
+ end
+
+ end
+
+end
View
35 vendor/mocha-0.4.0/lib/mocha/any_instance_method.rb
@@ -0,0 +1,35 @@
+require 'mocha/class_method'
+
+module Mocha
+
+ class AnyInstanceMethod < ClassMethod
+
+ def unstub
+ remove_new_method
+ restore_original_method
+ stubbee.any_instance.reset_mocha
+ end
+
+ def mock
+ stubbee.any_instance.mocha
+ end
+
+ def hide_original_method
+ stubbee.class_eval "alias_method :#{hidden_method}, :#{method}" if stubbee.method_defined?(method)
+ end
+
+ def define_new_method
+ stubbee.class_eval "def #{method}(*args, &block); self.class.any_instance.mocha.method_missing(:#{method}, *args, &block); end"
+ end
+
+ def remove_new_method
+ stubbee.class_eval "remove_method :#{method}"
+ end
+
+ def restore_original_method
+ stubbee.class_eval "alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}" if stubbee.method_defined?(hidden_method)
+ end
+
+ end
+
+end
View
113 vendor/mocha-0.4.0/lib/mocha/auto_verify.rb
@@ -0,0 +1,113 @@
+require 'mocha/mock'
+
+# Methods added to TestCase allowing creation of mock objects.
+#
+# Mocks created this way will have their expectations automatically verified at the end of the test.
+#
+# See Mocha::MockMethods for methods on mock objects.
+module Mocha
+
+ module AutoVerify
+
+ def mocks # :nodoc:
+ @mocks ||= []
+ end
+
+ def reset_mocks # :nodoc:
+ @mocks = nil
+ end
+
+ # :call-seq: mock(name) -> mock object
+ # mock(expected_methods = {}) -> mock object
+ # mock(name, expected_methods = {}) -> mock object
+ #
+ # Creates a mock object.
+ #
+ # +name+ is a +String+ identifier for the mock object.
+ #
+ # +expected_methods+ is a +Hash+ with expected method name symbols as keys and corresponding return values as values.
+ #
+ # Note that (contrary to expectations set up by #stub) these expectations <b>must</b> be fulfilled during the test.
+ # def test_product
+ # product = mock('ipod_product', :manufacturer => 'ipod', :price => 100)
+ # assert_equal 'ipod', product.manufacturer
+ # assert_equal 100, product.price
+ # # an error will be raised unless both Product#manufacturer and Product#price have been called
+ # end
+ def mock(*args)
+ name, expectations = name_and_expectations_from_args(args)
+ build_mock_with_expectations(:expects, expectations, name)
+ end
+
+ # :call-seq: stub(name) -> mock object
+ # stub(stubbed_methods = {}) -> mock object
+ # stub(name, stubbed_methods = {}) -> mock object
+ #
+ # Creates a mock object.
+ #
+ # +name+ is a +String+ identifier for the mock object.
+ #
+ # +stubbed_methods+ is a +Hash+ with stubbed method name symbols as keys and corresponding return values as values.
+ #
+ # Note that (contrary to expectations set up by #mock) these expectations <b>need not</b> be fulfilled during the test.
+ # def test_product
+ # product = stub('ipod_product', :manufacturer => 'ipod', :price => 100)
+ # assert_equal 'ipod', product.manufacturer
+ # assert_equal 100, product.price
+ # # an error will not be raised even if Product#manufacturer and Product#price have not been called
+ # end
+ def stub(*args)
+ name, expectations = name_and_expectations_from_args(args)
+ build_mock_with_expectations(:stubs, expectations, name)
+ end
+
+ # :call-seq: stub_everything(name) -> mock object
+ # stub_everything(stubbed_methods = {}) -> mock object
+ # stub_everything(name, stubbed_methods = {}) -> mock object
+ #
+ # Creates a mock object that accepts calls to any method.
+ #
+ # By default it will return +nil+ for any method call.
+ #
+ # +name+ and +stubbed_methods+ work in the same way as for #stub.
+ # def test_product
+ # product = stub_everything('ipod_product', :price => 100)
+ # assert_nil product.manufacturer
+ # assert_nil product.any_old_method
+ # assert_equal 100, product.price
+ # end
+ def stub_everything(*args)
+ name, expectations = name_and_expectations_from_args(args)
+ build_mock_with_expectations(:stub_everything, expectations, name)
+ end
+
+ def verify_mocks # :nodoc:
+ mocks.each { |mock| mock.verify { yield if block_given? } }
+ end
+
+ def teardown_mocks # :nodoc:
+ reset_mocks
+ end
+
+ def build_mock_with_expectations(expectation_type = :expects, expectations = {}, name = nil) # :nodoc:
+ stub_everything = (expectation_type == :stub_everything)
+ expectation_type = :stubs if expectation_type == :stub_everything
+ mock = Mocha::Mock.new(stub_everything, name)
+ expectations.each do |method, result|
+ mock.__send__(expectation_type, method).returns(result)
+ end
+ mocks << mock
+ mock
+ end
+
+ private
+
+ def name_and_expectations_from_args(args) # :nodoc:
+ name = args.first.is_a?(String) ? args.delete_at(0) : nil
+ expectations = args.first || {}
+ [name, expectations]
+ end
+
+ end
+
+end
View
35 vendor/mocha-0.4.0/lib/mocha/central.rb
@@ -0,0 +1,35 @@
+module Mocha
+
+ class Central
+
+ attr_accessor :stubba_methods
+
+ def initialize
+ self.stubba_methods = []
+ end
+
+ def stub(method)
+ unless stubba_methods.include?(method)
+ method.stub
+ stubba_methods.push method
+ end
+ end
+
+ def verify_all(&block)
+ unique_mocks.each { |mock| mock.verify(&block) }
+ end
+
+ def unique_mocks
+ stubba_methods.inject({}) { |mocks, method| mocks[method.mock.__id__] = method.mock; mocks }.values
+ end
+
+ def unstub_all
+ while stubba_methods.size > 0
+ method = stubba_methods.pop
+ method.unstub
+ end
+ end
+
+ end
+
+end
View
62 vendor/mocha-0.4.0/lib/mocha/class_method.rb
@@ -0,0 +1,62 @@
+require 'mocha/metaclass'
+
+module Mocha
+
+ class ClassMethod
+
+ attr_reader :stubbee, :method
+
+ def initialize(stubbee, method)
+ @stubbee, @method = stubbee, method
+ end
+
+ def stub
+ hide_original_method
+ define_new_method
+ end
+
+ def unstub
+ remove_new_method
+ restore_original_method
+ stubbee.reset_mocha
+ end
+
+ def mock
+ stubbee.mocha
+ end
+
+ def hide_original_method
+ stubbee.__metaclass__.class_eval "alias_method :#{hidden_method}, :#{method}" if stubbee.__metaclass__.method_defined?(method)
+ end
+
+ def define_new_method
+ stubbee.__metaclass__.class_eval "def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end"
+ end
+
+ def remove_new_method
+ stubbee.__metaclass__.class_eval "remove_method :#{method}"
+ end
+
+ def restore_original_method
+ stubbee.__metaclass__.class_eval "alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}" if stubbee.__metaclass__.method_defined?(hidden_method)
+ end
+
+ def hidden_method
+ method_name = method.to_s.gsub(/\W/) {|s| "_substituted_character_#{s[0]}_" }
+ "__stubba__#{method_name}__stubba__"
+ end
+
+ def eql?(other)
+ return false unless (other.class == self.class)
+ (stubbee == other.stubbee) and (method == other.method)
+ end
+
+ alias_method :==, :eql?
+
+ def to_s
+ "#{stubbee}.#{method}"
+ end
+
+ end
+
+end
View
295 vendor/mocha-0.4.0/lib/mocha/expectation.rb
@@ -0,0 +1,295 @@
+require 'mocha/infinite_range'
+require 'mocha/pretty_parameters'
+require 'mocha/expectation_error'
+
+class Object
+
+ alias_method :__is_a__, :is_a?
+
+end
+
+module Mocha
+ # Methods on expectations returned from Mocha::MockMethods#expects and Mocha::MockMethods#stubs
+ class Expectation
+
+ # :stopdoc:
+
+ class InvalidExpectation < Exception; end
+
+ class AlwaysEqual
+ def ==(other)
+ true
+ end
+ end
+
+ attr_reader :method_name, :backtrace
+
+ def initialize(mock, method_name, backtrace = nil)
+ @mock, @method_name = mock, method_name
+ @count = 1
+ @parameters, @parameter_block = AlwaysEqual.new, nil
+ @invoked, @return_value = 0, nil
+ @backtrace = backtrace || caller
+ @yield = nil
+ end
+
+ def yield?
+ @yield
+ end
+
+ def match?(method_name, *arguments)
+ if @parameter_block then
+ @parameter_block.call(*arguments)
+ else
+ (@method_name == method_name) and (@parameters == arguments)
+ end
+ end
+
+ # :startdoc:
+
+ # :call-seq: times(range) -> expectation
+ #
+ # Modifies expectation so that the number of calls to the expected method must be within a specific +range+.
+ #
+ # +range+ can be specified as an exact integer or as a range of integers
+ # object = mock()
+ # object.expects(:expected_method).times(3)
+ # 3.times { object.expected_method } # => verify succeeds
+ #
+ # object = mock()
+ # object.expects(:expected_method).times(3)
+ # 2.times { object.expected_method } # => verify fails
+ #
+ # object = mock()
+ # object.expects(:expected_method).times(2..4)
+ # 3.times { object.expected_method } # => verify succeeds
+ #
+ # object = mock()
+ # object.expects(:expected_method).times(2..4)
+ # object.expected_method # => verify fails
+ def times(range)
+ @count = range
+ self
+ end
+
+ # :call-seq: never -> expectation
+ #
+ # Modifies expectation so that the expected method must never be called.
+ # object = mock()
+ # object.expects(:expected_method).never
+ # object.expected_method # => verify fails
+ #
+ # object = mock()
+ # object.expects(:expected_method).never
+ # object.expected_method # => verify succeeds
+ def never
+ times(0)
+ self
+ end
+
+ # :call-seq: at_least(minimum_number_of_times) -> expectation
+ #
+ # Modifies expectation so that the expected method must be called at least a +minimum_number_of_times+.
+ # object = mock()
+ # object.expects(:expected_method).at_least(2)
+ # 3.times { object.expected_method } # => verify succeeds
+ #
+ # object = mock()
+ # object.expects(:expected_method).at_least(2)
+ # object.expected_method # => verify fails
+ def at_least(minimum_number_of_times)
+ times(Range.at_least(minimum_number_of_times))
+ self
+ end
+
+ # :call-seq: at_least_once() -> expectation
+ #
+ # Modifies expectation so that the expected method must be called at least once.
+ # object = mock()
+ # object.expects(:expected_method).at_least_once
+ # object.expected_method # => verify succeeds
+ #
+ # object = mock()
+ # object.expects(:expected_method).at_least_once
+ # # => verify fails
+ def at_least_once()
+ at_least(1)
+ self
+ end
+
+ # :call-seq: at_most(maximum_number_of_times) -> expectation
+ #
+ # Modifies expectation so that the expected method must be called at most a +maximum_number_of_times+.
+ # object = mock()
+ # object.expects(:expected_method).at_most(2)
+ # 2.times { object.expected_method } # => verify succeeds
+ #
+ # object = mock()
+ # object.expects(:expected_method).at_most(2)
+ # 3.times { object.expected_method } # => verify fails
+ def at_most(maximum_number_of_times)
+ times(Range.at_most(maximum_number_of_times))
+ self
+ end
+
+ # :call-seq: at_most_once() -> expectation
+ #
+ # Modifies expectation so that the expected method must be called at most once.
+ # object = mock()
+ # object.expects(:expected_method).at_most_once
+ # object.expected_method # => verify succeeds
+ #
+ # object = mock()
+ # object.expects(:expected_method).at_most_once
+ # 2.times { object.expected_method } # => verify fails
+ def at_most_once()
+ at_most(1)
+ self
+ end
+
+ # :call-seq: with(*arguments, &parameter_block) -> expectation
+ #
+ # Modifies expectation so that the expected method must be called with specified +arguments+.
+ # object = mock()
+ # object.expects(:expected_method).with(:param1, :param2)
+ # object.expected_method(:param1, :param2) # => verify succeeds
+ #
+ # object = mock()
+ # object.expects(:expected_method).with(:param1, :param2)
+ # object.expected_method(:param3) # => verify fails
+ # If a +parameter_block+ is given, the block is called with the parameters passed to the expected method.
+ # The expectation is matched if the block evaluates to +true+.
+ # object = mock()
+ # object.expects(:expected_method).with() { |value| value % 4 == 0 }
+ # object.expected_method(16) # => verify succeeds
+ #
+ # object = mock()
+ # object.expects(:expected_method).with() { |value| value % 4 == 0 }
+ # object.expected_method(17) # => verify fails
+ def with(*arguments, &parameter_block)
+ @parameters, @parameter_block = arguments, parameter_block
+ class << @parameters; def to_s; join(', '); end; end
+ self
+ end
+
+ # :call-seq: yields(*parameters) -> expectation
+ #
+ # Modifies expectation so that when the expected method is called, it yields with the specified +parameters+.
+ # object = mock()
+ # object.expects(:expected_method).yields('result')
+ # yielded_value = nil
+ # object.expected_method { |value| yielded_value = value }
+ # yielded_value # => 'result'
+ def yields(*parameters)
+ @yield = true
+ @parameters_to_yield = parameters
+ self
+ end
+
+ # :call-seq: returns(value) -> expectation
+ # :call-seq: returns(*values) -> expectation
+ #
+ # Modifies expectation so that when the expected method is called, it returns the specified +value+.
+ # object = mock()
+ # object.stubs(:stubbed_method).returns('result')
+ # object.stubbed_method # => 'result'
+ # object.stubbed_method # => 'result'
+ # If multiple +values+ are given, these are returned in turn on consecutive calls to the method.
+ # object = mock()
+ # object.stubs(:stubbed_method).returns(1, 2)
+ # object.stubbed_method # => 1
+ # object.stubbed_method # => 2
+ # If +value+ is a Proc, then expected method will return result of calling Proc.
+ # object = mock()
+ # object.stubs(:stubbed_method).returns(lambda { rand(100) })
+ # object.stubbed_method # => 41
+ # object.stubbed_method # => 77
+ def returns(*values)
+ @return_value = (values.size > 1) ? lambda { values.shift } : @return_value = values.first
+ self
+ end
+
+ # :call-seq: raises(exception = RuntimeError, message = nil) -> expectation
+ #
+ # Modifies expectation so that when the expected method is called, it raises the specified +exception+ with the specified +message+.
+ # object = mock()
+ # object.expects(:expected_method).raises(Exception, 'message')
+ # object.expected_method # => raises exception of class Exception and with message 'message'
+ def raises(exception = RuntimeError, message = nil)
+ @return_value = message ? lambda { raise exception, message } : lambda { raise exception }
+ self
+ end
+
+ # :stopdoc:
+
+ def invoke
+ @invoked += 1
+ yield(*@parameters_to_yield) if yield? and block_given?
+ @return_value.__is_a__(Proc) ? @return_value.call : @return_value
+ end
+
+ def verify
+ yield(self) if block_given?
+ unless (@count === @invoked) then
+ error = ExpectationError.new(error_message(@count, @invoked))
+ error.set_backtrace(filtered_backtrace)
+ raise error
+ end
+ end
+
+ def mocha_lib_directory
+ File.expand_path(File.join(File.dirname(__FILE__), "..")) + File::SEPARATOR
+ end
+
+ def filtered_backtrace
+ backtrace.reject { |location| Regexp.new(mocha_lib_directory).match(File.expand_path(location)) }
+ end
+
+ def method_signature
+ return "#{method_name}" if @parameters.__is_a__(AlwaysEqual)
+ "#{@method_name}(#{PrettyParameters.new(@parameters).pretty})"
+ end
+
+ def error_message(expected_count, actual_count)
+ "#{@mock.mocha_inspect}.#{method_signature} - expected calls: #{expected_count}, actual calls: #{actual_count}"
+ end
+
+ # :startdoc:
+
+ end
+
+ # :stopdoc:
+
+ class Stub < Expectation
+
+ def verify
+ true
+ end
+
+ end
+
+ class MissingExpectation < Expectation
+
+ def initialize(mock, method_name)
+ super
+ @invoked = true
+ end
+
+ def verify
+ msg = error_message(0, 1)
+ similar_expectations_list = similar_expectations.collect { |expectation| expectation.method_signature }.join("\n")
+ msg << "\nSimilar expectations:\n#{similar_expectations_list}" unless similar_expectations.empty?
+ error = ExpectationError.new(msg)
+ error.set_backtrace(filtered_backtrace)
+ raise error if @invoked
+ end
+
+ def similar_expectations
+ @mock.expectations.select { |expectation| expectation.method_name == self.method_name }
+ end
+
+ end
+
+ # :startdoc:
+
+end
View
6 vendor/mocha-0.4.0/lib/mocha/expectation_error.rb
@@ -0,0 +1,6 @@
+module Mocha
+
+ class ExpectationError < StandardError
+ end
+
+end
View
27 vendor/mocha-0.4.0/lib/mocha/infinite_range.rb
@@ -0,0 +1,27 @@
+class Range
+
+ def self.at_least(minimum_value)
+ Range.new(minimum_value, infinite)
+ end
+
+ def self.at_most(maximum_value)
+ Range.new(-infinite, maximum_value, false)
+ end
+
+ def self.infinite
+ 1/0.0
+ end
+
+ alias_method :__to_s__, :to_s
+
+ def to_s
+ if first.to_f.infinite? then
+ return "at most #{last}"
+ elsif last.to_f.infinite? then
+ return "at least #{first}"
+ else
+ __to_s__
+ end
+ end
+
+end
View
37 vendor/mocha-0.4.0/lib/mocha/inspect.rb
@@ -0,0 +1,37 @@
+require 'date'
+
+class Object
+ def mocha_inspect
+ inspect =~ /#</ ? "#<#{self.class}:0x#{self.__id__.to_s(16)}>" : inspect
+ end
+end
+
+class String
+ def mocha_inspect
+ inspect.gsub(/\"/, "'")
+ end
+end
+
+class Array
+ def mocha_inspect
+ "[#{collect { |member| member.mocha_inspect }.join(', ')}]"
+ end
+end
+
+class Hash
+ def mocha_inspect
+ "{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}"
+ end
+end
+
+class Time
+ def mocha_inspect
+ "#{inspect} (#{to_f} secs)"
+ end
+end
+
+class Date
+ def mocha_inspect
+ to_s
+ end
+end
View
8 vendor/mocha-0.4.0/lib/mocha/instance_method.rb
@@ -0,0 +1,8 @@
+require 'mocha/class_method'
+
+module Mocha
+
+ class InstanceMethod < ClassMethod
+ end
+
+end
View
7 vendor/mocha-0.4.0/lib/mocha/metaclass.rb
@@ -0,0 +1,7 @@
+class Object
+
+ def __metaclass__
+ class << self; self; end
+ end
+
+end
View
20 vendor/mocha-0.4.0/lib/mocha/mock.rb
@@ -0,0 +1,20 @@
+require 'mocha/mock_methods'
+
+module Mocha
+
+ class Mock
+
+ include MockMethods
+
+ def initialize(stub_everything = false, name = nil)
+ @stub_everything = stub_everything
+ @mock_name = name
+ end
+
+ def mocha_inspect
+ @mock_name ? "#<Mock:#{@mock_name}>" : "#<Mock:0x#{__id__.to_s(16)}>"
+ end
+
+ end
+
+end
View
122 vendor/mocha-0.4.0/lib/mocha/mock_methods.rb
@@ -0,0 +1,122 @@
+require 'mocha/expectation'
+require 'mocha/metaclass'
+
+module Mocha
+ # Methods added to mock objects.
+ # These methods all return an expectation which can be further modified by methods on Mocha::Expectation.
+ module MockMethods
+
+ # :stopdoc:
+
+ attr_reader :stub_everything
+
+ def expectations
+ @expectations ||= []
+ end
+
+ # :startdoc:
+
+ # :call-seq: expects(method_name) -> expectation
+ # expects(method_names) -> last expectation
+ #
+ # Adds an expectation that a method identified by +method_name+ symbol must be called exactly once with any parameters.
+ # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
+ # object = mock()
+ # object.expects(:method1)
+ # object.method1
+ # # no error raised
+ #
+ # object = mock()
+ # object.expects(:method1)
+ # # error raised, because method1 not called exactly once
+ # If +method_names+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
+ # object = mock()
+ # object.expects(:method1 => :result1, :method2 => :result2)
+ #
+ # # exactly equivalent to
+ #
+ # object = mock()
+ # object.expects(:method1).returns(:result1)
+ # object.expects(:method2).returns(:result2)
+ def expects(method_names, backtrace = nil)
+ method_names = method_names.is_a?(Hash) ? method_names : { method_names => nil }
+ method_names.each do |method_name, return_value|
+ expectations << Expectation.new(self, method_name, backtrace).returns(return_value)
+ self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
+ end
+ expectations.last
+ end
+
+ alias_method :__expects__, :expects
+
+ # :call-seq: stubs(method_name) -> expectation
+ # stubs(method_names) -> last expectation
+ #
+ # Adds an expectation that a method identified by +method_name+ symbol may be called any number of times with any parameters.
+ # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
+ # object = mock()
+ # object.stubs(:method1)
+ # object.method1
+ # object.method1
+ # # no error raised
+ # If +method_names+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
+ # object = mock()
+ # object.stubs(:method1 => :result1, :method2 => :result2)
+ #
+ # # exactly equivalent to
+ #
+ # object = mock()
+ # object.stubs(:method1).returns(:result1)
+ # object.stubs(:method2).returns(:result2)
+ def stubs(method_names, backtrace = nil)
+ method_names = method_names.is_a?(Hash) ? method_names : { method_names => nil }
+ method_names.each do |method_name, return_value|
+ expectations << Stub.new(self, method_name, backtrace).returns(return_value)
+ self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
+ end
+ expectations.last
+ end
+
+ alias_method :__stubs__, :stubs
+
+ # :stopdoc:
+
+ def method_missing(symbol, *arguments, &block)
+ matching_expectation = matching_expectation(symbol, *arguments)
+ if matching_expectation then
+ matching_expectation.invoke(&block)
+ elsif stub_everything then
+ return
+ else
+ begin
+ super_method_missing(symbol, *arguments, &block)
+ rescue NoMethodError
+ unexpected_method_called(symbol, *arguments)
+ end
+ end
+ end
+
+ def respond_to?(symbol)
+ expectations.any? { |expectation| expectation.method_name == symbol }
+ end
+
+ def super_method_missing(symbol, *arguments, &block)
+ raise NoMethodError
+ end
+
+ def unexpected_method_called(symbol, *arguments)
+ MissingExpectation.new(self, symbol).with(*arguments).verify
+ end
+
+ def matching_expectation(symbol, *arguments)
+ expectations.reverse.detect { |expectation| expectation.match?(symbol, *arguments) }
+ end
+
+ def verify(&block)
+ expectations.each { |expectation| expectation.verify(&block) }
+ end
+
+ # :startdoc:
+
+ end
+end
View
100 vendor/mocha-0.4.0/lib/mocha/object.rb
@@ -0,0 +1,100 @@
+require 'mocha/mock'
+require 'mocha/instance_method'
+require 'mocha/class_method'
+require 'mocha/any_instance_method'
+
+# Methods added all Objects.
+class Object
+
+ def mocha # :nodoc:
+ @mocha ||= Mocha::Mock.new
+ end
+
+ def reset_mocha # :nodoc:
+ @mocha = nil
+ end
+
+ def stubba_method # :nodoc:
+ Mocha::InstanceMethod
+ end
+
+ def stubba_object # :nodoc:
+ self
+ end
+
+ # :call-seq: expects(symbol) -> expectation
+ #
+ # Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
+ # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
+ # product = Product.new
+ # product.expects(:save).returns(true)
+ # assert_equal false, product.save
+ def expects(symbol)
+ method = stubba_method.new(stubba_object, symbol)
+ $stubba.stub(method)
+ mocha.expects(symbol, caller)
+ end
+
+ # :call-seq: stubs(symbol) -> expectation
+ #
+ # Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
+ # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
+ # product = Product.new
+ # product.stubs(:save).returns(true)
+ # assert_equal false, product.save
+ def stubs(symbol)
+ method = stubba_method.new(stubba_object, symbol)
+ $stubba.stub(method)
+ mocha.stubs(symbol, caller)
+ end
+
+ def verify # :nodoc:
+ mocha.verify
+ end
+
+end
+
+class Module # :nodoc:
+
+ def stubba_method
+ Mocha::ClassMethod
+ end
+
+end
+
+class Class
+
+ def stubba_method # :nodoc:
+ Mocha::ClassMethod
+ end
+
+ class AnyInstance # :nodoc:
+
+ def initialize(klass)
+ @stubba_object = klass
+ end
+
+ def stubba_method
+ Mocha::AnyInstanceMethod
+ end
+
+ def stubba_object
+ @stubba_object
+ end
+
+ end
+
+ # :call-seq: any_instance -> mock object
+ #
+ # Returns a mock object which will detect calls to any instance of this class.
+ # Product.any_instance.stubs(:save).returns(false)
+ # product_1 = Product.new
+ # assert_equal false, product_1.save
+ # product_2 = Product.new
+ # assert_equal false, product_2.save
+ def any_instance
+ @any_instance ||= AnyInstance.new(self)
+ end
+
+end
+
View
28 vendor/mocha-0.4.0/lib/mocha/pretty_parameters.rb
@@ -0,0 +1,28 @@
+require 'mocha/inspect'
+
+module Mocha
+
+ class PrettyParameters
+
+ def initialize(params)
+ @params = params
+ @params_string = params.mocha_inspect
+ end
+
+ def pretty
+ remove_outer_array_braces!
+ remove_outer_hash_braces!
+ @params_string
+ end
+
+ def remove_outer_array_braces!
+ @params_string = @params_string.gsub(/^\[|\]$/, '')
+ end
+
+ def remove_outer_hash_braces!
+ @params_string = @params_string.gsub(/^\{|\}$/, '') if @params.size == 1
+ end
+
+ end
+
+end
View
23 vendor/mocha-0.4.0/lib/mocha/setup_and_teardown.rb
@@ -0,0 +1,23 @@
+require 'mocha/central'
+
+module Mocha
+
+ module SetupAndTeardown
+
+ def setup_stubs
+ $stubba = Mocha::Central.new
+ end
+
+ def verify_stubs
+ $stubba.verify_all { yield if block_given? } if $stubba
+ end
+
+ def teardown_stubs
+ if $stubba then
+ $stubba.unstub_all
+ $stubba = nil
+ end
+ end
+
+ end
+end
View
30 vendor/mocha-0.4.0/lib/mocha/standalone.rb
@@ -0,0 +1,30 @@
+require 'mocha/auto_verify'
+require 'mocha/setup_and_teardown'
+
+module Mocha
+
+ module Standalone
+
+ include AutoVerify
+ include SetupAndTeardown
+
+ def mocha_setup
+ setup_stubs
+ end
+
+ def mocha_verify(&block)
+ verify_mocks(&block)
+ verify_stubs(&block)
+ end
+
+ def mocha_teardown
+ begin
+ teardown_mocks
+ ensure
+ teardown_stubs
+ end
+ end
+
+ end
+
+end
View
49 vendor/mocha-0.4.0/lib/mocha/test_case_adapter.rb
@@ -0,0 +1,49 @@
+require 'mocha/expectation_error'
+
+module Mocha
+
+ module TestCaseAdapter
+
+ def self.included(base)
+ base.class_eval do
+
+ alias_method :run_before_mocha_test_case_adapter, :run
+
+ def run(result)
+ yield(Test::Unit::TestCase::STARTED, name)
+ @_result = result
+ begin
+ mocha_setup
+ begin
+ setup
+ __send__(@method_name)
+ mocha_verify { add_assertion }
+ rescue Mocha::ExpectationError => e
+ add_failure(e.message, e.backtrace)
+ rescue Test::Unit::AssertionFailedError => e
+ add_failure(e.message, e.backtrace)
+ rescue StandardError, ScriptError
+ add_error($!)
+ ensure
+ begin
+ teardown
+ rescue Test::Unit::AssertionFailedError => e
+ add_failure(e.message, e.backtrace)
+ rescue StandardError, ScriptError
+ add_error($!)
+ end
+ end
+ ensure
+ mocha_teardown
+ end
+ result.add_run
+ yield(Test::Unit::TestCase::FINISHED, name)
+ end
+
+ end
+
+ end
+
+ end
+
+end
View
2  vendor/mocha-0.4.0/lib/mocha_standalone.rb
@@ -0,0 +1,2 @@
+require 'mocha/standalone'
+require 'mocha/object'