Sha256: 810b171abf5edb168b8480b91219e6c17eee6d2cb27df5356869893632741900

Contents?: true

Size: 1.54 KB

Versions: 61

Compression:

Stored size: 1.54 KB

Contents

module RSpec
  module Matchers
    module BuiltIn
      class StartAndEndWith
        include 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

61 entries across 56 versions & 5 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.7.4 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.7.3 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.7.2 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.7.1 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.7.0 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.6.9 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.6.8 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.6.7 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.6.6 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.6.5 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.6.4 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.6.3 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.6.2 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.6.1 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.6.0 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.5.17 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.5.16 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb
classiccms-0.5.15 vendor/bundle/gems/rspec-expectations-2.10.0/lib/rspec/matchers/built_in/start_and_end_with.rb