spec/support/have_styling_matcher.rb in roadie-2.4.3 vs spec/support/have_styling_matcher.rb in roadie-3.0.0.pre1

- old
+ new

@@ -1,25 +1,59 @@ RSpec::Matchers.define :have_styling do |rules| @selector = 'body > *:first' + normalized_rules = StylingExpectation.new(rules) - chain :at_selector do |selector| - @selector = selector + chain(:at_selector) { |selector| @selector = selector } + match { |document| normalized_rules == styles_at_selector(document) } + + description { + "have styles #{normalized_rules.inspect} at selector #{@selector.inspect}" + } + + failure_message_for_should { |document| + "expected styles at #{@selector.inspect} to be:\n#{normalized_rules}\nbut was:\n#{styles_at_selector(document)}" + } + + failure_message_for_should_not { + "expected styles at #{@selector.inspect} to not be:\n#{normalized_rules}" + } + + def styles_at_selector(document) + document.should have_selector(@selector) + StylingExpectation.new document.at_css(@selector)['style'] end +end - match do |document| - if rules.nil? - parsed_styles(document).blank? - else - rules.to_a.should == parsed_styles(document) +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 + else fail "I don't understand #{styling.inspect}!" end end - description { "have styles #{rules.inspect} at selector #{@selector.inspect}" } - failure_message_for_should { |document| "expected styles at #{@selector.inspect} to be #{rules.inspect} but was #{parsed_styles(document).inspect}" } - failure_message_for_should_not { "expected styles at #{@selector.inspect} to not be #{rules.inspect}" } + def ==(other) + rules == other.rules + end - def parsed_styles(document) - node = document.css(@selector).first - SpecHelpers.styling_of_node(node) + def to_s() rules.to_s end + + protected + attr_reader :rules + + private + def parse_rules(css) + css.split(';').map { |property| parse_property(property) } end -end + def parse_property(property) + rule, value = property.split(':', 2).map(&:strip) + [rule, normalize_quotes(value)] + end + + # JRuby's Nokogiri encodes quotes + def normalize_quotes(string) + string.gsub '%22', '"' + end +end