Sha256: ba4bc57760dc5c090acf36eff52c605aae4ca44340380ce126d1186cf2be54ad

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

# 
# FuzzyHash, to_fuzzy_hash, to_html
# 
begin
  require 'nokogiri'
  
  class RSpec::FuzzyHash < Hash
    def == o
      return true if super

      if o.respond_to? :each
        o.each do |k, v|
          return false if (self[k.to_sym] || self[k.to_s]) != v
        end
        return true
      end

      false
    end
  end
  
  ::Nokogiri::XML::Node.class_eval do
    def to_fuzzy_hash
      h = RSpec::FuzzyHash.new
      attributes.each{|n, v| h[n] = v.value}
      h[:content] = content
      h
    end
  end
  
  class String
    def to_xhtml css = nil    
      require 'nokogiri'

      node = Nokogiri::HTML(self)
      unless css
        node
      else
        nodes = node.css(css)
        raise "Elements for '#{css}' CSS query not found!" if nodes.size < 1
        raise "Found more than one elment for '#{css}' CSS query!" if nodes.size > 1
        nodes.first
      end
    end
  end
rescue LoadError
  warn "WARN: some specs require the 'nokogiri' gem, but there's no such gem on this system, these specs will be scipped!"
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby_ext-0.4.25 lib/rspec_ext/xhtml.rb
ruby_ext-0.4.24 lib/rspec_ext/xhtml.rb
ruby_ext-0.4.23 lib/rspec_ext/xhtml.rb
ruby_ext-0.4.22 lib/rspec_ext/xhtml.rb