Permalink
Browse files

adding rspec assertions

  • Loading branch information...
1 parent 8d30c3c commit f601a66650713dde76ae6a979eccfff60f50edf8 @tenderlove committed Jun 12, 2009
Showing with 85 additions and 21 deletions.
  1. +31 −18 lib/markup_validity.rb
  2. +17 −0 lib/markup_validity/rspec.rb
  3. +34 −0 spec/matcher_spec.rb
  4. +3 −3 test/test_markup_validity.rb
View
@@ -1,30 +1,43 @@
require 'nokogiri'
+require 'markup_validity/rspec'
module MarkupValidity
VERSION = '1.0.0'
- Dir.chdir(File.dirname(__FILE__)) do
- XHTML1_TRANSITIONAL = Nokogiri::XML::Schema(
- File.read('xhtml1-transitional.xsd')
- )
- end
+ class Validator # :nodoc:
+ Dir.chdir(File.dirname(__FILE__)) do
+ XHTML1_TRANSITIONAL = Nokogiri::XML::Schema(
+ File.read('xhtml1-transitional.xsd')
+ )
+ end
- def assert_xhtml_transitional xhtml
- lines = xhtml.split("\n")
- doc = Nokogiri::XML xhtml
+ attr_reader :errors
+ def initialize xml, reference = XHTML1_TRANSITIONAL
+ doc = Nokogiri::XML xml
+ @reference = reference
+ @xml = xml
+ @errors = reference.validate(doc)
+ end
- errors = XHTML1_TRANSITIONAL.validate(doc)
+ def valid?; @errors.length == 0; end
- 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 << ""
+ 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_equal 0, errors.length, strings.join("\n")
+ def assert_xhtml_transitional xhtml
+ validator = Validator.new xhtml
+ assert validator.valid?, validator.inspect
end
end
@@ -0,0 +1,17 @@
+module Spec
+ module Matchers
+ def be_xhtml_transitional
+ Matcher.new :be_xhtml_transitional do
+ validator = nil
+ match do |xhtml|
+ validator = MarkupValidity::Validator.new xhtml
+ validator.valid?
+ end
+
+ failure_message_for_should do |actual|
+ validator.inspect
+ end
+ end
+ end
+ end
+end
View
@@ -0,0 +1,34 @@
+$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'rubygems'
+require 'markup_validity'
+
+module Spec
+ module Matchers
+ describe "[actual.should] be_xhtml_transitional" do
+ it "is valid xhtml" do
+ xhtml = <<-eoxhtml
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>hello world</title>
+ </head>
+ <body>
+ </body>
+</html>
+ eoxhtml
+ xhtml.should be_xhtml_transitional
+ end
+
+ it "is not valid xhtml" do
+ xhtml = <<-eoxhtml
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ </head>
+ <body>
+ </body>
+</html>
+ eoxhtml
+ xhtml.should_not be_xhtml_transitional
+ end
+ end
+ end
+end
@@ -11,7 +11,7 @@ def initialize
@assertions = []
end
- def assert_equal *args
+ def assert *args
@assertions << args
end
end
@@ -28,7 +28,7 @@ def test_valid_xhtml
</html>
eoxhtml
- assert_equal [0, 0, ''], fu.assertions.first
+ assert_equal [true, ''], fu.assertions.first
end
def test_invalid_xhtml
@@ -42,7 +42,7 @@ def test_invalid_xhtml
</html>
eoxhtml
- assert_equal [0, 1], fu.assertions.first[0..1]
+ assert_equal false, fu.assertions.first.first
assert_match('Missing child element', fu.assertions.first.last)
end
end

0 comments on commit f601a66

Please sign in to comment.