Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

do not pollute! test unit class

  • Loading branch information...
commit 153d6c866d7b762d5c22181d47cb09a51f4fd978 1 parent 776b4f4
Aaron Patterson authored
59 lib/markup_validity.rb
View
@@ -1,65 +1,12 @@
require 'nokogiri'
+require 'markup_validity/validator'
+require 'markup_validity/assertions'
require 'markup_validity/rspec'
module MarkupValidity
VERSION = '1.0.0'
-
- class Validator # :nodoc:
- Dir.chdir(File.dirname(__FILE__)) do
- XHTML1_TRANSITIONAL = Nokogiri::XML::Schema(
- File.read('xhtml1-transitional.xsd')
- )
- XHTML1_STRICT = Nokogiri::XML::Schema(File.read('xhtml1-strict.xsd'))
- end
-
- attr_reader :errors
- def initialize xml, reference = XHTML1_TRANSITIONAL
- doc = Nokogiri::XML xml
- @reference = reference
- @xml = xml
- @errors = reference.validate(doc)
- end
-
- def valid?; @errors.length == 0; end
-
- def inspect
- lines = @xml.split("\n")
- strings = []
- errors.each do |error|
- strings << "Error on line: #{error.line - 1}:"
- strings << error.message.gsub(/\{[^\}]*\}/, '')
- Range.new(error.line - 2, error.line + 2).each { |number|
- strings << "#{number}: #{lines[number]}"
- }
- strings << ""
- end
- strings.join("\n")
- 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
- class Test::Unit::TestCase; include MarkupValidity; end
+ class Test::Unit::TestCase; include MarkupValidity::Assertions; end
end
25 lib/markup_validity/assertions.rb
View
@@ -0,0 +1,25 @@
+module MarkupValidity
+ module Assertions
+ ###
+ # 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
+end
34 lib/markup_validity/validator.rb
View
@@ -0,0 +1,34 @@
+module MarkupValidity
+ class Validator # :nodoc:
+ Dir.chdir(File.dirname(__FILE__)) do
+ XHTML1_TRANSITIONAL = Nokogiri::XML::Schema(
+ File.read('xhtml1-transitional.xsd')
+ )
+ XHTML1_STRICT = Nokogiri::XML::Schema(File.read('xhtml1-strict.xsd'))
+ end
+
+ attr_reader :errors
+ def initialize xml, reference = XHTML1_TRANSITIONAL
+ doc = Nokogiri::XML xml
+ @reference = reference
+ @xml = xml
+ @errors = reference.validate(doc)
+ end
+
+ def valid?; @errors.length == 0; end
+
+ def inspect
+ lines = @xml.split("\n")
+ strings = []
+ errors.each do |error|
+ strings << "Error on line: #{error.line - 1}:"
+ strings << error.message.gsub(/\{[^\}]*\}/, '')
+ Range.new(error.line - 2, error.line + 2).each { |number|
+ strings << "#{number}: #{lines[number]}"
+ }
+ strings << ""
+ end
+ strings.join("\n")
+ end
+ end
+end
0  lib/xhtml1-strict.xsd → lib/markup_validity/xhtml1-strict.xsd
View
File renamed without changes
0  lib/xhtml1-transitional.xsd → lib/markup_validity/xhtml1-transitional.xsd
View
File renamed without changes
0  lib/xml.xsd → lib/markup_validity/xml.xsd
View
File renamed without changes
2  test/test_markup_validity.rb
View
@@ -6,7 +6,7 @@ class TestMarkupValidity < Test::Unit::TestCase
include MarkupValidity::TestHelper
class FakeUnit
- include MarkupValidity
+ include MarkupValidity::Assertions
attr_reader :assertions
Please sign in to comment.
Something went wrong with that request. Please try again.