assert_valid_content

assert_valid_content is a RubyGem which validates a variety content types (e.g. (X)HTML, CSS, XML) as part of your unit or functional tests. It is inspired by assert_valid_markup and assert_valid_assert with the following goals:

  • Allow for offline use (i.e. not reliant on web services)

  • Validate a wide variety of content types: HTML, JS, JPEG, PDF, MP3, ad infinitum

  • Packaged as a RubyGem, instead of a Rails plugin

  • Provide for specification of more-stringent validation checks (e.g. image properties)

Validation Modules

Currently supported:

(X)HTML, XML

LibXML via libxml-ruby

CSS

W3C CSS Validator Service

Planned support:

CSS

W3C CSS Validator (local execution)

JPEG, PNG, et al.

ImageMagick via rmagick

MP3, OGG, et al.

ruby-mp3info

PDF, MPEG, etc.

?

Usage

Validation is performed primary through dynamic assertion functions, defined in AssertValidContent::Assertions (and automatically included into Test::Unit::TestCase, if it exists). The assertions are of the form:

assert_valid_TYPE( content )

where TYPE is a MIME type name (e.g. “html”, “css”, etc.), and content is a String, Pathname, or IO object, or any combination in an Array. If no content is specified, and the caller is a Rails functional test, the response body is used.

There are also a set of basic assertions:

assert_valid_content( type, content )
valid_content?( type, content ) # yields validator on invalid content

where type is a MIME type constant (e.g. “Mime::HTML”).

Examples

require 'rubygems'
require 'assert_valid_content'

class ContentTest < Test::Unit::TestCase
  # simple test
  def test_html
    html = "<html>...</html>"

    # these are all equivalent
    assert_valid_html html
    assert_valid_html StringIO.new(html)
    assert_valid_content Mime::HTML, html
    assert valid_content?(Mime::HTML, html)
  end

  # Rails controller test
  def test_index
    get :index
    assert_response :success
    assert_valid_html
  end

  # static content test
  assert_valid_css Dir.glob("#{RAILS_ROOT}/public/stylesheets/*.css")
end
Author

Steve Sloan ([email protected])

Website

github.com/CodeMonkeySteve/assert_valid_content

Copyright

Copyright © 2009 Steve Sloan

License

MIT