Sha256: 30d1fcde220902a97e3f8f19032e945f11a042b54e48999273c2af9255b824ba

Contents?: true

Size: 1.52 KB

Versions: 65

Compression:

Stored size: 1.52 KB

Contents

module RSpec
  module Matchers
    module BuiltIn
      class StartAndEndWith < BaseMatcher
        def initialize(*expected)
          @expected = expected.length == 1 ? expected.first : expected
        end

        def matches?(actual)
          @actual = actual.respond_to?(:[]) ? actual : (raise ArgumentError.new("#{actual.inspect} does not respond to :[]"))
          begin
            @expected.respond_to?(:length) ? subset_matches?(@expected, @actual) : element_matches?(@expected, @actual)
          rescue ArgumentError
            raise ArgumentError.new("#{actual.inspect} does not have ordered elements")
          end
        end

        def failure_message_for_should
          "expected #{@actual.inspect} to #{self.class.name.split('::').last.sub(/With/,'').downcase} with #{@expected.inspect}"
        end

        def failure_message_for_should_not
          "expected #{@actual.inspect} not to #{self.class.name.split('::').last.sub(/With/,'').downcase} with #{@expected.inspect}"
        end
      end

      class StartWith < StartAndEndWith
        def subset_matches?(expected, actual)
          actual[0, expected.length] == expected
        end

        def element_matches?(expected, actual)
          @actual[0] == @expected
        end
      end

      class EndWith < StartAndEndWith
        def subset_matches?(expected, actual)
          actual[-expected.length, expected.length] == expected
        end

        def element_matches?(expected, actual)
          actual[-1] == expected
        end
      end
    end
  end
end

Version data entries

65 entries across 65 versions & 19 rubygems

Version Path
rspec-expectations-2.14.2 lib/rspec/matchers/built_in/start_and_end_with.rb
rspec-expectations-2.14.1 lib/rspec/matchers/built_in/start_and_end_with.rb
tnargav-1.2.3 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.11.3/lib/rspec/matchers/built_in/start_and_end_with.rb
rspec-expectations-2.14.0 lib/rspec/matchers/built_in/start_and_end_with.rb
sshp-0.0.2 vendor/ruby/1.9.1/gems/rspec-expectations-2.13.0/lib/rspec/matchers/built_in/start_and_end_with.rb
sshp-0.0.1 vendor/ruby/1.9.1/gems/rspec-expectations-2.13.0/lib/rspec/matchers/built_in/start_and_end_with.rb
rspec-expectations-2.14.0.rc1 lib/rspec/matchers/built_in/start_and_end_with.rb
sidekiq-statsd-0.1.1 vendor/ruby/1.9.1/gems/rspec-expectations-2.13.0/lib/rspec/matchers/built_in/start_and_end_with.rb
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/rspec-expectations-2.13.0/lib/rspec/matchers/built_in/start_and_end_with.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/matchers/built_in/start_and_end_with.rb
vagrant-actionio-0.0.9 vendor/bundle/gems/rspec-expectations-2.13.0/lib/rspec/matchers/built_in/start_and_end_with.rb
rspec-expectations-2.13.0 lib/rspec/matchers/built_in/start_and_end_with.rb
remq-0.0.4 vendor/bundle/gems/rspec-expectations-2.12.1/lib/rspec/matchers/built_in/start_and_end_with.rb
remq-0.0.3 vendor/bundle/gems/rspec-expectations-2.12.1/lib/rspec/matchers/built_in/start_and_end_with.rb
librarian-puppet-0.9.8 vendor/gems/ruby/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/matchers/built_in/start_and_end_with.rb
rspec-expectations-2.12.1 lib/rspec/matchers/built_in/start_and_end_with.rb
rspec-expectations-2.12.0 lib/rspec/matchers/built_in/start_and_end_with.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.11.3/lib/rspec/matchers/built_in/start_and_end_with.rb
gem_repackager-0.1.0 support/gems/rspec-expectations-2.11.2/lib/rspec/matchers/built_in/start_and_end_with.rb
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/rspec-expectations-2.11.2/lib/rspec/matchers/built_in/start_and_end_with.rb