Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adding rspec assertions

  • Loading branch information...
commit f601a66650713dde76ae6a979eccfff60f50edf8 1 parent 8d30c3c
Aaron Patterson authored
49 lib/markup_validity.rb
... ... @@ -1,30 +1,43 @@
1 1 require 'nokogiri'
  2 +require 'markup_validity/rspec'
2 3
3 4 module MarkupValidity
4 5 VERSION = '1.0.0'
5 6
6   - Dir.chdir(File.dirname(__FILE__)) do
7   - XHTML1_TRANSITIONAL = Nokogiri::XML::Schema(
8   - File.read('xhtml1-transitional.xsd')
9   - )
10   - end
  7 + class Validator # :nodoc:
  8 + Dir.chdir(File.dirname(__FILE__)) do
  9 + XHTML1_TRANSITIONAL = Nokogiri::XML::Schema(
  10 + File.read('xhtml1-transitional.xsd')
  11 + )
  12 + end
11 13
12   - def assert_xhtml_transitional xhtml
13   - lines = xhtml.split("\n")
14   - doc = Nokogiri::XML xhtml
  14 + attr_reader :errors
  15 + def initialize xml, reference = XHTML1_TRANSITIONAL
  16 + doc = Nokogiri::XML xml
  17 + @reference = reference
  18 + @xml = xml
  19 + @errors = reference.validate(doc)
  20 + end
15 21
16   - errors = XHTML1_TRANSITIONAL.validate(doc)
  22 + def valid?; @errors.length == 0; end
17 23
18   - strings = []
19   - errors.each do |error|
20   - strings << "Error on line: #{error.line - 1}:"
21   - strings << error.message.gsub(/\{[^\}]*\}/, '')
22   - Range.new(error.line - 2, error.line + 2).each { |number|
23   - strings << "#{number}: #{lines[number]}"
24   - }
25   - strings << ""
  24 + def inspect
  25 + lines = @xml.split("\n")
  26 + strings = []
  27 + errors.each do |error|
  28 + strings << "Error on line: #{error.line - 1}:"
  29 + strings << error.message.gsub(/\{[^\}]*\}/, '')
  30 + Range.new(error.line - 2, error.line + 2).each { |number|
  31 + strings << "#{number}: #{lines[number]}"
  32 + }
  33 + strings << ""
  34 + end
  35 + strings.join("\n")
26 36 end
  37 + end
27 38
28   - assert_equal 0, errors.length, strings.join("\n")
  39 + def assert_xhtml_transitional xhtml
  40 + validator = Validator.new xhtml
  41 + assert validator.valid?, validator.inspect
29 42 end
30 43 end
17 lib/markup_validity/rspec.rb
... ... @@ -0,0 +1,17 @@
  1 +module Spec
  2 + module Matchers
  3 + def be_xhtml_transitional
  4 + Matcher.new :be_xhtml_transitional do
  5 + validator = nil
  6 + match do |xhtml|
  7 + validator = MarkupValidity::Validator.new xhtml
  8 + validator.valid?
  9 + end
  10 +
  11 + failure_message_for_should do |actual|
  12 + validator.inspect
  13 + end
  14 + end
  15 + end
  16 + end
  17 +end
34 spec/matcher_spec.rb
... ... @@ -0,0 +1,34 @@
  1 +$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
  2 +require 'rubygems'
  3 +require 'markup_validity'
  4 +
  5 +module Spec
  6 + module Matchers
  7 + describe "[actual.should] be_xhtml_transitional" do
  8 + it "is valid xhtml" do
  9 + xhtml = <<-eoxhtml
  10 +<html xmlns="http://www.w3.org/1999/xhtml">
  11 + <head>
  12 + <title>hello world</title>
  13 + </head>
  14 + <body>
  15 + </body>
  16 +</html>
  17 + eoxhtml
  18 + xhtml.should be_xhtml_transitional
  19 + end
  20 +
  21 + it "is not valid xhtml" do
  22 + xhtml = <<-eoxhtml
  23 +<html xmlns="http://www.w3.org/1999/xhtml">
  24 + <head>
  25 + </head>
  26 + <body>
  27 + </body>
  28 +</html>
  29 + eoxhtml
  30 + xhtml.should_not be_xhtml_transitional
  31 + end
  32 + end
  33 + end
  34 +end
6 test/test_markup_validity.rb
@@ -11,7 +11,7 @@ def initialize
11 11 @assertions = []
12 12 end
13 13
14   - def assert_equal *args
  14 + def assert *args
15 15 @assertions << args
16 16 end
17 17 end
@@ -28,7 +28,7 @@ def test_valid_xhtml
28 28 </html>
29 29 eoxhtml
30 30
31   - assert_equal [0, 0, ''], fu.assertions.first
  31 + assert_equal [true, ''], fu.assertions.first
32 32 end
33 33
34 34 def test_invalid_xhtml
@@ -42,7 +42,7 @@ def test_invalid_xhtml
42 42 </html>
43 43 eoxhtml
44 44
45   - assert_equal [0, 1], fu.assertions.first[0..1]
  45 + assert_equal false, fu.assertions.first.first
46 46 assert_match('Missing child element', fu.assertions.first.last)
47 47 end
48 48 end

0 comments on commit f601a66

Please sign in to comment.
Something went wrong with that request. Please try again.