summaryrefslogtreecommitdiff
path: root/test/suite.rb
blob: b7afa7990a3f42f6d6a1a6d01256b7d41b112b52 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
$mydir = File.dirname(__FILE__)
$:.unshift $mydir + '/../lib/'

$VERBOSE = true

require 'coderay'
CodeRay::Encoders[:tokens]
CodeRay::Encoders[:html]

require 'test/unit'
include Test::Unit

class CodeRaySuite < TestCase

  def self.dir &block
    @dir ||= File.dirname(@file)
    if block
      Dir.chdir @dir, &block
    end
    @dir
  end

  def dir &block
    self.class.dir(&block)
  end

  def extension
    'in.' + self.class::EXTENSION
  end

  def lang
    self.class::LANG
  end

  def test_ALL
    puts
    puts "    >> Running #{self.class.name} <<"
    puts
    scanner = CodeRay::Scanners[lang].new
    tokenizer = CodeRay::Encoders[:debug].new
    highlighter = CodeRay::Encoders[:html].new(
      :tab_width => 2,
      :line_numbers => :table,
      :wrap => :page,
      :hint => :debug,
      :css => :class
    )

    dir do
      for input in Dir["*.#{extension}"]
        puts "testing #{input}..."
        name = File.basename(input, ".#{extension}")
        output = name + '.out.' + tokenizer.file_extension
        code = File.open(input, 'rb') { |f| break f.read }

        scanner.string = code
        tokens = scanner.tokens
        result = tokenizer.encode_tokens tokens
        highlighted = highlighter.encode_tokens tokens

        File.open(name + '.html', 'w') do |f| f.write highlighted end

        if File.exist? output
          expected = File.read output
          ok = expected == result
          computed = output.sub('.out.', '.computed.')
          unless ok
            File.open(computed, 'w') { |f| f.write result }
            print `gvimdiff #{output} #{computed}` if ENV['diff']
          end
          assert(ok, "Scan error: #{computed} != #{output}") unless ENV['diff']
        else
          File.open(output, 'w') do |f| f.write result end
          puts "New test: #{output}"
        end

      end
    end
  end

end

require 'test/unit/testsuite'
$suite = TestSuite.new

def load_suite name
  begin
    suite = File.join($mydir, name, 'suite.rb')
    require suite
  rescue LoadError
    $stderr.puts <<-ERR

!! Suite #{suite} not found

    ERR
    false
  end
end

if subsuite = ARGV.find { |a| break $1 if a[/^([^-].*)/] } || ENV['scannerlang']
  load_suite(subsuite) or exit
else
  Dir[File.join($mydir, '*', '')].each { |suite| load_suite File.basename(suite) }
end

if ARGV.include? '-f'
  require 'test/unit/ui/fox/testrunner'
  UI::Fox::TestRunner.run $suite
else
  require 'test/unit/ui/console/testrunner'
  UI::Console::TestRunner.run $suite
end