Module: Test::Unit::Assertions

Defined in:
lib/html-validator.rb

Instance Method Summary collapse

Instance Method Details

#assert_is_html5_valid(page) ⇒ Object



14
15
16
17
18
19
# File 'lib/html-validator.rb', line 14

def assert_is_html5_valid(page)
  schema_path = schema_path_for('html5', 'html5.rng')
  schema = Nokogiri::XML::RelaxNG(File.open(schema_path))

  validate(page, schema)
end

#assert_is_xhtml_trans_valid(page) ⇒ Object



7
8
9
10
11
12
# File 'lib/html-validator.rb', line 7

def assert_is_xhtml_trans_valid(page)
  schema_path = schema_path_for('xhtml1-transitional.xsd')
  schema = Nokogiri::XML::Schema(open(schema_path))

  validate(page, schema)
end

#schema_path_for(*relative_name) ⇒ Object



21
22
23
# File 'lib/html-validator.rb', line 21

def schema_path_for(*relative_name)
  File.join(File.dirname(__FILE__), '..', 'schemas', *relative_name)
end

#validate(page, schema) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/html-validator.rb', line 25

def validate(page, schema)
  errors = schema.validate(Nokogiri::XML(page))

  if errors.any?
    message = "#{errors.count} errors:\n"
    errors.each { |error| message << "#{error}\n" }
    raise AssertionFailedError.new(message)
  end
end