* Remove unnecessary exception variable
* You can use strings, symbols, and anything else that handles to_s as a library name
- ~
- rspec_matchers
- be_loadable_matcher.rb
| be_loadable_matcher.rb |
|---|
39 chrisk module BeLoadableMatcher
39 chrisk
39 chrisk class BeLoadable #:nodoc:
39 chrisk def matches?(requirement)
39 chrisk @requirement = requirement
39 chrisk if @requirement.respond_to?(:satisfied?)
39 chrisk @requirement.satisfied?
39 chrisk elsif @requirement.respond_to?(:to_s)
39 chrisk @requirement = MultipleDependencyRequirement.new(:all, [requirement])
39 chrisk @requirement.satisfied?
39 chrisk end
39 chrisk end
39 chrisk
39 chrisk def failure_message
39 chrisk @requirement.failure_message
39 chrisk end
39 chrisk end
39 chrisk
39 chrisk
39 chrisk # Checks whether each dependency can be loaded with +require+, and fails if the
39 chrisk # result doesn't match your expectation. Example usage:
39 chrisk #
39 chrisk # one_of('image_science', 'RMagick', 'mini_magick').should be_loadable
39 chrisk # all_of('digest/sha1', 'digest/sha2', 'digest/md5').should be_loadable
39 chrisk # both_of(:openid, :yadis).should be_loadable
39 chrisk # either_of('maruku', 'RedCloth').should be_loadable
39 chrisk # 'chronic'.should be_loadable
39 chrisk # :hpricot.should be_loadable
39 chrisk def be_loadable
39 chrisk BeLoadable.new
39 chrisk end
39 chrisk
39 chrisk
39 chrisk
39 chrisk class MultipleDependencyRequirement #:nodoc:
39 chrisk # :type :any or :all
40 chrisk # :libraries an array of strings/symbols/etc. to try to +require+
39 chrisk def initialize(type, libraries)
39 chrisk @type = type
39 chrisk @libraries = libraries
39 chrisk @failed_libraries = []
39 chrisk end
39 chrisk
39 chrisk def satisfied?
39 chrisk libraries = @libraries.dup
39 chrisk libraries.each do |lib|
39 chrisk begin
39 chrisk require lib.to_s
40 chrisk rescue LoadError, MissingSourceFile
40 chrisk @failed_libraries << lib.to_s
39 chrisk end
39 chrisk end
39 chrisk
39 chrisk return (@type == :any && @failed_libraries.size < @libraries.size) ||
39 chrisk (@type == :all && @failed_libraries.empty?)
39 chrisk end
39 chrisk
39 chrisk def to_s
39 chrisk @libraries.map {|lib| "'#{lib}'"}.to_sentence
39 chrisk end
39 chrisk
39 chrisk def load_failures_to_s
39 chrisk @failed_libraries.map {|lib| "'#{lib}'"}.to_sentence
39 chrisk end
39 chrisk
39 chrisk def failure_message
39 chrisk if @failed_libraries.size == 1
39 chrisk "Make sure the #{load_failures_to_s} library is available."
39 chrisk elsif @type == :any
39 chrisk "Make sure at least one of these libraries is available: #{self} (all failed to load)."
39 chrisk else
39 chrisk "Make sure all of these libraries are available: #{self} (failed to load #{load_failures_to_s})."
39 chrisk end
39 chrisk end
39 chrisk end
39 chrisk
39 chrisk
39 chrisk # Specify that at least one of +libraries+ should be loadable. Example:
39 chrisk # one_of('maruku', 'RedCloth', 'BlueCloth').should be_loadable
39 chrisk # Aliased as +either_of+ for more-readable use with two libraries specified.
39 chrisk def one_of(*libraries)
39 chrisk MultipleDependencyRequirement.new(:any, libraries)
39 chrisk end
39 chrisk alias_method :either_of, :one_of
39 chrisk
39 chrisk
39 chrisk # Specify that every one of +libraries+ should be loadable. Example:
39 chrisk # all_of('digest/sha1', 'digest/sha2', 'digest/md5').should be_loadable
39 chrisk # Aliased as +both_of+ for more-readable use with two libraries specified.
39 chrisk def all_of(*libraries)
39 chrisk MultipleDependencyRequirement.new(:all, libraries)
39 chrisk end
39 chrisk alias_method :both_of, :all_of
39 chrisk
39 chrisk end
39 chrisk
39 chrisk
39 chrisk include BeLoadableMatcher
