forked from github-linguist/linguist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_language.rb
442 lines (398 loc) · 19.2 KB
/
test_language.rb
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
require 'linguist/language'
require 'test/unit'
require 'pygments'
class TestLanguage < Test::Unit::TestCase
include Linguist
Lexer = Pygments::Lexer
def test_lexer
assert_equal Lexer['ActionScript 3'], Language['ActionScript'].lexer
assert_equal Lexer['AspectJ'], Language['AspectJ'].lexer
assert_equal Lexer['Bash'], Language['Gentoo Ebuild'].lexer
assert_equal Lexer['Bash'], Language['Gentoo Eclass'].lexer
assert_equal Lexer['Bash'], Language['Shell'].lexer
assert_equal Lexer['C'], Language['OpenCL'].lexer
assert_equal Lexer['C'], Language['XS'].lexer
assert_equal Lexer['C++'], Language['C++'].lexer
assert_equal Lexer['Chapel'], Language['Chapel'].lexer
assert_equal Lexer['Coldfusion HTML'], Language['ColdFusion'].lexer
assert_equal Lexer['Coq'], Language['Coq'].lexer
assert_equal Lexer['FSharp'], Language['F#'].lexer
assert_equal Lexer['FSharp'], Language['F#'].lexer
assert_equal Lexer['Fortran'], Language['FORTRAN'].lexer
assert_equal Lexer['Gherkin'], Language['Cucumber'].lexer
assert_equal Lexer['Groovy'], Language['Groovy'].lexer
assert_equal Lexer['HTML'], Language['HTML'].lexer
assert_equal Lexer['HTML+Django/Jinja'], Language['HTML+Django'].lexer
assert_equal Lexer['HTML+PHP'], Language['HTML+PHP'].lexer
assert_equal Lexer['HTTP'], Language['HTTP'].lexer
assert_equal Lexer['JSON'], Language['JSON'].lexer
assert_equal Lexer['Java'], Language['ChucK'].lexer
assert_equal Lexer['Java'], Language['Java'].lexer
assert_equal Lexer['JavaScript'], Language['JavaScript'].lexer
assert_equal Lexer['LSL'], Language['LSL'].lexer
assert_equal Lexer['MOOCode'], Language['Moocode'].lexer
assert_equal Lexer['MuPAD'], Language['mupad'].lexer
assert_equal Lexer['NASM'], Language['Assembly'].lexer
assert_equal Lexer['OCaml'], Language['OCaml'].lexer
assert_equal Lexer['Ooc'], Language['ooc'].lexer
assert_equal Lexer['OpenEdge ABL'], Language['OpenEdge ABL'].lexer
assert_equal Lexer['REBOL'], Language['Rebol'].lexer
assert_equal Lexer['RHTML'], Language['HTML+ERB'].lexer
assert_equal Lexer['RHTML'], Language['RHTML'].lexer
assert_equal Lexer['Ruby'], Language['Crystal'].lexer
assert_equal Lexer['Ruby'], Language['Mirah'].lexer
assert_equal Lexer['Ruby'], Language['Ruby'].lexer
assert_equal Lexer['S'], Language['R'].lexer
assert_equal Lexer['Common Lisp'], Language['Emacs Lisp'].lexer
assert_equal Lexer['Scheme'], Language['Nu'].lexer
assert_equal Lexer['Racket'], Language['Racket'].lexer
assert_equal Lexer['Scheme'], Language['Scheme'].lexer
assert_equal Lexer['Standard ML'], Language['Standard ML'].lexer
assert_equal Lexer['TeX'], Language['TeX'].lexer
assert_equal Lexer['Verilog'], Language['Verilog'].lexer
assert_equal Lexer['XSLT'], Language['XSLT'].lexer
assert_equal Lexer['aspx-vb'], Language['ASP'].lexer
assert_equal Lexer['haXe'], Language['Haxe'].lexer
assert_equal Lexer['reStructuredText'], Language['reStructuredText'].lexer
end
def test_find_by_alias
assert_equal Language['ASP'], Language.find_by_alias('asp')
assert_equal Language['ASP'], Language.find_by_alias('aspx')
assert_equal Language['ASP'], Language.find_by_alias('aspx-vb')
assert_equal Language['ActionScript'], Language.find_by_alias('as3')
assert_equal Language['ApacheConf'], Language.find_by_alias('apache')
assert_equal Language['Assembly'], Language.find_by_alias('nasm')
assert_equal Language['Batchfile'], Language.find_by_alias('bat')
assert_equal Language['C#'], Language.find_by_alias('c#')
assert_equal Language['C#'], Language.find_by_alias('csharp')
assert_equal Language['C'], Language.find_by_alias('c')
assert_equal Language['C++'], Language.find_by_alias('c++')
assert_equal Language['C++'], Language.find_by_alias('cpp')
assert_equal Language['Chapel'], Language.find_by_alias('chpl')
assert_equal Language['CoffeeScript'], Language.find_by_alias('coffee')
assert_equal Language['CoffeeScript'], Language.find_by_alias('coffee-script')
assert_equal Language['ColdFusion'], Language.find_by_alias('cfm')
assert_equal Language['Common Lisp'], Language.find_by_alias('common-lisp')
assert_equal Language['Common Lisp'], Language.find_by_alias('lisp')
assert_equal Language['Darcs Patch'], Language.find_by_alias('dpatch')
assert_equal Language['Dart'], Language.find_by_alias('dart')
assert_equal Language['Emacs Lisp'], Language.find_by_alias('elisp')
assert_equal Language['Emacs Lisp'], Language.find_by_alias('emacs')
assert_equal Language['Emacs Lisp'], Language.find_by_alias('emacs-lisp')
assert_equal Language['Gettext Catalog'], Language.find_by_alias('pot')
assert_equal Language['HTML'], Language.find_by_alias('html')
assert_equal Language['HTML'], Language.find_by_alias('xhtml')
assert_equal Language['HTML+ERB'], Language.find_by_alias('html+erb')
assert_equal Language['HTML+ERB'], Language.find_by_alias('erb')
assert_equal Language['IRC log'], Language.find_by_alias('irc')
assert_equal Language['JSON'], Language.find_by_alias('json')
assert_equal Language['Java Server Pages'], Language.find_by_alias('jsp')
assert_equal Language['Java'], Language.find_by_alias('java')
assert_equal Language['JavaScript'], Language.find_by_alias('javascript')
assert_equal Language['JavaScript'], Language.find_by_alias('js')
assert_equal Language['Literate Haskell'], Language.find_by_alias('lhs')
assert_equal Language['Literate Haskell'], Language.find_by_alias('literate-haskell')
assert_equal Language['Objective-C'], Language.find_by_alias('objc')
assert_equal Language['OpenEdge ABL'], Language.find_by_alias('openedge')
assert_equal Language['OpenEdge ABL'], Language.find_by_alias('progress')
assert_equal Language['OpenEdge ABL'], Language.find_by_alias('abl')
assert_equal Language['Parrot Internal Representation'], Language.find_by_alias('pir')
assert_equal Language['PowerShell'], Language.find_by_alias('posh')
assert_equal Language['Puppet'], Language.find_by_alias('puppet')
assert_equal Language['Pure Data'], Language.find_by_alias('pure-data')
assert_equal Language['Raw token data'], Language.find_by_alias('raw')
assert_equal Language['Ruby'], Language.find_by_alias('rb')
assert_equal Language['Ruby'], Language.find_by_alias('ruby')
assert_equal Language['R'], Language.find_by_alias('r')
assert_equal Language['Scheme'], Language.find_by_alias('scheme')
assert_equal Language['Shell'], Language.find_by_alias('bash')
assert_equal Language['Shell'], Language.find_by_alias('sh')
assert_equal Language['Shell'], Language.find_by_alias('shell')
assert_equal Language['Shell'], Language.find_by_alias('zsh')
assert_equal Language['TeX'], Language.find_by_alias('tex')
assert_equal Language['TypeScript'], Language.find_by_alias('ts')
assert_equal Language['VimL'], Language.find_by_alias('vim')
assert_equal Language['VimL'], Language.find_by_alias('viml')
assert_equal Language['reStructuredText'], Language.find_by_alias('rst')
assert_equal Language['YAML'], Language.find_by_alias('yml')
assert_nil Language.find_by_alias(nil)
end
def test_groups
# Test a couple identity cases
assert_equal Language['Perl'], Language['Perl'].group
assert_equal Language['Python'], Language['Python'].group
assert_equal Language['Ruby'], Language['Ruby'].group
# Test a few special groups
assert_equal Language['Assembly'], Language['GAS'].group
assert_equal Language['C'], Language['OpenCL'].group
assert_equal Language['Haskell'], Language['Literate Haskell'].group
assert_equal Language['Java'], Language['Java Server Pages'].group
assert_equal Language['Python'], Language['Cython'].group
assert_equal Language['Python'], Language['NumPy'].group
assert_equal Language['Shell'], Language['Batchfile'].group
assert_equal Language['Shell'], Language['Gentoo Ebuild'].group
assert_equal Language['Shell'], Language['Gentoo Eclass'].group
assert_equal Language['Shell'], Language['Tcsh'].group
# Ensure everyone has a group
Language.all.each do |language|
assert language.group, "#{language} has no group"
end
end
# Used for code search indexing. Changing any of these values may
# require reindexing repositories.
def test_search_term
assert_equal 'perl', Language['Perl'].search_term
assert_equal 'python', Language['Python'].search_term
assert_equal 'ruby', Language['Ruby'].search_term
assert_equal 'common-lisp', Language['Common Lisp'].search_term
assert_equal 'html+erb', Language['HTML+ERB'].search_term
assert_equal 'max/msp', Language['Max'].search_term
assert_equal 'puppet', Language['Puppet'].search_term
assert_equal 'pure-data', Language['Pure Data'].search_term
assert_equal 'aspx-vb', Language['ASP'].search_term
assert_equal 'as3', Language['ActionScript'].search_term
assert_equal 'nasm', Language['Assembly'].search_term
assert_equal 'bat', Language['Batchfile'].search_term
assert_equal 'csharp', Language['C#'].search_term
assert_equal 'cpp', Language['C++'].search_term
assert_equal 'cfm', Language['ColdFusion'].search_term
assert_equal 'dpatch', Language['Darcs Patch'].search_term
assert_equal 'fsharp', Language['F#'].search_term
assert_equal 'pot', Language['Gettext Catalog'].search_term
assert_equal 'irc', Language['IRC log'].search_term
assert_equal 'lhs', Language['Literate Haskell'].search_term
assert_equal 'mirah', Language['Mirah'].search_term
assert_equal 'raw', Language['Raw token data'].search_term
assert_equal 'bash', Language['Shell'].search_term
assert_equal 'vim', Language['VimL'].search_term
assert_equal 'jsp', Language['Java Server Pages'].search_term
assert_equal 'rst', Language['reStructuredText'].search_term
end
def test_popular
assert Language['Ruby'].popular?
assert Language['Perl'].popular?
assert Language['Python'].popular?
assert Language['Assembly'].unpopular?
assert Language['Brainfuck'].unpopular?
end
def test_programming
assert_equal :programming, Language['JavaScript'].type
assert_equal :programming, Language['LSL'].type
assert_equal :programming, Language['Perl'].type
assert_equal :programming, Language['PowerShell'].type
assert_equal :programming, Language['Python'].type
assert_equal :programming, Language['Ruby'].type
assert_equal :programming, Language['TypeScript'].type
end
def test_markup
assert_equal :markup, Language['HTML'].type
assert_equal :markup, Language['SCSS'].type
end
def test_data
assert_equal :data, Language['YAML'].type
end
def test_prose
assert_equal :prose, Language['Markdown'].type
assert_equal :prose, Language['Org'].type
end
def test_other
assert_nil Language['Brainfuck'].type
assert_nil Language['Makefile'].type
end
def test_searchable
assert Language['Ruby'].searchable?
assert !Language['Gettext Catalog'].searchable?
assert Language['SQL'].searchable?
end
def test_find_by_name
assert_nil Language.find_by_name(nil)
ruby = Language['Ruby']
assert_equal ruby, Language.find_by_name('Ruby')
end
def test_find_all_by_name
Language.all.each do |language|
assert_equal language, Language.find_by_name(language.name)
assert_equal language, Language[language.name]
end
end
def test_find_all_by_alias
Language.all.each do |language|
language.aliases.each do |name|
assert_equal language, Language.find_by_alias(name)
assert_equal language, Language[name]
end
end
end
def test_find_by_extension
assert_equal [], Language.find_by_extension('.factor-rc')
assert_equal [], Language.find_by_extension('foo.rb')
assert_equal [Language['Ruby']], Language.find_by_extension('rb')
assert_equal [Language['Ruby']], Language.find_by_extension('.rb')
assert_equal [Language['M'], Language['Mathematica'], Language['Matlab'], Language['Mercury'], Language['Objective-C']], Language.find_by_extension('.m')
end
def test_find_all_by_extension
Language.all.each do |language|
language.extensions.each do |extension|
assert_include Language.find_by_extension(extension), language
end
end
end
def test_find_by_filename
assert_equal [Language['Shell']], Language.find_by_filename('PKGBUILD')
assert_equal [Language['Ruby']], Language.find_by_filename('foo.rb')
assert_equal [Language['Ruby']], Language.find_by_filename('foo/bar.rb')
assert_equal [Language['Ruby']], Language.find_by_filename('Rakefile')
assert_equal [Language['Ruby']], Language.find_by_filename('PKGBUILD.rb')
assert_equal Language['ApacheConf'], Language.find_by_filename('httpd.conf').first
assert_equal [Language['ApacheConf']], Language.find_by_filename('.htaccess')
assert_equal Language['Nginx'], Language.find_by_filename('nginx.conf').first
assert_equal ['C', 'C++', 'Objective-C'], Language.find_by_filename('foo.h').map(&:name).sort
assert_equal [], Language.find_by_filename('rb')
assert_equal [], Language.find_by_filename('.null')
assert_equal [Language['Shell']], Language.find_by_filename('.bashrc')
assert_equal [Language['Shell']], Language.find_by_filename('bash_profile')
assert_equal [Language['Shell']], Language.find_by_filename('.zshrc')
assert_equal [Language['Clojure']], Language.find_by_filename('riemann.config')
assert_equal [Language['HTML+Django']], Language.find_by_filename('index.jinja')
assert_equal [Language['Chapel']], Language.find_by_filename('examples/hello.chpl')
end
def test_find_by_shebang
assert_equal 'ruby', Linguist.interpreter_from_shebang("#!/usr/bin/ruby\n# baz")
{ [] => ["",
"foo",
"#bar",
"#baz",
"///",
"\n\n\n\n\n",
" #!/usr/sbin/ruby",
"\n#!/usr/sbin/ruby"],
['Ruby'] => ["#!/usr/bin/env ruby\n# baz",
"#!/usr/sbin/ruby\n# bar",
"#!/usr/bin/ruby\n# foo",
"#!/usr/sbin/ruby",
"#!/usr/sbin/ruby foo bar baz\n"],
['R'] => ["#!/usr/bin/env Rscript\n# example R script\n#\n"],
['Shell'] => ["#!/usr/bin/bash\n", "#!/bin/sh"],
['Python'] => ["#!/bin/python\n# foo\n# bar\n# baz",
"#!/usr/bin/python2.7\n\n\n\n",
"#!/usr/bin/python3\n\n\n\n"],
["Common Lisp"] => ["#!/usr/bin/sbcl --script\n\n"]
}.each do |languages, bodies|
bodies.each do |body|
assert_equal([body, languages.map{|l| Language[l]}],
[body, Language.find_by_shebang(body)])
end
end
end
def test_find
assert_equal 'Ruby', Language['Ruby'].name
assert_equal 'Ruby', Language['ruby'].name
assert_equal 'C++', Language['C++'].name
assert_equal 'C++', Language['c++'].name
assert_equal 'C++', Language['cpp'].name
assert_equal 'C#', Language['C#'].name
assert_equal 'C#', Language['c#'].name
assert_equal 'C#', Language['csharp'].name
assert_nil Language['defunkt']
assert_nil Language[nil]
end
def test_find_ignores_case
assert_equal 'AGS Script', Language['ags script'].name
assert_equal 'AGS Script', Language['ags sCRIPT'].name
end
def test_find_by_name_ignores_case
assert_equal 'AGS Script', Language.find_by_name('ags script').name
assert_equal 'AGS Script', Language.find_by_name('ags sCRIPT').name
end
def test_find_by_alias_ignores_case
refute_includes Language['AGS Script'].aliases, 'AGS'
assert_equal 'AGS Script', Language.find_by_alias('AGS').name
end
def test_name
assert_equal 'Perl', Language['Perl'].name
assert_equal 'Python', Language['Python'].name
assert_equal 'Ruby', Language['Ruby'].name
end
def test_escaped_name
assert_equal 'C', Language['C'].escaped_name
assert_equal 'C%23', Language['C#'].escaped_name
assert_equal 'C%2B%2B', Language['C++'].escaped_name
assert_equal 'Objective-C', Language['Objective-C'].escaped_name
assert_equal 'Common%20Lisp', Language['Common Lisp'].escaped_name
end
def test_error_without_name
assert_raise ArgumentError do
Language.new :name => nil
end
end
def test_color
assert_equal '#701516', Language['Ruby'].color
assert_equal '#3581ba', Language['Python'].color
assert_equal '#f1e05a', Language['JavaScript'].color
assert_equal '#31859c', Language['TypeScript'].color
assert_equal '#3d9970', Language['LSL'].color
end
def test_colors
assert Language.colors.include?(Language['Ruby'])
assert Language.colors.include?(Language['Python'])
end
def test_ace_mode
assert_equal 'c_cpp', Language['C++'].ace_mode
assert_equal 'coffee', Language['CoffeeScript'].ace_mode
assert_equal 'csharp', Language['C#'].ace_mode
assert_equal 'css', Language['CSS'].ace_mode
assert_equal 'lsl', Language['LSL'].ace_mode
assert_equal 'javascript', Language['JavaScript'].ace_mode
end
def test_ace_modes
assert Language.ace_modes.include?(Language['Ruby'])
assert !Language.ace_modes.include?(Language['FORTRAN'])
end
def test_wrap
assert_equal false, Language['C'].wrap
assert_equal true, Language['Markdown'].wrap
end
def test_extensions
assert Language['LSL'].extensions.include?('.lsl')
assert Language['Perl'].extensions.include?('.pl')
assert Language['Python'].extensions.include?('.py')
assert Language['Ruby'].extensions.include?('.rb')
end
def test_primary_extension
assert_equal '.pl', Language['Perl'].primary_extension
assert_equal '.py', Language['Python'].primary_extension
assert_equal '.rb', Language['Ruby'].primary_extension
assert_equal '.js', Language['JavaScript'].primary_extension
assert_equal '.coffee', Language['CoffeeScript'].primary_extension
assert_equal '.t', Language['Turing'].primary_extension
assert_equal '.ts', Language['TypeScript'].primary_extension
# This is a nasty requirement, but there's some code in GitHub that
# expects this. Really want to drop this.
Language.all.each do |language|
assert language.primary_extension, "#{language} has no primary extension"
end
end
def test_eql
assert Language['Ruby'].eql?(Language['Ruby'])
assert !Language['Ruby'].eql?(Language['Python'])
end
def test_by_type
assert !Language.by_type(:prose).nil?
end
def test_colorize
assert_equal <<-HTML.chomp, Language['Ruby'].colorize("def foo\n 'foo'\nend\n")
<div class="highlight"><pre><span class="k">def</span> <span class="nf">foo</span>
<span class="s1">'foo'</span>
<span class="k">end</span>
</pre></div>
HTML
end
def test_colorize_with_options
assert_equal <<-HTML.chomp, Language['Ruby'].colorize("def foo\n 'foo'\nend\n", :options => { :cssclass => "highlight highlight-ruby" })
<div class="highlight highlight-ruby"><pre><span class="k">def</span> <span class="nf">foo</span>
<span class="s1">'foo'</span>
<span class="k">end</span>
</pre></div>
HTML
end
end