summaryrefslogtreecommitdiff
path: root/test/coderay_suite.rb
blob: 7829a54c0328eb1fee7cdc81f843d56fa51b297e (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
$mydir = File.dirname __FILE__
$:.unshift File.join($mydir, '..', 'lib')

require 'coderay'

module CodeRay

  require 'test/unit'

  class TestCase < Test::Unit::TestCase

    class << self
      def inherited child
        CodeRay::TestSuite << child.suite
      end

      # Calls its block with the working directory set to the examples
      # for this test case.
      def dir
        examples = File.join $mydir, lang.to_s
        Dir.chdir examples do
          yield
        end
      end

      def lang
        @lang ||= name.downcase.to_sym
      end

      def extension extension = nil
        if extension
          @extension = extension.to_s
        else
          @extension ||= lang.to_s
        end
      end
    end

    def extension
      @extension ||= 'in.' + self.class.extension
    end

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

      self.class.dir do
        for input in Dir["*.#{extension}"]
          next if ENV['testonly'] and ENV['testonly'] != File.basename(input, ".#{extension}")
          print "testing #{input}: "
          name = File.basename(input, ".#{extension}")
          output = name + '.out.' + tokenizer.file_extension
          code = File.open(input, 'rb') { |f| break f.read }

          unless ENV['noincremental']
            print 'incremental, '
            for size in 0..[code.size, 300].min
              print size, '.' if ENV['showprogress']
              scanner.string = code[0,size]
              scanner.tokenize
            end
          end

          print 'complete, '
          scanner.string = code
          tokens = scanner.tokens
          result = tokenizer.encode_tokens tokens

          if File.exist? output
            expected = File.open(output, 'rb') { |f| break f.read }
            ok = expected == result
            computed = output.sub('.out.', '.computed.')
            unless ok
              File.open(computed, 'wb') { |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, 'wb') do |f| f.write result end
            puts "New test: #{output}"
          end

          print 'highlighting, '
          highlighted = highlighter.encode_tokens tokens
          File.open(name + '.html', 'w') { |f| f.write highlighted }

          puts 'finished.'
        end
      end
    end

  end

  require 'test/unit/testsuite'

  class TestSuite
    @suite = Test::Unit::TestSuite.new 'CodeRay::Scanners'
    class << self

      def << sub_suite
        @suite << sub_suite
      end

      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

      def load
        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
      end

      def run
        load
        $VERBOSE = true
        if ARGV.include? '-f'
          require 'test/unit/ui/fox/testrunner'
          Test::Unit::UI::Fox::TestRunner
        else
          require 'test/unit/ui/console/testrunner'
          Test::Unit::UI::Console::TestRunner
        end.run @suite
      end
    end
  end

end