Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7a12a85136
Fetching contributors…

Cannot retrieve contributors at this time

file 41 lines (37 sloc) 1.224 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# encoding: UTF-8

require 'tmpdir'
require 'vines'
require 'ext/nokogiri'
require 'minitest/autorun'

class MiniTest::Spec

  # Build an <iq> xml node with the given attributes. This is useful as a
  # quick way to build a node to use as expected stanza output from a
  # Stream#write call.
  #
  # options - The Hash of xml attributes to include on the iq element. Attribute
  # values of nil or empty? are excluded from the generated element.
  # :body - The String xml content to include in the iq element.
  #
  # Examples
  #
  # iq(from: from, id: 42, to: to, type: 'result', body: card)
  #
  # Returns a Nokogiri::XML::Node.
  def iq(options)
    body = options.delete(:body)
    options.delete_if {|k, v| v.nil? || v.to_s.empty? }
    attrs = options.map {|k, v| "#{k}=\"#{v}\"" }.join(' ')
    node("<iq #{attrs}>#{body}</iq>")
  end

  # Parse xml into a nokogiri node. Strip excessive whitespace from the xml
  # content before parsing because it affects comparisons in MiniTest::Mock
  # expectations.
  #
  # xml - The String of xml content to parse.
  #
  # Returns a Nokogiri::XML::Node.
  def node(xml)
    xml = xml.strip.gsub(/\n|\s{2,}/, '')
    Nokogiri::XML(xml).root
  end
end
Something went wrong with that request. Please try again.