require "active_record" require "with_model" require "nokogiri" require "schematic" RSpec.configure do |config| config.extend WithModel end ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ":memory:") def validate_xml_against_xsd(xml, xsd) require "tempfile" tempfile = Tempfile.new("schematic") tempfile << xsd tempfile.rewind xsd = Nokogiri::XML::Schema(tempfile) doc = Nokogiri::XML.parse(xml) errors = [] xsd.validate(doc).each do |error| errors << error.message end errors.should == [] ensure tempfile.close end def validate_xsd(xml) xsd_schema_file = File.join(File.dirname(__FILE__), "xsd", "XMLSchema.xsd") meta_xsd = Nokogiri::XML::Schema(File.open(xsd_schema_file)) doc = Nokogiri::XML.parse(xml) meta_xsd.validate(doc).each do |error| error.message.should be_nil end end def sanitize_xml(xml) xml.split("\n").reject(&:blank?).map(&:strip).join("\n") end def generate_xsd_for_model(model, header_element = nil) xsd_generator = model.schematic_sandbox.xsd_generator output = <<-XML #{header_element} #{yield} XML sanitize_xml(output) end