Sha256: 7d5b389de777c528264ac2d317d857299fd3aedba903d09289b92c11a68a6b72

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

RSpec::Matchers.define :have_styling do |rules|
  normalized_rules = StylingExpectation.new(rules)

  chain(:at_selector) { |selector| @selector = selector }
  match { |document|
    @selector ||= "body > *:first"
    normalized_rules == styles_at_selector(document)
  }

  description {
    "have styles #{normalized_rules.inspect} at selector #{@selector.inspect}"
  }

  failure_message { |document|
    "expected styles at #{@selector.inspect} to be:\n#{normalized_rules}\nbut was:\n#{styles_at_selector(document)}"
  }

  failure_message_when_negated {
    "expected styles at #{@selector.inspect} to not be:\n#{normalized_rules}"
  }

  def styles_at_selector(document)
    expect(document).to have_selector(@selector)
    StylingExpectation.new document.at_css(@selector)["style"]
  end
end

class StylingExpectation
  def initialize(styling)
    case styling
    when String then @rules = parse_rules(styling)
    when Array then @rules = styling
    when Hash then @rules = styling.to_a
    when nil then @rules = []
    else fail "I don't understand #{styling.inspect}!"
    end
  end

  def ==(other)
    rules == other.rules
  end

  def to_s
    rules.to_s
  end

  protected

  attr_reader :rules

  private

  def parse_rules(css)
    css.split(";").map { |property| parse_property(property) }
  end

  def parse_property(property)
    rule, value = property.split(":", 2).map(&:strip)
    [rule, value]
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
roadie-5.2.1 spec/support/have_styling_matcher.rb
roadie-5.2.0 spec/support/have_styling_matcher.rb
roadie-5.1.0 spec/support/have_styling_matcher.rb
roadie-5.0.1 spec/support/have_styling_matcher.rb
roadie-5.0.0 spec/support/have_styling_matcher.rb