File: test_parser.rb

package info (click to toggle)
libfeedtools-ruby 0.2.29%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,004 kB
  • ctags: 1,385
  • sloc: ruby: 18,815; sql: 39; makefile: 6
file content (68 lines) | stat: -rw-r--r-- 1,961 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require File.join(File.dirname(__FILE__), 'preamble')

require 'html5/treebuilders'
require 'html5/html5parser'


$tree_types_to_test = ['simpletree', 'rexml']

begin
  require 'hpricot'
  $tree_types_to_test.push('hpricot')
rescue LoadError
end

$CHECK_PARSER_ERRORS = ARGV.delete('-p') # TODO

puts 'Testing tree builders: ' + $tree_types_to_test * ', '


class Html5ParserTestCase < Test::Unit::TestCase
  include HTML5
  include TestSupport

  html5_test_files('tree-construction').each do |test_file|

    test_name = File.basename(test_file).sub('.dat', '')

    TestData.new(test_file, %w(data errors document-fragment document)).
      each_with_index do |(input, errors, inner_html, expected), index|

      errors = errors.split("\n")
      expected = expected.gsub("\n| ","\n")[2..-1]

      $tree_types_to_test.each do |tree_name|
        define_method 'test_%s_%d_%s' % [ test_name, index + 1, tree_name ] do

          parser = HTMLParser.new(:tree => TreeBuilders[tree_name])
        
          if inner_html
            parser.parse_fragment(input, inner_html)
          else
            parser.parse(input)
          end
        
          actual_output = convertTreeDump(parser.tree.testSerializer(parser.tree.document))

          assert_equal sortattrs(expected), sortattrs(actual_output), [
            '', 'Input:', input,
            '', 'Expected:', expected,
            '', 'Recieved:', actual_output
          ].join("\n")

          actual_errors = parser.errors.map do |(line, col), message, datavars|
            'Line: %i Col: %i %s' % [line, col, E[message] % datavars]
          end
          assert_equal errors.length, parser.errors.length, [
            '', 'Input', input,
            '', "Expected errors (#{errors.length}):", errors.join("\n"),
            '', "Actual errors (#{actual_errors.length}):",
                 actual_errors.join("\n")
          ].join("\n")
          
        end
      end
    end
  end

end