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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
$mydir = File.dirname __FILE__
$:.unshift File.join($mydir, '..', 'lib')
require 'coderay'
$stdout.sync = true
# from Ruby Facets (http://facets.rubyforge.org/)
class Array
def shuffle!
s = size
each_index do |j|
i = ::Kernel.rand(s-j)
self[j], self[j+i] = at(j+i), at(j) unless i.zero?
end
self
end
end
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
)
max = ENV.fetch('max', 500).to_i
unless ENV['norandom']
print "Random test"
if ENV['showprogress']
print ': '
progress = ''
end
for size in 0..max
if ENV['showprogress']
print "\b" * progress.size
progress = '(%d)' % size
print progress
end
srand size + 17
scanner.string = Array.new(size) { rand 256 }.pack 'c*'
scanner.tokenize
end
puts ', finished.'
end
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'
if ENV['showprogress']
print ': '
progress = ''
end
for size in 0..max
break if size > code.size
if ENV['showprogress']
print "\b" * progress.size
progress = '(%d)' % size
print progress
end
scanner.string = code[0,size]
scanner.tokenize
end
print ', '
end
unless ENV['noshuffled'] or code.size < [0].pack('Q').size
print 'shuffled'
if ENV['showprogress']
print ': '
progress = ''
end
code_bits = code[0,max].unpack('Q*') # split into quadwords...
(max / 4).times do |i|
if ENV['showprogress']
print "\b" * progress.size
progress = '(%d)' % i
print progress
end
srand i
code_bits.shuffle! # ...mix...
scanner.string = code_bits.pack('Q*') # ...and join again
scanner.tokenize
end
# highlighted = highlighter.encode_tokens scanner.tokenize
# File.open(name + '.shuffled.html', 'w') { |f| f.write highlighted }
print ', '
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'] or ENV['noassert']
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 + '.actual.html', 'w') { |f| f.write highlighted }
puts 'finished.'
end
end unless ENV['noexamples']
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['lang']
load_suite(subsuite) or exit
else
Dir[File.join($mydir, '*', '')].sort.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
|