Mercurial > hg > soundsoftware-site
comparison vendor/gems/coderay-1.0.0/test/functional/basic.rb @ 909:cbb26bc654de redmine-1.3
Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author | Chris Cannam |
---|---|
date | Fri, 24 Feb 2012 19:09:32 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
908:c6c2cbd0afee | 909:cbb26bc654de |
---|---|
1 # encoding: utf-8 | |
2 require 'test/unit' | |
3 require File.expand_path('../../lib/assert_warning', __FILE__) | |
4 | |
5 $:.unshift File.expand_path('../../../lib', __FILE__) | |
6 require 'coderay' | |
7 | |
8 class BasicTest < Test::Unit::TestCase | |
9 | |
10 def test_version | |
11 assert_nothing_raised do | |
12 assert_match(/\A\d\.\d\.\d?\z/, CodeRay::VERSION) | |
13 end | |
14 end | |
15 | |
16 RUBY_TEST_CODE = 'puts "Hello, World!"' | |
17 | |
18 RUBY_TEST_TOKENS = [ | |
19 ['puts', :ident], | |
20 [' ', :space], | |
21 [:begin_group, :string], | |
22 ['"', :delimiter], | |
23 ['Hello, World!', :content], | |
24 ['"', :delimiter], | |
25 [:end_group, :string] | |
26 ].flatten | |
27 def test_simple_scan | |
28 assert_nothing_raised do | |
29 assert_equal RUBY_TEST_TOKENS, CodeRay.scan(RUBY_TEST_CODE, :ruby).tokens | |
30 end | |
31 end | |
32 | |
33 RUBY_TEST_HTML = 'puts <span class="string"><span class="delimiter">"</span>' + | |
34 '<span class="content">Hello, World!</span><span class="delimiter">"</span></span>' | |
35 def test_simple_highlight | |
36 assert_nothing_raised do | |
37 assert_equal RUBY_TEST_HTML, CodeRay.scan(RUBY_TEST_CODE, :ruby).html | |
38 end | |
39 end | |
40 | |
41 def test_scan_file | |
42 CodeRay.scan_file __FILE__ | |
43 end | |
44 | |
45 def test_encode | |
46 assert_equal 1, CodeRay.encode('test', :python, :count) | |
47 end | |
48 | |
49 def test_encode_tokens | |
50 assert_equal 1, CodeRay.encode_tokens(CodeRay::Tokens['test', :string], :count) | |
51 end | |
52 | |
53 def test_encode_file | |
54 assert_equal File.read(__FILE__), CodeRay.encode_file(__FILE__, :text) | |
55 end | |
56 | |
57 def test_highlight | |
58 assert_match '<pre>test</pre>', CodeRay.highlight('test', :python) | |
59 end | |
60 | |
61 def test_highlight_file | |
62 assert_match "require <span class=\"string\"><span class=\"delimiter\">'</span><span class=\"content\">test/unit</span><span class=\"delimiter\">'</span></span>\n", CodeRay.highlight_file(__FILE__) | |
63 end | |
64 | |
65 def test_duo | |
66 assert_equal(RUBY_TEST_CODE, | |
67 CodeRay::Duo[:plain, :text].highlight(RUBY_TEST_CODE)) | |
68 assert_equal(RUBY_TEST_CODE, | |
69 CodeRay::Duo[:plain => :text].highlight(RUBY_TEST_CODE)) | |
70 end | |
71 | |
72 def test_duo_stream | |
73 assert_equal(RUBY_TEST_CODE, | |
74 CodeRay::Duo[:plain, :text].highlight(RUBY_TEST_CODE, :stream => true)) | |
75 end | |
76 | |
77 def test_comment_filter | |
78 assert_equal <<-EXPECTED, CodeRay.scan(<<-INPUT, :ruby).comment_filter.text | |
79 #!/usr/bin/env ruby | |
80 | |
81 code | |
82 | |
83 more code | |
84 EXPECTED | |
85 #!/usr/bin/env ruby | |
86 =begin | |
87 A multi-line comment. | |
88 =end | |
89 code | |
90 # A single-line comment. | |
91 more code # and another comment, in-line. | |
92 INPUT | |
93 end | |
94 | |
95 def test_lines_of_code | |
96 assert_equal 2, CodeRay.scan(<<-INPUT, :ruby).lines_of_code | |
97 #!/usr/bin/env ruby | |
98 =begin | |
99 A multi-line comment. | |
100 =end | |
101 code | |
102 # A single-line comment. | |
103 more code # and another comment, in-line. | |
104 INPUT | |
105 rHTML = <<-RHTML | |
106 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" | |
107 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
108 | |
109 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
110 <head> | |
111 <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> | |
112 <title><%= controller.controller_name.titleize %>: <%= controller.action_name %></title> | |
113 <%= stylesheet_link_tag 'scaffold' %> | |
114 </head> | |
115 <body> | |
116 | |
117 <p style="color: green"><%= flash[:notice] %></p> | |
118 | |
119 <div id="main"> | |
120 <%= yield %> | |
121 </div> | |
122 | |
123 </body> | |
124 </html> | |
125 RHTML | |
126 assert_equal 0, CodeRay.scan(rHTML, :html).lines_of_code | |
127 assert_equal 0, CodeRay.scan(rHTML, :php).lines_of_code | |
128 assert_equal 0, CodeRay.scan(rHTML, :yaml).lines_of_code | |
129 assert_equal 4, CodeRay.scan(rHTML, :erb).lines_of_code | |
130 end | |
131 | |
132 def test_list_of_encoders | |
133 assert_kind_of(Array, CodeRay::Encoders.list) | |
134 assert CodeRay::Encoders.list.include?(:count) | |
135 end | |
136 | |
137 def test_list_of_scanners | |
138 assert_kind_of(Array, CodeRay::Scanners.list) | |
139 assert CodeRay::Scanners.list.include?(:text) | |
140 end | |
141 | |
142 def test_token_kinds | |
143 assert_kind_of Hash, CodeRay::TokenKinds | |
144 for kind, css_class in CodeRay::TokenKinds | |
145 assert_kind_of Symbol, kind | |
146 if css_class != false | |
147 assert_kind_of String, css_class, "TokenKinds[%p] == %p" % [kind, css_class] | |
148 end | |
149 end | |
150 assert_equal 'reserved', CodeRay::TokenKinds[:reserved] | |
151 assert_warning 'Undefined Token kind: :shibboleet' do | |
152 assert_equal false, CodeRay::TokenKinds[:shibboleet] | |
153 end | |
154 end | |
155 | |
156 class Milk < CodeRay::Encoders::Encoder | |
157 FILE_EXTENSION = 'cocoa' | |
158 end | |
159 | |
160 class HoneyBee < CodeRay::Encoders::Encoder | |
161 end | |
162 | |
163 def test_encoder_file_extension | |
164 assert_nothing_raised do | |
165 assert_equal 'html', CodeRay::Encoders::Page::FILE_EXTENSION | |
166 assert_equal 'cocoa', Milk::FILE_EXTENSION | |
167 assert_equal 'cocoa', Milk.new.file_extension | |
168 assert_equal 'honeybee', HoneyBee::FILE_EXTENSION | |
169 assert_equal 'honeybee', HoneyBee.new.file_extension | |
170 end | |
171 assert_raise NameError do | |
172 HoneyBee::MISSING_CONSTANT | |
173 end | |
174 end | |
175 | |
176 def test_encoder_tokens | |
177 encoder = CodeRay::Encoders::Encoder.new | |
178 encoder.send :setup, {} | |
179 assert_raise(ArgumentError) { encoder.token :strange, '' } | |
180 encoder.token 'test', :debug | |
181 end | |
182 | |
183 def test_encoder_deprecated_interface | |
184 encoder = CodeRay::Encoders::Encoder.new | |
185 encoder.send :setup, {} | |
186 assert_warning 'Using old Tokens#<< interface.' do | |
187 encoder << ['test', :content] | |
188 end | |
189 assert_raise ArgumentError do | |
190 encoder << [:strange, :input] | |
191 end | |
192 assert_raise ArgumentError do | |
193 encoder.encode_tokens [['test', :token]] | |
194 end | |
195 end | |
196 | |
197 def encoder_token_interface_deprecation_warning_given | |
198 CodeRay::Encoders::Encoder.send :class_variable_get, :@@CODERAY_TOKEN_INTERFACE_DEPRECATION_WARNING_GIVEN | |
199 end | |
200 | |
201 def test_scanner_file_extension | |
202 assert_equal 'rb', CodeRay::Scanners::Ruby.file_extension | |
203 assert_equal 'rb', CodeRay::Scanners::Ruby.new.file_extension | |
204 assert_equal 'java', CodeRay::Scanners::Java.file_extension | |
205 assert_equal 'java', CodeRay::Scanners::Java.new.file_extension | |
206 end | |
207 | |
208 def test_scanner_lang | |
209 assert_equal :ruby, CodeRay::Scanners::Ruby.lang | |
210 assert_equal :ruby, CodeRay::Scanners::Ruby.new.lang | |
211 assert_equal :java, CodeRay::Scanners::Java.lang | |
212 assert_equal :java, CodeRay::Scanners::Java.new.lang | |
213 end | |
214 | |
215 def test_scanner_tokenize | |
216 assert_equal ['foo', :plain], CodeRay::Scanners::Plain.new.tokenize('foo') | |
217 assert_equal [['foo', :plain], ['bar', :plain]], CodeRay::Scanners::Plain.new.tokenize(['foo', 'bar']) | |
218 CodeRay::Scanners::Plain.new.tokenize 42 | |
219 end | |
220 | |
221 def test_scanner_tokens | |
222 scanner = CodeRay::Scanners::Plain.new | |
223 scanner.tokenize('foo') | |
224 assert_equal ['foo', :plain], scanner.tokens | |
225 scanner.string = '' | |
226 assert_equal ['', :plain], scanner.tokens | |
227 end | |
228 | |
229 def test_scanner_line_and_column | |
230 scanner = CodeRay::Scanners::Plain.new "foo\nbär+quux" | |
231 assert_equal 0, scanner.pos | |
232 assert_equal 1, scanner.line | |
233 assert_equal 1, scanner.column | |
234 scanner.scan(/foo/) | |
235 assert_equal 3, scanner.pos | |
236 assert_equal 1, scanner.line | |
237 assert_equal 4, scanner.column | |
238 scanner.scan(/\n/) | |
239 assert_equal 4, scanner.pos | |
240 assert_equal 2, scanner.line | |
241 assert_equal 1, scanner.column | |
242 scanner.scan(/b/) | |
243 assert_equal 5, scanner.pos | |
244 assert_equal 2, scanner.line | |
245 assert_equal 2, scanner.column | |
246 scanner.scan(/a/) | |
247 assert_equal 5, scanner.pos | |
248 assert_equal 2, scanner.line | |
249 assert_equal 2, scanner.column | |
250 scanner.scan(/ä/) | |
251 assert_equal 7, scanner.pos | |
252 assert_equal 2, scanner.line | |
253 assert_equal 4, scanner.column | |
254 scanner.scan(/r/) | |
255 assert_equal 8, scanner.pos | |
256 assert_equal 2, scanner.line | |
257 assert_equal 5, scanner.column | |
258 end | |
259 | |
260 def test_scanner_use_subclasses | |
261 assert_raise NotImplementedError do | |
262 CodeRay::Scanners::Scanner.new | |
263 end | |
264 end | |
265 | |
266 class InvalidScanner < CodeRay::Scanners::Scanner | |
267 end | |
268 | |
269 def test_scanner_scan_tokens | |
270 assert_raise NotImplementedError do | |
271 InvalidScanner.new.tokenize '' | |
272 end | |
273 end | |
274 | |
275 class RaisingScanner < CodeRay::Scanners::Scanner | |
276 def scan_tokens encoder, options | |
277 raise_inspect 'message', [], :initial | |
278 end | |
279 end | |
280 | |
281 def test_scanner_raise_inspect | |
282 assert_raise CodeRay::Scanners::Scanner::ScanError do | |
283 RaisingScanner.new.tokenize '' | |
284 end | |
285 end | |
286 | |
287 def test_scan_a_frozen_string | |
288 assert_nothing_raised do | |
289 CodeRay.scan RUBY_VERSION, :ruby | |
290 CodeRay.scan RUBY_VERSION, :plain | |
291 end | |
292 end | |
293 | |
294 def test_scan_a_non_string | |
295 assert_nothing_raised do | |
296 CodeRay.scan 42, :ruby | |
297 CodeRay.scan nil, :ruby | |
298 CodeRay.scan self, :ruby | |
299 CodeRay.encode ENV.to_hash, :ruby, :page | |
300 CodeRay.highlight CodeRay, :plain | |
301 end | |
302 end | |
303 | |
304 end |