Permalink
Browse files

test/unit can handle arbitrary schema

  • Loading branch information...
1 parent 91f5005 commit 776b4f451b3207bb9f3ab65e141060d76d61162c @tenderlove committed Jun 12, 2009
Showing with 85 additions and 0 deletions.
  1. +11 −0 lib/markup_validity.rb
  2. +24 −0 test/assets/order.xml
  3. +33 −0 test/assets/shipment.xsd
  4. +17 −0 test/test_markup_validity.rb
@@ -37,16 +37,27 @@ def inspect
end
end
+ ###
+ # Assert that +xhtml+ is valid XHTML Transitional
def assert_xhtml_transitional xhtml
validator = Validator.new xhtml
assert validator.valid?, validator.inspect
end
alias :assert_xhtml :assert_xhtml_transitional
+ ###
+ # Assert that +xhtml+ is valid XHTML Strict
def assert_xhtml_strict xhtml
validator = Validator.new xhtml
assert validator.valid?, validator.inspect
end
+
+ ###
+ # Assert that +xml+ conforms to +schema+
+ def assert_schema schema, xml
+ validator = Validator.new xml, Nokogiri::XML::Schema(schema)
+ assert validator.valid?, validator.inspect
+ end
end
if defined? Test::Unit::TestCase
View
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+
+<shiporder orderid="889923"
+xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+xsi:noNamespaceSchemaLocation="shiporder.xsd">
+ <orderperson>John Smith</orderperson>
+ <shipto>
+ <name>Ola Nordmann</name>
+ <address>Langgt 23</address>
+ <city>4000 Stavanger</city>
+ <country>Norway</country>
+ </shipto>
+ <item>
+ <title>Empire Burlesque</title>
+ <note>Special Edition</note>
+ <quantity>1</quantity>
+ <price>10.90</price>
+ </item>
+ <item>
+ <title>Hide your heart</title>
+ <quantity>1</quantity>
+ <price>9.90</price>
+ </item>
+</shiporder>
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+
+<xs:element name="shiporder">
+ <xs:complexType>
+ <xs:sequence>
+ <xs:element name="orderperson" type="xs:string"/>
+ <xs:element name="shipto">
+ <xs:complexType>
+ <xs:sequence>
+ <xs:element name="name" type="xs:string"/>
+ <xs:element name="address" type="xs:string"/>
+ <xs:element name="city" type="xs:string"/>
+ <xs:element name="country" type="xs:string"/>
+ </xs:sequence>
+ </xs:complexType>
+ </xs:element>
+ <xs:element name="item" maxOccurs="unbounded">
+ <xs:complexType>
+ <xs:sequence>
+ <xs:element name="title" type="xs:string"/>
+ <xs:element name="note" type="xs:string" minOccurs="0"/>
+ <xs:element name="quantity" type="xs:positiveInteger"/>
+ <xs:element name="price" type="xs:decimal"/>
+ </xs:sequence>
+ </xs:complexType>
+ </xs:element>
+ </xs:sequence>
+ <xs:attribute name="orderid" type="xs:string" use="required"/>
+ </xs:complexType>
+</xs:element>
+
+</xs:schema>
@@ -26,6 +26,7 @@ def setup
def test_valid_xhtml_transitional
@fu.assert_xhtml_transitional valid_document
assert_equal [true, ''], @fu.assertions.first
+ assert_xhtml_transitional valid_document
end
def test_valid_xhtml
@@ -49,4 +50,20 @@ def test_invalid_xhtml_strict
assert_equal false, @fu.assertions.first.first
assert_match('Missing child element', @fu.assertions.first.last)
end
+
+ def test_valid_schema
+ xsd = File.join(File.dirname(__FILE__), 'assets', 'shipment.xsd')
+ xml = File.join(File.dirname(__FILE__), 'assets', 'order.xml')
+ @fu.assert_schema File.read(xsd), File.read(xml)
+ assert_equal [true, ''], @fu.assertions.first
+ end
+
+ def test_invalid_schema
+ xsd = File.join(File.dirname(__FILE__), 'assets', 'shipment.xsd')
+ xml = File.read File.join(File.dirname(__FILE__), 'assets', 'order.xml')
+ xml.gsub!(/<name>[^<]*<\/name>/, '')
+ @fu.assert_schema File.read(xsd), xml
+ assert_equal false, @fu.assertions.first.first
+ assert_match('This element is not', @fu.assertions.first.last)
+ end
end

0 comments on commit 776b4f4

Please sign in to comment.