#!/usr/bin/env ruby require File.join(File.dirname(__FILE__), 'helper') require 'erb' require 'w3c_validators' class ValidateFixtures < Test::Unit::TestCase include W3CValidators def setup @v = MarkupValidator.new sleep 1 # delay per WC3 request end HTML_4_0_TEMPLATE = < <%= test_name %> <%= content %> EOD XHTML_1_0_TEMPLATE = < <%= test_name %> <%= content %> EOD fixtures.each do |name, doc| if doc['html'] && (doc['valid_html'].nil? || doc['valid_html']) define_method("test_html_output_validity_of_#{name}") do assert_produces_valid_html(name, doc['html']) end define_method("test_xhtml_output_validity_of_#{name}") do assert_produces_valid_xhtml(name, doc['html']) end end end private def assert_produces_valid_html(test_name, content) body = ERB.new(HTML_4_0_TEMPLATE, nil,'-%').result(binding) assert_validates(body) end def assert_produces_valid_xhtml(test_name, content) body = ERB.new(XHTML_1_0_TEMPLATE, nil,'-%').result(binding) assert_validates(body) end def assert_validates(body) results = @v.validate_text(body) errors = results.errors warnings = results.warnings.reject {|w| w.message_id == "247" } # NET-enabling start-tag requires SHORTTAG YES. assert(errors.empty?, "Validator errors: \n" + errors.collect {|e| "'#{e.to_s}'"}.join("\n")) assert(warnings.empty?, "Validator warnings: \n" + warnings.collect {|w| "'#{w.to_s}'"}.join("\n")) end end