Sha256: 60e8aa99d5e0ff0c16163018c27d825743db773654f88a9b62b25be44a2ac8b9

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

module Leadlight
  module LinkMatchers
    extend RSpec::Matchers::DSL

    matcher :expand_to do |expected|
      match{|actual|
        @failures = []
        href_matches?(actual, expected) && params_match?(actual, expected)
      }

      chain(:given) do |params|
        @params = params
      end

      chain(:with_params) do |expected_params|
        @expected_params = expected_params
      end

      failure_message_for_should do |actual|
        @failures.join("\n")
      end

      def expanded
        CGI.unescape(expanded_link.href.to_s)
      end

      def expanded_link
        actual.expand(@params)
      end

      def subscript_expanded
        CGI.unescape(actual[@params].href.to_s)
      end

      def params_match?(actual, expected)
        if @expected_params
          @expected_params.each do |key, value|
            unless expanded_link.params[key] == value
              @failures << "Expected #{expanded_link.params.inspect} to include "\
                           "#{key.inspect} => #{value.inspect}"
              return false
            end
          end
        end
        return true
      end

      def href_matches?(actual, expected)
        if expanded == expected && subscript_expanded == expected
          true
        else
          @failures << "expected #{actual} to expand to #{expected.inspect} given " \
                       "params #{@params.inspect}, but got #{expanded.inspect}"
          false
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
leadlight-0.1.0 spec/support/link_matchers.rb
leadlight-0.0.7 spec/support/link_matchers.rb
leadlight-0.0.6 spec/support/link_matchers.rb